← All changes
|
modules/global-classes/global-class-post.php
+29
-87
4.1.4
→
4.1.0-beta1
View file →
| @@ -3,11 +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 | -use Elementor\Modules\GlobalClasses\Utils\Global_Class_Data_Normalizer; | |
| 10 | 7 | use WP_Post; |
| 11 | 8 | |
| 12 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | 10 | exit; // Exit if accessed directly. |
| @@ -19,9 +16,8 @@ | ||
| 19 | 16 | const META_KEY_VERSION = '_elementor_version'; |
| 20 | 17 | const META_KEY_ID = '_elementor_global_class_id'; |
| 21 | 18 | const META_KEY_DATA = '_elementor_global_class_data'; |
| 22 | 19 | const META_KEY_DATA_PREVIEW = '_elementor_global_class_data_preview'; |
| 23 | - const META_KEY_EDITED = '_elementor_global_class_edited'; | |
| 24 | 20 | |
| 25 | 21 | protected array $context_keys = [ |
| 26 | 22 | 'data' => [ |
| 27 | 23 | 'frontend' => self::META_KEY_DATA, |
| @@ -35,9 +31,9 @@ | ||
| 35 | 31 | $this->post = $post; |
| 36 | 32 | } |
| 37 | 33 | |
| 38 | 34 | public static function from_post( WP_Post $post, bool $is_preview = false ): self { |
| 39 | - return ( new static( $post ) )->set_preview( $is_preview ); | |
| 35 | + return ( new self( $post ) )->set_preview( $is_preview ); | |
| 40 | 36 | } |
| 41 | 37 | |
| 42 | 38 | public static function from_post_id( int $post_id, bool $is_preview = false ): ?self { |
| 43 | 39 | $post = get_post( $post_id ); |
| @@ -45,25 +41,25 @@ | ||
| 45 | 41 | if ( ! $post || Global_Class_Post_Type::CPT !== $post->post_type ) { |
| 46 | 42 | return null; |
| 47 | 43 | } |
| 48 | 44 | |
| 49 | - return ( new static( $post ) )->set_preview( $is_preview ); | |
| 45 | + return ( new self( $post ) )->set_preview( $is_preview ); | |
| 50 | 46 | } |
| 51 | 47 | |
| 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(); | |
| 48 | + public static function find_by_class_id( string $class_id, bool $is_preview = false ): ?self { | |
| 49 | + $posts = get_posts( [ | |
| 50 | + 'post_type' => Global_Class_Post_Type::CPT, | |
| 51 | + 'post_status' => 'publish', | |
| 52 | + 'posts_per_page' => 1, | |
| 53 | + 'meta_key' => self::META_KEY_ID, | |
| 54 | + 'meta_value' => $class_id, | |
| 55 | + ] ); | |
| 54 | 56 | |
| 55 | - if ( ! $kit ) { | |
| 57 | + if ( empty( $posts ) ) { | |
| 56 | 58 | return null; |
| 57 | 59 | } |
| 58 | 60 | |
| 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 ); | |
| 61 | + return ( new self( $posts[0] ) )->set_preview( $is_preview ); | |
| 66 | 62 | } |
| 67 | 63 | |
| 68 | 64 | public function get_post_id(): int { |
| 69 | 65 | return $this->post->ID; |
| @@ -82,8 +78,12 @@ | ||
| 82 | 78 | public function get_label(): string { |
| 83 | 79 | return $this->post->post_title; |
| 84 | 80 | } |
| 85 | 81 | |
| 82 | + public function get_order(): int { | |
| 83 | + return (int) $this->post->menu_order; | |
| 84 | + } | |
| 85 | + | |
| 86 | 86 | public function get_data( bool $skip_migration = false ): array { |
| 87 | 87 | $data = $this->get_context_data(); |
| 88 | 88 | $meta_key = $this->get_context_key( 'data' ); |
| 89 | 89 | |
| @@ -120,59 +120,22 @@ | ||
| 120 | 120 | |
| 121 | 121 | public function to_array( bool $skip_migration = false ): array { |
| 122 | 122 | $data = $this->get_data( $skip_migration ); |
| 123 | 123 | |
| 124 | - $class_id = $this->get_class_id(); | |
| 124 | + $result = [ | |
| 125 | + 'id' => $this->get_class_id(), | |
| 126 | + 'label' => $this->get_label(), | |
| 127 | + 'type' => $data['type'] ?? 'class', | |
| 128 | + 'variants' => $data['variants'] ?? [], | |
| 129 | + ]; | |
| 125 | 130 | |
| 126 | - return Global_Class_Data_Normalizer::normalize_style( | |
| 127 | - $class_id, | |
| 128 | - array_merge( $data, [ 'label' => $this->get_label() ] ) | |
| 129 | - ); | |
| 130 | - } | |
| 131 | - | |
| 132 | - public function was_edited(): bool { | |
| 133 | - $last_edited = $this->get_last_edited_timestamp(); | |
| 134 | - $creation = $this->get_creation_timestamp(); | |
| 135 | - | |
| 136 | - return $last_edited > $creation; | |
| 137 | - } | |
| 138 | - | |
| 139 | - public function has_edit_timestamp(): bool { | |
| 140 | - return $this->get_last_edited_timestamp() > 0; | |
| 141 | - } | |
| 142 | - | |
| 143 | - private function get_creation_timestamp(): int { | |
| 144 | - $dt = get_post_datetime( $this->post, 'date', 'gmt' ); | |
| 145 | - | |
| 146 | - if ( $dt ) { | |
| 147 | - return (int) $dt->format( 'U' ); | |
| 131 | + if ( array_key_exists( 'sync_to_v3', $data ) ) { | |
| 132 | + $result['sync_to_v3'] = (bool) $data['sync_to_v3']; | |
| 148 | 133 | } |
| 149 | 134 | |
| 150 | - return (int) strtotime( (string) $this->post->post_date ); | |
| 135 | + return $result; | |
| 151 | 136 | } |
| 152 | 137 | |
| 153 | - private function get_last_edited_timestamp(): int { | |
| 154 | - $raw = get_post_meta( $this->post->ID, self::META_KEY_EDITED, true ); | |
| 155 | - | |
| 156 | - if ( self::is_timestamp( $raw ) ) { | |
| 157 | - return (int) $raw; | |
| 158 | - } | |
| 159 | - | |
| 160 | - return 0; | |
| 161 | - } | |
| 162 | - | |
| 163 | - private static function is_timestamp( $value ): bool { | |
| 164 | - if ( ! is_numeric( $value ) ) { | |
| 165 | - return false; | |
| 166 | - } | |
| 167 | - | |
| 168 | - return (int) $value > 0; | |
| 169 | - } | |
| 170 | - | |
| 171 | - protected function get_current_timestamp(): int { | |
| 172 | - return (int) time(); | |
| 173 | - } | |
| 174 | - | |
| 175 | 138 | public function update_label( string $label ): bool { |
| 176 | 139 | $result = wp_update_post( [ |
| 177 | 140 | 'ID' => $this->post->ID, |
| 178 | 141 | 'post_title' => $label, |
| @@ -177,12 +140,8 @@ | ||
| 177 | 140 | 'ID' => $this->post->ID, |
| 178 | 141 | 'post_title' => $label, |
| 179 | 142 | ] ); |
| 180 | 143 | |
| 181 | - if ( ! is_wp_error( $result ) && ! $this->is_preview() ) { | |
| 182 | - update_post_meta( $this->post->ID, self::META_KEY_EDITED, $this->get_current_timestamp() ); | |
| 183 | - } | |
| 184 | - | |
| 185 | 144 | return ! is_wp_error( $result ); |
| 186 | 145 | } |
| 187 | 146 | |
| 188 | 147 | private function get_context_data(): array { |
| @@ -210,12 +169,8 @@ | ||
| 210 | 169 | } |
| 211 | 170 | |
| 212 | 171 | update_post_meta( $this->post->ID, self::META_KEY_VERSION, $version ); |
| 213 | 172 | |
| 214 | - if ( ! $this->is_preview() ) { | |
| 215 | - update_post_meta( $this->post->ID, self::META_KEY_EDITED, $this->get_current_timestamp() ); | |
| 216 | - } | |
| 217 | - | |
| 218 | 173 | return false !== $result; |
| 219 | 174 | } |
| 220 | 175 | |
| 221 | 176 | public static function create( |
| @@ -221,9 +176,9 @@ | ||
| 221 | 176 | public static function create( |
| 222 | 177 | string $class_id, |
| 223 | 178 | string $label, |
| 224 | 179 | array $data, |
| 225 | - ?Kit $kit = null, | |
| 180 | + int $order = 0, | |
| 226 | 181 | string $version = ELEMENTOR_VERSION |
| 227 | 182 | ): ?self { |
| 228 | 183 | $post_id = wp_insert_post( [ |
| 229 | 184 | 'post_type' => Global_Class_Post_Type::CPT, |
| @@ -228,8 +183,9 @@ | ||
| 228 | 183 | $post_id = wp_insert_post( [ |
| 229 | 184 | 'post_type' => Global_Class_Post_Type::CPT, |
| 230 | 185 | 'post_title' => $label, |
| 231 | 186 | 'post_status' => 'publish', |
| 187 | + 'menu_order' => $order, | |
| 232 | 188 | ] ); |
| 233 | 189 | |
| 234 | 190 | if ( is_wp_error( $post_id ) || ! $post_id ) { |
| 235 | 191 | return null; |
| @@ -234,27 +190,13 @@ | ||
| 234 | 190 | if ( is_wp_error( $post_id ) || ! $post_id ) { |
| 235 | 191 | return null; |
| 236 | 192 | } |
| 237 | 193 | |
| 238 | - $normalized_data = Global_Class_Data_Normalizer::normalize_style_fields( $data ); | |
| 239 | - | |
| 240 | 194 | update_post_meta( $post_id, self::META_KEY_ID, $class_id ); |
| 241 | - update_post_meta( $post_id, self::META_KEY_DATA, $normalized_data ); | |
| 195 | + update_post_meta( $post_id, self::META_KEY_DATA, $data ); | |
| 242 | 196 | update_post_meta( $post_id, self::META_KEY_VERSION, $version ); |
| 243 | 197 | |
| 244 | - $kit = $kit ?? Plugin::$instance->kits_manager->get_active_kit(); | |
| 245 | - | |
| 246 | - if ( $kit ) { | |
| 247 | - Global_Classes_Post_IDs::make( $kit )->set( $class_id, (int) $post_id ); | |
| 248 | - } | |
| 249 | - | |
| 250 | - $instance = self::from_post_id( $post_id ); | |
| 251 | - | |
| 252 | - if ( $instance ) { | |
| 253 | - update_post_meta( $post_id, self::META_KEY_EDITED, $instance->get_creation_timestamp() ); | |
| 254 | - } | |
| 255 | - | |
| 256 | - return $instance; | |
| 198 | + return self::from_post_id( $post_id ); | |
| 257 | 199 | } |
| 258 | 200 | |
| 259 | 201 | public function delete(): bool { |
| 260 | 202 | $result = wp_delete_post( $this->post->ID, true ); |