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