PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.2.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.2.3
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.2.3, at includes/Builder/Types/BaseTemplate.php

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