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

244 lines 5.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 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 if(!did_action( 'elementor/init' )){
61 Plugin::instance()->init();
62 }
63 $this->platform_data = Plugin::$instance->documents->get( $this->post->ID );
64 } else {
65 $this->platform_data = &$this;
66 }
67 }
68 }
69
70 public static function get_properties(): array {
71 return [];
72 }
73
74 final public static function get_property( $key, $import_settings = [] ) {
75 $properties = static::get_properties($import_settings);
76
77 return $properties[ $key ] ?? null;
78 }
79
80 public function __get( $name ) {
81 if ( $name == 'page_template' ) {
82 return $this->post->page_template;
83 }
84 if ( $name == 'platform_data' ) {
85 return $this->platform_data;
86 }
87
88 return null;
89 }
90
91 final public function get_platform() {
92 if ( ! $this->platform ) {
93 if ( $this->post->post_type == Source::CPT ) {
94 $this->platform = get_post_meta( $this->post->ID, self::PLATFORM_META_KEY, true );
95 } elseif ( $this->get_meta( '_elementor_edit_mode' ) == 'builder' || $this->post->post_type == 'elementor_library' ) {
96 $this->platform = 'elementor';
97 } else {
98 $this->platform = 'gutenberg';
99 }
100 }
101
102 return $this->platform;
103 }
104
105 /**
106 * Is it an elementor template?
107 *
108 * @return bool
109 */
110 final public function is_elementor_template(): bool {
111 return $this->get_platform() === 'elementor';
112 }
113
114 final public function get_meta( $key ) {
115 return get_post_meta( $this->post->ID, $key, true );
116 }
117
118 final public function update_meta( $key, $value ) {
119 return update_metadata( 'post', $this->post->ID, $key, $value );
120 }
121
122 final public function delete_meta( $key, $value = '' ): bool {
123 return delete_metadata( 'post', $this->post->ID, $key, $value );
124 }
125
126 final public function is_published(): bool {
127 if ( ! $this->is_published ) {
128 $this->is_published = $this->post->post_status === 'publish';
129 }
130
131 return $this->is_published;
132 }
133
134 public function get_main_id(): int {
135 if ( ! $this->main_id ) {
136 $post_id = $this->post->ID;
137
138 $parent_post_id = wp_is_post_revision( $post_id );
139
140 if ( $parent_post_id ) {
141 $post_id = $parent_post_id;
142 }
143
144 $this->main_id = $post_id;
145 }
146
147 return $this->main_id;
148 }
149
150 public function print_content() {
151 echo $this->get_content( true );
152 }
153
154 public function get_content( $with_css = false ): string {
155 if ( $this->is_elementor_template() && class_exists('Elementor\Plugin') ) {
156 /**
157 * @var Document $document ;
158 */
159 $document = &$this->platform_data;
160
161 return $document->get_content( $with_css );
162 }
163 $post = get_post( $this->get_main_id() );
164 $content = $post->post_content;
165 return apply_filters( 'templately_template_content', $content, $this->get_main_id() );
166 }
167
168 public function import( array $data ) {
169 if ( $this->is_elementor_template() ) {
170 if ( isset( $data['import_settings']['conditions'] ) ) {
171 unset( $data['import_settings']['conditions'] );
172 }
173
174 /**
175 * @var Document $document
176 */
177 $document = &$this->platform_data;
178 $document->import( $data );
179 $document->set_is_built_with_elementor( true );
180 } else {
181 $this->update_post( [
182 'post_content' => wp_slash( $data['content'] )
183 ] );
184 }
185
186 if ( $this->get_property( 'condition', $data["import_settings"] ) ) {
187 $this->update_conditions( [ $this->get_property( 'condition', $data["import_settings"] ) ] );
188 }
189 }
190
191 public function has_logo($template_content){
192 if ( $this->is_elementor_template() ) {
193 $usage = get_post_meta($this->get_main_id(), '_elementor_controls_usage', true);
194 if(is_array($usage) && array_key_exists('tl-site-logo', $usage)){
195 return true;
196 }
197 }
198 else{
199 if(!empty($template_content["content"])){
200 $blocks = parse_blocks( $template_content["content"] );
201 $block = Helper::get_block_by_name($blocks, 'essential-blocks/advanced-image');
202 if(!empty($block['attrs']['imgSource']) && 'site-logo' === $block['attrs']['imgSource']){
203 return true;
204 }
205 }
206 }
207
208 return false;
209 }
210
211 protected function update_conditions( array $conditions = [] ) {
212 if ( empty( $conditions ) || ! is_array( $conditions ) ) {
213 return;
214 }
215
216 templately()->theme_builder::$conditions_manager->save_conditions( $this->get_main_id(), $conditions );
217 }
218
219 public function get_edit_url() {
220 if ( $this->is_elementor_template() && class_exists('Elementor\Plugin') ) {
221 return $this->platform_data->get_edit_url();
222 }
223
224 $link = get_edit_post_link( $this->get_main_id(), 'link' );
225
226 if ( ! $link ) {
227 return new WP_Error( 'went_wrong', __( 'Something went wrong.' ) );
228 }
229
230 return $link;
231 }
232
233 public function update_post( $data ) {
234 if ( ! isset( $data['ID'] ) ) {
235 $data['ID'] = $this->post->ID;
236 }
237 $updated = wp_update_post( $data );
238 if ( is_wp_error( $updated ) ) {
239 return false;
240 }
241
242 return $updated;
243 }
244 }