PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.1.5
ShopBuilder – WooCommerce Builder For Elementor v2.1.5
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 / Admin / Ajax / AdminSettings.php

AdminSettings.php in ShopBuilder – WooCommerce Builder For Elementor 2.1.5, at app/Controllers/Admin/Ajax/AdminSettings.php

165 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace RadiusTheme\SB\Controllers\Admin\Ajax;
4
5 use RadiusTheme\SB\Helpers\Fns;
6 use RadiusTheme\SB\Models\DataModel;
7 use RadiusTheme\SB\Models\Settings;
8 use RadiusTheme\SB\Traits\SingletonTrait;
9
10 // Do not allow directly accessing this file.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit( 'This script cannot be accessed directly.' );
13 }
14
15 /**
16 * Settings Page.
17 */
18 class AdminSettings {
19 /**
20 * Singleton Trait.
21 */
22 use SingletonTrait;
23
24 /**
25 * Construct function
26 */
27 private function __construct() {
28 add_action( 'wp_ajax_rtsb_settings_fields', [ $this, 'get_settings_fields' ] );
29 add_action( 'wp_ajax_rtsb_settings_data', [ $this, 'get_settings_data' ] );
30 add_action( 'wp_ajax_rtsb_save_settings_data', [ $this, 'save_settings_data' ] );
31 add_action( 'wp_ajax_rtsb_toggle_modules_activation', [ $this, 'toggle_modules' ] );
32
33 add_action( 'wp_ajax_rtsb_get_multiselect_data', [ $this, 'get_multiselect_data' ] );
34 }
35
36 /**
37 * Get settings fields
38 *
39 * @return void
40 */
41 public function get_multiselect_data() {
42 $func_with_param = sanitize_text_field( $_REQUEST['func_with_param'] ?? '' );
43 $s = sanitize_text_field( $_REQUEST['s'] ?? '' );
44 $decodedString = stripslashes( $func_with_param );
45 $functionArray = json_decode( $decodedString, true );
46 $className = $functionArray[0];
47 $methodName = $functionArray[1];
48 $argument = $functionArray[2] ?? [];
49 $result = [];
50 $params = [];
51 if ( class_exists( $className ) && method_exists( $className, $methodName ) ) {
52 $params[] = $s; // Modify this array with your parameters
53 $params[] = $argument;
54 $result = call_user_func_array( [ $className, $methodName ], $params );
55 }
56 wp_send_json_success( $result );
57 }
58
59 /**
60 * Get settings fields
61 *
62 * @return void
63 */
64 public function get_settings_fields() {
65 $data = Settings::instance()->get_fields();
66 wp_send_json_success( $data );
67 }
68
69 /**
70 * Get settings Data
71 *
72 * @return void
73 */
74 public function get_settings_data() {
75 $data = Settings::instance()->get_data();
76 wp_send_json_success( $data );
77 }
78
79 /**
80 * Save settings data
81 *
82 * @return void
83 */
84 public function save_settings_data() {
85 $section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : '';
86 $block_id = isset( $_POST['block_id'] ) ? sanitize_text_field( wp_unslash( $_POST['block_id'] ) ) : '';
87 $rawOptions = isset( $_POST['options'] ) ? $_POST['options'] : []; // Fns::set_options Sanitized all array values Before saving.
88
89 $sections = Settings::instance()->get_sections();
90 $options = [];
91 if ( ! empty( $sections[ $section_id ]['list'][ $block_id ]['fields'] ) ) {
92 $fields = $sections[ $section_id ]['list'][ $block_id ]['fields'];
93 $db_options = Fns::get_options( $section_id, $block_id );
94 foreach ( $fields as $field_id => $field ) {
95 if ( ! array_key_exists( $field_id, $db_options ) ) {
96 $type = 'array' === gettype( $field['value'] ) ? [] : '';
97 $options[ $field_id ] = ! empty( $field['value'] ) ? $field['value'] : $type;
98 }
99 }
100 }
101
102 $rawOptions = wp_parse_args( $rawOptions, $options );
103
104 $status = Fns::set_options( $section_id, $block_id, $rawOptions );
105
106 if ( boolval( $status['status'] ) ) {
107 do_action( 'rtsb/after/saved/settings/success', $section_id, $block_id, $rawOptions );
108 wp_send_json_success( $status );
109 } else {
110 wp_send_json_error( $status );
111 }
112 }
113
114 /**
115 * Toggle modules
116 *
117 * @return void
118 */
119 public function toggle_modules() {
120 $section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : '';
121 $module_ids = isset( $_POST['module_ids'] ) ? array_map( 'sanitize_text_field', $_POST['module_ids'] ) : [];
122 $type = isset( $_POST['type'] ) && $_POST['type'] === 'active' ? 'active' : false;
123
124 if ( ! $section_id || empty( $module_ids ) ) {
125 wp_send_json_error(
126 [
127 'message' => esc_html__( 'Section , block or options may be empty', 'shopbuilder' ),
128 ]
129 );
130 }
131 $sections = Settings::instance()->get_sections();
132 if ( empty( $sections[ $section_id ] ) ) {
133 wp_send_json_error(
134 [
135 'message' => esc_html__( 'No section found with given data', 'shopbuilder' ),
136 ]
137 );
138 }
139 $options = DataModel::source()->get_option( $section_id, [] );
140 foreach ( $module_ids as $module_id ) {
141 if ( isset( $sections[ $section_id ]['list'][ $module_id ] ) ) {
142 if ( isset( $sections[ $section_id ]['list'][ $module_id ]['package'] ) && 'pro-disabled' === $sections[ $section_id ]['list'][ $module_id ]['package'] ) {
143 continue;
144 }
145 if ( $type === 'active' ) {
146 $options[ $module_id ]['active'] = 'on';
147 $sections[ $section_id ]['list'][ $module_id ]['active'] = 'on';
148 } else {
149 $options[ $module_id ]['active'] = '';
150 $sections[ $section_id ]['list'][ $module_id ]['active'] = '';
151 }
152 }
153 }
154
155 DataModel::source()->set_option( $section_id, $options );
156
157 wp_send_json_success(
158 [
159 'message' => esc_html__( 'Successfully Saved', 'shopbuilder' ),
160 'sections' => $sections,
161 ]
162 );
163 }
164 }
165