test_banner( $attribute_name ); // Add an admin notice to confirm add_action( 'admin_notices', function () use ( $attribute_name ) { echo '

Test banner triggered for: ' . esc_html( $attribute_name ) . '

'; } ); } } /** * Check if a newly created WooCommerce attribute has a direct mapping to Meta. * * @param int $id Attribute ID. * @param array $data Attribute data. */ public function check_new_woocommerce_attribute( $id, $data ) { // Only show to users who can manage WooCommerce if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } // Get the attribute name from the data $attribute_name = isset( $data['attribute_name'] ) ? $data['attribute_name'] : ''; if ( empty( $attribute_name ) ) { return; } // Check if this attribute maps to any Meta field $maps_to_meta = $this->attribute_maps_to_meta( $attribute_name ); if ( ! $maps_to_meta ) { $this->queue_unmapped_attribute_banner( $attribute_name ); } } /** * Check if a newly created attribute has a direct mapping to Meta. * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ public function check_new_attribute_mapping( $term_id, $tt_id, $taxonomy ) { // Only check for attribute taxonomies if ( ! $this->is_attribute_taxonomy( $taxonomy ) ) { return; } // Only show to users who can manage WooCommerce if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } // Get the attribute name from taxonomy $attribute_name = str_replace( 'pa_', '', $taxonomy ); // Check if this attribute maps to any Meta field $maps_to_meta = $this->attribute_maps_to_meta( $attribute_name ); if ( ! $maps_to_meta ) { $this->queue_unmapped_attribute_banner( $attribute_name ); } } /** * Check if a taxonomy is an attribute taxonomy. * * @param string $taxonomy Taxonomy name. * @return bool */ private function is_attribute_taxonomy( $taxonomy ) { return strpos( $taxonomy, 'pa_' ) === 0; } /** * Check if an attribute name maps to a Meta field. * * @param string $attribute_name Attribute name (without pa_ prefix). * @return bool */ private function attribute_maps_to_meta( $attribute_name ) { if ( ! class_exists( 'WooCommerce\Facebook\ProductAttributeMapper' ) ) { return false; } // Use the same comprehensive logic as the ProductAttributeMapper $mapped_field = ProductAttributeMapper::check_attribute_mapping( 'pa_' . $attribute_name ); // If we get a mapping result, the attribute is mapped return false !== $mapped_field; } /** * Queue a banner for an unmapped attribute. * * @param string $attribute_name The attribute name. */ private function queue_unmapped_attribute_banner( $attribute_name ) { $banner_data = array( 'attribute_name' => $attribute_name, 'timestamp' => time(), ); // Increase duration to 30 minutes to account for page redirects set_transient( 'fb_new_unmapped_attribute_banner', $banner_data, 1800 ); // Also store a flag to show the banner immediately on the current page set_transient( 'fb_show_banner_now', true, 300 ); } /** * Display the unmapped attribute banner. */ public function display_unmapped_attribute_banner() { // Check if we should force show the banner now (for immediate display) $show_now = get_transient( 'fb_show_banner_now' ); $should_show = $this->should_show_banner(); if ( ! $should_show && ! $show_now ) { return; } $banner_data = get_transient( 'fb_new_unmapped_attribute_banner' ); if ( ! $banner_data || ! isset( $banner_data['attribute_name'] ) ) { return; } // Clear the immediate show flag if it was set if ( $show_now ) { delete_transient( 'fb_show_banner_now' ); } $attribute_name = $banner_data['attribute_name']; $display_name = ucfirst( str_replace( array( '_', '-' ), ' ', $attribute_name ) ); // Build the mapper URL $mapper_url = add_query_arg( array( 'page' => 'wc-facebook', 'tab' => 'product-attributes', ), admin_url( 'admin.php' ) ); ?>

', '' ); ?>

id, $allowed_screens, true ); } /** * Dismiss the banner via AJAX. */ public function dismiss_banner() { check_ajax_referer( 'dismiss_fb_banner', 'nonce' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( -1 ); } delete_transient( 'fb_new_unmapped_attribute_banner' ); wp_die(); } /** * Manual method to test the banner (for debugging). * * @param string $attribute_name The attribute name to test. */ public function test_banner( $attribute_name = 'escobar' ) { $this->queue_unmapped_attribute_banner( $attribute_name ); } /** * AJAX handler to test the banner. */ public function ajax_test_banner() { check_ajax_referer( 'test_fb_banner', 'nonce' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( -1 ); } $attribute_name = isset( $_POST['attribute'] ) ? sanitize_text_field( wp_unslash( $_POST['attribute'] ) ) : 'escobar'; $this->test_banner( $attribute_name ); wp_send_json_success( 'Banner queued for: ' . $attribute_name ); } }