Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove sync not possible modal. #2665

Merged
merged 2 commits into from
Nov 19, 2023
Merged
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
70 changes: 0 additions & 70 deletions assets/js/admin/products-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,76 +667,6 @@ jQuery( document ).ready( function( $ ) {
// toggle Sell on Instagram checkbox on page load
toggleFacebookSellOnInstagramSetting( isProductReadyForCommerce(), facebookSettingsPanel );

let submitProductSave = false;

$( 'form#post input[type="submit"]' ).on( 'click', function( e ) {

if ( shouldShowMissingGoogleProductCategoryAlert() ) {
return showMissingGoogleProductCategoryAlert( e );
}

if ( ! submitProductSave ) {
e.preventDefault();
} else {
return true;
}

let $submitButton = $( this ),
productID = parseInt( $( 'input#post_ID' ).val(), 10 ),
productCat = [],
// this query will get tags when not using checkboxes
productTag = $( 'textarea[name="tax_input[product_tag]"]' ).length ? $( 'textarea[name="tax_input[product_tag]"]' ).val().split( ',' ) : [],
syncEnabled = simpleProductSyncModeSelect.val() !== 'sync_disabled',
varSyncEnabled = isSyncEnabledForVariableProduct();

$( '#taxonomy-product_cat input[name="tax_input[product_cat][]"]:checked' ).each( function() {
productCat.push( parseInt( $( this ).val(), 10 ) );
} );

// this query will get tags when using checkboxes
$( '#taxonomy-product_tag input[name="tax_input[product_tag][]"]:checked' ).each( function() {
productTag.push( parseInt( $( this ).val(), 10 ) );
} );

if ( productID > 0 ) {

$.post( facebook_for_woocommerce_products_admin.ajax_url, {
action: 'facebook_for_woocommerce_set_product_sync_prompt',
security: facebook_for_woocommerce_products_admin.set_product_sync_prompt_nonce,
sync_enabled: syncEnabled ? 'enabled' : 'disabled',
var_sync_enabled: varSyncEnabled ? 'enabled' : 'disabled',
product: productID,
categories: productCat,
tags: productTag
}, function( response ) {

// open modal if visibility checkbox is checked or if there are conflicting terms set for sync exclusion
if ( response && ! response.success && syncEnabled ) {

closeExistingModal();

// open new modal, populate template with AJAX response data
new $.WCBackboneModal.View( {
target: 'facebook-for-woocommerce-modal',
string: response.data
} );

} else {

// no modal displayed: submit form as normal
submitProductSave = true;
$submitButton.trigger( 'click' );
}
} );

} else {

// no modal displayed: submit form as normal
submitProductSave = true;
$submitButton.trigger( 'click' );
}

} );
}


Expand Down
108 changes: 1 addition & 107 deletions includes/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class AJAX {
*/
public function __construct() {

// maybe output a modal prompt when toggling product sync in bulk or individual product actions
add_action( 'wp_ajax_facebook_for_woocommerce_set_product_sync_prompt', array( $this, 'handle_set_product_sync_prompt' ) );
// 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' ) );

// maybe output a modal prompt when setting excluded terms
Expand Down Expand Up @@ -158,111 +157,6 @@ public function get_sync_status() {
}


/**
* Maybe triggers a modal warning when the merchant toggles sync enabled status on a product.
*
* @internal
*
* @since 1.10.0
*/
public function handle_set_product_sync_prompt() {

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

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$product_id = isset( $_POST['product'] ) ? (int) wc_clean( wp_unslash( $_POST['product'] ) ) : 0;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$sync_enabled = isset( $_POST['sync_enabled'] ) ? (string) wc_clean( wp_unslash( $_POST['sync_enabled'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$var_sync_enabled = isset( $_POST['var_sync_enabled'] ) ? (string) wc_clean( wp_unslash( $_POST['var_sync_enabled'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$product_cats = isset( $_POST['categories'] ) ? (array) wc_clean( wp_unslash( $_POST['categories'] ) ) : array();
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$product_tags = isset( $_POST['tags'] ) ? (array) wc_clean( wp_unslash( $_POST['tags'] ) ) : array();

if ( $product_id > 0 && in_array( $var_sync_enabled, array( 'enabled', 'disabled' ), true ) && in_array( $sync_enabled, array( 'enabled', 'disabled' ), true ) ) {

$product = wc_get_product( $product_id );

if ( $product instanceof \WC_Product ) {

if ( ( 'enabled' === $sync_enabled && ! $product->is_type( 'variable' ) ) || ( 'enabled' === $var_sync_enabled && $product->is_type( 'variable' ) ) ) {

$has_excluded_terms = false;

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

// Try with categories first, since we have already IDs.
$has_excluded_terms = ! empty( $product_cats ) && array_intersect( $product_cats, $integration->get_excluded_product_category_ids() );

// The form post can send an array with empty items, so filter them out.
$product_tags = array_filter( $product_tags, 'strlen' );

// Try next with tags, but WordPress only gives us tag names.
if ( ! $has_excluded_terms && ! empty( $product_tags ) ) {

$product_tag_ids = array();

foreach ( $product_tags as $product_tag_name_or_id ) {

$term = get_term_by( 'name', $product_tag_name_or_id, 'product_tag' );

if ( $term instanceof \WP_Term ) {

$product_tag_ids[] = $term->term_id;

} else {

$term = get_term( (int) $product_tag_name_or_id, 'product_tag' );

if ( $term instanceof \WP_Term ) {
$product_tag_ids[] = $term->term_id;
}
}
}

$has_excluded_terms = ! empty( $product_tag_ids ) && array_intersect( $product_tag_ids, $integration->get_excluded_product_tag_ids() );
}
}

if ( $has_excluded_terms ) {

ob_start();

?>
<a
id="facebook-for-woocommerce-go-to-settings"
class="button button-large"
href="<?php echo esc_url( add_query_arg( 'tab', Product_Sync::ID, facebook_for_woocommerce()->get_settings_url() ) ); ?>"
><?php esc_html_e( 'Go to Settings', 'facebook-for-woocommerce' ); ?></a>
<button
id="facebook-for-woocommerce-cancel-sync"
class="button button-large button-primary"
onclick="jQuery( '.modal-close' ).trigger( 'click' )"
><?php esc_html_e( 'Cancel', 'facebook-for-woocommerce' ); ?></button>
<?php

$buttons = ob_get_clean();

wp_send_json_error(
array(
'message' => sprintf(
/* translators: Placeholder %s - <br/> tag */
__( 'This product belongs to a category or tag that is excluded from the Facebook catalog sync. It will not sync to Facebook. %sTo sync this product to Facebook, click Go to Settings and remove the category or tag exclusion or click Cancel and update the product\'s category / tag assignments.', 'facebook-for-woocommerce' ),
'<br/><br/>'
),
'buttons' => $buttons,
)
);
}
}
}
}

wp_send_json_success();
}


/**
* Maybe triggers a modal warning when the merchant toggles sync enabled status in bulk.
*
Expand Down
56 changes: 0 additions & 56 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ public function __construct() {
$this->product_sets = new Admin\Product_Sets();
// add a modal in admin product pages
add_action( 'admin_footer', array( $this, 'render_modal_template' ) );
// may trigger the modal to open to warn the merchant about a conflict with the current product terms
add_action( 'admin_footer', array( $this, 'validate_product_excluded_terms' ) );

// add admin notice to inform that disabled products may need to be deleted manually
add_action( 'admin_notices', array( $this, 'maybe_show_product_disabled_sync_notice' ) );
Expand Down Expand Up @@ -1518,58 +1516,4 @@ public function render_modal_template() {
}


/**
* Maybe triggers the modal to open on the product edit screen on page load.
*
* If the product is set to be synced in Facebook, but belongs to a term that is set to be excluded, the modal prompts the merchant for action.
*
* @internal
*
* @since 1.10.0
*/
public function validate_product_excluded_terms() {
global $current_screen, $post;
if ( $post && $current_screen && $current_screen->id === 'product' ) :
$product = wc_get_product( $post );
if ( $product instanceof \WC_Product
&& Products::is_sync_enabled_for_product( $product )
&& Products::is_sync_excluded_for_product_terms( $product )
) :
?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {

var productID = parseInt( $( 'input#post_ID' ).val(), 10 ),
productTag = $( 'textarea[name=\"tax_input[product_tag]\"]' ).val().split( ',' ),
productCat = [];

$( '#taxonomy-product_cat input[name=\"tax_input[product_cat][]\"]:checked' ).each( function() {
productCat.push( parseInt( $( this ).val(), 10 ) );
} );

$.post( facebook_for_woocommerce_products_admin.ajax_url, {
action: 'facebook_for_woocommerce_set_product_sync_prompt',
security: facebook_for_woocommerce_products_admin.set_product_sync_prompt_nonce,
sync_enabled: 'enabled',
product: productID,
categories: productCat,
tags: productTag
}, function( response ) {

if ( response && ! response.success ) {

$( '#wc-backbone-modal-dialog .modal-close' ).trigger( 'click' );

new $.WCBackboneModal.View( {
target: 'facebook-for-woocommerce-modal',
string: response.data
} );
}
} );
} );
</script>
<?php
endif;
endif;
}
}