From 26d23b8d02b9bc45504ff1389098ff19b4a8c6f4 Mon Sep 17 00:00:00 2001 From: Noah Ostrowski Date: Mon, 5 May 2025 19:15:11 -0700 Subject: [PATCH 1/3] Setting up shipping profile feed upload button and updating shipping profile feed upload to every day instead of every hour --- assets/js/admin/enhanced-settings-sync.js | 33 +++++++++++++++++ includes/AJAX.php | 35 +++++++++++++++---- includes/Admin/Settings_Screens/Shops.php | 26 ++++++++++++-- .../ShippingProfiles/ShippingProfilesFeed.php | 5 --- 4 files changed, 84 insertions(+), 15 deletions(-) diff --git a/assets/js/admin/enhanced-settings-sync.js b/assets/js/admin/enhanced-settings-sync.js index 56adb5332..6644253bd 100644 --- a/assets/js/admin/enhanced-settings-sync.js +++ b/assets/js/admin/enhanced-settings-sync.js @@ -73,4 +73,37 @@ jQuery(document).ready(function($) { button.prop('disabled', false); }); }); + + /** + * Handle the sync shipping profiles button click event + * + * @since 3.5.0 + * + * @param {object} event + */ + $('#wc-facebook-enhanced-settings-sync-shipping-profiles').click(function(event) { + event.preventDefault(); + var button = $(this); + + button.html('Syncing...'); + button.prop('disabled', true); + + var data = { + action: "wc_facebook_sync_shipping_profiles", + nonce: wc_facebook_enhanced_settings_sync.sync_shipping_profiles_nonce + }; + + $.post(wc_facebook_enhanced_settings_sync.ajax_url, data, function(response) { + if (response.success) { + button.html('Sync completed'); + button.prop('disabled', false); + } else { + button.html('Sync failed'); + button.prop('disabled', false); + } + }).fail(function() { + button.html('Sync failed'); + button.prop('disabled', false); + }); + }); }); diff --git a/includes/AJAX.php b/includes/AJAX.php index 74c82cf5c..405987f5f 100644 --- a/includes/AJAX.php +++ b/includes/AJAX.php @@ -45,6 +45,9 @@ public function __construct() { // sync all coupons via AJAX add_action( 'wp_ajax_wc_facebook_sync_coupons', array( $this, 'sync_coupons' ) ); + // sync all shipping profiles via AJAX + add_action( 'wp_ajax_wc_facebook_sync_shipping_profiles', array( $this, 'sync_shipping_profiles' ) ); + // get the current sync status add_action( 'wp_ajax_wc_facebook_get_sync_status', array( $this, 'get_sync_status' ) ); @@ -117,13 +120,13 @@ public function sync_products() { } } - /** - * Syncs all coupons via AJAX. - * - * @internal - * - * @since 3.5.0 - */ + /** + * Syncs all coupons via AJAX. + * + * @internal + * + * @since 3.5.0 + */ public function sync_coupons() { check_admin_referer( Shops::ACTION_SYNC_COUPONS, 'nonce' ); @@ -135,6 +138,24 @@ public function sync_coupons() { } } + /** + * Syncs all shipping profiles via AJAX. + * + * @internal + * + * @since 3.5.0 + */ + public function sync_shipping_profiles() { + check_admin_referer( Shops::ACTION_SYNC_SHIPPING_PROFILES, 'nonce' ); + + try { + facebook_for_woocommerce()->feed_manager->get_feed_instance( 'shipping_profiles' )->regenerate_feed(); + wp_send_json_success(); + } catch ( \Exception $exception ) { + wp_send_json_error( $exception->getMessage() ); + } + } + /** * Gets the current sync status. diff --git a/includes/Admin/Settings_Screens/Shops.php b/includes/Admin/Settings_Screens/Shops.php index e26ce8950..4440b23ab 100644 --- a/includes/Admin/Settings_Screens/Shops.php +++ b/includes/Admin/Settings_Screens/Shops.php @@ -31,6 +31,9 @@ class Shops extends Abstract_Settings_Screen { /** @var string */ const ACTION_SYNC_COUPONS = 'wc_facebook_sync_coupons'; + /** @var string */ + const ACTION_SYNC_SHIPPING_PROFILES = 'wc_facebook_sync_shipping_profiles'; + /** * Shops constructor. * @@ -126,9 +129,10 @@ public function enqueue_assets() { 'wc-facebook-enhanced-settings-sync', 'wc_facebook_enhanced_settings_sync', array( - 'ajax_url' => admin_url( 'admin-ajax.php' ), - 'sync_products_nonce' => wp_create_nonce( self::ACTION_SYNC_PRODUCTS ), - 'sync_coupons_nonce' => wp_create_nonce( self::ACTION_SYNC_COUPONS ), + 'ajax_url' => admin_url( 'admin-ajax.php' ), + 'sync_products_nonce' => wp_create_nonce( self::ACTION_SYNC_PRODUCTS ), + 'sync_coupons_nonce' => wp_create_nonce( self::ACTION_SYNC_COUPONS ), + 'sync_shipping_profiles_nonce' => wp_create_nonce( self::ACTION_SYNC_SHIPPING_PROFILES ), ) ); } @@ -234,6 +238,22 @@ class="button"

+ + + Shipping profiles sync + + + +

+ Manually sync your shipping profiles from WooCommerce to your shop. It may take a couple of minutes for the changes to populate. +

+ + diff --git a/includes/Feed/ShippingProfiles/ShippingProfilesFeed.php b/includes/Feed/ShippingProfiles/ShippingProfilesFeed.php index c54488028..72a639359 100644 --- a/includes/Feed/ShippingProfiles/ShippingProfilesFeed.php +++ b/includes/Feed/ShippingProfiles/ShippingProfilesFeed.php @@ -58,11 +58,6 @@ protected static function get_data_stream_name(): string { return FeedManager::SHIPPING_PROFILES; } - protected static function get_feed_gen_interval(): int { - return HOUR_IN_SECONDS; - } - - /** * @throws \Exception If an error is encountered mapping shipping profile data. */ From 97f8f89536d3c85ef08f467430636f9de8911b50 Mon Sep 17 00:00:00 2001 From: Noah Ostrowski Date: Tue, 6 May 2025 08:40:26 -0700 Subject: [PATCH 2/3] Fixing test causing issue --- tests/Unit/RolloutSwitchesTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Unit/RolloutSwitchesTest.php b/tests/Unit/RolloutSwitchesTest.php index 0fc140604..6c8bcfdb8 100644 --- a/tests/Unit/RolloutSwitchesTest.php +++ b/tests/Unit/RolloutSwitchesTest.php @@ -5,7 +5,7 @@ use WooCommerce\Facebook\Framework\Api\Exception as ApiException; use WooCommerce\Facebook\RolloutSwitches; -class RolloutSwitchesTest extends \WooCommerce\Facebook\Tests\Unit\AbstractWPUnitTestWithSafeFiltering { +class RolloutSwitchesTest extends \WooCommerce\Facebook\Tests\AbstractWPUnitTestWithSafeFiltering { /** * Facebook Graph API endpoint. @@ -20,7 +20,7 @@ class RolloutSwitchesTest extends \WooCommerce\Facebook\Tests\Unit\AbstractWPUni * @var string */ private $version = Api::API_VERSION; - + /** * @var Api */ @@ -36,8 +36,8 @@ public function setUp(): void { parent::setUp(); $this->api = new Api( $this->access_token ); } - - public function test_api() { + + public function test_api() { $response = function( $result, $parsed_args, $url ) { $this->assertEquals( 'GET', $parsed_args['method'] ); $url_params = "access_token={$this->access_token}&fbe_external_business_id={$this->external_business_id}"; @@ -52,7 +52,7 @@ public function test_api() { ]; }; $this->add_filter_with_safe_teardown( 'pre_http_request', $response, 10, 3 ); - + $response = $this->api->get_rollout_switches( $this->external_business_id ); $this->assertEquals([ [ @@ -70,8 +70,8 @@ public function test_api() { ], $response->get_data()); } - public function test_plugin() { - + public function test_plugin() { + // mock the active filters to test business values $plugin = facebook_for_woocommerce(); $plugin_ref_obj = new ReflectionObject( $plugin ); @@ -98,7 +98,7 @@ public function test_plugin() { ) )))); $prop_api->setValue( $plugin, $mock_api ); - + $switch_mock = $this->getMockBuilder(RolloutSwitches::class) ->setConstructorArgs( array( $plugin ) ) ->onlyMethods(['is_switch_active']) @@ -122,7 +122,7 @@ public function test_plugin() { // If the feature is active and in the response -> response value $this->assertEquals( $switch_mock->is_switch_enabled("switch_a"), true ); $this->assertEquals( $switch_mock->is_switch_enabled("switch_b"), false ); - + // If the switch is active but not in the response -> TRUE $this->assertEquals( $switch_mock->is_switch_enabled("switch_d"), true ); } From 260b4d73e7979e50ff2fc4952ac526c5ea048286 Mon Sep 17 00:00:00 2001 From: Noah Ostrowski Date: Tue, 6 May 2025 08:55:35 -0700 Subject: [PATCH 3/3] Reverting file back to main as another PR/diff will fix it --- tests/Unit/RolloutSwitchesTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Unit/RolloutSwitchesTest.php b/tests/Unit/RolloutSwitchesTest.php index 6c8bcfdb8..0fc140604 100644 --- a/tests/Unit/RolloutSwitchesTest.php +++ b/tests/Unit/RolloutSwitchesTest.php @@ -5,7 +5,7 @@ use WooCommerce\Facebook\Framework\Api\Exception as ApiException; use WooCommerce\Facebook\RolloutSwitches; -class RolloutSwitchesTest extends \WooCommerce\Facebook\Tests\AbstractWPUnitTestWithSafeFiltering { +class RolloutSwitchesTest extends \WooCommerce\Facebook\Tests\Unit\AbstractWPUnitTestWithSafeFiltering { /** * Facebook Graph API endpoint. @@ -20,7 +20,7 @@ class RolloutSwitchesTest extends \WooCommerce\Facebook\Tests\AbstractWPUnitTest * @var string */ private $version = Api::API_VERSION; - + /** * @var Api */ @@ -36,8 +36,8 @@ public function setUp(): void { parent::setUp(); $this->api = new Api( $this->access_token ); } - - public function test_api() { + + public function test_api() { $response = function( $result, $parsed_args, $url ) { $this->assertEquals( 'GET', $parsed_args['method'] ); $url_params = "access_token={$this->access_token}&fbe_external_business_id={$this->external_business_id}"; @@ -52,7 +52,7 @@ public function test_api() { ]; }; $this->add_filter_with_safe_teardown( 'pre_http_request', $response, 10, 3 ); - + $response = $this->api->get_rollout_switches( $this->external_business_id ); $this->assertEquals([ [ @@ -70,8 +70,8 @@ public function test_api() { ], $response->get_data()); } - public function test_plugin() { - + public function test_plugin() { + // mock the active filters to test business values $plugin = facebook_for_woocommerce(); $plugin_ref_obj = new ReflectionObject( $plugin ); @@ -98,7 +98,7 @@ public function test_plugin() { ) )))); $prop_api->setValue( $plugin, $mock_api ); - + $switch_mock = $this->getMockBuilder(RolloutSwitches::class) ->setConstructorArgs( array( $plugin ) ) ->onlyMethods(['is_switch_active']) @@ -122,7 +122,7 @@ public function test_plugin() { // If the feature is active and in the response -> response value $this->assertEquals( $switch_mock->is_switch_enabled("switch_a"), true ); $this->assertEquals( $switch_mock->is_switch_enabled("switch_b"), false ); - + // If the switch is active but not in the response -> TRUE $this->assertEquals( $switch_mock->is_switch_enabled("switch_d"), true ); }