PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.1.2
ShopBuilder – WooCommerce Builder For Elementor v2.1.2
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.2, at app/Controllers/BuilderController.php

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