PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 4.8.2
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v4.8.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 1 year ago api.php 4 years ago base.php 1 year ago hooks.php 3 years ago templates.php 3 years ago
action.php
506 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 $css_path = str_replace('content.html', 'style.txt', $template_path);
133
134 if ( is_file( $template_path ) ) {
135 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
136 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
137
138 $wp_filesystem = new \WP_Filesystem_Direct(null);
139 $fileContent = $wp_filesystem->get_contents($template_path);
140 $get_generated_css = '';
141
142 if( file_exists($css_path) ){
143 $get_generated_css = $wp_filesystem->get_contents($css_path);
144 }
145
146 if(!is_null( $fileContent ) && $edit_with === 'gutenberg'){
147 wp_update_post([
148 'ID' => $this->template_id,
149 'post_content' => $fileContent
150 ]);
151
152 update_post_meta( $this->template_id, 'gutenova_css', $get_generated_css );
153 }
154
155 if ( !is_null( $fileContent ) && $edit_with === 'elementor' ) {
156
157 add_filter('elementor/files/allow_unfiltered_upload', '__return_true');
158
159 $result = \Elementor\Plugin::$instance->templates_manager->import_template( [
160 'fileData' => base64_encode( $fileContent ),
161 'fileName' => 'shopengine-content.json'
162 ] );
163
164 $imported_template_id = $result[0]['template_id'];
165 $template_data = get_post_meta( $imported_template_id, '_elementor_data', true );
166 update_post_meta( $template_id, '_elementor_data', $template_data );
167 wp_delete_post( $imported_template_id );
168 }
169 }
170 }
171
172 return [
173 'saved' => true,
174 'data' => [
175 'id' => $template_id,
176 'title' => $title,
177 'type' => Template_Cpt::TYPE,
178 'activated_templates' => $activated_templates
179 ],
180 'status' => esc_html__( 'Template settings inserted', 'shopengine' )
181 ];
182 }
183
184 public function update()
185 {
186 $form_settings = self::$form_settings;
187
188 $title = ( $form_settings['form_title'] != '' ) ? $form_settings['form_title'] : 'New Template # ' . time();
189
190 wp_update_post( [
191 'ID' => $this->template_id,
192 'post_title' => $title,
193 'post_status' => 'publish'
194 ] );
195
196 update_post_meta( $this->template_id, self::PK__SHOPENGINE_TEMPLATE, $form_settings );
197 update_post_meta( $this->template_id, self::get_meta_key_for_type(), $this->template_type );
198
199 $activated_templates = $this->update_template( self::get_activated_templates() );
200 self::set_activated_templates( $activated_templates );
201
202 $response = [
203 'saved' => true,
204 'data' => [
205 'id' => $this->template_id,
206 'title' => $title,
207 'type' => Template_Cpt::TYPE,
208 'activated_templates' => $activated_templates
209 ],
210 'status' => esc_html__( 'Template settings updated', 'shopengine' )
211 ];
212
213 if ( isset( $form_settings['category_id'] ) ) {
214 $response['data']['category_title'] = get_the_category_by_ID( $form_settings['category_id'] );
215 }
216
217 return $response;
218 }
219
220 /**
221 * @param $templates
222 * @return mixed
223 */
224 public function create_template( $templates )
225 {
226 update_post_meta( $this->template_id, 'language_code', $this->language_code );
227
228 if ( isset( $templates[$this->template_type]['lang'][$this->language_code] ) ) {
229 $category_key = self::get_template_key( $templates[$this->template_type]['lang'][$this->language_code], 'category_id', $this->category_id );
230
231 if ( is_integer( $category_key ) ) {
232 $templates[$this->template_type]['lang'][$this->language_code][$category_key] = $this->template_values();
233 return $templates;
234 }
235 }
236
237 $templates[$this->template_type]['lang'][$this->language_code][] = $this->template_values();
238
239 return $templates;
240 }
241
242 /**
243 * @param $templates
244 * @return mixed
245 */
246 public function update_template( array $templates )
247 {
248
249 if ( isset( $templates[$this->template_type]['lang'][$this->old_language_code] ) ) {
250 $key = self::get_template_key( $templates[$this->template_type]['lang'][$this->old_language_code], 'template_id', $this->template_id );
251
252 if ( is_integer( $key ) ) {
253 unset( $templates[$this->template_type]['lang'][$this->old_language_code][$key] );
254 }
255 }
256
257 return $this->create_template( $templates );
258 }
259
260 public function template_values()
261 {
262 return [
263 'template_id' => $this->template_id,
264 'status' => self::get_form_value( 'status', false ),
265 'category_id' => $this->category_id
266 ];
267 }
268
269 /**
270 * @param $post_id
271 * @return mixed
272 */
273 public function get_all_data( $post_id )
274 {
275 $language_code = get_post_meta( $post_id, 'language_code', true );
276
277 if ( empty( $language_code ) ) {
278 $language_code = 'en';
279 }
280
281 $activated_templates = self::get_activated_templates();
282 $post = get_post( $post_id );
283 $data = get_post_meta( $post->ID, self::PK__SHOPENGINE_TEMPLATE, true );
284 $data['language_code'] = $language_code;
285 $data['edit_with_option'] = get_post_meta( $post->ID, self::get_meta_key_for_edit_with(), true );
286
287 if ( !empty( $activated_templates[$data['form_type']]['lang'][$language_code] ) ) {
288 $templates = $activated_templates[$data['form_type']]['lang'][$language_code];
289 $key = self::get_template_key( $templates, 'template_id', $post->ID );
290
291 if ( is_integer( $key ) ) {
292 $data['status'] = $templates[$key]['status'];
293 } else {
294 $data['status'] = false;
295 }
296 }
297
298 return $data;
299 }
300
301 /**
302 * @param $template_id
303 */
304 public static function delete_template_form_activated_list( $template_id )
305 {
306 $data = get_post_meta( $template_id, Action::PK__SHOPENGINE_TEMPLATE, true );
307
308 if ( is_array( $data ) && isset( $data['form_type'] ) ) {
309 $language_code = get_post_meta( $template_id, 'language_code', true );
310
311 $activated_templates = self::get_activated_templates();
312
313 if ( empty( $language_code ) ) {
314 $language_code = 'en';
315 }
316
317 if ( !empty( $activated_templates[$data['form_type']]['lang'][$language_code] ) ) {
318 $templates = $activated_templates[$data['form_type']]['lang'][$language_code];
319 $key = self::get_template_key( $templates, 'template_id', $template_id );
320
321 if ( is_integer( $key ) ) {
322 unset( $activated_templates[$data['form_type']]['lang'][$language_code][$key] );
323 }
324 }
325
326 self::set_activated_templates( $activated_templates );
327 }
328 }
329
330 public static function get_activated_templates()
331 {
332 $activated_templates = get_option( self::ACTIVATED_TEMPLATES );
333
334 if (false === $activated_templates) {
335 return [];
336 }
337
338 $activated_templates = maybe_unserialize($activated_templates);
339
340 return is_array($activated_templates) ? $activated_templates : [];
341 }
342
343 /**
344 * @param array $activated_templates
345 */
346 public static function set_activated_templates( array $activated_templates )
347 {
348 return update_option( Action::ACTIVATED_TEMPLATES, serialize( $activated_templates ) );
349 }
350
351 /**
352 * @param $template_id
353 * @param array $activated_templates
354 * @return mixed
355 */
356 public static function get_template_data( $template_id, array $activated_templates )
357 {
358 $language_code = get_post_meta( intval( $template_id ), 'language_code', true );
359 $data = get_post_meta( $template_id, Action::PK__SHOPENGINE_TEMPLATE, true );
360
361 if ( is_array( $data ) && isset( $data['form_type'] ) ) {
362 if ( isset( $activated_templates[$data['form_type']]['lang'][$language_code] ) ) {
363 $templates = $activated_templates[$data['form_type']]['lang'][$language_code];
364 $template_key = self::get_template_key( $templates, 'template_id', $template_id );
365
366 if ( is_integer( $template_key ) ) {
367 return $templates[$template_key];
368 }
369 }
370 }
371
372 return false;
373 }
374
375 /**
376 * @param $template_id
377 * @param array $activated_templates
378 * @return mixed
379 */
380 public static function is_template_active( $template_id, array $activated_templates )
381 {
382 $template_data = self::get_template_data( $template_id, $activated_templates );
383 return $template_data['status'] ?? false;
384 }
385
386 /**
387 * @param $template_id
388 * @param array $activated_templates
389 * @return mixed
390 */
391 public static function get_template_category_id( $template_id, array $activated_templates )
392 {
393 $template_data = self::get_template_data( $template_id, $activated_templates );
394 return $template_data['category_id'] ?? false;
395 }
396
397 /**
398 * @param $templates
399 * @param $type
400 * @param $category_id_or_template_id
401 */
402 public static function get_template_key( $templates, $type = 'category_id', $category_id_or_template_id = 0 )
403 {
404
405 foreach ( $templates as $key => $template ) {
406 if ( $template[$type] == $category_id_or_template_id ) {
407 return $key;
408 }
409 }
410
411 return false;
412 }
413
414 public static function get_meta_key_for_type()
415 {
416 return self::PK__SHOPENGINE_TEMPLATE . '__type';
417 }
418
419 public static function get_meta_key_for_edit_with()
420 {
421 return self::PK__SHOPENGINE_TEMPLATE . '__edit_with';
422 }
423
424 /**
425 * @param $template_id
426 */
427 public static function edit_with( $template_id )
428 {
429
430 if ( static::$edit_with ) {
431 return static::$edit_with;
432 }
433
434 $edit_with = get_post_meta( $template_id, Action::get_meta_key_for_edit_with(), true );
435 static::$edit_with = empty( $edit_with ) ? Action::EDIT_WITH_ELEMENTOR : $edit_with;
436
437 return static::$edit_with;
438 }
439
440 /**
441 * @param $pid
442 * @return mixed
443 */
444 public static function is_edit_with_gutenberg( $pid )
445 {
446 $edit_with = get_post_meta( $pid, Action::get_meta_key_for_edit_with(), true );
447 $edit_with = empty( $edit_with ) ? Action::EDIT_WITH_ELEMENTOR : $edit_with;
448
449 return $edit_with === self::EDIT_WITH_GUTENBERG;
450 }
451
452 /**
453 * @param $field
454 * @param $default
455 */
456 private static function get_form_value( $field, $default = '' )
457 {
458 return !empty( self::$form_settings[$field] ) ? self::$form_settings[$field] : $default;
459 }
460
461 /**
462 * @param $form_settings
463 * @param $fields
464 */
465 private function set_form_values( $form_settings, $fields = null )
466 {
467 $fields = $this->get_fields();
468
469 foreach ( $form_settings as $key => $value ) {
470 if ( isset( $fields[$key] ) ) {
471 self::$form_settings[$key] = $value;
472 }
473 }
474 }
475
476 public function get_fields()
477 {
478 return [
479 'form_title' => [
480 'name' => 'form_title'
481 ],
482 'form_type' => [
483 'name' => 'form_type'
484 ],
485 'status' => [
486 'name' => 'status'
487 ],
488 'edit_with_option' => [
489 'name' => 'edit_with_option'
490 ],
491 'sample_design' => [
492 'name' => 'sample_design'
493 ],
494 'category_id' => [
495 'name' => 'category_id'
496 ],
497 'language_code' => [
498 'name' => 'language_code'
499 ],
500 'old_language_code' => [
501 'name' => 'old_language_code'
502 ]
503 ];
504 }
505 }
506