Skip to content
Closed
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
217 changes: 0 additions & 217 deletions facebook-commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,6 @@ public function __construct( WC_Facebookcommerce $facebook_for_woocommerce ) {

add_action( 'admin_enqueue_scripts', [ $this, 'load_assets' ] );

add_action(
'wp_ajax_ajax_sync_all_fb_products',
[ $this, 'ajax_sync_all_fb_products' ],
self::FB_PRIORITY_MID
);

add_action(
'wp_ajax_ajax_check_feed_upload_status',
[ $this, 'ajax_check_feed_upload_status' ],
Expand Down Expand Up @@ -2174,217 +2168,6 @@ public function ajax_reset_all_fb_products() {
wp_die();
}

/**
* Special function to run all visible products through on_product_publish
*
* @internal
*/
public function ajax_sync_all_fb_products() {
WC_Facebookcommerce_Utils::check_woo_ajax_permissions( 'syncall products', true );
check_ajax_referer( 'wc_facebook_settings_jsx' );

$this->sync_facebook_products();
}

/**
* Syncs Facebook products using the GraphAPI.
*
* It can either use a Feed upload or update each product individually based on the selecetd method.
* Ends the request sending a JSON response indicating success or failure.
*
* @since 1.10.2
*/
private function sync_facebook_products() {
try {
$this->sync_facebook_products_using_background_processor();
wp_send_json_success();
} catch ( PluginException $e ) {
// Access token has expired
if ( 190 === $e->getCode() ) {
$error_message = __( 'Your connection has expired.', 'facebook-for-woocommerce' ) . ' <strong>' . __( 'Please click Manage connection > Advanced Options > Update Token to refresh your connection to Facebook.', 'facebook-for-woocommerce' ) . '</strong>';
} else {
$error_message = $e->getMessage();
}

$message = sprintf(
/* translators: Placeholders %s - error message */
__( 'There was an error trying to sync the products to Facebook. %s', 'facebook-for-woocommerce' ),
$error_message
);

wp_send_json_error( [ 'error' => $message ] );
}
}

/**
* Syncs Facebook products using the background processor.
*
* @since 1.10.2
*
* @return bool
* @throws PluginException If the plugin is not configured or the Catalog ID is missing.
*/
private function sync_facebook_products_using_background_processor() {
if ( ! $this->is_configured() || ! $this->get_product_catalog_id() ) {
Logger::log(
sprintf( 'Not syncing, the plugin is not configured or the Catalog ID is missing' ),
[],
array(
'should_send_log_to_meta' => false,
'should_save_log_in_woocommerce' => true,
'woocommerce_log_level' => \WC_Log_Levels::WARNING,
)
);
throw new PluginException( __( 'The plugin is not configured or the Catalog ID is missing.', 'facebook-for-woocommerce' ) );
}

$this->remove_resync_message();

$currently_syncing = get_transient( self::FB_SYNC_IN_PROGRESS );
if ( isset( $this->background_processor ) ) {
if ( $this->background_processor->is_updating() ) {
$this->background_processor->handle_cron_healthcheck();
$currently_syncing = 1;
}
}

if ( $currently_syncing ) {
Logger::log(
'Not syncing again, sync already in progress',
[],
array(
'should_send_log_to_meta' => false,
'should_save_log_in_woocommerce' => true,
'woocommerce_log_level' => \WC_Log_Levels::DEBUG,
)
);
WC_Facebookcommerce_Utils::fblog(
'Tried to sync during an in-progress sync!',
[],
true
);
throw new PluginException( __( 'A product sync is in progress. Please wait until the sync finishes before starting a new one.', 'facebook-for-woocommerce' ) );
}

try {
$catalog = $this->facebook_for_woocommerce->get_api()->get_catalog( $this->get_product_catalog_id() );
} catch ( ApiException $e ) {
$message = sprintf( 'There was an error trying to delete a product set item: %s', $e->getMessage() );
Logger::log(
$message,
[],
array(
'should_send_log_to_meta' => false,
'should_save_log_in_woocommerce' => true,
'woocommerce_log_level' => \WC_Log_Levels::ERROR,
)
);
}
if ( $catalog->id ) {
Logger::log(
'Not syncing, invalid product catalog!',
[],
array(
'should_send_log_to_meta' => false,
'should_save_log_in_woocommerce' => true,
'woocommerce_log_level' => \WC_Log_Levels::WARNING,
)
);
WC_Facebookcommerce_Utils::fblog(
'Tried to sync with an invalid product catalog!',
[],
true
);
throw new PluginException( __( "We've detected that your Facebook Product Catalog is no longer valid. This may happen if it was deleted, but could also be a temporary error. If the error persists, please click Manage connection > Advanced Options > Remove and setup the plugin again.", 'facebook-for-woocommerce' ) );
}

// Get all published posts. First unsynced then already-synced.
$post_ids_new = WC_Facebookcommerce_Utils::get_wp_posts( self::FB_PRODUCT_GROUP_ID, 'NOT EXISTS' );
$post_ids_old = WC_Facebookcommerce_Utils::get_wp_posts( self::FB_PRODUCT_GROUP_ID, 'EXISTS' );

$total_new = count( $post_ids_new );
$total_old = count( $post_ids_old );
$post_ids = array_merge( $post_ids_new, $post_ids_old );
$total = count( $post_ids );

WC_Facebookcommerce_Utils::fblog(
'Attempting to sync ' . $total . ' ( ' .
$total_new . ' new) products with settings: ',
$this->settings,
false
);

// Check for background processing (Woo 3.x.x)
if ( isset( $this->background_processor ) ) {
$starting_message = sprintf(
'Starting background sync to Facebook: %d products...',
$total
);
set_transient( self::FB_SYNC_IN_PROGRESS, true, self::FB_SYNC_TIMEOUT );
set_transient( self::FB_SYNC_REMAINING, (int) $total );
$this->display_info_message( $starting_message );
Logger::log(
$starting_message,
[],
array(
'should_send_log_to_meta' => false,
'should_save_log_in_woocommerce' => true,
'woocommerce_log_level' => \WC_Log_Levels::DEBUG,
)
);
foreach ( $post_ids as $post_id ) {
$this->background_processor->push_to_queue( $post_id );
}

$this->background_processor->save()->dispatch();
// reset FB_SYNC_REMAINING to avoid race condition
set_transient( self::FB_SYNC_REMAINING, (int) $total );
// handle_cron_healthcheck must be called
// https://github.com/A5hleyRich/wp-background-processing/issues/34
$this->background_processor->handle_cron_healthcheck();
} else {
// Oldschool sync for WooCommerce 2.x
$count = ( $total_old === $total ) ? 0 : $total_old;
foreach ( $post_ids as $post_id ) {
// Repeatedly overwrite sync total while in actual sync loop
set_transient( self::FB_SYNC_IN_PROGRESS, true, self::FB_SYNC_TIMEOUT );

$this->display_sticky_message(
sprintf(
'Syncing products to Facebook: %d out of %d...',
// Display different # when resuming to avoid confusion.
min( $count, $total ),
$total
),
true
);

$this->on_product_publish( $post_id );
++$count;
}
Logger::log(
'Synced ' . $count . ' products',
[],
array(
'should_send_log_to_meta' => false,
'should_save_log_in_woocommerce' => true,
'woocommerce_log_level' => \WC_Log_Levels::DEBUG,
)
);
$this->remove_sticky_message();
$this->display_info_message( 'Facebook product sync complete!' );
delete_transient( self::FB_SYNC_IN_PROGRESS );
WC_Facebookcommerce_Utils::fblog(
'Product sync complete. Total products synced: ' . $count
);
}

// https://codex.wordpress.org/Function_Reference/wp_reset_postdata
wp_reset_postdata();

return true;
}

/**
* Toggles product visibility via AJAX.
*
Expand Down