PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.0.8
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.0.8
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Builder / Types / BaseTemplate.php

BaseTemplate.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.0.8, at includes/Builder/Types/BaseTemplate.php

217 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Builder\Types;
4
5 use Elementor\Core\Base\Document;
6 use Elementor\Plugin;
7 use Exception;
8 use Templately\Builder\Source;
9 use WP_Error;
10 use WP_Post;
11
12 abstract class BaseTemplate {
13 const TYPE_META_KEY = '_templately_template_type';
14 const PLATFORM_META_KEY = '_templately_template_platform';
15 public $platform;
16
17 protected $post;
18
19 private $platform_data;
20 /**
21 * @var int
22 */
23 private $main_id;
24 /**
25 * @var bool
26 */
27 private $is_published = false;
28
29 abstract static public function get_type(): string;
30
31 abstract static public function get_title(): string;
32
33 abstract static public function get_plural_title(): string;
34
35 /**
36 * @throws Exception
37 */
38 public function __construct( $data ) {
39 if ( $data ) {
40 if ( empty( $data['post_id'] ) ) {
41 $this->post = new WP_Post( (object) [] );
42 } else {
43 $this->post = get_post( $data['post_id'] );
44
45 if ( ! $this->post ) {
46 throw new Exception( sprintf( 'Post ID #%s does not exist.', $data['post_id'] ) );
47 }
48 }
49 }
50
51 if ( $this->post ) {
52 $platform = $this->get_platform();
53
54 if ( $platform === 'elementor' && class_exists('Elementor\Plugin') ) {
55 // \Essential_Addons_Elementor\Classes\Bootstrap::instance();
56 // if ( Plugin::$instance->documents == null ) {
57 // Plugin::instance()->init();
58 // }
59
60 $this->platform_data = Plugin::$instance->documents->get( $this->post->ID );
61 } else {
62 $this->platform_data = &$this;
63 }
64 }
65 }
66
67 public static function get_properties(): array {
68 return [];
69 }
70
71 final public static function get_property( $key ) {
72 $properties = static::get_properties();
73
74 return $properties[ $key ] ?? null;
75 }
76
77 public function __get( $name ) {
78 if ( $name == 'page_template' ) {
79 return $this->post->page_template;
80 }
81 if ( $name == 'platform_data' ) {
82 return $this->platform_data;
83 }
84
85 return null;
86 }
87
88 final public function get_platform() {
89 if ( ! $this->platform ) {
90 if ( $this->post->post_type == Source::CPT ) {
91 $this->platform = get_post_meta( $this->post->ID, self::PLATFORM_META_KEY, true );
92 } elseif ( $this->get_meta( '_elementor_edit_mode' ) == 'builder' || $this->post->post_type == 'elementor_library' ) {
93 $this->platform = 'elementor';
94 } else {
95 $this->platform = 'gutenberg';
96 }
97 }
98
99 return $this->platform;
100 }
101
102 /**
103 * Is it an elementor template?
104 *
105 * @return bool
106 */
107 final public function is_elementor_template(): bool {
108 return $this->get_platform() === 'elementor';
109 }
110
111 final public function get_meta( $key ) {
112 return get_post_meta( $this->post->ID, $key, true );
113 }
114
115 final public function update_meta( $key, $value ) {
116 return update_metadata( 'post', $this->post->ID, $key, $value );
117 }
118
119 final public function delete_meta( $key, $value = '' ): bool {
120 return delete_metadata( 'post', $this->post->ID, $key, $value );
121 }
122
123 final public function is_published(): bool {
124 if ( ! $this->is_published ) {
125 $this->is_published = $this->post->post_status === 'publish';
126 }
127
128 return $this->is_published;
129 }
130
131 public function get_main_id(): int {
132 if ( ! $this->main_id ) {
133 $post_id = $this->post->ID;
134
135 $parent_post_id = wp_is_post_revision( $post_id );
136
137 if ( $parent_post_id ) {
138 $post_id = $parent_post_id;
139 }
140
141 $this->main_id = $post_id;
142 }
143
144 return $this->main_id;
145 }
146
147 public function print_content() {
148 echo $this->get_content( true );
149 }
150
151 public function get_content( $with_css = false ): string {
152 if ( $this->is_elementor_template() && class_exists('Elementor\Plugin') ) {
153 /**
154 * @var Document $document ;
155 */
156 $document = &$this->platform_data;
157
158 return $document->get_content( $with_css );
159 }
160 $post = get_post( $this->get_main_id() );
161
162 return $post->post_content;
163 }
164
165 public function import( array $data ) {
166 if ( $this->is_elementor_template() ) {
167 /**
168 * @var Document $document
169 */
170 $document = &$this->platform_data;
171 $document->import( $data );
172 $document->set_is_built_with_elementor( true );
173 } else {
174 $this->update_post( [
175 'post_content' => wp_slash( $data['content'] )
176 ] );
177 }
178
179 if ( $this->get_property( 'condition' ) ) {
180 $this->update_conditions( [ $this->get_property( 'condition' ) ] );
181 }
182 }
183
184 protected function update_conditions( array $conditions = [] ) {
185 if ( empty( $conditions ) || ! is_array( $conditions ) ) {
186 return;
187 }
188
189 templately()->theme_builder::$conditions_manager->save_conditions( $this->get_main_id(), $conditions );
190 }
191
192 public function get_edit_url() {
193 if ( $this->is_elementor_template() ) {
194 return $this->platform_data->get_edit_url();
195 }
196
197 $link = get_edit_post_link( $this->get_main_id(), 'link' );
198
199 if ( ! $link ) {
200 return new WP_Error( 'went_wrong', __( 'Something went wrong.' ) );
201 }
202
203 return $link;
204 }
205
206 public function update_post( $data ) {
207 if ( ! isset( $data['ID'] ) ) {
208 $data['ID'] = $this->post->ID;
209 }
210 $updated = wp_update_post( $data );
211 if ( is_wp_error( $updated ) ) {
212 return false;
213 }
214
215 return $updated;
216 }
217 }