Skip to content

Commit

Permalink
Sync: Enable full sync immediately for woocommerce module (#39254)
Browse files Browse the repository at this point in the history
* Woocommerce Module changes for Full Sync Immediately

* Deprecate table_name and use only table with the prefix

* Ensure full sync limits are getting populated
  • Loading branch information
darssen authored Sep 9, 2024
1 parent 7a5af0f commit 0e3716e
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Sync: Enable Full Sync Immediately for woocommerce module
4 changes: 4 additions & 0 deletions projects/packages/sync/src/class-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,10 @@ public static function is_multi_network() {
'chunk_size' => 100,
'max_chunks' => 10,
),
'woocommerce' => array(
'chunk_size' => 100,
'max_chunks' => 10,
),
);

/**
Expand Down
16 changes: 15 additions & 1 deletion projects/packages/sync/src/modules/class-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,30 @@ public function id_field() {
}

/**
* The table in the database.
* The table name.
*
* @access public
*
* @return string
* @deprecated since $$next-version$$ Use table() instead.
*/
public function table_name() {
_deprecated_function( __METHOD__, '$$next-version$$', 'Automattic\\Jetpack\\Sync\\Comments->table' );
return 'comments';
}

/**
* The table in the database with the prefix.
*
* @access public
*
* @return string|bool
*/
public function table() {
global $wpdb;
return $wpdb->comments;
}

/**
* Retrieve a comment by its ID.
*
Expand Down
47 changes: 38 additions & 9 deletions projects/packages/sync/src/modules/class-module.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Automattic\Jetpack\Sync\Modules;

use Automattic\Jetpack\Sync\Defaults;
use Automattic\Jetpack\Sync\Functions;
use Automattic\Jetpack\Sync\Listener;
use Automattic\Jetpack\Sync\Replicastore;
Expand Down Expand Up @@ -49,16 +50,40 @@ public function id_field() {
}

/**
* The table in the database.
* The table name.
*
* @access public
*
* @return string|bool
* @deprecated since $$next-version$$ Use table() instead.
*/
public function table_name() {
_deprecated_function( __METHOD__, '$$next-version$$', 'Automattic\\Jetpack\\Sync\\Module->table' );
return false;
}

/**
* The table in the database with the prefix.
*
* @access public
*
* @return string|bool
*/
public function table() {
return false;
}

/**
* The full sync action name for this module.
*
* @access public
*
* @return string
*/
public function full_sync_action_name() {
return 'jetpack_full_sync_' . $this->name();
}

// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable

/**
Expand Down Expand Up @@ -298,7 +323,7 @@ public function get_next_chunk( $config, $status, $chunk_size ) {
return $wpdb->get_col(
"
SELECT {$this->id_field()}
FROM {$wpdb->{$this->table_name()}}
FROM {$this->table()}
WHERE {$this->get_where_sql( $config )}
AND {$this->id_field()} < {$status['last_sent']}
ORDER BY {$this->id_field()}
Expand All @@ -321,7 +346,7 @@ public function get_last_item( $config ) {
return $wpdb->get_var(
"
SELECT {$this->id_field()}
FROM {$wpdb->{$this->table_name()}}
FROM {$this->table()}
WHERE {$this->get_where_sql( $config )}
ORDER BY {$this->id_field()}
LIMIT 1
Expand Down Expand Up @@ -357,7 +382,12 @@ public function send_full_sync_actions( $config, $status, $send_until ) {
$status['last_sent'] = $this->get_initial_last_sent();
}

$limits = Settings::get_setting( 'full_sync_limits' )[ $this->name() ];
$limits = Settings::get_setting( 'full_sync_limits' )[ $this->name() ] ??
Defaults::get_default_setting( 'full_sync_limits' )[ $this->name() ] ??
array(
'max_chunks' => 10,
'chunk_size' => 100,
);

$chunks_sent = 0;

Expand All @@ -375,7 +405,7 @@ public function send_full_sync_actions( $config, $status, $send_until ) {
$status['finished'] = true;
return $status;
}
$result = $this->send_action( 'jetpack_full_sync_' . $this->name(), array( $objects, $status['last_sent'] ) );
$result = $this->send_action( $this->full_sync_action_name(), array( $objects, $status['last_sent'] ) );
if ( is_wp_error( $result ) || $wpdb->last_error ) {
$status['error'] = true;
return $status;
Expand Down Expand Up @@ -571,14 +601,13 @@ public function get_objects_by_id( $object_type, $ids ) {
* @return array|bool An array of min and max ids for each batch. FALSE if no table can be found.
*/
public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) {
global $wpdb;

if ( ! $this->table_name() ) {
if ( ! $this->table() ) {
return false;
}

$results = array();
$table = $wpdb->{$this->table_name()};
$table = $this->table();
$current_max = 0;
$current_min = 1;
$id_field = $this->id_field();
Expand Down Expand Up @@ -628,7 +657,7 @@ public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = fa
*/
public function total( $config ) {
global $wpdb;
$table = $wpdb->{$this->table_name()};
$table = $this->table();
$where = $this->get_where_sql( $config );

// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
Expand Down
16 changes: 15 additions & 1 deletion projects/packages/sync/src/modules/class-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,30 @@ public function name() {
}

/**
* The table in the database.
* The table name.
*
* @access public
*
* @return string
* @deprecated since $$next-version$$ Use table() instead.
*/
public function table_name() {
_deprecated_function( __METHOD__, '$$next-version$$', 'Automattic\\Jetpack\\Sync\\Posts->table' );
return 'posts';
}

/**
* The table in the database with the prefix.
*
* @access public
*
* @return string|bool
*/
public function table() {
global $wpdb;
return $wpdb->posts;
}

/**
* Retrieve a post by its ID.
*
Expand Down
16 changes: 15 additions & 1 deletion projects/packages/sync/src/modules/class-term-relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,30 @@ public function id_field() {
}

/**
* The table in the database.
* The table name.
*
* @access public
*
* @return string
* @deprecated since $$next-version$$ Use table() instead.
*/
public function table_name() {
_deprecated_function( __METHOD__, '$$next-version$$', 'Automattic\\Jetpack\\Sync\\Term_Relationships->table' );
return 'term_relationships';
}

/**
* The table in the database with the prefix.
*
* @access public
*
* @return string|bool
*/
public function table() {
global $wpdb;
return $wpdb->term_relationships;
}

/**
* Initialize term relationships action listeners for full sync.
*
Expand Down
18 changes: 16 additions & 2 deletions projects/packages/sync/src/modules/class-terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,30 @@ public function id_field() {
}

/**
* The table in the database.
* The table name.
*
* @access public
*
* @return string
* @deprecated since $$next-version$$ Use table() instead.
*/
public function table_name() {
_deprecated_function( __METHOD__, '$$next-version$$', 'Automattic\\Jetpack\\Sync\\Terms->table' );
return 'term_taxonomy';
}

/**
* The table in the database with the prefix.
*
* @access public
*
* @return string|bool
*/
public function table() {
global $wpdb;
return $wpdb->term_taxonomy;
}

/**
* Allows WordPress.com servers to retrieve term-related objects via the sync API.
*
Expand Down Expand Up @@ -284,7 +298,7 @@ public function filter_set_object_terms_no_update( $args ) {
* @return array $args The expanded hook parameters.
*/
public function expand_term_taxonomy_id( $args ) {
list( $term_taxonomy_ids, $previous_end ) = $args;
list( $term_taxonomy_ids, $previous_end ) = $args;

return array(
'terms' => get_terms(
Expand Down
16 changes: 15 additions & 1 deletion projects/packages/sync/src/modules/class-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,30 @@ public function name() {
}

/**
* The table in the database.
* The table name.
*
* @access public
*
* @return string
* @deprecated since $$next-version$$ Use table() instead.
*/
public function table_name() {
_deprecated_function( __METHOD__, '$$next-version$$', 'Automattic\\Jetpack\\Sync\\Users->table' );
return 'usermeta';
}

/**
* The table in the database with the prefix.
*
* @access public
*
* @return string|bool
*/
public function table() {
global $wpdb;
return $wpdb->usermeta;
}

/**
* The id field in the database.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,21 @@ public function name() {
* @access public
*
* @return string
* @deprecated since $$next-version$$ Use table() instead.
*/
public function table_name() {
_deprecated_function( __METHOD__, '$$next-version$$', 'Automattic\\Jetpack\\Sync\\WooCommerce_HPOS_Orders->table' );
return $this->order_table_name;
}

/**
* The table in the database with the prefix.
*
* @access public
*
* @return string|bool
*/
public function table() {
return $this->order_table_name;
}

Expand Down Expand Up @@ -403,7 +416,7 @@ protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) { // ph
public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- We return all order count for full sync, so confit is not required.
global $wpdb;

$query = "SELECT count(*) FROM {$this->table_name()} WHERE {$this->get_where_sql( $config ) }";
$query = "SELECT count(*) FROM {$this->table()} WHERE {$this->get_where_sql( $config ) }";
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Hardcoded query, no user variable
$count = (int) $wpdb->get_var( $query );

Expand All @@ -421,7 +434,7 @@ public function estimate_full_sync_actions( $config ) { // phpcs:ignore Variable
* @return array Number of actions enqueued, and next module state.
*/
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
return $this->enqueue_all_ids_as_action( 'full_sync_orders', $this->table_name(), 'id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
return $this->enqueue_all_ids_as_action( 'full_sync_orders', $this->table(), 'id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
}

/**
Expand Down
38 changes: 37 additions & 1 deletion projects/packages/sync/src/modules/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,52 @@ class WooCommerce extends Module {
private $order_item_table_name;

/**
* The table in the database.
* The table name.
*
* @access public
*
* @return string
* @deprecated since $$next-version$$ Use table() instead.
*/
public function table_name() {
_deprecated_function( __METHOD__, '$$next-version$$', 'Automattic\\Jetpack\\Sync\\WooCommerce->table' );
return $this->order_item_table_name;
}

/**
* The table in the database with the prefix.
*
* @access public
*
* @return string|bool
*/
public function table() {
global $wpdb;
return $wpdb->prefix . 'woocommerce_order_items';
}

/**
* The id field in the database.
*
* @access public
*
* @return string
*/
public function id_field() {
return 'order_item_id';
}

/**
* The full sync action name for this module.
*
* @access public
*
* @return string
*/
public function full_sync_action_name() {
return 'jetpack_full_sync_woocommerce_order_items';
}

/**
* Constructor.
*
Expand Down

0 comments on commit 0e3716e

Please sign in to comment.