← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/modules/class-term-relationships.php
+50
-10
12.3.2
→
16.3-beta
View file →
| @@ -9,8 +9,12 @@ | ||
| 9 | 9 | |
| 10 | 10 | use Automattic\Jetpack\Sync\Listener; |
| 11 | 11 | use Automattic\Jetpack\Sync\Settings; |
| 12 | 12 | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit( 0 ); | |
| 15 | +} | |
| 16 | + | |
| 13 | 17 | /** |
| 14 | 18 | * Class to handle sync for term relationships. |
| 15 | 19 | */ |
| 16 | 20 | class Term_Relationships extends Module { |
| @@ -55,19 +59,33 @@ | ||
| 55 | 59 | return 'object_id'; |
| 56 | 60 | } |
| 57 | 61 | |
| 58 | 62 | /** |
| 59 | - * The table in the database. | |
| 63 | + * The table name. | |
| 60 | 64 | * |
| 61 | 65 | * @access public |
| 62 | 66 | * |
| 63 | 67 | * @return string |
| 68 | + * @deprecated since 3.11.0 Use table() instead. | |
| 64 | 69 | */ |
| 65 | 70 | public function table_name() { |
| 71 | + _deprecated_function( __METHOD__, '3.11.0', 'Automattic\\Jetpack\\Sync\\Term_Relationships->table' ); | |
| 66 | 72 | return 'term_relationships'; |
| 67 | 73 | } |
| 68 | 74 | |
| 69 | 75 | /** |
| 76 | + * The table in the database with the prefix. | |
| 77 | + * | |
| 78 | + * @access public | |
| 79 | + * | |
| 80 | + * @return string|bool | |
| 81 | + */ | |
| 82 | + public function table() { | |
| 83 | + global $wpdb; | |
| 84 | + return $wpdb->term_relationships; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 70 | 88 | * Initialize term relationships action listeners for full sync. |
| 71 | 89 | * |
| 72 | 90 | * @access public |
| 73 | 91 | * |
| @@ -118,12 +136,12 @@ | ||
| 118 | 136 | * ORDER BY object_id DESC, term_taxonomy_id DESC LIMIT 1000 |
| 119 | 137 | */ |
| 120 | 138 | $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 ); |
| 121 | 139 | // Request term relationships in groups of N for efficiency. |
| 122 | - $objects_count = is_countable( $objects ) ? count( $objects ) : 0; | |
| 123 | - if ( ! $objects_count ) { | |
| 140 | + if ( ! is_countable( $objects ) || count( $objects ) === 0 ) { | |
| 124 | 141 | return array( $items_enqueued_count, true ); |
| 125 | 142 | } |
| 143 | + $objects_count = count( $objects ); | |
| 126 | 144 | $items = array_chunk( $objects, $term_relationships_full_sync_item_size ); |
| 127 | 145 | $last_object_enqueued = $this->bulk_enqueue_full_sync_term_relationships( $items, $last_object_enqueued ); |
| 128 | 146 | $items_enqueued_count += count( $items ); |
| 129 | 147 | $limit = min( $limit - $objects_count, self::QUERY_LIMIT ); |
| @@ -163,13 +181,16 @@ | ||
| 163 | 181 | global $wpdb; |
| 164 | 182 | |
| 165 | 183 | return $wpdb->get_results( |
| 166 | 184 | $wpdb->prepare( |
| 167 | - "SELECT object_id, term_taxonomy_id | |
| 168 | - FROM $wpdb->term_relationships | |
| 169 | - WHERE ( object_id = %d AND term_taxonomy_id < %d ) OR ( object_id < %d ) | |
| 170 | - ORDER BY object_id DESC, term_taxonomy_id | |
| 171 | - DESC LIMIT %d", | |
| 185 | + "SELECT tr.object_id, tr.term_taxonomy_id | |
| 186 | + FROM $wpdb->term_relationships tr INNER JOIN $wpdb->term_taxonomy tt | |
| 187 | + ON tr.term_taxonomy_id=tt.term_taxonomy_id | |
| 188 | + WHERE " . | |
| 189 | + Settings::get_whitelisted_taxonomies_sql() // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 190 | + . ' AND ( ( tr.object_id = %d AND tr.term_taxonomy_id < %d ) OR ( tr.object_id < %d ) ) | |
| 191 | + ORDER BY tr.object_id DESC, tr.term_taxonomy_id | |
| 192 | + DESC LIMIT %d', | |
| 172 | 193 | $status['last_sent']['object_id'], |
| 173 | 194 | $status['last_sent']['term_taxonomy_id'], |
| 174 | 195 | $status['last_sent']['object_id'], |
| 175 | 196 | $chunk_size |
| @@ -178,9 +199,28 @@ | ||
| 178 | 199 | ); |
| 179 | 200 | } |
| 180 | 201 | |
| 181 | 202 | /** |
| 203 | + * Return last_item to send for Module Full Sync Configuration. | |
| 182 | 204 | * |
| 205 | + * @param array $config This module Full Sync configuration. | |
| 206 | + * | |
| 207 | + * @return array|object|null | |
| 208 | + */ | |
| 209 | + public function get_last_item( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 210 | + global $wpdb; | |
| 211 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 212 | + return $wpdb->get_results( | |
| 213 | + "SELECT object_id, term_taxonomy_id | |
| 214 | + FROM $wpdb->term_relationships | |
| 215 | + ORDER BY object_id , term_taxonomy_id | |
| 216 | + LIMIT 1", | |
| 217 | + ARRAY_A | |
| 218 | + ); | |
| 219 | + } | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * | |
| 183 | 223 | * Enqueue all $items within `jetpack_full_sync_term_relationships` actions. |
| 184 | 224 | * |
| 185 | 225 | * @param array $items Groups of objects to sync. |
| 186 | 226 | * @param array $previous_interval_end Last item enqueued. |
| @@ -207,10 +247,10 @@ | ||
| 207 | 247 | global $wpdb; |
| 208 | 248 | |
| 209 | 249 | $query = "SELECT COUNT(*) FROM $wpdb->term_relationships"; |
| 210 | 250 | |
| 211 | - // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared | |
| 212 | - $count = $wpdb->get_var( $query ); | |
| 251 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 252 | + $count = (int) $wpdb->get_var( $query ); | |
| 213 | 253 | |
| 214 | 254 | return (int) ceil( $count / Settings::get_setting( 'term_relationships_full_sync_item_size' ) ); |
| 215 | 255 | } |
| 216 | 256 | |