| @@ -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,8 +66,27 @@ | ||
| 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 | 91 | * @since 3.1.2 |
| 73 | 92 | * |
| @@ -77,10 +96,16 @@ | ||
| 77 | 96 | public function add_to_queue( $object_id ) { |
| 78 | 97 | if ( ! is_numeric( $object_id ) ) { |
| 79 | 98 | return false; |
| 80 | 99 | } |
| 81 | - $this->sync_queue[ $object_id ] = true; | |
| 82 | 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 | + | |
| 83 | 108 | /** |
| 84 | 109 | * Fires after item is added to sync queue |
| 85 | 110 | * |
| 86 | 111 | * @hook ep_after_add_to_queue |
| @@ -87,9 +112,9 @@ | ||
| 87 | 112 | * @param {int} $object_id ID of object |
| 88 | 113 | * @param {array} $sync_queue Current sync queue |
| 89 | 114 | * @since 3.1.2 |
| 90 | 115 | */ |
| 91 | - 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() ); | |
| 92 | 117 | |
| 93 | 118 | return true; |
| 94 | 119 | } |
| 95 | 120 | |
| @@ -105,10 +130,15 @@ | ||
| 105 | 130 | if ( ! is_numeric( $object_id ) ) { |
| 106 | 131 | return false; |
| 107 | 132 | } |
| 108 | 133 | |
| 109 | - 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 | + } | |
| 110 | 138 | |
| 139 | + unset( $this->sync_queue[ $current_blog_id ][ $object_id ] ); | |
| 140 | + | |
| 111 | 141 | /** |
| 112 | 142 | * Fires after item is removed from sync queue |
| 113 | 143 | * |
| 114 | 144 | * @hook ep_after_remove_from_queue |
| @@ -115,14 +145,28 @@ | ||
| 115 | 145 | * @param {int} $object_id ID of object |
| 116 | 146 | * @param {array} $sync_queue Current sync queue |
| 117 | 147 | * @since 3.5 |
| 118 | 148 | */ |
| 119 | - 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() ); | |
| 120 | 150 | |
| 121 | 151 | return true; |
| 122 | 152 | } |
| 123 | 153 | |
| 124 | 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 | + /** | |
| 125 | 169 | * Sync queued objects if the EP_SYNC_CHUNK_LIMIT is reached. |
| 126 | 170 | * |
| 127 | 171 | * @since 3.1.2 |
| 128 | 172 | * @return boolean |
| @@ -128,9 +172,9 @@ | ||
| 128 | 172 | * @return boolean |
| 129 | 173 | */ |
| 130 | 174 | public function index_sync_on_chunk_limit() { |
| 131 | 175 | if ( defined( 'EP_SYNC_CHUNK_LIMIT' ) && is_numeric( EP_SYNC_CHUNK_LIMIT ) && |
| 132 | - 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 ) { | |
| 133 | 177 | $this->index_sync_queue(); |
| 134 | 178 | } |
| 135 | 179 | return true; |
| 136 | 180 | } |
| @@ -158,42 +202,57 @@ | ||
| 158 | 202 | if ( empty( $this->sync_queue ) ) { |
| 159 | 203 | return; |
| 160 | 204 | } |
| 161 | 205 | |
| 162 | - /** | |
| 163 | - * Allow other code to intercept the sync process | |
| 164 | - * | |
| 165 | - * @hook pre_ep_index_sync_queue | |
| 166 | - * @param {boolean} $bail True to skip the rest of index_sync_queue(), false to continue normally | |
| 167 | - * @param {SyncManager} $sync_manager SyncManager instance for the indexable | |
| 168 | - * @param {string} $indexable_slug Slug of the indexable being synced | |
| 169 | - * @since 3.5 | |
| 170 | - */ | |
| 171 | - if ( apply_filters( 'pre_ep_index_sync_queue', false, $this, $this->indexable_slug ) ) { | |
| 172 | - return; | |
| 173 | - } | |
| 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 | + } | |
| 174 | 211 | |
| 175 | - /** | |
| 176 | - * Backwards compat for pre-3.0 | |
| 177 | - */ | |
| 178 | - foreach ( $this->sync_queue as $object_id => $value ) { | |
| 212 | + if ( $current_blog_id !== $blog_id ) { | |
| 213 | + switch_to_blog( $blog_id ); | |
| 214 | + } | |
| 215 | + | |
| 179 | 216 | /** |
| 180 | - * Fires when object in queue are synced | |
| 217 | + * Allow other code to intercept the sync process | |
| 181 | 218 | * |
| 182 | - * @hook ep_sync_on_meta_update_queue | |
| 183 | - * @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 | |
| 184 | 224 | */ |
| 185 | - do_action( 'ep_sync_on_meta_update', $object_id ); | |
| 186 | - } | |
| 225 | + if ( apply_filters( 'pre_ep_index_sync_queue', false, $this, $this->indexable_slug ) ) { | |
| 226 | + return; | |
| 227 | + } | |
| 187 | 228 | |
| 188 | - // Bulk sync them all. | |
| 189 | - 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 | + } | |
| 190 | 241 | |
| 191 | - /** | |
| 192 | - * Make sure to reset sync queue in case an shutdown happens before a redirect | |
| 193 | - * when a redirect has already been triggered. | |
| 194 | - */ | |
| 195 | - $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 | + } | |
| 196 | 255 | } |
| 197 | 256 | |
| 198 | 257 | /** |
| 199 | 258 | * Check if we can index content in the current blog |