diff --git a/assets/js/admin/facebook-product-settings.js b/assets/js/admin/facebook-product-settings.js new file mode 100644 index 000000000..02e77c7bd --- /dev/null +++ b/assets/js/admin/facebook-product-settings.js @@ -0,0 +1,34 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved + * + * This source code is licensed under the license found in the + * LICENSE file in the root directory of this source tree. + * + * @package FacebookCommerce + */ +jQuery( document ).ready( function( $ ) { + + + /** + * Toggles (enables/disables) Facebook setting fields. + * + * @since x.y.z + * + * @param {boolean} enabled + */ + function toggleFacebookSettings( enabled ) { + + $( '.enable-if-sync-enabled' ).prop( 'disabled', ! enabled ); + } + + + const syncEnabledCheckbox = $( '#fb_sync_enabled' ); + + syncEnabledCheckbox.on( 'click', function() { + toggleFacebookSettings( $( this ).prop( 'checked' ) ); + } ); + + toggleFacebookSettings( syncEnabledCheckbox.prop( 'checked' ) ); +} ); + + diff --git a/facebook-commerce.php b/facebook-commerce.php index 72c9811e5..94c0ba8c2 100644 --- a/facebook-commerce.php +++ b/facebook-commerce.php @@ -8,6 +8,8 @@ * @package FacebookCommerce */ +use SkyVerge\WooCommerce\Facebook\Products; + if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } @@ -245,54 +247,8 @@ public function __construct() { // Only load product processing hooks if we have completed setup. if ( $this->api_key && $this->product_catalog_id ) { - add_action( - 'woocommerce_process_product_meta_simple', - array( $this, 'on_simple_product_publish' ), - 10, // Action priority - 1 // Args passed to on_product_publish (should be 'id') - ); - - add_action( - 'woocommerce_process_product_meta_variable', - array( $this, 'on_variable_product_publish' ), - 10, // Action priority - 1 // Args passed to on_product_publish (should be 'id') - ); - add_action( - 'woocommerce_process_product_meta_booking', - array( $this, 'on_simple_product_publish' ), - 10, // Action priority - 1 // Args passed to on_product_publish (should be 'id') - ); - - add_action( - 'woocommerce_process_product_meta_external', - array( $this, 'on_simple_product_publish' ), - 10, // Action priority - 1 // Args passed to on_product_publish (should be 'id') - ); - - add_action( - 'woocommerce_process_product_meta_subscription', - array( $this, 'on_product_publish' ), - 10, // Action priority - 1 // Args passed to on_product_publish (should be 'id') - ); - - add_action( - 'woocommerce_process_product_meta_variable-subscription', - array( $this, 'on_product_publish' ), - 10, // Action priority - 1 // Args passed to on_product_publish (should be 'id') - ); - - add_action( - 'woocommerce_process_product_meta_bundle', - array( $this, 'on_product_publish' ), - 10, // Action priority - 1 // Args passed to on_product_publish (should be 'id') - ); + add_action( 'woocommerce_process_product_meta', [ $this, 'on_product_save' ] ); add_action( 'woocommerce_product_quick_edit_save', @@ -324,17 +280,6 @@ public function __construct() { 3 ); - // Product data tab - add_filter( - 'woocommerce_product_data_tabs', - array( $this, 'fb_new_product_tab' ) - ); - - add_action( - 'woocommerce_product_data_panels', - array( $this, 'fb_new_product_tab_content' ) - ); - add_action( 'wp_ajax_ajax_fb_toggle_visibility', array( $this, 'ajax_fb_toggle_visibility' ) @@ -449,121 +394,33 @@ public function ajax_fb_background_check_queue() { wp_die(); } + + /** + * Adds a new tab to the Product edit page. + * + * @internal + * @deprecated since x.y.z + * + * @param array $tabs array of tabs + * @return array + */ public function fb_new_product_tab( $tabs ) { - $tabs['fb_commerce_tab'] = array( - 'label' => __( 'Facebook', 'facebook-for-woocommerce' ), - 'target' => 'facebook_options', - 'class' => array( 'show_if_simple', 'show_if_variable' ), - ); - return $tabs; - } - public function fb_new_product_tab_content() { - global $post; - $woo_product = new WC_Facebook_Product( $post->ID ); - $description = get_post_meta( - $post->ID, - self::FB_PRODUCT_DESCRIPTION, - true - ); + wc_deprecated_function( __METHOD__, 'x.y.z', '\\SkyVerge\\WooCommerce\\Facebook\\Admin::add_product_settings_tab()' ); - $price = get_post_meta( - $post->ID, - WC_Facebook_Product::FB_PRODUCT_PRICE, - true - ); + return $tabs; + } - $image = get_post_meta( - $post->ID, - WC_Facebook_Product::FB_PRODUCT_IMAGE, - true - ); - $image_setting = null; - if ( WC_Facebookcommerce_Utils::is_variable_type( $woo_product->get_type() ) ) { - $image_setting = $woo_product->get_use_parent_image(); - } + /** + * Adds content to the new Facebook tab on the Product edit page. + * + * @internal + * @deprecated since x.y.z + */ + public function fb_new_product_tab_content() { - // 'id' attribute needs to match the 'target' parameter set above - ?>
- self::FB_PRODUCT_DESCRIPTION, - 'label' => __( 'Facebook Description', 'facebook-for-woocommerce' ), - 'desc_tip' => 'true', - 'description' => __( - 'Custom (plain-text only) description for product on Facebook. ' . - 'If blank, product description will be used. ' . - 'If product description is blank, shortname will be used.', - 'facebook-for-woocommerce' - ), - 'cols' => 40, - 'rows' => 20, - 'value' => $description, - ) - ); - woocommerce_wp_textarea_input( - array( - 'id' => WC_Facebook_Product::FB_PRODUCT_IMAGE, - 'label' => __( 'Facebook Product Image', 'facebook-for-woocommerce' ), - 'desc_tip' => 'true', - 'description' => __( - 'Image URL for product on Facebook. Must be an absolute URL ' . - 'e.g. https://...' . - 'This can be used to override the primary image that will be ' . - 'used on Facebook for this product. If blank, the primary ' . - 'product image in Woo will be used as the primary image on FB.', - 'facebook-for-woocommerce' - ), - 'cols' => 40, - 'rows' => 10, - 'value' => $image, - ) - ); - woocommerce_wp_text_input( - array( - 'id' => WC_Facebook_Product::FB_PRODUCT_PRICE, - 'label' => __( - 'Facebook Price (' . - get_woocommerce_currency_symbol() . ')', - 'facebook-for-woocommerce' - ), - 'desc_tip' => 'true', - 'description' => __( - 'Custom price for product on Facebook. ' . - 'Please enter in monetary decimal (.) format without thousand ' . - 'separators and currency symbols. ' . - 'If blank, product price will be used. ', - 'facebook-for-woocommerce' - ), - 'cols' => 40, - 'rows' => 60, - 'value' => $price, - ) - ); - if ( $image_setting !== null ) { - woocommerce_wp_checkbox( - array( - 'id' => self::FB_VARIANT_IMAGE, - 'label' => __( 'Use Parent Image', 'facebook-for-woocommerce' ), - 'required' => false, - 'desc_tip' => 'true', - 'description' => __( - ' By default, the primary image uploaded to Facebook is the image' . - ' specified in each variant, if provided. ' . - ' However, if you enable this setting, the ' . - ' image of the parent will be used as the primary image' . - ' for this product and all its variants instead.' - ), - 'value' => $image_setting ? 'yes' : 'no', - ) - ); - } - ?> -
-
- get_type() ) { + + case 'simple': + case 'booking': + case 'external': + $this->on_simple_product_publish( $wp_id ); + break; + + case 'variable': + $this->on_variable_product_publish( $wp_id ); + break; + + case 'subscription': + case 'variable-subscription': + case 'bundle': + $this->on_product_publish( $wp_id ); + break; + } + } + } + + function on_product_delete( $wp_id ) { $woo_product = new WC_Facebook_Product( $wp_id ); if ( ! $woo_product->exists() ) { diff --git a/includes/Admin.php b/includes/Admin.php index 23d9d9dcc..ff72a623b 100644 --- a/includes/Admin.php +++ b/includes/Admin.php @@ -27,6 +27,9 @@ class Admin { */ public function __construct() { + // enqueue admin scripts + add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); + // add admin notification in case of site URL change add_action( 'admin_notices', [ $this, 'validate_cart_url' ] ); @@ -41,6 +44,27 @@ public function __construct() { // add bulk actions to manage products sync add_filter( 'bulk_actions-edit-product', [ $this, 'add_products_sync_bulk_actions' ], 40 ); add_action( 'handle_bulk_actions-edit-product', [ $this, 'handle_products_sync_bulk_actions' ] ); + + // add Product data tab + add_filter( 'woocommerce_product_data_tabs', [ $this, 'add_product_settings_tab' ] ); + add_action( 'woocommerce_product_data_panels', [ $this, 'add_product_settings_tab_content' ] ); + } + + + /** + * Enqueues admin scripts. + * + * @internal + * + * @since x.y.z + */ + public function enqueue_scripts() { + global $current_screen; + + if ( isset( $current_screen->id ) && 'product' === $current_screen->id ) { + + wp_enqueue_script( 'wc_facebook_product_settings_js', plugins_url( '/facebook-for-woocommerce/assets/js/admin/facebook-product-settings.js' ), [ 'jquery' ], \WC_Facebookcommerce::PLUGIN_VERSION ); + } } @@ -167,7 +191,8 @@ public function add_products_by_sync_enabled_input_filter() { return; } - $choice = isset( $_GET['fb_sync_enabled'] ) ? (string) $_GET['fb_sync_enabled'] : ''; + // phpcs:ignore WordPress.Security.NonceVerification.Recommended + $choice = isset( $_GET['fb_sync_enabled'] ) ? (string) sanitize_text_field( wp_unslash( $_GET['fb_sync_enabled'] ) ) : ''; ?>