← All changes
|
modules/global-classes/global-classes-post-ids.php
+14
-107
4.1.2
→
4.3.0-beta3
View file →
| @@ -1,13 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\GlobalClasses; |
| 4 | 4 | |
| 5 | -use Elementor\Core\Base\Document; | |
| 6 | 5 | use Elementor\Core\Kits\Documents\Kit; |
| 7 | -use Elementor\Modules\GlobalClasses\Concerns\Has_Kit_Dependency; | |
| 8 | -use Elementor\Plugin; | |
| 9 | -use Elementor\TemplateLibrary\Source_Local; | |
| 6 | +use Elementor\Core\Kits\Concerns\Has_Kit_Dependency; | |
| 7 | +use Elementor\Modules\GlobalClasses\Utils\Kit_Utils; | |
| 10 | 8 | use WP_Post; |
| 11 | 9 | |
| 12 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | 11 | exit; |
| @@ -17,10 +15,8 @@ | ||
| 17 | 15 | use Has_Kit_Dependency; |
| 18 | 16 | |
| 19 | 17 | const META_KEY = '_elementor_global_classes_post_ids'; |
| 20 | 18 | |
| 21 | - const BACKFILL_BATCH_SIZE = 100; | |
| 22 | - | |
| 23 | 19 | private ?array $cache = null; |
| 24 | 20 | |
| 25 | 21 | public static function make( ?Kit $kit = null ): self { |
| 26 | 22 | $instance = new self(); |
| @@ -40,29 +36,10 @@ | ||
| 40 | 36 | if ( Global_Class_Post_Type::CPT !== $post->post_type ) { |
| 41 | 37 | return; |
| 42 | 38 | } |
| 43 | 39 | |
| 44 | - $kit_ids = get_posts( [ | |
| 45 | - 'post_type' => Source_Local::CPT, | |
| 46 | - 'post_status' => 'any', | |
| 47 | - 'fields' => 'ids', | |
| 48 | - 'posts_per_page' => -1, | |
| 49 | - 'no_found_rows' => true, | |
| 50 | - 'update_post_meta_cache' => false, | |
| 51 | - 'meta_query' => [ | |
| 52 | - [ | |
| 53 | - 'key' => Document::TYPE_META_KEY, | |
| 54 | - 'value' => 'kit', | |
| 55 | - ], | |
| 56 | - ], | |
| 57 | - ] ); | |
| 58 | - | |
| 59 | - foreach ( $kit_ids as $kit_id ) { | |
| 60 | - $kit = Plugin::$instance->kits_manager->get_kit( $kit_id ); | |
| 61 | - | |
| 62 | - if ( $kit ) { | |
| 63 | - self::make( $kit )->remove_post_id( $post_id ); | |
| 64 | - } | |
| 40 | + foreach ( Kit_Utils::get_all_kit_documents() as $kit ) { | |
| 41 | + self::make( $kit )->remove_post_id( $post_id ); | |
| 65 | 42 | } |
| 66 | 43 | } |
| 67 | 44 | |
| 68 | 45 | public function get_post_id( string $class_id ): ?int { |
| @@ -67,21 +44,21 @@ | ||
| 67 | 44 | |
| 68 | 45 | public function get_post_id( string $class_id ): ?int { |
| 69 | 46 | $map = $this->read_map(); |
| 70 | 47 | |
| 71 | - if ( isset( $map[ $class_id ] ) ) { | |
| 72 | - $post_id = (int) $map[ $class_id ]; | |
| 48 | + if ( ! isset( $map[ $class_id ] ) ) { | |
| 49 | + return null; | |
| 50 | + } | |
| 73 | 51 | |
| 74 | - if ( get_post( $post_id ) ) { | |
| 75 | - return $post_id; | |
| 76 | - } | |
| 52 | + $post_id = (int) $map[ $class_id ]; | |
| 77 | 53 | |
| 78 | - $this->remove_post_id( $post_id ); | |
| 54 | + if ( get_post( $post_id ) ) { | |
| 55 | + return $post_id; | |
| 79 | 56 | } |
| 80 | 57 | |
| 81 | - $resolved = $this->backfill( [ $class_id ] ); | |
| 58 | + $this->remove_post_id( $post_id ); | |
| 82 | 59 | |
| 83 | - return $resolved[ $class_id ] ?? null; | |
| 60 | + return null; | |
| 84 | 61 | } |
| 85 | 62 | |
| 86 | 63 | public function get_post_ids( array $class_ids ): array { |
| 87 | 64 | if ( empty( $class_ids ) ) { |
| @@ -87,15 +64,13 @@ | ||
| 87 | 64 | if ( empty( $class_ids ) ) { |
| 88 | 65 | return []; |
| 89 | 66 | } |
| 90 | 67 | |
| 91 | - $map = $this->read_map(); | |
| 68 | + $map = $this->read_map(); | |
| 92 | 69 | $resolved = []; |
| 93 | - $missing = []; | |
| 94 | 70 | |
| 95 | 71 | foreach ( $class_ids as $class_id ) { |
| 96 | 72 | if ( ! isset( $map[ $class_id ] ) ) { |
| 97 | - $missing[] = $class_id; | |
| 98 | 73 | continue; |
| 99 | 74 | } |
| 100 | 75 | |
| 101 | 76 | $post_id = (int) $map[ $class_id ]; |
| @@ -102,16 +77,12 @@ | ||
| 102 | 77 | |
| 103 | 78 | if ( get_post( $post_id ) ) { |
| 104 | 79 | $resolved[ $class_id ] = $post_id; |
| 105 | 80 | } else { |
| 106 | - $missing[] = $class_id; | |
| 81 | + $this->remove_post_id( $post_id ); | |
| 107 | 82 | } |
| 108 | 83 | } |
| 109 | 84 | |
| 110 | - if ( ! empty( $missing ) ) { | |
| 111 | - $resolved += $this->backfill( $missing ); | |
| 112 | - } | |
| 113 | - | |
| 114 | 85 | return $resolved; |
| 115 | 86 | } |
| 116 | 87 | |
| 117 | 88 | public function set( string $class_id, int $post_id ): void { |
| @@ -163,72 +134,8 @@ | ||
| 163 | 134 | return; |
| 164 | 135 | } |
| 165 | 136 | |
| 166 | 137 | $this->write_map( $filtered ); |
| 167 | - } | |
| 168 | - | |
| 169 | - private function backfill( array $class_ids ): array { | |
| 170 | - $resolved = []; | |
| 171 | - | |
| 172 | - foreach ( array_chunk( array_values( array_unique( $class_ids ) ), self::BACKFILL_BATCH_SIZE ) as $chunk ) { | |
| 173 | - $resolved += $this->resolve_via_query( $chunk ); | |
| 174 | - } | |
| 175 | - | |
| 176 | - if ( ! empty( $resolved ) ) { | |
| 177 | - $this->set_many( $resolved ); | |
| 178 | - } | |
| 179 | - | |
| 180 | - return $resolved; | |
| 181 | - } | |
| 182 | - | |
| 183 | - private function resolve_via_query( array $class_ids ): array { | |
| 184 | - if ( empty( $class_ids ) ) { | |
| 185 | - return []; | |
| 186 | - } | |
| 187 | - | |
| 188 | - global $wpdb; | |
| 189 | - | |
| 190 | - $placeholders = implode( ',', array_fill( 0, count( $class_ids ), '%s' ) ); | |
| 191 | - | |
| 192 | - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 193 | - $sql = $wpdb->prepare( | |
| 194 | - "SELECT pm.post_id, pm.meta_value | |
| 195 | - FROM {$wpdb->postmeta} pm | |
| 196 | - INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id | |
| 197 | - WHERE pm.meta_key = %s | |
| 198 | - AND pm.meta_value IN ($placeholders) | |
| 199 | - AND p.post_type = %s | |
| 200 | - AND p.post_status = %s", | |
| 201 | - array_merge( | |
| 202 | - [ Global_Class_Post::META_KEY_ID ], | |
| 203 | - $class_ids, | |
| 204 | - [ Global_Class_Post_Type::CPT, 'publish' ] | |
| 205 | - ) | |
| 206 | - ); | |
| 207 | - // phpcs:enable | |
| 208 | - | |
| 209 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $sql is already prepared via $wpdb->prepare() above. | |
| 210 | - $rows = $wpdb->get_results( $sql ); | |
| 211 | - | |
| 212 | - $candidates = []; | |
| 213 | - | |
| 214 | - foreach ( $rows as $row ) { | |
| 215 | - $candidates[ (string) $row->meta_value ][] = (int) $row->post_id; | |
| 216 | - } | |
| 217 | - | |
| 218 | - $resolved = []; | |
| 219 | - | |
| 220 | - foreach ( $candidates as $class_id => $post_ids ) { | |
| 221 | - sort( $post_ids ); | |
| 222 | - $resolved[ $class_id ] = $post_ids[0]; | |
| 223 | - $duplicates = array_slice( $post_ids, 1 ); | |
| 224 | - | |
| 225 | - foreach ( $duplicates as $duplicate_id ) { | |
| 226 | - wp_delete_post( $duplicate_id, true ); | |
| 227 | - } | |
| 228 | - } | |
| 229 | - | |
| 230 | - return $resolved; | |
| 231 | 138 | } |
| 232 | 139 | |
| 233 | 140 | private function read_map(): array { |
| 234 | 141 | if ( null !== $this->cache ) { |