PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
← All changes | jetpack_vendor/automattic/jetpack-sync/src/class-replicastore.php +61 -50 13.6.2 → 16.3-beta View file →
@@ -12,8 +12,12 @@
12 12 use Automattic\Jetpack\Sync\Replicastore\Table_Checksum_Users;
13 13 use Exception;
14 14 use WP_Error;
15 15
16 +if ( ! defined( 'ABSPATH' ) ) {
17 + exit( 0 );
18 +}
19 +
16 20 /**
17 21 * An implementation of Replicastore Interface which returns data stored in a WordPress.org DB.
18 22 * This is useful to compare values in the local WP DB to values in the synced replica store
19 23 */
@@ -722,8 +726,47 @@
722 726 }
723 727 }
724 728
725 729 /**
730 + * Delete metadata with a certain key and value for all objects.
731 + *
732 + * @access public
733 + *
734 + * @param string $type Meta type.
735 + * @param string $meta_key Meta key.
736 + * @param mixed $meta_value Meta value to match.
737 + */
738 + public function delete_metadata_by_key_value( $type, $meta_key, $meta_value ) {
739 + global $wpdb;
740 +
741 + $table = _get_meta_table( $type );
742 + if ( ! $table ) {
743 + return false;
744 + }
745 +
746 + if ( '' === $meta_value || null === $meta_value || false === $meta_value ) {
747 + return false;
748 + }
749 +
750 + $column = sanitize_key( $type . '_id' );
751 + $meta_value = maybe_serialize( $meta_value );
752 +
753 + $object_ids = $wpdb->get_col( $wpdb->prepare( 'SELECT DISTINCT %i FROM %i WHERE meta_key = %s AND meta_value = %s', $column, $table, $meta_key, $meta_value ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct replica query is needed to identify caches invalidated below.
754 + $wpdb->delete( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Direct replica mutation.
755 + $table,
756 + array(
757 + 'meta_key' => $meta_key,
758 + 'meta_value' => $meta_value,
759 + ),
760 + array( '%s', '%s' )
761 + );
762 +
763 + foreach ( $object_ids as $object_id ) {
764 + wp_cache_delete( $object_id, $type . '_meta' );
765 + }
766 + }
767 +
768 + /**
726 769 * Retrieve value of a constant based on the constant name.
727 770 *
728 771 * We explicitly return null instead of false if the constant doesn't exist.
729 772 *
@@ -1174,9 +1217,8 @@
1174 1217 }
1175 1218
1176 1219 /**
1177 1220 * Retrieve all the checksums we are interested in.
1178 - * Currently that is posts, comments, post meta and comment meta.
1179 1221 *
1180 1222 * @access public
1181 1223 *
1182 1224 * @param boolean $perform_text_conversion If text fields should be latin1 converted.
@@ -1183,62 +1225,28 @@
1183 1225 *
1184 1226 * @return array Checksums.
1185 1227 */
1186 1228 public function checksum_all( $perform_text_conversion = false ) {
1187 - $post_checksum = $this->checksum_histogram( 'posts', null, null, null, null, true, '', false, false, $perform_text_conversion );
1188 - $comments_checksum = $this->checksum_histogram( 'comments', null, null, null, null, true, '', false, false, $perform_text_conversion );
1189 - $post_meta_checksum = $this->checksum_histogram( 'postmeta', null, null, null, null, true, '', false, false, $perform_text_conversion );
1190 - $comment_meta_checksum = $this->checksum_histogram( 'commentmeta', null, null, null, null, true, '', false, false, $perform_text_conversion );
1191 - $terms_checksum = $this->checksum_histogram( 'terms', null, null, null, null, true, '', false, false, $perform_text_conversion );
1192 - $term_relationships_checksum = $this->checksum_histogram( 'term_relationships', null, null, null, null, true, '', false, false, $perform_text_conversion );
1193 - $term_taxonomy_checksum = $this->checksum_histogram( 'term_taxonomy', null, null, null, null, true, '', false, false, $perform_text_conversion );
1229 + $all_checksum_tables = Table_Checksum::get_allowed_tables();
1194 1230
1195 - $result = array(
1196 - 'posts' => $this->summarize_checksum_histogram( $post_checksum ),
1197 - 'comments' => $this->summarize_checksum_histogram( $comments_checksum ),
1198 - 'post_meta' => $this->summarize_checksum_histogram( $post_meta_checksum ),
1199 - 'comment_meta' => $this->summarize_checksum_histogram( $comment_meta_checksum ),
1200 - 'terms' => $this->summarize_checksum_histogram( $terms_checksum ),
1201 - 'term_relationships' => $this->summarize_checksum_histogram( $term_relationships_checksum ),
1202 - 'term_taxonomy' => $this->summarize_checksum_histogram( $term_taxonomy_checksum ),
1203 - );
1231 + unset( $all_checksum_tables['users'] ); // Handled separately - TODO.
1232 + unset( $all_checksum_tables['usermeta'] ); // Handled separately - TODO.
1233 + unset( $all_checksum_tables['termmeta'] ); // Handled separately - TODO.
1234 + unset( $all_checksum_tables['links'] ); // Not supported yet. Consider removing from default config.
1235 + unset( $all_checksum_tables['options'] ); // Not supported yet. Consider removing from default config.
1204 1236
1205 - /**
1206 - * WooCommerce tables
1207 - */
1237 + $all_checksum_tables = array_unique( array_keys( $all_checksum_tables ) );
1208 1238
1209 - /**
1210 - * On WordPress.com, we can't directly check if the site has support for WooCommerce.
1211 - * Having the option to override the functionality here helps with syncing WooCommerce tables.
1212 - *
1213 - * @since 10.1
1214 - *
1215 - * @param bool If we should we force-enable WooCommerce tables support.
1216 - */
1217 - $force_woocommerce_support = apply_filters( 'jetpack_table_checksum_force_enable_woocommerce', false );
1239 + $result = array();
1218 1240
1219 - if ( $force_woocommerce_support || class_exists( 'WooCommerce' ) ) {
1220 - /**
1221 - * Guard in Try/Catch as it's possible for the WooCommerce class to exist, but
1222 - * the tables to not. If we don't do this, the response will be just the exception, without
1223 - * returning any valid data. This will prevent us from ever performing a checksum/fix
1224 - * for sites like this.
1225 - * It's better to just skip the tables in the response, instead of completely failing.
1226 - */
1227 -
1241 + foreach ( $all_checksum_tables as $table ) {
1242 + $result_key = in_array( $table, array( 'postmeta', 'commentmeta' ), true ) ? str_replace( 'meta', '_meta', $table ) : $table;
1228 1243 try {
1229 - $woocommerce_order_items_checksum = $this->checksum_histogram( 'woocommerce_order_items' );
1230 - $result['woocommerce_order_items'] = $this->summarize_checksum_histogram( $woocommerce_order_items_checksum );
1244 + $checksum = $this->checksum_histogram( $table, null, null, null, null, true, '', false, false, $perform_text_conversion );
1245 + $result[ $result_key ] = $this->summarize_checksum_histogram( $checksum );
1231 1246 } catch ( Exception $ex ) {
1232 - $result['woocommerce_order_items'] = null;
1247 + $result[ $result_key ] = null;
1233 1248 }
1234 -
1235 - try {
1236 - $woocommerce_order_itemmeta_checksum = $this->checksum_histogram( 'woocommerce_order_itemmeta' );
1237 - $result['woocommerce_order_itemmeta'] = $this->summarize_checksum_histogram( $woocommerce_order_itemmeta_checksum );
1238 - } catch ( Exception $ex ) {
1239 - $result['woocommerce_order_itemmeta'] = null;
1240 - }
1241 1249 }
1242 1250
1243 1251 return $result;
1244 1252 }
@@ -1323,9 +1331,9 @@
1323 1331
1324 1332 try {
1325 1333 $range_edges = $checksum_table->get_range_edges( $start_id, $end_id );
1326 1334 } catch ( Exception $ex ) {
1327 - return new WP_Error( 'invalid_range_edges', '[' . $start_id . '-' . $end_id . ']: ' . $ex->getMessage() );
1335 + return new WP_Error( 'invalid_range_edges', '[' . ( $start_id ?? 'null' ) . '-' . ( $end_id ?? 'null' ) . ']: ' . $ex->getMessage() );
1328 1336 }
1329 1337
1330 1338 if ( $only_range_edges ) {
1331 1339 return $range_edges;
@@ -1369,9 +1377,12 @@
1369 1377 $histogram[ $ids_range['min_range'] ] = $batch_checksum;
1370 1378 } else {
1371 1379 $histogram[ "{$ids_range[ 'min_range' ]}-{$ids_range[ 'max_range' ]}" ] = $batch_checksum;
1372 1380 }
1373 -
1381 + // If ids_range['max_range'] is PHP_INT_MAX, we've reached the end of the table. Edge case causing the loop to never end.
1382 + if ( PHP_INT_MAX === (int) $ids_range['max_range'] ) {
1383 + break;
1384 + }
1374 1385 $previous_max_id = $ids_range['max_range'] + 1;
1375 1386 // If we've reached the max_range lets bail out.
1376 1387 if ( $previous_max_id > $range_edges['max_range'] ) {
1377 1388 break;