diff --git a/assets/js/admin/enhanced-settings-sync.js b/assets/js/admin/enhanced-settings-sync.js index 6644253bd..ff3381c04 100644 --- a/assets/js/admin/enhanced-settings-sync.js +++ b/assets/js/admin/enhanced-settings-sync.js @@ -106,4 +106,37 @@ jQuery(document).ready(function($) { button.prop('disabled', false); }); }); + + /** + * Handle the sync navigation menu button click event + * + * @since 3.5.0 + * + * @param {object} event + */ + $('#wc-facebook-enhanced-settings-sync-navigation-menu').click(function(event) { + event.preventDefault(); + var button = $(this); + + button.html('Syncing...'); + button.prop('disabled', true); + + var data = { + action: "wc_facebook_sync_navigation_menu", + nonce: wc_facebook_enhanced_settings_sync.sync_navigation_menu_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 93fe5d165..a32ae362d 100644 --- a/includes/AJAX.php +++ b/includes/AJAX.php @@ -49,6 +49,9 @@ public function __construct() { // sync all shipping profiles via AJAX add_action( 'wp_ajax_wc_facebook_sync_shipping_profiles', array( $this, 'sync_shipping_profiles' ) ); + // sync navigation menu via AJAX + add_action( 'wp_ajax_wc_facebook_sync_navigation_menu', array( $this, 'sync_navigation_menu' ) ); + // get the current sync status add_action( 'wp_ajax_wc_facebook_get_sync_status', array( $this, 'get_sync_status' ) ); @@ -184,6 +187,24 @@ public function sync_shipping_profiles() { } } + /** + * Syncs navigation menu via AJAX. + * + * @internal + * + * @since 3.5.0 + */ + public function sync_navigation_menu() { + check_admin_referer( Shops::ACTION_SYNC_NAVIGATION_MENU, 'nonce' ); + + try { + facebook_for_woocommerce()->feed_manager->get_feed_instance( 'navigation_menu' )->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 4440b23ab..88be63733 100644 --- a/includes/Admin/Settings_Screens/Shops.php +++ b/includes/Admin/Settings_Screens/Shops.php @@ -34,6 +34,9 @@ class Shops extends Abstract_Settings_Screen { /** @var string */ const ACTION_SYNC_SHIPPING_PROFILES = 'wc_facebook_sync_shipping_profiles'; + /** @var string */ + const ACTION_SYNC_NAVIGATION_MENU = 'wc_facebook_sync_navigation_menu'; + /** * Shops constructor. * @@ -133,6 +136,7 @@ public function enqueue_assets() { '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 ), + 'sync_navigation_menu_nonce' => wp_create_nonce( self::ACTION_SYNC_NAVIGATION_MENU ), ) ); } @@ -254,6 +258,22 @@ class="button"
+