PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 3.1.0
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v3.1.0
4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / core / builders / action.php
shopengine / core / builders Last commit date
action.php 3 years ago api.php 3 years ago base.php 3 years ago hooks.php 3 years ago templates.php 3 years ago
action.php
485 lines
1 <?php
2
3 namespace ShopEngine\Core\Builders;
4
5 use Elementor\Core\Files\Uploads_Manager;
6 use ShopEngine\Core\Sample_Designs\Base;
7 use ShopEngine\Core\Template_Cpt;
8 use ShopEngine\Traits\Singleton;
9
10 defined( 'ABSPATH' ) || exit;
11
12 class Action
13 {
14 use Singleton;
15
16 const PK__SHOPENGINE_TEMPLATE = 'shopengine_template__post_meta';
17 const EDIT_WITH_GUTENBERG = 'gutenberg';
18 const EDIT_WITH_ELEMENTOR = 'elementor';
19 const ACTIVATED_TEMPLATES = 'shopengine_activated_templates';
20
21 /**
22 * @var mixed
23 */
24 private static $form_settings;
25 /**
26 * @var string
27 */
28 private static $edit_with = '';
29 /**
30 * @var mixed
31 */
32 public $template_id;
33 /**
34 * @var mixed
35 */
36 public $category_id;
37 /**
38 * @var mixed
39 */
40 public $old_language_code;
41 /**
42 * @var mixed
43 */
44 public $template_type;
45 /**
46 * @var mixed
47 */
48 public $language_code;
49 /**
50 * @var mixed
51 */
52 public $template_status;
53
54 /**
55 * @param $template_id
56 * @param $form_settings
57 * @return mixed
58 */
59 public function store( $template_id, $form_settings )
60 {
61 $this->set_form_values( $form_settings );
62 $this->template_id = $template_id;
63 $this->template_type = self::get_form_value( 'form_type', 'single' );
64 $this->category_id = self::get_form_value( 'category_id', 0 );
65 $this->language_code = self::get_form_value( 'language_code', 'en' );
66 $this->old_language_code = self::get_form_value( 'old_language_code', 'en' );
67
68 if ( $this->template_id == 0 ) {
69 global $wp_rewrite;
70 $wp_rewrite->flush_rules();
71 $wp_rewrite->init();
72
73 return $this->insert();
74 } else {
75 return $this->update();
76 }
77 }
78
79 public function insert()
80 {
81 $form_settings = self::$form_settings;
82 $title = ( $form_settings['form_title'] != '' ) ? $form_settings['form_title'] : 'New Template # ' . time();
83
84 $template_id = wp_insert_post( [
85 'post_title' => $title,
86 'post_status' => 'publish',
87 'post_type' => Template_Cpt::TYPE
88 ] );
89
90 $this->template_id = $template_id;
91
92 $activated_templates = $this->create_template( self::get_activated_templates() );
93 self::set_activated_templates( $activated_templates );
94
95 /**
96 * Get template default meta
97 */
98 $edit_with = self::get_form_value( 'edit_with_option', self::EDIT_WITH_GUTENBERG );
99
100 /**
101 * Set template default meta
102 */
103 update_post_meta( $template_id, self::get_meta_key_for_type(), $this->template_type );
104 update_post_meta( $template_id, self::PK__SHOPENGINE_TEMPLATE, $form_settings );
105 update_post_meta( $template_id, self::get_meta_key_for_edit_with(), $edit_with );
106
107 /**
108 * If this template is dependent on elementor page builder then this meta will be saved
109 */
110
111 if ( $edit_with === self::EDIT_WITH_ELEMENTOR ) {
112 /**
113 * Auto elementor canvas style
114 */
115
116 if ( in_array( $this->template_type, ['quick_checkout', 'quick_view'] ) ) {
117 update_post_meta( $template_id, '_wp_page_template', 'elementor_canvas' );
118 } else {
119 update_post_meta( $template_id, '_wp_page_template', 'elementor_header_footer' );
120 }
121
122 update_post_meta( $template_id, '_elementor_edit_mode', 'builder' );
123 update_post_meta( $template_id, '_elementor_version', '3.4.6' );
124 }
125
126 if ( !empty( $form_settings['sample_design'] ) ) {
127 /**
128 * Get ready made template data
129 */
130 $template_path = Base::instance()->get_content_path( $form_settings['sample_design'] );
131
132 if ( is_file( $template_path ) ) {
133
134 $fileContent = file_get_contents( $template_path );
135
136 if ( !is_null( $fileContent ) ) {
137
138 add_filter('elementor/files/allow_unfiltered_upload', '__return_true');
139
140 $result = \Elementor\Plugin::$instance->templates_manager->import_template( [
141 'fileData' => base64_encode( $fileContent ),
142 'fileName' => 'shopengine-content.json'
143 ] );
144
145 $imported_template_id = $result[0]['template_id'];
146 $template_data = get_post_meta( $imported_template_id, '_elementor_data', true );
147 update_post_meta( $template_id, '_elementor_data', $template_data );
148 wp_delete_post( $imported_template_id );
149 }
150 }
151 }
152
153 return [
154 'saved' => true,
155 'data' => [
156 'id' => $template_id,
157 'title' => $title,
158 'type' => Template_Cpt::TYPE,
159 'activated_templates' => $activated_templates
160 ],
161 'status' => esc_html__( 'Template settings inserted', 'shopengine' )
162 ];
163 }
164
165 public function update()
166 {
167 $form_settings = self::$form_settings;
168
169 $title = ( $form_settings['form_title'] != '' ) ? $form_settings['form_title'] : 'New Template # ' . time();
170
171 wp_update_post( [
172 'ID' => $this->template_id,
173 'post_title' => $title,
174 'post_status' => 'publish'
175 ] );
176
177 update_post_meta( $this->template_id, self::PK__SHOPENGINE_TEMPLATE, $form_settings );
178 update_post_meta( $this->template_id, self::get_meta_key_for_type(), $this->template_type );
179
180 $activated_templates = $this->update_template( self::get_activated_templates() );
181 self::set_activated_templates( $activated_templates );
182
183 $response = [
184 'saved' => true,
185 'data' => [
186 'id' => $this->template_id,
187 'title' => $title,
188 'type' => Template_Cpt::TYPE,
189 'activated_templates' => $activated_templates
190 ],
191 'status' => esc_html__( 'Template settings updated', 'shopengine' )
192 ];
193
194 if ( isset( $form_settings['category_id'] ) ) {
195 $response['data']['category_title'] = get_the_category_by_ID( $form_settings['category_id'] );
196 }
197
198 return $response;
199 }
200
201 /**
202 * @param $templates
203 * @return mixed
204 */
205 public function create_template( $templates )
206 {
207 update_post_meta( $this->template_id, 'language_code', $this->language_code );
208
209 if ( isset( $templates[$this->template_type]['lang'][$this->language_code] ) ) {
210 $category_key = self::get_template_key( $templates[$this->template_type]['lang'][$this->language_code], 'category_id', $this->category_id );
211
212 if ( is_integer( $category_key ) ) {
213 $templates[$this->template_type]['lang'][$this->language_code][$category_key] = $this->template_values();
214 return $templates;
215 }
216 }
217
218 $templates[$this->template_type]['lang'][$this->language_code][] = $this->template_values();
219
220 return $templates;
221 }
222
223 /**
224 * @param $templates
225 * @return mixed
226 */
227 public function update_template( array $templates )
228 {
229
230 if ( isset( $templates[$this->template_type]['lang'][$this->old_language_code] ) ) {
231 $key = self::get_template_key( $templates[$this->template_type]['lang'][$this->old_language_code], 'template_id', $this->template_id );
232
233 if ( is_integer( $key ) ) {
234 unset( $templates[$this->template_type]['lang'][$this->old_language_code][$key] );
235 }
236 }
237
238 return $this->create_template( $templates );
239 }
240
241 public function template_values()
242 {
243 return [
244 'template_id' => $this->template_id,
245 'status' => self::get_form_value( 'status', false ),
246 'category_id' => $this->category_id
247 ];
248 }
249
250 /**
251 * @param $post_id
252 * @return mixed
253 */
254 public function get_all_data( $post_id )
255 {
256 $language_code = get_post_meta( $post_id, 'language_code', true );
257
258 if ( empty( $language_code ) ) {
259 $language_code = 'en';
260 }
261
262 $activated_templates = self::get_activated_templates();
263 $post = get_post( $post_id );
264 $data = get_post_meta( $post->ID, self::PK__SHOPENGINE_TEMPLATE, true );
265 $data['language_code'] = $language_code;
266 $data['edit_with_option'] = get_post_meta( $post->ID, self::get_meta_key_for_edit_with(), true );
267
268 if ( !empty( $activated_templates[$data['form_type']]['lang'][$language_code] ) ) {
269 $templates = $activated_templates[$data['form_type']]['lang'][$language_code];
270 $key = self::get_template_key( $templates, 'template_id', $post->ID );
271
272 if ( is_integer( $key ) ) {
273 $data['status'] = $templates[$key]['status'];
274 } else {
275 $data['status'] = false;
276 }
277 }
278
279 return $data;
280 }
281
282 /**
283 * @param $template_id
284 */
285 public static function delete_template_form_activated_list( $template_id )
286 {
287 $data = get_post_meta( $template_id, Action::PK__SHOPENGINE_TEMPLATE, true );
288
289 if ( is_array( $data ) && isset( $data['form_type'] ) ) {
290 $language_code = get_post_meta( $template_id, 'language_code', true );
291
292 $activated_templates = self::get_activated_templates();
293
294 if ( empty( $language_code ) ) {
295 $language_code = 'en';
296 }
297
298 if ( !empty( $activated_templates[$data['form_type']]['lang'][$language_code] ) ) {
299 $templates = $activated_templates[$data['form_type']]['lang'][$language_code];
300 $key = self::get_template_key( $templates, 'template_id', $template_id );
301
302 if ( is_integer( $key ) ) {
303 unset( $activated_templates[$data['form_type']]['lang'][$language_code][$key] );
304 }
305 }
306
307 self::set_activated_templates( $activated_templates );
308 }
309 }
310
311 public static function get_activated_templates()
312 {
313 $activated_templates = get_option( self::ACTIVATED_TEMPLATES );
314
315 if ( false === $activated_templates ) {
316 return [];
317 }
318
319 return unserialize( $activated_templates );
320 }
321
322 /**
323 * @param array $activated_templates
324 */
325 public static function set_activated_templates( array $activated_templates )
326 {
327 return update_option( Action::ACTIVATED_TEMPLATES, serialize( $activated_templates ) );
328 }
329
330 /**
331 * @param $template_id
332 * @param array $activated_templates
333 * @return mixed
334 */
335 public static function get_template_data( $template_id, array $activated_templates )
336 {
337 $language_code = get_post_meta( intval( $template_id ), 'language_code', true );
338 $data = get_post_meta( $template_id, Action::PK__SHOPENGINE_TEMPLATE, true );
339
340 if ( is_array( $data ) && isset( $data['form_type'] ) ) {
341 if ( isset( $activated_templates[$data['form_type']]['lang'][$language_code] ) ) {
342 $templates = $activated_templates[$data['form_type']]['lang'][$language_code];
343 $template_key = self::get_template_key( $templates, 'template_id', $template_id );
344
345 if ( is_integer( $template_key ) ) {
346 return $templates[$template_key];
347 }
348 }
349 }
350
351 return false;
352 }
353
354 /**
355 * @param $template_id
356 * @param array $activated_templates
357 * @return mixed
358 */
359 public static function is_template_active( $template_id, array $activated_templates )
360 {
361 $template_data = self::get_template_data( $template_id, $activated_templates );
362 return $template_data['status'] ?? false;
363 }
364
365 /**
366 * @param $template_id
367 * @param array $activated_templates
368 * @return mixed
369 */
370 public static function get_template_category_id( $template_id, array $activated_templates )
371 {
372 $template_data = self::get_template_data( $template_id, $activated_templates );
373 return $template_data['category_id'] ?? false;
374 }
375
376 /**
377 * @param $templates
378 * @param $type
379 * @param $category_id_or_template_id
380 */
381 public static function get_template_key( $templates, $type = 'category_id', $category_id_or_template_id = 0 )
382 {
383
384 foreach ( $templates as $key => $template ) {
385 if ( $template[$type] == $category_id_or_template_id ) {
386 return $key;
387 }
388 }
389
390 return false;
391 }
392
393 public static function get_meta_key_for_type()
394 {
395 return self::PK__SHOPENGINE_TEMPLATE . '__type';
396 }
397
398 public static function get_meta_key_for_edit_with()
399 {
400 return self::PK__SHOPENGINE_TEMPLATE . '__edit_with';
401 }
402
403 /**
404 * @param $template_id
405 */
406 public static function edit_with( $template_id )
407 {
408
409 if ( static::$edit_with ) {
410 return static::$edit_with;
411 }
412
413 $edit_with = get_post_meta( $template_id, Action::get_meta_key_for_edit_with(), true );
414 static::$edit_with = empty( $edit_with ) ? Action::EDIT_WITH_ELEMENTOR : $edit_with;
415
416 return static::$edit_with;
417 }
418
419 /**
420 * @param $pid
421 * @return mixed
422 */
423 public static function is_edit_with_gutenberg( $pid )
424 {
425 $edit_with = get_post_meta( $pid, Action::get_meta_key_for_edit_with(), true );
426 $edit_with = empty( $edit_with ) ? Action::EDIT_WITH_ELEMENTOR : $edit_with;
427
428 return $edit_with === self::EDIT_WITH_GUTENBERG;
429 }
430
431 /**
432 * @param $field
433 * @param $default
434 */
435 private static function get_form_value( $field, $default = '' )
436 {
437 return !empty( self::$form_settings[$field] ) ? self::$form_settings[$field] : $default;
438 }
439
440 /**
441 * @param $form_settings
442 * @param $fields
443 */
444 private function set_form_values( $form_settings, $fields = null )
445 {
446 $fields = $this->get_fields();
447
448 foreach ( $form_settings as $key => $value ) {
449 if ( isset( $fields[$key] ) ) {
450 self::$form_settings[$key] = $value;
451 }
452 }
453 }
454
455 public function get_fields()
456 {
457 return [
458 'form_title' => [
459 'name' => 'form_title'
460 ],
461 'form_type' => [
462 'name' => 'form_type'
463 ],
464 'status' => [
465 'name' => 'status'
466 ],
467 'edit_with_option' => [
468 'name' => 'edit_with_option'
469 ],
470 'sample_design' => [
471 'name' => 'sample_design'
472 ],
473 'category_id' => [
474 'name' => 'category_id'
475 ],
476 'language_code' => [
477 'name' => 'language_code'
478 ],
479 'old_language_code' => [
480 'name' => 'old_language_code'
481 ]
482 ];
483 }
484 }
485