PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.1.4
ShopBuilder – WooCommerce Builder For Elementor v2.1.4
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Controllers / BuilderController.php

BuilderController.php in ShopBuilder – WooCommerce Builder For Elementor 2.1.4, at app/Controllers/BuilderController.php

625 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main BuilderController class.
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Controllers;
9
10 use RadiusTheme\SB\Helpers\Fns;
11 use RadiusTheme\SB\Helpers\BuilderFns;
12 use RadiusTheme\SB\Traits\SingletonTrait;
13 use RadiusTheme\SB\Controllers\Builder\BuilderCpt;
14 use RadiusTheme\SB\Controllers\Hooks\BuilderHooks;
15 use RadiusTheme\SB\Elementor\Controls\ImageSelectorControl;
16 use RadiusTheme\SB\Elementor\Controls\Select2AjaxControl;
17 use RadiusTheme\SB\Models\ElementList;
18
19 // Do not allow directly accessing this file.
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit( 'This script cannot be accessed directly.' );
22 }
23
24 /**
25 * Builder Controller
26 */
27 class BuilderController {
28 /**
29 * @var array
30 */
31 private static $cache = [];
32
33 /**
34 * Builder page id.
35 *
36 * @var integer
37 */
38 private $builder_page_id = 0;
39
40 /**
41 * Page Edit By.
42 *
43 * @var string
44 */
45 private $page_edit_with = '';
46
47 /**
48 * Singleton Trait
49 */
50 use SingletonTrait;
51
52 /**
53 * Construct function
54 */
55 private function __construct() {
56 $this->builder_page_id = BuilderFns::builder_page_id_by_page();
57 $this->page_edit_with = Fns::page_edit_with( $this->builder_page_id );
58
59 if ( ! is_admin() ) {
60 BuilderHooks::instance();
61 }
62
63 BuilderCpt::instance();
64
65 $this->elementor_init();
66 $this->both_builder_frontend();
67
68 // add_action( 'elementor/init', [ $this, 'elementor_init' ] );
69 add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'editor_scripts' ] );
70 // add_action( 'template_redirect', [ $this, 'both_builder_frontend' ], 99 );
71
72 // RT Select2 Ajax.
73 add_action( 'wp_ajax_rt_select2_object_search', [ $this, 'select2_ajax_posts_filter_autocomplete' ] );
74 add_action( 'wp_ajax_nopriv_rt_select2_object_search', [ $this, 'select2_ajax_posts_filter_autocomplete' ] );
75 // Select2 ajax save data.
76 add_action( 'wp_ajax_rt_select2_get_title', [ $this, 'select2_ajax_get_posts_value_titles' ] );
77 add_action( 'wp_ajax_nopriv_rt_select2_get_title', [ $this, 'select2_ajax_get_posts_value_titles' ] );
78 }
79
80 /**
81 * Apply for frontend.
82 *
83 * @return void
84 */
85 public function both_builder_frontend() {
86 add_action( 'rtsb/builder/template/before/content', [ $this, 'builder_template_before_content' ] );
87 add_action( 'rtsb/builder/template/after/content', [ $this, 'builder_template_after_content' ] );
88 }
89
90 /**
91 * Register Category
92 *
93 * @return void
94 */
95 public function elementor_init() {
96 add_action( 'elementor/elements/categories_registered', [ $this, 'add_elementor_widget_categories' ] );
97 add_action( 'elementor/widgets/register', [ $this, 'init_widgets' ] );
98 add_action( 'elementor/controls/register', [ $this, 'init_controls' ] );
99 add_filter( 'elementor/editor/localize_settings', [ $this, 'promotePremiumWidgets' ] );
100 add_action( 'elementor/icons_manager/additional_tabs', [ $this, 'rtsb_icons' ] );
101 }
102
103 /**
104 * Register Category
105 *
106 * @param object $elements_manager Category hooks.
107 *
108 * @return void
109 */
110 public function add_elementor_widget_categories( $elements_manager ) {
111 $type = BuilderFns::builder_type( get_the_ID() );
112 $builder_type = ! empty( BuilderFns::builder_page_types()[ $type ] ) ? BuilderFns::builder_page_types()[ $type ] : '';
113 $builder_type = str_replace( 'My Account - ', '', $builder_type );
114 $categories['rtsb-shopbuilder'] = [
115 'title' => esc_html__( 'ShopBuilder', 'shopbuilder' ) . ' - ' . ucfirst( $builder_type ),
116 'icon' => 'fa fa-plug',
117 ];
118 $categories['rtsb-shopbuilder-general'] = [
119 'title' => esc_html__( 'Shopbuilder - General', 'shopbuilder' ),
120 'icon' => 'fa fa-plug',
121 ];
122
123 $categories = apply_filters( 'rtsb_elementor_widgets_category_lists', $categories );
124 $other_categories = $elements_manager->get_categories();
125 $categories = array_merge(
126 array_slice( $other_categories, 0, 1 ),
127 $categories,
128 array_slice( $other_categories, 1 )
129 );
130
131 $set_categories = function ( $categories ) {
132 $this->categories = $categories;
133 };
134
135 $set_categories->call( $elements_manager, $categories );
136 }
137
138 /**
139 * Init Controls
140 *
141 * Include controls files and register them
142 *
143 * @param object $controls_manager Controls Manager.
144 *
145 * @since 1.0.0
146 */
147 public function init_controls( $controls_manager ) {
148 $controls_manager->register( new ImageSelectorControl() );
149 $controls_manager->register( new Select2AjaxControl() );
150 }
151
152 /**
153 * Init Widgets
154 *
155 * Include widgets files and register them
156 *
157 * @since 1.0.0
158 */
159 public function init_widgets( $widgets_manager ) {
160
161 $get_list = ElementList::instance()->get_list( true, 'active' );
162 foreach ( $get_list as $element ) {
163 if ( isset( $element['active'] ) && 'on' !== $element['active'] ) {
164 continue;
165 }
166
167 if ( ! empty( $element['package'] ) && 'pro-disabled' === $element['package'] ) {
168 continue;
169 }
170
171 if ( empty( $element['base_class'] ) || ! class_exists( $element['base_class'] ) ) {
172 continue;
173 }
174
175 if ( ! empty( $element['category'] ) ) {
176 $widget = false;
177 $import = apply_filters( 'rtsb_import_status', false );
178 switch ( $element['category'] ) {
179 case 'product':
180 if ( BuilderFns::is_product() || $import ) {
181 $widget = new $element['base_class']();
182 }
183 break;
184 case 'shop':
185 if ( BuilderFns::is_archive() || BuilderFns::is_shop() || $import ) {
186 $widget = new $element['base_class']();
187 }
188 break;
189 case 'archive':
190 if ( BuilderFns::is_archive() || $import ) {
191 $widget = new $element['base_class']();
192 }
193 break;
194 case 'cart':
195 if ( BuilderFns::is_cart() || $import ) {
196 $widget = new $element['base_class']();
197 }
198 break;
199 case 'checkout':
200 if ( BuilderFns::is_checkout() || $import ) {
201 $widget = new $element['base_class']();
202 }
203 break;
204 case 'general':
205 $widget = new $element['base_class']();
206 break;
207 default:
208 }
209 $widget = apply_filters( 'rtsb/element/list/init/widgets', $widget, $element );
210
211 if ( $widget && is_object( $widget ) ) {
212 $widgets_manager->register( $widget );
213 }
214 }
215 }
216 }
217
218 /**
219 * Adding custom icons in Elementor
220 *
221 * @param array $tabs Tabs.
222 *
223 * @return array
224 */
225 public function rtsb_icons( $tabs = [] ) {
226 $custom_icons = $this->get_icons();
227
228 $tabs['shopbuilder-icons'] = [
229 'name' => 'rtsb-fonts',
230 'label' => esc_html__( 'ShopBuilder Icons', 'shopbuilder' ),
231 'labelIcon' => 'fab fa-elementor',
232 'prefix' => 'rtsb-icon-',
233 'displayPrefix' => 'rtsb-icon',
234 'url' => rtsb()->get_assets_uri( 'css/frontend/rtsb-fonts.css' ),
235 'icons' => $custom_icons,
236 'ver' => '1.0',
237 ];
238
239 return $tabs;
240 }
241
242 /**
243 * Editor JS.
244 *
245 * @return void
246 */
247 public function editor_scripts() {
248 $version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? time() : RTSB_VERSION;
249
250 wp_enqueue_script(
251 'rtsb-editor-script',
252 rtsb()->get_assets_uri( 'js/backend/editor.js' ),
253 [
254 'jquery',
255 'elementor-editor',
256 'jquery-elementor-select2',
257 ],
258 $version,
259 true
260 );
261 $params = [
262 'isEditorForBuilder' => false,
263 ];
264 if ( BuilderFns::is_builder_preview() ) {
265 $params['isEditorForBuilder'] = true;
266 }
267 wp_localize_script( 'rtsb-editor-script', 'rtsbTemplateBuilderEditorScript', $params );
268 wp_enqueue_style( 'rtsb-el-editor-style', rtsb()->get_assets_uri( 'css/backend/elementor-editor.css' ), [], $version );
269 }
270
271 /**
272 * Before Content for Elementor.
273 *
274 * @return void
275 */
276 public function builder_template_before_content() {
277 /**
278 * Before Header-Footer page template content.
279 *
280 * Fires before the content of Elementor Header-Footer page template.
281 *
282 * @since 2.0.0
283 */
284 if ( did_action( 'elementor/loaded' ) && 'elementor' === $this->page_edit_with ) {
285 do_action( 'elementor/page_templates/header-footer/before_content' );
286 }
287
288 $parent_class = apply_filters( 'rtsb/builder/content/parent_class', [] );
289
290 if ( BuilderFns::is_product() ) {
291 $product_id = Fns::get_prepared_product_id();
292 $parent_class = wc_get_product_class( $parent_class, $product_id );
293 }
294
295 if ( BuilderFns::is_shop() ) {
296 $parent_class[] = 'shop';
297 }
298
299 if ( BuilderFns::is_archive() ) {
300 $parent_class[] = 'archive';
301 }
302
303 if ( BuilderFns::is_checkout() ) {
304 $parent_class[] = 'checkout-page ';
305 }
306
307 ?>
308 <!-- Before Content start -->
309 <div class="<?php echo esc_attr( implode( ' ', $parent_class ) ); ?>">
310 <?php
311 if ( BuilderFns::is_checkout() ) {
312 if ( ! apply_filters( 'rtsb/multi/step/checkout/widget', true ) ) {
313 return;
314 }
315 ?>
316 <form name="checkout" method="post" class="rtsb-woocommerce-checkout checkout" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data">
317 <?php
318 }
319 }
320
321 /**
322 * After Content for Elementor.
323 *
324 * @return void
325 */
326 public function builder_template_after_content() {
327 /**
328 * After Header-Footer page template content.
329 *
330 * Fires after the content of Elementor Header-Footer page template.
331 *
332 * @since 2.0.0
333 */
334 if ( did_action( 'elementor/loaded' ) && 'elementor' === $this->page_edit_with ) {
335 do_action( 'elementor/page_templates/header-footer/after_content' );
336 }
337
338 if ( BuilderFns::is_checkout() ) {
339 if ( ! apply_filters( 'rtsb/multi/step/checkout/widget', true ) ) {
340 return;
341 }
342 ?>
343 </form>
344 <?php
345 }
346 ?>
347 </div>
348 <!-- After Content end -->
349 <?php
350 }
351
352 /**
353 * Ajax callback for rt-select2
354 *
355 * @return void
356 */
357 public function select2_ajax_posts_filter_autocomplete() {
358
359 $query_per_page = 15;
360 $post_type = 'post';
361 $source_name = 'post_type';
362 $paged = $_POST['page'] ?? 1;
363
364 if ( ! empty( $_POST['post_type'] ) ) {
365 $post_type = sanitize_text_field( $_POST['post_type'] );
366 }
367
368 if ( ! empty( $_POST['source_name'] ) ) {
369 $source_name = sanitize_text_field( $_POST['source_name'] );
370 }
371
372 $search = ! empty( $_POST['search'] ) ? sanitize_text_field( $_POST['search'] ) : '';
373 $results = $post_list = [];
374 switch ( $source_name ) {
375 case 'taxonomy':
376 $args = [
377 'hide_empty' => false,
378 'orderby' => 'name',
379 'order' => 'ASC',
380 'search' => $search,
381 'number' => '5',
382 ];
383
384 if ( $post_type !== 'all' ) {
385 $args['taxonomy'] = $post_type;
386 }
387
388 $post_list = wp_list_pluck( get_terms( $args ), 'name', 'term_id' );
389 break;
390 case 'user':
391 $users = [];
392
393 foreach ( get_users( [ 'search' => "*{$search}*" ] ) as $user ) {
394 $user_id = $user->ID;
395 $user_name = $user->display_name;
396 $users[ $user_id ] = $user_name;
397 }
398
399 $post_list = $users;
400 break;
401 default:
402 $post_list = $this->get_query_data( $post_type, $query_per_page, $search, $paged );
403 }
404
405 $pagination = true;
406 if ( count( $post_list ) < $query_per_page ) {
407 $pagination = false;
408 }
409 if ( ! empty( $post_list ) ) {
410 foreach ( $post_list as $key => $item ) {
411 $results[] = [
412 'text' => $item,
413 'id' => $key,
414 ];
415 }
416 }
417 wp_send_json(
418 [
419 'results' => $results,
420 'pagination' => [ 'more' => $pagination ],
421 ]
422 );
423 }
424
425
426 /**
427 * Ajax callback for rt-select2
428 *
429 * @return void
430 */
431 public function select2_ajax_get_posts_value_titles() {
432
433 if ( empty( $_POST['id'] ) ) {
434 wp_send_json_error( [] );
435 }
436
437 if ( empty( array_filter( $_POST['id'] ) ) ) {
438 wp_send_json_error( [] );
439 }
440 $ids = array_map( 'intval', $_POST['id'] );
441 $source_name = ! empty( $_POST['source_name'] ) ? sanitize_text_field( $_POST['source_name'] ) : '';
442
443 switch ( $source_name ) {
444 case 'taxonomy':
445 $args = [
446 'hide_empty' => false,
447 'orderby' => 'name',
448 'order' => 'ASC',
449 'include' => implode( ',', $ids ),
450 ];
451
452 if ( $_POST['post_type'] !== 'all' ) {
453 $args['taxonomy'] = sanitize_text_field( $_POST['post_type'] );
454 }
455
456 $response = wp_list_pluck( get_terms( $args ), 'name', 'term_id' );
457 break;
458 case 'user':
459 $users = [];
460
461 foreach ( get_users( [ 'include' => $ids ] ) as $user ) {
462 $user_id = $user->ID;
463 $user_name = $user->display_name . '-' . $user->ID;
464 $users[ $user_id ] = $user_name;
465 }
466
467 $response = $users;
468 break;
469 default:
470 $post_info = get_posts(
471 [
472 'post_type' => sanitize_text_field( $_POST['post_type'] ),
473 'include' => implode( ',', $ids ),
474 ]
475 );
476 $response = wp_list_pluck( $post_info, 'post_title', 'ID' );
477 }
478
479 if ( ! empty( $response ) ) {
480 wp_send_json_success( [ 'results' => $response ] );
481 } else {
482 wp_send_json_error( [] );
483 }
484 }
485
486
487 /**
488 * Ajax callback for rt-select2
489 *
490 * @param $post_type
491 * @param $limit
492 * @param $search
493 * @param $paged
494 *
495 * @return array
496 */
497 public function get_query_data( $post_type = 'any', $limit = 10, $search = '', $paged = 1 ) {
498 global $wpdb;
499 $where = '';
500 $data = [];
501
502 if ( - 1 == $limit ) {
503 $limit = '';
504 } elseif ( 0 == $limit ) {
505 $limit = 'limit 0,1';
506 } else {
507 $offset = 0;
508 if ( $paged ) {
509 $offset = ( $paged - 1 ) * $limit;
510 }
511 $limit = $wpdb->prepare( ' limit %d, %d', esc_sql( $offset ), esc_sql( $limit ) );
512 }
513
514 if ( 'any' === $post_type ) {
515 $in_search_post_types = get_post_types( [ 'exclude_from_search' => false ] );
516 if ( empty( $in_search_post_types ) ) {
517 $where .= ' AND 1=0 ';
518 } else {
519 $where .= " AND {$wpdb->posts}.post_type IN ('" . join(
520 "', '",
521 array_map( 'esc_sql', $in_search_post_types )
522 ) . "')";
523 }
524 } elseif ( ! empty( $post_type ) ) {
525 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", esc_sql( $post_type ) );
526 }
527
528 if ( ! empty( $search ) ) {
529 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title LIKE %s", '%' . esc_sql( $search ) . '%' );
530 }
531
532 $query = "select post_title,ID from $wpdb->posts where post_status = 'publish' {$where} {$limit}";
533 $results = $wpdb->get_results( $query );
534
535 if ( ! empty( $results ) ) {
536 foreach ( $results as $row ) {
537 $data[ $row->ID ] = $row->post_title . ' [#' . $row->ID . ']';
538 }
539 }
540
541 return $data;
542 }
543
544 /**
545 * Promotion.
546 *
547 * @param array $config Config.
548 * @return array
549 */
550 public function promotePremiumWidgets( $config ) {
551 if ( rtsb()->has_pro() ) {
552 return $config;
553 }
554
555 if ( ! empty( $config['initial_document']['panel']['elements_categories']['rtsb-shopbuilder']['title'] ) ) {
556 $title = $config['initial_document']['panel']['elements_categories']['rtsb-shopbuilder']['title'];
557
558 if ( 'ShopBuilder - Shop' === $title || 'ShopBuilder - Archive' === $title ) {
559 if ( ! isset( $config['promotionWidgets'] ) || ! is_array( $config['promotionWidgets'] ) ) {
560 $config['promotionWidgets'] = [];
561 }
562
563 $pro_widgets = [
564 [
565 'name' => 'rtsb-ajax-product-filters',
566 'title' => esc_html__( 'Ajax Product Filters', 'testimonial-slider-showcase' ),
567 'description' => esc_html__( 'Ajax Product Filters', 'testimonial-slider-showcase' ),
568 'icon' => 'rtsb-el-custom rtsb-element icon-rtsb-ajax-product-filters rtsb-promotional-element',
569 'categories' => '[ "rtsb-shopbuilder" ]',
570 ],
571 ];
572
573 $config['promotionWidgets'] = array_merge( $config['promotionWidgets'], $pro_widgets );
574
575 return $config;
576 }
577 }
578
579 return $config;
580 }
581
582 /**
583 * Get Icons.
584 *
585 * @return array
586 */
587 public function get_icons() {
588 return [
589 'heart-empty',
590 'heart',
591 'eye',
592 'exchange',
593 'plus',
594 'minus',
595 'avatar',
596 'pay',
597 'share',
598 'clock',
599 'check-alt',
600 'check',
601 'delete',
602 'marker',
603 'list',
604 'list-2',
605 'power',
606 'cart',
607 'cart-2',
608 'cart-3',
609 'downloads',
610 'zoom',
611 'user-edit',
612 'grid',
613 'filter',
614 'billing',
615 'login',
616 'payment',
617 'search',
618 'edit',
619 'coupon',
620 'arrows-cw',
621 'trash-empty',
622 ];
623 }
624 }
625