Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions assets/js/admin/enhanced-settings-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* 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($) {
/**
* Handle the sync products button click event
*
* @since 3.5.0
*
* @param {object} event
*/
$('#wc-facebook-enhanced-settings-sync-products').click(function(event) {
event.preventDefault();
var button = $(this);

button.html('Syncing...');
button.prop('disabled', true);

var data = {
action: "wc_facebook_sync_products",
nonce: wc_facebook_enhanced_settings_sync.sync_products_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);
});
});

/**
* Handle the sync coupons button click event
*
* @since 3.5.0
*
* @param {object} event
*/
$('#wc-facebook-enhanced-settings-sync-coupons').click(function(event) {
event.preventDefault();
var button = $(this);

button.html('Syncing...');
button.prop('disabled', true);

var data = {
action: "wc_facebook_sync_coupons",
nonce: wc_facebook_enhanced_settings_sync.sync_coupons_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);
});
});
});
82 changes: 36 additions & 46 deletions includes/AJAX.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
// phpcs:ignoreFile
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*
Expand All @@ -13,9 +12,10 @@

use WooCommerce\Facebook\Framework\Helper;
use WooCommerce\Facebook\Admin\Settings_Screens\Product_Sync;
use WooCommerce\Facebook\Admin\Settings_Screens\Shops;
use WooCommerce\Facebook\Framework\Plugin\Exception as PluginException;

defined( 'ABSPATH' ) or exit;
defined( 'ABSPATH' ) || exit;

/**
* AJAX handler.
Expand All @@ -33,7 +33,6 @@ class AJAX {
* @since 1.10.0
*/
public function __construct() {

// maybe output a modal prompt when toggling product sync in bulk
add_action( 'wp_ajax_facebook_for_woocommerce_set_product_sync_bulk_action_prompt', array( $this, 'handle_set_product_sync_bulk_action_prompt' ) );

Expand All @@ -43,6 +42,9 @@ public function __construct() {
// sync all products via AJAX
add_action( 'wp_ajax_wc_facebook_sync_products', array( $this, 'sync_products' ) );

// sync all coupons via AJAX
add_action( 'wp_ajax_wc_facebook_sync_coupons', array( $this, 'sync_coupons' ) );

// get the current sync status
add_action( 'wp_ajax_wc_facebook_get_sync_status', array( $this, 'get_sync_status' ) );

Expand All @@ -57,49 +59,40 @@ public function __construct() {
* @internal
*
* @since 2.1.0
*
* @throws PluginException If the nonce is invalid or a search term is not provided.
*/
public function admin_search_product_attributes() {

try {

if ( ! wp_verify_nonce( Helper::get_requested_value( 'security' ), self::ACTION_SEARCH_PRODUCT_ATTRIBUTES ) ) {
throw new PluginException( 'Invalid nonce' );
}

$term = Helper::get_requested_value( 'term' );

if ( ! $term ) {
throw new PluginException( 'A search term is required' );
}

$product = wc_get_product( (int) Helper::get_requested_value( 'request_data' ) );

if ( ! $product instanceof \WC_Product ) {
throw new PluginException( 'A valid product ID is required' );
}

$attributes = Admin\Products::get_available_product_attribute_names( $product );

// filter out any attributes whose slug or proper name don't at least partially match the search term
$results = array_filter(
$attributes,
function( $name, $slug ) use ( $term ) {

function ( $name, $slug ) use ( $term ) {
return false !== stripos( $name, $term ) || false !== stripos( $slug, $term );

},
ARRAY_FILTER_USE_BOTH
);

wp_send_json( $results );

} catch ( PluginException $exception ) {

die();
}
}


/**
* Syncs all products via AJAX.
*
Expand All @@ -116,9 +109,30 @@ public function sync_products() {

check_admin_referer( Product_Sync::ACTION_SYNC_PRODUCTS, 'nonce' );

facebook_for_woocommerce()->get_products_sync_handler()->create_or_update_all_products();
try {
facebook_for_woocommerce()->get_products_sync_handler()->create_or_update_all_products();
wp_send_json_success();
} catch ( \Exception $exception ) {
wp_send_json_error( $exception->getMessage() );
}
}

wp_send_json_success();
/**
* Syncs all coupons via AJAX.
*
* @internal
*
* @since 3.5.0
*/
public function sync_coupons() {
check_admin_referer( Shops::ACTION_SYNC_COUPONS, 'nonce' );

try {
facebook_for_woocommerce()->feed_manager->get_feed_instance( 'promotions' )->regenerate_feed();
wp_send_json_success();
} catch ( \Exception $exception ) {
wp_send_json_error( $exception->getMessage() );
}
}


Expand All @@ -130,7 +144,6 @@ public function sync_products() {
* @since 2.0.0
*/
public function get_sync_status() {

check_admin_referer( Product_Sync::ACTION_GET_SYNC_STATUS, 'nonce' );

$remaining_products = 0;
Expand All @@ -142,7 +155,6 @@ public function get_sync_status() {
);

if ( ! empty( $jobs ) ) {

// there should only be one processing job at a time, pluck the latest to convey status
$job = $jobs[0];

Expand All @@ -165,7 +177,6 @@ public function get_sync_status() {
* @since 1.10.0
*/
public function handle_set_product_sync_bulk_action_prompt() {

check_ajax_referer( 'set-product-sync-bulk-action-prompt', 'security' );

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
Expand All @@ -188,7 +199,6 @@ public function handle_set_product_sync_bulk_action_prompt() {

// show modal if there's at least one product that belongs to an excluded term
if ( $has_excluded_term ) {

ob_start();

?>
Expand Down Expand Up @@ -227,7 +237,6 @@ class="button button-large button-primary"
* @since 1.10.0
*/
public function handle_set_excluded_terms_prompt() {

check_ajax_referer( 'set-excluded-terms-prompt', 'security' );

// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
Expand All @@ -250,9 +259,7 @@ public function handle_set_excluded_terms_prompt() {
}
}

// query for products with sync enabled, belonging to the added term IDs and not belonging to the term IDs that are already stored in the setting
$products = $this->get_products_to_be_excluded( $new_category_ids, $new_tag_ids );

if ( ! empty( $products ) ) {

ob_start();
Expand All @@ -275,7 +282,7 @@ class="button button-large button-primary"
wp_send_json_error(
array(
'message' => sprintf(
/* translators: Placeholder %s - <br/> tags */
/* translators: Placeholder %s - <br/> tags */
__( 'The categories and/or tags that you have selected to exclude from sync contain products that are currently synced to Facebook.%sTo exclude these products from the Facebook sync, click Exclude Products. To review the category / tag exclusion settings, click Cancel.', 'facebook-for-woocommerce' ),
'<br/><br/>'
),
Expand Down Expand Up @@ -304,8 +311,6 @@ class="button button-large button-primary"
* @return int[]
*/
private function get_products_to_be_excluded( $new_excluded_categories = array(), $new_excluded_tags = array() ) {

// products with sync enabled
$sync_enabled_meta_query = array(
'relation' => 'OR',
array(
Expand All @@ -325,20 +330,15 @@ private function get_products_to_be_excluded( $new_excluded_categories = array()
);

if ( ! empty( $new_excluded_categories ) ) {

// products that belong to the new excluded categories
$categories_tax_query = array(
'taxonomy' => 'product_cat',
'terms' => $new_excluded_categories,
);

if ( $integration = facebook_for_woocommerce()->get_integration() ) {

// products that do not belong to the saved excluded categories
$integration = facebook_for_woocommerce()->get_integration();
if ( $integration ) {
$saved_excluded_categories = $integration->get_excluded_product_category_ids();

if ( ! empty( $saved_excluded_categories ) ) {

$categories_tax_query = array(
'relation' => 'AND',
$categories_tax_query,
Expand All @@ -355,20 +355,15 @@ private function get_products_to_be_excluded( $new_excluded_categories = array()
}

if ( ! empty( $new_excluded_tags ) ) {

// products that belong to the new excluded tags
$tags_tax_query = array(
'taxonomy' => 'product_tag',
'terms' => $new_excluded_tags,
);

if ( $integration = facebook_for_woocommerce()->get_integration() ) {

$integration = facebook_for_woocommerce()->get_integration();
if ( $integration ) {
$save_excluded_tags = $integration->get_excluded_product_tag_ids();

if ( ! empty( $save_excluded_tags ) ) {

// products that do not belong to the saved excluded tags
$tags_tax_query = array(
'relation' => 'AND',
$tags_tax_query,
Expand All @@ -382,11 +377,8 @@ private function get_products_to_be_excluded( $new_excluded_categories = array()
}

if ( empty( $products_query_vars['tax_query'] ) ) {

$products_query_vars['tax_query'] = $tags_tax_query;

} else {

$products_query_vars['tax_query'] = array(
'relation' => 'OR',
$products_query_vars,
Expand All @@ -399,6 +391,4 @@ private function get_products_to_be_excluded( $new_excluded_categories = array()

return $products_query->posts;
}


}
Loading