← All changes
|
modules/global-classes/global-class-post.php
+20
-80
4.3.0-beta3
→
4.1.0-dev3
View file →
| @@ -3,10 +3,8 @@ | ||
| 3 | 3 | namespace Elementor\Modules\GlobalClasses; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Modules\AtomicWidgets\PropTypeMigrations\Migrations_Orchestrator; |
| 6 | 6 | use Elementor\Modules\GlobalClasses\Concerns\Has_Preview_Context; |
| 7 | -use Elementor\Plugin; | |
| 8 | -use Elementor\Core\Kits\Documents\Kit; | |
| 9 | 7 | use Elementor\Modules\GlobalClasses\Utils\Global_Class_Data_Normalizer; |
| 10 | 8 | use WP_Post; |
| 11 | 9 | |
| 12 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -48,22 +46,22 @@ | ||
| 48 | 46 | |
| 49 | 47 | return ( new static( $post ) )->set_preview( $is_preview ); |
| 50 | 48 | } |
| 51 | 49 | |
| 52 | - public static function find_by_class_id( string $class_id, bool $is_preview = false, ?Kit $kit = null ): ?self { | |
| 53 | - $kit = $kit ?? Plugin::$instance->kits_manager->get_active_kit(); | |
| 50 | + public static function find_by_class_id( string $class_id, bool $is_preview = false ): ?self { | |
| 51 | + $posts = get_posts( [ | |
| 52 | + 'post_type' => Global_Class_Post_Type::CPT, | |
| 53 | + 'post_status' => 'publish', | |
| 54 | + 'posts_per_page' => 1, | |
| 55 | + 'meta_key' => self::META_KEY_ID, | |
| 56 | + 'meta_value' => $class_id, | |
| 57 | + ] ); | |
| 54 | 58 | |
| 55 | - if ( ! $kit ) { | |
| 59 | + if ( empty( $posts ) ) { | |
| 56 | 60 | return null; |
| 57 | 61 | } |
| 58 | 62 | |
| 59 | - $post_id = Global_Classes_Post_IDs::make( $kit )->get_post_id( $class_id ); | |
| 60 | - | |
| 61 | - if ( ! $post_id ) { | |
| 62 | - return null; | |
| 63 | - } | |
| 64 | - | |
| 65 | - return self::from_post_id( $post_id, $is_preview ); | |
| 63 | + return ( new self( $posts[0] ) )->set_preview( $is_preview ); | |
| 66 | 64 | } |
| 67 | 65 | |
| 68 | 66 | public function get_post_id(): int { |
| 69 | 67 | return $this->post->ID; |
| @@ -82,8 +80,12 @@ | ||
| 82 | 80 | public function get_label(): string { |
| 83 | 81 | return $this->post->post_title; |
| 84 | 82 | } |
| 85 | 83 | |
| 84 | + public function get_order(): int { | |
| 85 | + return (int) $this->post->menu_order; | |
| 86 | + } | |
| 87 | + | |
| 86 | 88 | public function get_data( bool $skip_migration = false ): array { |
| 87 | 89 | $data = $this->get_context_data(); |
| 88 | 90 | $meta_key = $this->get_context_key( 'data' ); |
| 89 | 91 | |
| @@ -100,8 +102,12 @@ | ||
| 100 | 102 | return $data; |
| 101 | 103 | } |
| 102 | 104 | |
| 103 | 105 | private function migrate_data( array &$data, string $meta_key ): void { |
| 106 | + if ( ! Migrations_Orchestrator::is_active() ) { | |
| 107 | + return; | |
| 108 | + } | |
| 109 | + | |
| 104 | 110 | $post_id = $this->post->ID; |
| 105 | 111 | |
| 106 | 112 | Migrations_Orchestrator::make()->migrate( |
| 107 | 113 | $data, |
| @@ -192,20 +198,8 @@ | ||
| 192 | 198 | |
| 193 | 199 | return is_array( $data ) ? $data : []; |
| 194 | 200 | } |
| 195 | 201 | |
| 196 | - private function get_preview_data(): array { | |
| 197 | - $data = get_post_meta( $this->post->ID, self::META_KEY_DATA_PREVIEW, true ); | |
| 198 | - | |
| 199 | - return is_array( $data ) ? $data : []; | |
| 200 | - } | |
| 201 | - | |
| 202 | - private function get_version(): string { | |
| 203 | - $version = get_post_meta( $this->post->ID, self::META_KEY_VERSION, true ); | |
| 204 | - | |
| 205 | - return is_string( $version ) ? $version : ''; | |
| 206 | - } | |
| 207 | - | |
| 208 | 202 | public function update_data( |
| 209 | 203 | array $data, |
| 210 | 204 | string $version = ELEMENTOR_VERSION |
| 211 | 205 | ): bool { |
| @@ -229,9 +223,9 @@ | ||
| 229 | 223 | public static function create( |
| 230 | 224 | string $class_id, |
| 231 | 225 | string $label, |
| 232 | 226 | array $data, |
| 233 | - ?Kit $kit = null, | |
| 227 | + int $order = 0, | |
| 234 | 228 | string $version = ELEMENTOR_VERSION |
| 235 | 229 | ): ?self { |
| 236 | 230 | $post_id = wp_insert_post( [ |
| 237 | 231 | 'post_type' => Global_Class_Post_Type::CPT, |
| @@ -236,8 +230,9 @@ | ||
| 236 | 230 | $post_id = wp_insert_post( [ |
| 237 | 231 | 'post_type' => Global_Class_Post_Type::CPT, |
| 238 | 232 | 'post_title' => $label, |
| 239 | 233 | 'post_status' => 'publish', |
| 234 | + 'menu_order' => $order, | |
| 240 | 235 | ] ); |
| 241 | 236 | |
| 242 | 237 | if ( is_wp_error( $post_id ) || ! $post_id ) { |
| 243 | 238 | return null; |
| @@ -248,14 +243,8 @@ | ||
| 248 | 243 | update_post_meta( $post_id, self::META_KEY_ID, $class_id ); |
| 249 | 244 | update_post_meta( $post_id, self::META_KEY_DATA, $normalized_data ); |
| 250 | 245 | update_post_meta( $post_id, self::META_KEY_VERSION, $version ); |
| 251 | 246 | |
| 252 | - $kit = $kit ?? Plugin::$instance->kits_manager->get_active_kit(); | |
| 253 | - | |
| 254 | - if ( $kit ) { | |
| 255 | - Global_Classes_Post_IDs::make( $kit )->set( $class_id, (int) $post_id ); | |
| 256 | - } | |
| 257 | - | |
| 258 | 247 | $instance = self::from_post_id( $post_id ); |
| 259 | 248 | |
| 260 | 249 | if ( $instance ) { |
| 261 | 250 | update_post_meta( $post_id, self::META_KEY_EDITED, $instance->get_creation_timestamp() ); |
| @@ -267,55 +256,6 @@ | ||
| 267 | 256 | public function delete(): bool { |
| 268 | 257 | $result = wp_delete_post( $this->post->ID, true ); |
| 269 | 258 | |
| 270 | 259 | return false !== $result; |
| 271 | - } | |
| 272 | - | |
| 273 | - public function clone( Kit $target_kit ): ?self { | |
| 274 | - return self::clone_to_kit( $this->get_class_id(), $this, $target_kit ); | |
| 275 | - } | |
| 276 | - | |
| 277 | - public static function clone_to_other_kit( string $style_id, Kit $source_kit, Kit $target_kit ): ?self { | |
| 278 | - $source_post = self::find_by_class_id( $style_id, false, $source_kit ); | |
| 279 | - | |
| 280 | - if ( ! $source_post ) { | |
| 281 | - return null; | |
| 282 | - } | |
| 283 | - | |
| 284 | - return self::clone_to_kit( $style_id, $source_post, $target_kit ); | |
| 285 | - } | |
| 286 | - | |
| 287 | - private static function clone_to_kit( string $style_id, self $source_post, Kit $target_kit ): ?self { | |
| 288 | - $new_post_id = wp_insert_post( [ | |
| 289 | - 'post_type' => Global_Class_Post_Type::CPT, | |
| 290 | - 'post_title' => $source_post->get_label(), | |
| 291 | - 'post_status' => 'publish', | |
| 292 | - ] ); | |
| 293 | - | |
| 294 | - if ( is_wp_error( $new_post_id ) || ! $new_post_id ) { | |
| 295 | - return null; | |
| 296 | - } | |
| 297 | - | |
| 298 | - update_post_meta( $new_post_id, self::META_KEY_ID, $style_id ); | |
| 299 | - update_post_meta( $new_post_id, self::META_KEY_VERSION, $source_post->get_version() ); | |
| 300 | - | |
| 301 | - $frontend_data = $source_post->get_frontend_data(); | |
| 302 | - $preview_data = $source_post->get_preview_data(); | |
| 303 | - $last_edited_timestamp = get_post_meta( $source_post->get_post_id(), self::META_KEY_EDITED, true ); | |
| 304 | - | |
| 305 | - if ( ! empty( $frontend_data ) ) { | |
| 306 | - update_post_meta( $new_post_id, self::META_KEY_DATA, $frontend_data ); | |
| 307 | - } | |
| 308 | - | |
| 309 | - if ( ! empty( $preview_data ) ) { | |
| 310 | - update_post_meta( $new_post_id, self::META_KEY_DATA_PREVIEW, $preview_data ); | |
| 311 | - } | |
| 312 | - | |
| 313 | - if ( $last_edited_timestamp ) { | |
| 314 | - update_post_meta( $new_post_id, self::META_KEY_EDITED, $last_edited_timestamp ); | |
| 315 | - } | |
| 316 | - | |
| 317 | - Global_Classes_Post_IDs::make( $target_kit )->set( $style_id, (int) $new_post_id ); | |
| 318 | - | |
| 319 | - return self::from_post_id( $new_post_id ); | |
| 320 | 260 | } |
| 321 | 261 | } |