From f18b23a3a9378fb0a98856d436aa9ebf94e47429 Mon Sep 17 00:00:00 2001 From: Yury German Date: Sun, 23 Jan 2022 18:37:36 -0500 Subject: Updating Classic Editor, Google Authenticatior, Jetpack, Public Post Preview, Table of Contents, Wordpress Importer Signed-off-by: Yury German --- .../src/modules/class-term-relationships.php | 244 +++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/modules/class-term-relationships.php (limited to 'plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/modules/class-term-relationships.php') diff --git a/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/modules/class-term-relationships.php b/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/modules/class-term-relationships.php new file mode 100644 index 00000000..2a7c22e3 --- /dev/null +++ b/plugins/jetpack/jetpack_vendor/automattic/jetpack-sync/src/modules/class-term-relationships.php @@ -0,0 +1,244 @@ + self::MAX_INT, + 'term_taxonomy_id' => self::MAX_INT, + ); + + while ( $limit > 0 ) { + /* + * SELECT object_id, term_taxonomy_id + * FROM $wpdb->term_relationships + * WHERE ( object_id = 11 AND term_taxonomy_id < 14 ) OR ( object_id < 11 ) + * ORDER BY object_id DESC, term_taxonomy_id DESC LIMIT 1000 + */ + $objects = $wpdb->get_results( $wpdb->prepare( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships WHERE ( object_id = %d AND term_taxonomy_id < %d ) OR ( object_id < %d ) ORDER BY object_id DESC, term_taxonomy_id DESC LIMIT %d", $last_object_enqueued['object_id'], $last_object_enqueued['term_taxonomy_id'], $last_object_enqueued['object_id'], $limit ), ARRAY_A ); + // Request term relationships in groups of N for efficiency. + $objects_count = count( $objects ); + if ( ! count( $objects ) ) { + return array( $items_enqueued_count, true ); + } + $items = array_chunk( $objects, $term_relationships_full_sync_item_size ); + $last_object_enqueued = $this->bulk_enqueue_full_sync_term_relationships( $items, $last_object_enqueued ); + $items_enqueued_count += count( $items ); + $limit = min( $limit - $objects_count, self::QUERY_LIMIT ); + } + + // We need to do this extra check in case $max_items_to_enqueue * $term_relationships_full_sync_item_size == relationships objects left. + $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE ( object_id = %d AND term_taxonomy_id < %d ) OR ( object_id < %d ) ORDER BY object_id DESC, term_taxonomy_id DESC LIMIT %d", $last_object_enqueued['object_id'], $last_object_enqueued['term_taxonomy_id'], $last_object_enqueued['object_id'], 1 ) ); + if ( 0 === (int) $count ) { + return array( $items_enqueued_count, true ); + } + + return array( $items_enqueued_count, $last_object_enqueued ); + } + + /** + * Return the initial last sent object. + * + * @return string|array initial status. + */ + public function get_initial_last_sent() { + return array( + 'object_id' => self::MAX_INT, + 'term_taxonomy_id' => self::MAX_INT, + ); + } + + /** + * Given the Module Full Sync Configuration and Status return the next chunk of items to send. + * + * @param array $config This module Full Sync configuration. + * @param array $status This module Full Sync status. + * @param int $chunk_size Chunk size. + * + * @return array|object|null + */ + public function get_next_chunk( $config, $status, $chunk_size ) { + global $wpdb; + + return $wpdb->get_results( + $wpdb->prepare( + "SELECT object_id, term_taxonomy_id + FROM $wpdb->term_relationships + WHERE ( object_id = %d AND term_taxonomy_id < %d ) OR ( object_id < %d ) + ORDER BY object_id DESC, term_taxonomy_id + DESC LIMIT %d", + $status['last_sent']['object_id'], + $status['last_sent']['term_taxonomy_id'], + $status['last_sent']['object_id'], + $chunk_size + ), + ARRAY_A + ); + } + + /** + * + * Enqueue all $items within `jetpack_full_sync_term_relationships` actions. + * + * @param array $items Groups of objects to sync. + * @param array $previous_interval_end Last item enqueued. + * + * @return array Last enqueued object. + */ + public function bulk_enqueue_full_sync_term_relationships( $items, $previous_interval_end ) { + $listener = Listener::get_instance(); + $items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $items, $previous_interval_end ); + $listener->bulk_enqueue_full_sync_actions( 'jetpack_full_sync_term_relationships', $items_with_previous_interval_end ); + $last_item = end( $items ); + return end( $last_item ); + } + + /** + * Retrieve an estimated number of actions that will be enqueued. + * + * @access public + * + * @param array $config Full sync configuration for this sync module. + * @return int Number of items yet to be enqueued. + */ + public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + global $wpdb; + + $query = "SELECT COUNT(*) FROM $wpdb->term_relationships"; + + // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared + $count = $wpdb->get_var( $query ); + + return (int) ceil( $count / Settings::get_setting( 'term_relationships_full_sync_item_size' ) ); + } + + /** + * Retrieve the actions that will be sent for this module during a full sync. + * + * @access public + * + * @return array Full sync actions of this module. + */ + public function get_full_sync_actions() { + return array( 'jetpack_full_sync_term_relationships' ); + } + + /** + * Expand the term relationships within a hook before they are serialized and sent to the server. + * + * @access public + * + * @param array $args The hook parameters. + * @return array $args The expanded hook parameters. + */ + public function expand_term_relationships( $args ) { + list( $term_relationships, $previous_end ) = $args; + + return array( + 'term_relationships' => $term_relationships, + 'previous_end' => $previous_end, + ); + } +} -- cgit v1.2.3-65-gdbad