| @@ -42,9 +42,9 @@ | ||
| 42 | 42 | |
| 43 | 43 | if ( defined( 'EP_SYNC_CHUNK_LIMIT' ) && is_numeric( EP_SYNC_CHUNK_LIMIT ) ) { |
| 44 | 44 | /** |
| 45 | 45 | * We also sync when we exceed Chunk limit set. |
| 46 | - * This is sometimes useful when posts are generated programatically. | |
| 46 | + * This is sometimes useful when posts are generated programmatically. | |
| 47 | 47 | */ |
| 48 | 48 | add_action( 'ep_after_add_to_queue', [ $this, 'index_sync_on_chunk_limit' ] ); |
| 49 | 49 | } |
| 50 | 50 | /** |
| @@ -66,12 +66,32 @@ | ||
| 66 | 66 | $this->setup(); |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | + * Get sync queue. | |
| 71 | + * | |
| 72 | + * @since 5.0.0 | |
| 73 | + * @param int $blog_id Blog ID to retrieve queue. | |
| 74 | + * @return array | |
| 75 | + */ | |
| 76 | + public function get_sync_queue( $blog_id = false ) { | |
| 77 | + if ( ! $blog_id ) { | |
| 78 | + $blog_id = get_current_blog_id(); | |
| 79 | + } | |
| 80 | + | |
| 81 | + if ( ! isset( $this->sync_queue[ $blog_id ] ) ) { | |
| 82 | + $this->sync_queue[ $blog_id ] = []; | |
| 83 | + } | |
| 84 | + | |
| 85 | + return $this->sync_queue[ $blog_id ]; | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 70 | 89 | * Add an object to the sync queue. |
| 71 | 90 | * |
| 72 | - * @param id $object_id object ID to sync | |
| 73 | - * @since 3.1.2 | |
| 91 | + * @since 3.1.2 | |
| 92 | + * | |
| 93 | + * @param int $object_id Object ID to sync. | |
| 74 | 94 | * @return boolean |
| 75 | 95 | */ |
| 76 | 96 | public function add_to_queue( $object_id ) { |
| 77 | 97 | if ( ! is_numeric( $object_id ) ) { |
| @@ -76,10 +96,16 @@ | ||
| 76 | 96 | public function add_to_queue( $object_id ) { |
| 77 | 97 | if ( ! is_numeric( $object_id ) ) { |
| 78 | 98 | return false; |
| 79 | 99 | } |
| 80 | - $this->sync_queue[ $object_id ] = true; | |
| 81 | 100 | |
| 101 | + $current_blog_id = get_current_blog_id(); | |
| 102 | + if ( ! isset( $this->sync_queue[ $current_blog_id ] ) ) { | |
| 103 | + $this->sync_queue[ $current_blog_id ] = []; | |
| 104 | + } | |
| 105 | + | |
| 106 | + $this->sync_queue[ $current_blog_id ][ $object_id ] = true; | |
| 107 | + | |
| 82 | 108 | /** |
| 83 | 109 | * Fires after item is added to sync queue |
| 84 | 110 | * |
| 85 | 111 | * @hook ep_after_add_to_queue |
| @@ -86,9 +112,9 @@ | ||
| 86 | 112 | * @param {int} $object_id ID of object |
| 87 | 113 | * @param {array} $sync_queue Current sync queue |
| 88 | 114 | * @since 3.1.2 |
| 89 | 115 | */ |
| 90 | - do_action( 'ep_after_add_to_queue', $object_id, $this->sync_queue ); | |
| 116 | + do_action( 'ep_after_add_to_queue', $object_id, $this->get_sync_queue() ); | |
| 91 | 117 | |
| 92 | 118 | return true; |
| 93 | 119 | } |
| 94 | 120 | |
| @@ -94,10 +120,11 @@ | ||
| 94 | 120 | |
| 95 | 121 | /** |
| 96 | 122 | * Remove an object from the sync queue. |
| 97 | 123 | * |
| 98 | - * @param id $object_id object ID to remove from the queue | |
| 99 | - * @since 3.5 | |
| 124 | + * @since 3.5 | |
| 125 | + * | |
| 126 | + * @param int $object_id Object ID to remove from the queue. | |
| 100 | 127 | * @return boolean |
| 101 | 128 | */ |
| 102 | 129 | public function remove_from_queue( $object_id ) { |
| 103 | 130 | if ( ! is_numeric( $object_id ) ) { |
| @@ -103,10 +130,15 @@ | ||
| 103 | 130 | if ( ! is_numeric( $object_id ) ) { |
| 104 | 131 | return false; |
| 105 | 132 | } |
| 106 | 133 | |
| 107 | - unset( $this->sync_queue[ $object_id ] ); | |
| 134 | + $current_blog_id = get_current_blog_id(); | |
| 135 | + if ( ! isset( $this->sync_queue[ $current_blog_id ] ) ) { | |
| 136 | + $this->sync_queue[ $current_blog_id ] = []; | |
| 137 | + } | |
| 108 | 138 | |
| 139 | + unset( $this->sync_queue[ $current_blog_id ][ $object_id ] ); | |
| 140 | + | |
| 109 | 141 | /** |
| 110 | 142 | * Fires after item is removed from sync queue |
| 111 | 143 | * |
| 112 | 144 | * @hook ep_after_remove_from_queue |
| @@ -113,14 +145,28 @@ | ||
| 113 | 145 | * @param {int} $object_id ID of object |
| 114 | 146 | * @param {array} $sync_queue Current sync queue |
| 115 | 147 | * @since 3.5 |
| 116 | 148 | */ |
| 117 | - do_action( 'ep_after_remove_from_queue', $object_id, $this->sync_queue ); | |
| 149 | + do_action( 'ep_after_remove_from_queue', $object_id, $this->get_sync_queue() ); | |
| 118 | 150 | |
| 119 | 151 | return true; |
| 120 | 152 | } |
| 121 | 153 | |
| 122 | 154 | /** |
| 155 | + * Reset the sync queue. | |
| 156 | + * | |
| 157 | + * @since 5.0.0 | |
| 158 | + * @param int $blog_id Blog ID to reset queue | |
| 159 | + */ | |
| 160 | + public function reset_sync_queue( $blog_id = false ) { | |
| 161 | + if ( ! $blog_id ) { | |
| 162 | + $blog_id = get_current_blog_id(); | |
| 163 | + } | |
| 164 | + | |
| 165 | + $this->sync_queue[ $blog_id ] = []; | |
| 166 | + } | |
| 167 | + | |
| 168 | + /** | |
| 123 | 169 | * Sync queued objects if the EP_SYNC_CHUNK_LIMIT is reached. |
| 124 | 170 | * |
| 125 | 171 | * @since 3.1.2 |
| 126 | 172 | * @return boolean |
| @@ -126,9 +172,9 @@ | ||
| 126 | 172 | * @return boolean |
| 127 | 173 | */ |
| 128 | 174 | public function index_sync_on_chunk_limit() { |
| 129 | 175 | if ( defined( 'EP_SYNC_CHUNK_LIMIT' ) && is_numeric( EP_SYNC_CHUNK_LIMIT ) && |
| 130 | - is_array( $this->sync_queue ) && count( $this->sync_queue ) > EP_SYNC_CHUNK_LIMIT ) { | |
| 176 | + is_array( $this->get_sync_queue() ) && count( $this->get_sync_queue() ) > EP_SYNC_CHUNK_LIMIT ) { | |
| 131 | 177 | $this->index_sync_queue(); |
| 132 | 178 | } |
| 133 | 179 | return true; |
| 134 | 180 | } |
| @@ -146,9 +192,8 @@ | ||
| 146 | 192 | |
| 147 | 193 | return $location; |
| 148 | 194 | } |
| 149 | 195 | |
| 150 | - | |
| 151 | 196 | /** |
| 152 | 197 | * Sync objects in queue. |
| 153 | 198 | * |
| 154 | 199 | * @since 3.0 |
| @@ -157,42 +202,57 @@ | ||
| 157 | 202 | if ( empty( $this->sync_queue ) ) { |
| 158 | 203 | return; |
| 159 | 204 | } |
| 160 | 205 | |
| 161 | - /** | |
| 162 | - * Allow other code to intercept the sync process | |
| 163 | - * | |
| 164 | - * @hook pre_ep_index_sync_queue | |
| 165 | - * @param {boolean} $bail True to skip the rest of index_sync_queue(), false to continue normally | |
| 166 | - * @param {SyncManager} $sync_manager SyncManager instance for the indexable | |
| 167 | - * @param {string} $indexable_slug Slug of the indexable being synced | |
| 168 | - * @since 3.5 | |
| 169 | - */ | |
| 170 | - if ( apply_filters( 'pre_ep_index_sync_queue', false, $this, $this->indexable_slug ) ) { | |
| 171 | - return; | |
| 172 | - } | |
| 206 | + $current_blog_id = get_current_blog_id(); | |
| 207 | + foreach ( $this->sync_queue as $blog_id => $sync_queue ) { | |
| 208 | + if ( empty( $sync_queue ) ) { | |
| 209 | + continue; | |
| 210 | + } | |
| 173 | 211 | |
| 174 | - /** | |
| 175 | - * Backwards compat for pre-3.0 | |
| 176 | - */ | |
| 177 | - foreach ( $this->sync_queue as $object_id => $value ) { | |
| 212 | + if ( $current_blog_id !== $blog_id ) { | |
| 213 | + switch_to_blog( $blog_id ); | |
| 214 | + } | |
| 215 | + | |
| 178 | 216 | /** |
| 179 | - * Fires when object in queue are synced | |
| 217 | + * Allow other code to intercept the sync process | |
| 180 | 218 | * |
| 181 | - * @hook ep_sync_on_meta_update_queue | |
| 182 | - * @param {int} $object_id ID of object | |
| 219 | + * @hook pre_ep_index_sync_queue | |
| 220 | + * @param {boolean} $bail True to skip the rest of index_sync_queue(), false to continue normally | |
| 221 | + * @param {SyncManager} $sync_manager SyncManager instance for the indexable | |
| 222 | + * @param {string} $indexable_slug Slug of the indexable being synced | |
| 223 | + * @since 3.5 | |
| 183 | 224 | */ |
| 184 | - do_action( 'ep_sync_on_meta_update', $object_id ); | |
| 185 | - } | |
| 225 | + if ( apply_filters( 'pre_ep_index_sync_queue', false, $this, $this->indexable_slug ) ) { | |
| 226 | + return; | |
| 227 | + } | |
| 186 | 228 | |
| 187 | - // Bulk sync them all. | |
| 188 | - Indexables::factory()->get( $this->indexable_slug )->bulk_index_dynamically( array_keys( $this->sync_queue ) ); | |
| 229 | + /** | |
| 230 | + * Backwards compat for pre-3.0 | |
| 231 | + */ | |
| 232 | + foreach ( $sync_queue as $object_id => $value ) { | |
| 233 | + /** | |
| 234 | + * Fires when object in queue are synced | |
| 235 | + * | |
| 236 | + * @hook ep_sync_on_meta_update_queue | |
| 237 | + * @param {int} $object_id ID of object | |
| 238 | + */ | |
| 239 | + do_action( 'ep_sync_on_meta_update', $object_id ); | |
| 240 | + } | |
| 189 | 241 | |
| 190 | - /** | |
| 191 | - * Make sure to reset sync queue in case an shutdown happens before a redirect | |
| 192 | - * when a redirect has already been triggered. | |
| 193 | - */ | |
| 194 | - $this->sync_queue = []; | |
| 242 | + // Bulk sync them all. | |
| 243 | + Indexables::factory()->get( $this->indexable_slug )->bulk_index_dynamically( array_keys( $this->get_sync_queue( $blog_id ) ) ); | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * Make sure to reset sync queue in case an shutdown happens before a redirect | |
| 247 | + * when a redirect has already been triggered. | |
| 248 | + */ | |
| 249 | + $this->reset_sync_queue( $blog_id ); | |
| 250 | + | |
| 251 | + if ( $current_blog_id !== $blog_id ) { | |
| 252 | + restore_current_blog(); | |
| 253 | + } | |
| 254 | + } | |
| 195 | 255 | } |
| 196 | 256 | |
| 197 | 257 | /** |
| 198 | 258 | * Check if we can index content in the current blog |