From 94d812dc32778257e4709c662ee77656eba9e7f0 Mon Sep 17 00:00:00 2001 From: Sayan Pandey Date: Thu, 13 Mar 2025 18:26:11 +0000 Subject: [PATCH 1/9] Relocating Bulk sync mode --- includes/Admin.php | 168 +++++++++++++++++++++++---------------------- 1 file changed, 85 insertions(+), 83 deletions(-) diff --git a/includes/Admin.php b/includes/Admin.php index 11a0049bf..28a6825b8 100644 --- a/includes/Admin.php +++ b/includes/Admin.php @@ -32,6 +32,12 @@ class Admin { /** @var string the "sync disabled" sync mode slug */ const SYNC_MODE_SYNC_DISABLED = 'sync_disabled'; + /** @var string the "include" sync mode for bulk edit */ + const BULK_SYNC_MODE_INCLUDE = 'include'; + + /** @var string the "exclude" sync mode for bulk edit */ + const BULK_SYNC_MODE_EXCLUDE = 'exclude'; + /** @var Product_Categories the product category admin handler */ protected $product_categories; @@ -93,8 +99,8 @@ public function __construct() { add_filter( 'request', array( $this, 'filter_products_by_sync_enabled' ) ); // add bulk actions to manage products sync - add_filter( 'bulk_actions-edit-product', array( $this, 'add_products_sync_bulk_actions' ), 40 ); - add_action( 'handle_bulk_actions-edit-product', array( $this, 'handle_products_sync_bulk_actions' ) ); + add_action('woocommerce_product_bulk_edit_end', array($this,'add_facebook_sync_bulk_edit_dropdown_at_bottom')); + add_action( 'woocommerce_product_bulk_edit_save', array($this,'handle_custom_bulk_sync'), 10, 1 ); // add Product data tab add_filter( 'woocommerce_product_data_tabs', array( $this, 'add_product_settings_tab' ) ); @@ -513,6 +519,37 @@ public function add_products_by_sync_enabled_input_filter() { + + is_virtual() && ! Products::is_sync_enabled_for_product( $product ) ) { - $enabling_sync_virtual_products[ $product->get_id() ] = $product; - } elseif ( $product->is_type( 'variable' ) ) { - // collect the virtual variations - foreach ( $product->get_children() as $variation_id ) { - $variation = wc_get_product( $variation_id ); - if ( $variation && $variation->is_virtual() && ! Products::is_sync_enabled_for_product( $variation ) ) { - $enabling_sync_virtual_products[ $product->get_id() ] = $product; - $enabling_sync_virtual_variations[ $variation->get_id() ] = $variation; - } - } - }//end if - }//end if + if ( $this::BULK_SYNC_MODE_INCLUDE === $sync_mode ) { + if ( $product->is_virtual() && ! Products::is_sync_enabled_for_product( $product ) ) { + $enabling_sync_virtual_products[ $product->get_id() ] = $product; + } elseif ( $product->is_type( 'variable' ) ) { + // collect the virtual variations + foreach ( $product->get_children() as $variation_id ) { + $variation = wc_get_product( $variation_id ); + if ( $variation && $variation->is_virtual() && ! Products::is_sync_enabled_for_product( $variation ) ) { + $enabling_sync_virtual_products[ $product->get_id() ] = $product; + $enabling_sync_virtual_variations[ $variation->get_id() ] = $variation; + } + }//end foreach }//end if - }//end foreach + }//end if + }//end if - if ( ! empty( $enabling_sync_virtual_products ) || ! empty( $enabling_sync_virtual_variations ) ) { - // display notice if enabling sync for virtual products or variations - set_transient( 'wc_' . facebook_for_woocommerce()->get_id() . '_enabling_virtual_products_sync_show_notice_' . get_current_user_id(), true, 15 * MINUTE_IN_SECONDS ); - set_transient( 'wc_' . facebook_for_woocommerce()->get_id() . '_enabling_virtual_products_sync_affected_products_' . get_current_user_id(), array_keys( $enabling_sync_virtual_products ), 15 * MINUTE_IN_SECONDS ); + if ( ! empty( $enabling_sync_virtual_products ) || ! empty( $enabling_sync_virtual_variations ) ) { + // display notice if enabling sync for virtual products or variations + set_transient( 'wc_' . facebook_for_woocommerce()->get_id() . '_enabling_virtual_products_sync_show_notice_' . get_current_user_id(), true, 15 * MINUTE_IN_SECONDS ); + set_transient( 'wc_' . facebook_for_woocommerce()->get_id() . '_enabling_virtual_products_sync_affected_products_' . get_current_user_id(), array_keys( $enabling_sync_virtual_products ), 15 * MINUTE_IN_SECONDS ); - // set visibility for virtual products - foreach ( $enabling_sync_virtual_products as $product ) { + // set visibility for virtual products + foreach ( $enabling_sync_virtual_products as $product ) { - // do not set visibility for variable products - if ( ! $product->is_type( 'variable' ) ) { - Products::set_product_visibility( $product, false ); - } + // do not set visibility for variable products + if ( ! $product->is_type( 'variable' ) ) { + Products::set_product_visibility( $product, false ); } + } - // set visibility for virtual variations - foreach ( $enabling_sync_virtual_variations as $variation ) { + // set visibility for virtual variations + foreach ( $enabling_sync_virtual_variations as $variation ) { - Products::set_product_visibility( $variation, false ); - } - }//end if + Products::set_product_visibility( $variation, false ); + } + }//end if - if ( 'facebook_include' === $action ) { + $products[] = $product; - Products::enable_sync_for_products( $products ); + if ( $this::BULK_SYNC_MODE_INCLUDE == $sync_mode ) { - $this->resync_products( $products ); + Products::enable_sync_for_products( $products ); - } elseif ( 'facebook_exclude' === $action ) { + $this->resync_products( $products ); - Products::disable_sync_for_products( $products ); + } elseif ( $this::BULK_SYNC_MODE_EXCLUDE == $sync_mode ) { - self::add_product_disabled_sync_notice( count( $products ) ); - } - }//end if - }//end if + Products::disable_sync_for_products( $products ); - return $redirect; + self::add_product_disabled_sync_notice( count( $products ) ); + } + } //end if } - /** * Re-syncs the given products. * From 48d8689308a879bfe00112da30cc7b33195345d2 Mon Sep 17 00:00:00 2001 From: Sayan Pandey Date: Fri, 14 Mar 2025 11:19:08 +0000 Subject: [PATCH 2/9] Adding updated strings from figma --- includes/Admin.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/includes/Admin.php b/includes/Admin.php index 28a6825b8..54e01dd39 100644 --- a/includes/Admin.php +++ b/includes/Admin.php @@ -99,8 +99,8 @@ public function __construct() { add_filter( 'request', array( $this, 'filter_products_by_sync_enabled' ) ); // add bulk actions to manage products sync - add_action('woocommerce_product_bulk_edit_end', array($this,'add_facebook_sync_bulk_edit_dropdown_at_bottom')); - add_action( 'woocommerce_product_bulk_edit_save', array($this,'handle_custom_bulk_sync'), 10, 1 ); + add_action( 'woocommerce_product_bulk_edit_end', array( $this, 'add_facebook_sync_bulk_edit_dropdown_at_bottom' ) ); + add_action( 'woocommerce_product_bulk_edit_save', array( $this, 'handle_custom_bulk_sync' ), 10, 1 ); // add Product data tab add_filter( 'woocommerce_product_data_tabs', array( $this, 'add_product_settings_tab' ) ); @@ -524,9 +524,7 @@ public function add_products_by_sync_enabled_input_filter() { * Adds a dropdown input to Include or Exclude product in Facebook Bulk Sync. * * @internal - * */ - function add_facebook_sync_bulk_edit_dropdown_at_bottom() { global $typenow; @@ -539,11 +537,12 @@ function add_facebook_sync_bulk_edit_dropdown_at_bottom() { ?> @@ -895,7 +894,6 @@ private function add_query_vars_to_find_hidden_products( array $query_vars ) { * * @internal * - * * @param string $redirect admin URL used by WordPress to redirect after performing the bulk action * @return string */ @@ -903,7 +901,7 @@ public function handle_custom_bulk_sync( $product_edit ) { $sync_mode = isset( $_GET['facebook_bulk_sync_options'] ) ? (string) sanitize_text_field( wp_unslash( $_GET['facebook_bulk_sync_options'] ) ) : null; - if ($sync_mode) { + if ( $sync_mode ) { /** @var \WC_Product[] $enabling_sync_virtual_products virtual products that are being included */ $enabling_sync_virtual_products = []; /** @var \WC_Product_Variation[] $enabling_sync_virtual_variations virtual variations that are being included */ @@ -950,13 +948,13 @@ public function handle_custom_bulk_sync( $product_edit ) { $products[] = $product; - if ( $this::BULK_SYNC_MODE_INCLUDE == $sync_mode ) { + if ( $this::BULK_SYNC_MODE_INCLUDE === $sync_mode ) { Products::enable_sync_for_products( $products ); $this->resync_products( $products ); - } elseif ( $this::BULK_SYNC_MODE_EXCLUDE == $sync_mode ) { + } elseif ( $this::BULK_SYNC_MODE_EXCLUDE === $sync_mode ) { Products::disable_sync_for_products( $products ); From 57b51743fcdd0d5f28677053b9cacbe104b26363 Mon Sep 17 00:00:00 2001 From: Sayan Pandey Date: Fri, 14 Mar 2025 11:33:47 +0000 Subject: [PATCH 3/9] Fixing lint issues --- includes/Admin.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/includes/Admin.php b/includes/Admin.php index 54e01dd39..b8e1356b3 100644 --- a/includes/Admin.php +++ b/includes/Admin.php @@ -525,7 +525,7 @@ public function add_products_by_sync_enabled_input_filter() { * * @internal */ - function add_facebook_sync_bulk_edit_dropdown_at_bottom() { + public function add_facebook_sync_bulk_edit_dropdown_at_bottom() { global $typenow; if ( 'product' !== $typenow ) { @@ -537,7 +537,7 @@ function add_facebook_sync_bulk_edit_dropdown_at_bottom() { ?> diff --git a/includes/Products.php b/includes/Products.php index c578c913d..c10bfa027 100644 --- a/includes/Products.php +++ b/includes/Products.php @@ -91,7 +91,7 @@ private static function set_sync_for_products( array $products, $enabled ) { } // Remove excluded product from FB. - if ( "no" === $enabled && self::product_should_be_deleted( $product ) ) { + if ( "no" === $enabled ) { facebook_for_woocommerce()->get_integration()->delete_fb_product( $product ); } From 83a294f116f8deefb73ad65eac65a942c665a0a7 Mon Sep 17 00:00:00 2001 From: Sayan Pandey Date: Fri, 14 Mar 2025 15:29:31 +0000 Subject: [PATCH 5/9] Removing lint whitespace --- includes/Admin.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/includes/Admin.php b/includes/Admin.php index d3fd92770..b05e27e10 100644 --- a/includes/Admin.php +++ b/includes/Admin.php @@ -480,9 +480,7 @@ public function add_product_list_table_columns_content( $column ) { } if ( $should_sync ) { - esc_html_e( 'Synced', 'facebook-for-woocommerce' ); - } else { esc_html_e( 'Not synced', 'facebook-for-woocommerce' ); if ( ! empty( $no_sync_reason ) ) { From b978a45f763d9961425ee171d1ad79cbc04f53e5 Mon Sep 17 00:00:00 2001 From: Sayan Pandey Date: Wed, 19 Mar 2025 15:49:16 +0000 Subject: [PATCH 6/9] Fixing tests --- .../Unit/WCFacebookCommerceIntegrationTest.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/Unit/WCFacebookCommerceIntegrationTest.php b/tests/Unit/WCFacebookCommerceIntegrationTest.php index 3a2c301cf..fc2f2f368 100644 --- a/tests/Unit/WCFacebookCommerceIntegrationTest.php +++ b/tests/Unit/WCFacebookCommerceIntegrationTest.php @@ -573,11 +573,17 @@ public function test_on_product_save_existing_simple_product_sync_enabled_update */ public function test_on_product_save_existing_simple_product_sync_disabled_updates_the_product() { $product_to_update = WC_Helper_Product::create_simple_product(); - $product_to_delete = WC_Helper_Product::create_simple_product(); + + // The idea of the following mock is to ovveride the delete_product_item. + // The test is that the product item is being deleted as when it is marked for do not sync. + // The mock below is hit otherwise it would generate a random Mock_Response and throw error + $integration_mock = $this->createMock(WC_Facebookcommerce_Integration::class); + $integration_mock->method('delete_product_item'); + $this->integration = $integration_mock; $_POST['wc_facebook_sync_mode'] = Admin::SYNC_MODE_SYNC_DISABLED; - $_POST[ WC_Facebook_Product::FB_REMOVE_FROM_SYNC ] = $product_to_delete->get_id(); + $_POST[ WC_Facebook_Product::FB_REMOVE_FROM_SYNC ] = $product_to_update->get_id(); $product_to_update->set_stock_status( 'instock' ); @@ -586,11 +592,8 @@ public function test_on_product_save_existing_simple_product_sync_disabled_updat $this->integration->on_product_save( $product_to_update->get_id() ); - $this->assertEquals( null, get_post_meta( $product_to_delete->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, true ) ); - $this->assertEquals( null, get_post_meta( $product_to_delete->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_GROUP_ID, true ) ); - $this->assertEquals( 'no', get_post_meta( $product_to_update->get_id(), Products::SYNC_ENABLED_META_KEY, true ) ); - $this->assertEquals( 'no', get_post_meta( $product_to_update->get_id(), Products::VISIBILITY_META_KEY, true ) ); - + $this->assertEquals( 'facebook-product-item-id', get_post_meta( $product_to_update->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, true ) ); + $this->assertEquals( null, get_post_meta( $product_to_update->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_GROUP_ID, true ) ); } /** From eeb211beb89873e7c6b2cb1e17d8287474d8da57 Mon Sep 17 00:00:00 2001 From: Sayan Pandey Date: Tue, 1 Apr 2025 16:11:50 +0100 Subject: [PATCH 7/9] Fixing typo --- tests/Unit/WCFacebookCommerceIntegrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/WCFacebookCommerceIntegrationTest.php b/tests/Unit/WCFacebookCommerceIntegrationTest.php index fc2f2f368..97a3451e8 100644 --- a/tests/Unit/WCFacebookCommerceIntegrationTest.php +++ b/tests/Unit/WCFacebookCommerceIntegrationTest.php @@ -574,7 +574,7 @@ public function test_on_product_save_existing_simple_product_sync_enabled_update public function test_on_product_save_existing_simple_product_sync_disabled_updates_the_product() { $product_to_update = WC_Helper_Product::create_simple_product(); - // The idea of the following mock is to ovveride the delete_product_item. + // The idea of the following mock is to overide the delete_product_item. // The test is that the product item is being deleted as when it is marked for do not sync. // The mock below is hit otherwise it would generate a random Mock_Response and throw error $integration_mock = $this->createMock(WC_Facebookcommerce_Integration::class); From 65f838a3a0b753a0c8a0ead855e110087e61722e Mon Sep 17 00:00:00 2001 From: Sayan Pandey Date: Wed, 2 Apr 2025 14:30:10 +0100 Subject: [PATCH 8/9] Providng better names for the flags --- includes/Admin.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/Admin.php b/includes/Admin.php index b05e27e10..6fe15c6c4 100644 --- a/includes/Admin.php +++ b/includes/Admin.php @@ -33,10 +33,10 @@ class Admin { const SYNC_MODE_SYNC_DISABLED = 'sync_disabled'; /** @var string the "include" sync mode for bulk edit */ - const BULK_SYNC_MODE_INCLUDE = 'include'; + const INCLUDE_FACEBOOK_SYNC = 'fb_sync_enabled'; /** @var string the "exclude" sync mode for bulk edit */ - const BULK_SYNC_MODE_EXCLUDE = 'exclude'; + const EXCLUDE_FACEBOOK_SYNC = 'fb_sync_disabled'; /** @var Product_Categories the product category admin handler */ protected $product_categories; @@ -537,8 +537,8 @@ public function add_facebook_sync_bulk_edit_dropdown_at_bottom() { @@ -906,7 +906,7 @@ public function handle_custom_bulk_sync( $product_edit ) { $product = wc_get_product( $product_edit ); if ( $product ) { - if ( $this::BULK_SYNC_MODE_INCLUDE === $sync_mode ) { + if ( $this::INCLUDE_FACEBOOK_SYNC === $sync_mode ) { if ( $product->is_virtual() && ! Products::is_sync_enabled_for_product( $product ) ) { $enabling_sync_virtual_products[ $product->get_id() ] = $product; } elseif ( $product->is_type( 'variable' ) ) { @@ -945,13 +945,13 @@ public function handle_custom_bulk_sync( $product_edit ) { $products[] = $product; - if ( $this::BULK_SYNC_MODE_INCLUDE === $sync_mode ) { + if ( $this::INCLUDE_FACEBOOK_SYNC === $sync_mode ) { Products::enable_sync_for_products( $products ); $this->resync_products( $products ); - } elseif ( $this::BULK_SYNC_MODE_EXCLUDE === $sync_mode ) { + } elseif ( $this::EXCLUDE_FACEBOOK_SYNC === $sync_mode ) { Products::disable_sync_for_products( $products ); From 1ccded0408aaf7e159581278f8db54f21c42ce0e Mon Sep 17 00:00:00 2001 From: Sayan Pandey Date: Thu, 3 Apr 2025 17:30:18 +0100 Subject: [PATCH 9/9] Fixing Diff comments --- facebook-commerce.php | 3 --- includes/Admin.php | 57 +++++++++++++++---------------------------- 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/facebook-commerce.php b/facebook-commerce.php index d7566a03b..75f23a91d 100644 --- a/facebook-commerce.php +++ b/facebook-commerce.php @@ -853,9 +853,6 @@ public function on_product_save( int $wp_id ) { $this->save_product_settings( $product ); } else { // if previously enabled, add a notice on the next page load - if ( Products::is_sync_enabled_for_product( $product ) ) { - Admin::add_product_disabled_sync_notice(); - } Products::disable_sync_for_products( [ $product ] ); if ( in_array( $wp_id, $products_to_delete_from_facebook, true ) ) { $this->delete_fb_product( $product ); diff --git a/includes/Admin.php b/includes/Admin.php index 6fe15c6c4..e9161a7d9 100644 --- a/includes/Admin.php +++ b/includes/Admin.php @@ -33,10 +33,10 @@ class Admin { const SYNC_MODE_SYNC_DISABLED = 'sync_disabled'; /** @var string the "include" sync mode for bulk edit */ - const INCLUDE_FACEBOOK_SYNC = 'fb_sync_enabled'; + const BULK_EDIT_SYNC = 'bulk_edit_sync'; /** @var string the "exclude" sync mode for bulk edit */ - const EXCLUDE_FACEBOOK_SYNC = 'fb_sync_disabled'; + const BULK_EDIT_DELETE = 'bulk_edit_delete'; /** @var Product_Categories the product category admin handler */ protected $product_categories; @@ -100,7 +100,7 @@ public function __construct() { // add bulk actions to manage products sync add_action( 'woocommerce_product_bulk_edit_end', array( $this, 'add_facebook_sync_bulk_edit_dropdown_at_bottom' ) ); - add_action( 'woocommerce_product_bulk_edit_save', array( $this, 'handle_custom_bulk_sync' ), 10, 1 ); + add_action( 'woocommerce_product_bulk_edit_save', array( $this, 'handle_products_sync_bulk_actions' ), 10, 1 ); // add Product data tab add_filter( 'woocommerce_product_data_tabs', array( $this, 'add_product_settings_tab' ) ); @@ -537,8 +537,8 @@ public function add_facebook_sync_bulk_edit_dropdown_at_bottom() { @@ -893,7 +893,7 @@ private function add_query_vars_to_find_hidden_products( array $query_vars ) { * * @param string $product_edit the product metadata that is being edited. */ - public function handle_custom_bulk_sync( $product_edit ) { + public function handle_products_sync_bulk_actions( $product_edit ) { $sync_mode = isset( $_GET['facebook_bulk_sync_options'] ) ? (string) sanitize_text_field( wp_unslash( $_GET['facebook_bulk_sync_options'] ) ) : null; @@ -905,19 +905,19 @@ public function handle_custom_bulk_sync( $product_edit ) { /** @var \WC_Product $product to store the product meta data */ $product = wc_get_product( $product_edit ); - if ( $product ) { - if ( $this::INCLUDE_FACEBOOK_SYNC === $sync_mode ) { - if ( $product->is_virtual() && ! Products::is_sync_enabled_for_product( $product ) ) { + if ( $product && $this::BULK_EDIT_SYNC === $sync_mode ) { + if ( $product->is_virtual() && ! Products::is_sync_enabled_for_product( $product ) ) { + $enabling_sync_virtual_products[ $product->get_id() ] = $product; + } elseif ( $product->is_type( 'variable' ) ) { + // collect the virtual variations + foreach ( $product->get_children() as $variation_id ) { + $variation = wc_get_product( $variation_id ); + if ( $variation && $variation->is_virtual() && ! Products::is_sync_enabled_for_product( $variation ) ) { + $enabling_sync_virtual_variations[ $variation->get_id() ] = $variation; + } + }//end foreach + if ( ! empty( $enabling_sync_virtual_variations ) ) { $enabling_sync_virtual_products[ $product->get_id() ] = $product; - } elseif ( $product->is_type( 'variable' ) ) { - // collect the virtual variations - foreach ( $product->get_children() as $variation_id ) { - $variation = wc_get_product( $variation_id ); - if ( $variation && $variation->is_virtual() && ! Products::is_sync_enabled_for_product( $variation ) ) { - $enabling_sync_virtual_products[ $product->get_id() ] = $product; - $enabling_sync_virtual_variations[ $variation->get_id() ] = $variation; - } - }//end foreach }//end if }//end if }//end if @@ -945,17 +945,16 @@ public function handle_custom_bulk_sync( $product_edit ) { $products[] = $product; - if ( $this::INCLUDE_FACEBOOK_SYNC === $sync_mode ) { + if ( $this::BULK_EDIT_SYNC === $sync_mode ) { Products::enable_sync_for_products( $products ); $this->resync_products( $products ); - } elseif ( $this::EXCLUDE_FACEBOOK_SYNC === $sync_mode ) { + } elseif ( $this::BULK_EDIT_DELETE === $sync_mode ) { Products::disable_sync_for_products( $products ); - self::add_product_disabled_sync_notice( count( $products ) ); } } //end if } @@ -991,22 +990,6 @@ private function resync_products( array $products ) { } } - - /** - * Adds a transient so an informational notice is displayed on the next page load. - * - * @since 2.0.0 - * - * @param int $count number of products - */ - public static function add_product_disabled_sync_notice( $count = 1 ) { - - if ( ! facebook_for_woocommerce()->get_admin_notice_handler()->is_notice_dismissed( 'wc-' . facebook_for_woocommerce()->get_id_dasherized() . '-product-disabled-sync' ) ) { - set_transient( 'wc_' . facebook_for_woocommerce()->get_id() . '_show_product_disabled_sync_notice_' . get_current_user_id(), $count, MINUTE_IN_SECONDS ); - } - } - - /** * Adds a message for after a product or set of products get excluded from sync. *