PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta2
Elementor Website Builder – more than just a page builder v4.3.0-beta2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / default-styles / default-style-post.php

default-style-post.php in Elementor Website Builder – more than just a page builder 4.3.0-beta2, at modules/default-styles/default-style-post.php

180 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\DefaultStyles;
4
5 use Elementor\Core\Kits\Documents\Kit;
6 use Elementor\Modules\AtomicWidgets\PropTypeMigrations\Migrations_Orchestrator;
7 use Elementor\Modules\DefaultStyles\Utils\Default_Style_Data_Normalizer;
8 use Elementor\Plugin;
9 use WP_Post;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 class Default_Style_Post {
16 const META_KEY_VERSION = '_elementor_version';
17 const META_KEY_TAG = '_elementor_default_style_tag';
18 const META_KEY_DATA = '_elementor_default_style_data';
19
20 private WP_Post $post;
21
22 private function __construct( WP_Post $post ) {
23 $this->post = $post;
24 }
25
26 public static function from_post( WP_Post $post ): self {
27 return new static( $post );
28 }
29
30 public static function from_post_id( int $post_id ): ?self {
31 $post = get_post( $post_id );
32
33 if ( ! $post || Default_Style_Post_Type::CPT !== $post->post_type ) {
34 return null;
35 }
36
37 return new static( $post );
38 }
39
40 public static function find_by_tag( string $tag, ?Kit $kit = null ): ?self {
41 $kit = $kit ?? Plugin::$instance->kits_manager->get_active_kit();
42
43 if ( ! $kit ) {
44 return null;
45 }
46
47 $post_id = Default_Styles_Tag_Post_IDs::make( $kit )->get_post_id( $tag );
48
49 if ( ! $post_id ) {
50 return null;
51 }
52
53 return self::from_post_id( $post_id );
54 }
55
56 public function get_post_id(): int {
57 return $this->post->ID;
58 }
59
60 public function get_tag(): string {
61 $tag = get_post_meta( $this->post->ID, self::META_KEY_TAG, true );
62
63 return is_string( $tag ) ? $tag : '';
64 }
65
66 public function get_data( bool $skip_migration = false ): array {
67 $data = get_post_meta( $this->post->ID, self::META_KEY_DATA, true );
68 $data = is_array( $data ) ? $data : [];
69
70 if ( ! empty( $data ) && ! $skip_migration ) {
71 $this->migrate_data( $data );
72 }
73
74 return $data;
75 }
76
77 private function migrate_data( array &$data ): void {
78 $post_id = $this->post->ID;
79 $meta_key = self::META_KEY_DATA;
80
81 Migrations_Orchestrator::make()->migrate(
82 $data,
83 $post_id,
84 $meta_key,
85 function ( $migrated ) use ( $post_id, $meta_key ) {
86 update_post_meta( $post_id, $meta_key, $migrated );
87 clean_post_cache( $post_id );
88 }
89 );
90 }
91
92 public function to_array( bool $skip_migration = false ): array {
93 $data = $this->get_data( $skip_migration );
94 $tag = $this->get_tag();
95
96 return Default_Style_Data_Normalizer::normalize_style( $tag, $data );
97 }
98
99 public function update_data( array $data, string $version = ELEMENTOR_VERSION ): bool {
100 $normalized_data = Default_Style_Data_Normalizer::normalize_style_fields( $data );
101 $existing_data = get_post_meta( $this->post->ID, self::META_KEY_DATA, true );
102 $existing_data = is_array( $existing_data ) ? $existing_data : [];
103
104 if ( $existing_data === $normalized_data ) {
105 update_post_meta( $this->post->ID, self::META_KEY_VERSION, $version );
106
107 return true;
108 }
109
110 $result = update_post_meta( $this->post->ID, self::META_KEY_DATA, $normalized_data );
111
112 update_post_meta( $this->post->ID, self::META_KEY_VERSION, $version );
113
114 return false !== $result;
115 }
116
117 public static function create( string $tag, array $data, ?Kit $kit = null, string $version = ELEMENTOR_VERSION ): ?self {
118 $post_id = wp_insert_post( [
119 'post_type' => Default_Style_Post_Type::CPT,
120 'post_title' => $tag,
121 'post_status' => 'publish',
122 ] );
123
124 if ( is_wp_error( $post_id ) || ! $post_id ) {
125 return null;
126 }
127
128 $normalized_data = Default_Style_Data_Normalizer::normalize_style_fields( $data );
129
130 update_post_meta( $post_id, self::META_KEY_TAG, $tag );
131 update_post_meta( $post_id, self::META_KEY_DATA, $normalized_data );
132 update_post_meta( $post_id, self::META_KEY_VERSION, $version );
133
134 $kit = $kit ?? Plugin::$instance->kits_manager->get_active_kit();
135
136 if ( $kit ) {
137 Default_Styles_Tag_Post_IDs::make( $kit )->set( $tag, (int) $post_id );
138 }
139
140 return self::from_post_id( $post_id );
141 }
142
143 public function delete(): bool {
144 $result = wp_delete_post( $this->post->ID, true );
145
146 return false !== $result;
147 }
148
149 public static function clone_to_other_kit( string $tag, Kit $source_kit, Kit $target_kit ): ?self {
150 $source_post = self::find_by_tag( $tag, $source_kit );
151
152 if ( ! $source_post ) {
153 return null;
154 }
155
156 $new_post_id = wp_insert_post( [
157 'post_type' => Default_Style_Post_Type::CPT,
158 'post_title' => $tag,
159 'post_status' => 'publish',
160 ] );
161
162 if ( is_wp_error( $new_post_id ) || ! $new_post_id ) {
163 return null;
164 }
165
166 update_post_meta( $new_post_id, self::META_KEY_TAG, $tag );
167 update_post_meta( $new_post_id, self::META_KEY_VERSION, get_post_meta( $source_post->get_post_id(), self::META_KEY_VERSION, true ) );
168
169 $source_data = $source_post->get_data();
170
171 if ( ! empty( $source_data ) ) {
172 update_post_meta( $new_post_id, self::META_KEY_DATA, $source_data );
173 }
174
175 Default_Styles_Tag_Post_IDs::make( $target_kit )->set( $tag, (int) $new_post_id );
176
177 return self::from_post_id( $new_post_id );
178 }
179 }
180