diff --git a/projects/packages/sync/changelog/update-sync-checksum b/projects/packages/sync/changelog/update-sync-checksum new file mode 100644 index 0000000000000..93c3a7d00ab12 --- /dev/null +++ b/projects/packages/sync/changelog/update-sync-checksum @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Sync Checksums: Refactor checksum logic to allow 'jetpack_sync_checksum_allowed_tables' filter to work with audits + + diff --git a/projects/packages/sync/src/class-replicastore.php b/projects/packages/sync/src/class-replicastore.php index 06c0188a8871f..7f227cdcd8b92 100644 --- a/projects/packages/sync/src/class-replicastore.php +++ b/projects/packages/sync/src/class-replicastore.php @@ -1183,82 +1183,25 @@ public function get_allowed_mime_types( $user_id ) { // phpcs:ignore VariableAna * @return array Checksums. */ public function checksum_all( $perform_text_conversion = false ) { - $post_checksum = $this->checksum_histogram( 'posts', null, null, null, null, true, '', false, false, $perform_text_conversion ); - $comments_checksum = $this->checksum_histogram( 'comments', null, null, null, null, true, '', false, false, $perform_text_conversion ); - $post_meta_checksum = $this->checksum_histogram( 'postmeta', null, null, null, null, true, '', false, false, $perform_text_conversion ); - $comment_meta_checksum = $this->checksum_histogram( 'commentmeta', null, null, null, null, true, '', false, false, $perform_text_conversion ); - $terms_checksum = $this->checksum_histogram( 'terms', null, null, null, null, true, '', false, false, $perform_text_conversion ); - $term_relationships_checksum = $this->checksum_histogram( 'term_relationships', null, null, null, null, true, '', false, false, $perform_text_conversion ); - $term_taxonomy_checksum = $this->checksum_histogram( 'term_taxonomy', null, null, null, null, true, '', false, false, $perform_text_conversion ); - - $result = array( - 'posts' => $this->summarize_checksum_histogram( $post_checksum ), - 'comments' => $this->summarize_checksum_histogram( $comments_checksum ), - 'post_meta' => $this->summarize_checksum_histogram( $post_meta_checksum ), - 'comment_meta' => $this->summarize_checksum_histogram( $comment_meta_checksum ), - 'terms' => $this->summarize_checksum_histogram( $terms_checksum ), - 'term_relationships' => $this->summarize_checksum_histogram( $term_relationships_checksum ), - 'term_taxonomy' => $this->summarize_checksum_histogram( $term_taxonomy_checksum ), - ); + $all_checksum_tables = Table_Checksum::get_allowed_tables(); - /** - * WooCommerce tables - */ - - /** - * On WordPress.com, we can't directly check if the site has support for WooCommerce. - * Having the option to override the functionality here helps with syncing WooCommerce tables. - * - * @since 10.1 - * - * @param bool If we should we force-enable WooCommerce tables support. - */ - $force_woocommerce_support = apply_filters( 'jetpack_table_checksum_force_enable_woocommerce', false ); - - if ( $force_woocommerce_support || class_exists( 'WooCommerce' ) ) { - /** - * Guard in Try/Catch as it's possible for the WooCommerce class to exist, but - * the tables to not. If we don't do this, the response will be just the exception, without - * returning any valid data. This will prevent us from ever performing a checksum/fix - * for sites like this. - * It's better to just skip the tables in the response, instead of completely failing. - */ + unset( $all_checksum_tables['users'] ); // Handled separately - TODO. + unset( $all_checksum_tables['usermeta'] ); // Handled separately - TODO. + unset( $all_checksum_tables['termmeta'] ); // Handled separately - TODO. + unset( $all_checksum_tables['links'] ); // Not supported yet. Consider removing from default config. + unset( $all_checksum_tables['options'] ); // Not supported yet. Consider removing from default config. - try { - $woocommerce_order_items_checksum = $this->checksum_histogram( 'woocommerce_order_items' ); - $result['woocommerce_order_items'] = $this->summarize_checksum_histogram( $woocommerce_order_items_checksum ); - } catch ( Exception $ex ) { - $result['woocommerce_order_items'] = null; - } + $all_checksum_tables = array_unique( array_keys( $all_checksum_tables ) ); + + $result = array(); + foreach ( $all_checksum_tables as $table ) { + $result_key = in_array( $table, array( 'postmeta', 'commentmeta' ), true ) ? str_replace( 'meta', '_meta', $table ) : $table; try { - $woocommerce_order_itemmeta_checksum = $this->checksum_histogram( 'woocommerce_order_itemmeta' ); - $result['woocommerce_order_itemmeta'] = $this->summarize_checksum_histogram( $woocommerce_order_itemmeta_checksum ); + $checksum = $this->checksum_histogram( $table, null, null, null, null, true, '', false, false, $perform_text_conversion ); + $result[ $result_key ] = $this->summarize_checksum_histogram( $checksum ); } catch ( Exception $ex ) { - $result['woocommerce_order_itemmeta'] = null; - } - - if ( Table_Checksum::enable_woocommerce_hpos_tables() ) { - try { - $woocommerce_hpos_orders_checksum = $this->checksum_histogram( 'wc_orders' ); - $result['wc_orders'] = $this->summarize_checksum_histogram( $woocommerce_hpos_orders_checksum ); - } catch ( Exception $ex ) { - $result['wc_orders'] = null; - } - - try { - $woocommerce_hpos_order_addresses_checksum = $this->checksum_histogram( 'wc_order_addresses' ); - $result['wc_order_addresses'] = $this->summarize_checksum_histogram( $woocommerce_hpos_order_addresses_checksum ); - } catch ( Exception $ex ) { - $result['wc_order_addresses'] = null; - } - - try { - $woocommerce_hpos_order_operational_data_checksum = $this->checksum_histogram( 'wc_order_operational_data' ); - $result['wc_order_operational_data'] = $this->summarize_checksum_histogram( $woocommerce_hpos_order_operational_data_checksum ); - } catch ( Exception $ex ) { - $result['wc_order_operational_data'] = null; - } + $result[ $result_key ] = null; } } diff --git a/projects/packages/sync/src/replicastore/class-table-checksum.php b/projects/packages/sync/src/replicastore/class-table-checksum.php index 30d0bc1caaf7c..3118c6a2c8bd7 100644 --- a/projects/packages/sync/src/replicastore/class-table-checksum.php +++ b/projects/packages/sync/src/replicastore/class-table-checksum.php @@ -148,7 +148,7 @@ public function __construct( $table, $salt = null, $perform_text_conversion = fa $this->salt = $salt; - $this->default_tables = $this->get_default_tables(); + $this->default_tables = static::get_default_tables(); $this->perform_text_conversion = $perform_text_conversion; @@ -181,7 +181,7 @@ public function __construct( $table, $salt = null, $perform_text_conversion = fa * * @return array */ - protected function get_default_tables() { + protected static function get_default_tables() { global $wpdb; return array( @@ -295,7 +295,7 @@ protected function get_default_tables() { 'key_fields' => array( 'order_item_id' ), 'checksum_fields' => array( 'order_id' ), 'checksum_text_fields' => array( 'order_item_name', 'order_item_type' ), - 'is_table_enabled_callback' => array( $this, 'enable_woocommerce_tables' ), + 'is_table_enabled_callback' => 'Automattic\Jetpack\Sync\Replicastore\Table_Checksum::enable_woocommerce_tables', ), 'woocommerce_order_itemmeta' => array( 'table' => "{$wpdb->prefix}woocommerce_order_itemmeta", @@ -306,7 +306,7 @@ protected function get_default_tables() { 'parent_table' => 'woocommerce_order_items', 'parent_join_field' => 'order_item_id', 'table_join_field' => 'order_item_id', - 'is_table_enabled_callback' => array( $this, 'enable_woocommerce_tables' ), + 'is_table_enabled_callback' => 'Automattic\Jetpack\Sync\Replicastore\Table_Checksum::enable_woocommerce_tables', ), 'wc_orders' => array( 'table' => "{$wpdb->prefix}wc_orders", @@ -381,6 +381,15 @@ protected function get_default_tables() { ); } + /** + * Get allowed table configurations. + * + * @return array + */ + public static function get_allowed_tables() { + return apply_filters( 'jetpack_sync_checksum_allowed_tables', static::get_default_tables() ); + } + /** * Prepare field params based off provided configuration. * @@ -872,7 +881,7 @@ public function calculate_checksum( $range_from = null, $range_to = null, $filte * * @return bool */ - protected function enable_woocommerce_tables() { + public static function enable_woocommerce_tables() { /** * On WordPress.com, we can't directly check if the site has support for WooCommerce. * Having the option to override the functionality here helps with syncing WooCommerce tables. @@ -889,14 +898,8 @@ protected function enable_woocommerce_tables() { return true; } - // No need to proceed if WooCommerce is not available. - if ( ! class_exists( 'WooCommerce' ) ) { - return false; - } - - // TODO more checks if needed. Probably query the DB to make sure the tables exist. - - return true; + // If the 'woocommerce' module is enabled, this means that WooCommerce class exists. + return false !== Sync\Modules::get_module( 'woocommerce' ); } /**