| @@ -2,11 +2,10 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace RadiusTheme\SB\Controllers\Admin\Ajax; |
| 4 | 4 | |
| 5 | 5 | use RadiusTheme\SB\Helpers\Fns; |
| 6 | -use RadiusTheme\SB\Helpers\Cache; | |
| 6 | +use RadiusTheme\SB\Models\DataModel; | |
| 7 | 7 | use RadiusTheme\SB\Models\Settings; |
| 8 | -use RadiusTheme\SB\Models\DataModel; | |
| 9 | 8 | use RadiusTheme\SB\Traits\SingletonTrait; |
| 10 | 9 | |
| 11 | 10 | // Do not allow directly accessing this file. |
| 12 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -25,11 +24,15 @@ | ||
| 25 | 24 | /** |
| 26 | 25 | * Construct function |
| 27 | 26 | */ |
| 28 | 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' ] ); | |
| 29 | 30 | add_action( 'wp_ajax_rtsb_save_settings_data', [ $this, 'save_settings_data' ] ); |
| 30 | 31 | add_action( 'wp_ajax_rtsb_toggle_modules_activation', [ $this, 'toggle_modules' ] ); |
| 32 | + | |
| 31 | 33 | add_action( 'wp_ajax_rtsb_get_multiselect_data', [ $this, 'get_multiselect_data' ] ); |
| 34 | + | |
| 32 | 35 | } |
| 33 | 36 | |
| 34 | 37 | /** |
| 35 | 38 | * Get settings fields |
| @@ -36,14 +39,10 @@ | ||
| 36 | 39 | * |
| 37 | 40 | * @return void |
| 38 | 41 | */ |
| 39 | 42 | public function get_multiselect_data() { |
| 40 | - if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) || ! current_user_can( 'manage_options' ) ) { | |
| 41 | - wp_send_json_error( esc_html__( 'Security error: Insufficient permissions.', 'shopbuilder' ) ); | |
| 42 | - } | |
| 43 | - | |
| 44 | - $func_with_param = sanitize_text_field( $_REQUEST['func_with_param'] ?? '' ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash | |
| 45 | - $s = sanitize_text_field( $_REQUEST['s'] ?? '' ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash | |
| 43 | + $func_with_param = sanitize_text_field( $_REQUEST['func_with_param'] ?? '' ); | |
| 44 | + $s = sanitize_text_field( $_REQUEST['s'] ?? '' ); | |
| 46 | 45 | $decodedString = stripslashes( $func_with_param ); |
| 47 | 46 | $functionArray = json_decode( $decodedString, true ); |
| 48 | 47 | $className = $functionArray[0]; |
| 49 | 48 | $methodName = $functionArray[1]; |
| @@ -50,9 +49,9 @@ | ||
| 50 | 49 | $argument = $functionArray[2] ?? []; |
| 51 | 50 | $result = []; |
| 52 | 51 | $params = []; |
| 53 | 52 | if ( class_exists( $className ) && method_exists( $className, $methodName ) ) { |
| 54 | - $params[] = $s; | |
| 53 | + $params[] = $s; // Modify this array with your parameters | |
| 55 | 54 | $params[] = $argument; |
| 56 | 55 | $result = call_user_func_array( [ $className, $methodName ], $params ); |
| 57 | 56 | } |
| 58 | 57 | wp_send_json_success( $result ); |
| @@ -58,8 +57,18 @@ | ||
| 58 | 57 | wp_send_json_success( $result ); |
| 59 | 58 | } |
| 60 | 59 | |
| 61 | 60 | /** |
| 61 | + * Get settings fields | |
| 62 | + * | |
| 63 | + * @return void | |
| 64 | + */ | |
| 65 | + public function get_settings_fields() { | |
| 66 | + $data = Settings::instance()->get_fields(); | |
| 67 | + wp_send_json_success( $data ); | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 62 | 71 | * Get settings Data |
| 63 | 72 | * |
| 64 | 73 | * @return void |
| 65 | 74 | */ |
| @@ -73,44 +82,19 @@ | ||
| 73 | 82 | * |
| 74 | 83 | * @return void |
| 75 | 84 | */ |
| 76 | 85 | public function save_settings_data() { |
| 77 | - if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) || ! current_user_can( 'manage_options' ) ) { | |
| 78 | - wp_send_json_error( esc_html__( 'Security error: Insufficient permissions.', 'shopbuilder' ) ); | |
| 79 | - } | |
| 80 | - | |
| 81 | 86 | $section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : ''; |
| 82 | 87 | $block_id = isset( $_POST['block_id'] ) ? sanitize_text_field( wp_unslash( $_POST['block_id'] ) ) : ''; |
| 83 | - // Fns::set_options Sanitized all array values Before saving. | |
| 84 | - $rawOptions = $_POST['options'] ?? []; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash | |
| 85 | - | |
| 86 | - $sections = Settings::instance()->get_sections(); | |
| 87 | - $options = []; | |
| 88 | - if ( ! empty( $sections[ $section_id ]['list'][ $block_id ]['fields'] ) ) { | |
| 89 | - $fields = $sections[ $section_id ]['list'][ $block_id ]['fields']; | |
| 90 | - $db_options = Fns::get_options( $section_id, $block_id ); | |
| 91 | - foreach ( $fields as $field_id => $field ) { | |
| 92 | - if ( ! array_key_exists( $field_id, $db_options ) ) { | |
| 93 | - $type = 'array' === gettype( $field['value'] ) ? [] : ''; | |
| 94 | - $options[ $field_id ] = ! empty( $field['value'] ) ? $field['value'] : $type; | |
| 95 | - } | |
| 96 | - } | |
| 97 | - } | |
| 98 | - | |
| 99 | - $rawOptions = wp_parse_args( $rawOptions, $options ); | |
| 100 | - | |
| 101 | - $status = Fns::set_options( $section_id, $block_id, $rawOptions ); | |
| 102 | - | |
| 88 | + $rawOptions = isset( $_POST['options'] ) ? $_POST['options'] : []; // Fns::set_options Sanitized all array values Before saving. | |
| 89 | + $status = Fns::set_options( $section_id, $block_id, $rawOptions ); | |
| 103 | 90 | if ( boolval( $status['status'] ) ) { |
| 104 | - do_action( 'rtsb/after/saved/settings/success/' . $section_id . '/' . $block_id, $rawOptions ); | |
| 105 | 91 | do_action( 'rtsb/after/saved/settings/success', $section_id, $block_id, $rawOptions ); |
| 106 | - | |
| 107 | - Cache::clear_asset_cache(); | |
| 108 | - | |
| 109 | 92 | wp_send_json_success( $status ); |
| 110 | 93 | } else { |
| 111 | 94 | wp_send_json_error( $status ); |
| 112 | 95 | } |
| 96 | + | |
| 113 | 97 | } |
| 114 | 98 | |
| 115 | 99 | /** |
| 116 | 100 | * Toggle modules |
| @@ -117,20 +101,11 @@ | ||
| 117 | 101 | * |
| 118 | 102 | * @return void |
| 119 | 103 | */ |
| 120 | 104 | public function toggle_modules() { |
| 121 | - | |
| 122 | - if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) || ! current_user_can( 'manage_options' ) ) { | |
| 123 | - wp_send_json_error( | |
| 124 | - [ | |
| 125 | - 'message' => esc_html__( 'Security error: Insufficient permissions.', 'shopbuilder' ), | |
| 126 | - ] | |
| 127 | - ); | |
| 128 | - } | |
| 129 | - | |
| 130 | 105 | $section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : ''; |
| 131 | - $module_ids = isset( $_POST['module_ids'] ) ? array_map( 'sanitize_text_field', $_POST['module_ids'] ) : []; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash | |
| 132 | - $type = isset( $_POST['type'] ) && 'active' === sanitize_text_field( wp_unslash( $_POST['type'] ) ) ? 'active' : false; | |
| 106 | + $module_ids = isset( $_POST['module_ids'] ) ? array_map( 'sanitize_text_field', $_POST['module_ids'] ) : []; | |
| 107 | + $type = isset( $_POST['type'] ) && $_POST['type'] === 'active' ? 'active' : false; | |
| 133 | 108 | |
| 134 | 109 | if ( ! $section_id || empty( $module_ids ) ) { |
| 135 | 110 | wp_send_json_error( |
| 136 | 111 | [ |
| @@ -137,9 +112,8 @@ | ||
| 137 | 112 | 'message' => esc_html__( 'Section , block or options may be empty', 'shopbuilder' ), |
| 138 | 113 | ] |
| 139 | 114 | ); |
| 140 | 115 | } |
| 141 | - | |
| 142 | 116 | $sections = Settings::instance()->get_sections(); |
| 143 | 117 | if ( empty( $sections[ $section_id ] ) ) { |
| 144 | 118 | wp_send_json_error( |
| 145 | 119 | [ |
| @@ -152,9 +126,9 @@ | ||
| 152 | 126 | if ( isset( $sections[ $section_id ]['list'][ $module_id ] ) ) { |
| 153 | 127 | if ( isset( $sections[ $section_id ]['list'][ $module_id ]['package'] ) && 'pro-disabled' === $sections[ $section_id ]['list'][ $module_id ]['package'] ) { |
| 154 | 128 | continue; |
| 155 | 129 | } |
| 156 | - if ( 'active' === $type ) { | |
| 130 | + if ( $type === 'active' ) { | |
| 157 | 131 | $options[ $module_id ]['active'] = 'on'; |
| 158 | 132 | $sections[ $section_id ]['list'][ $module_id ]['active'] = 'on'; |
| 159 | 133 | } else { |
| 160 | 134 | $options[ $module_id ]['active'] = ''; |
| @@ -160,13 +134,11 @@ | ||
| 160 | 134 | $options[ $module_id ]['active'] = ''; |
| 161 | 135 | $sections[ $section_id ]['list'][ $module_id ]['active'] = ''; |
| 162 | 136 | } |
| 163 | 137 | } |
| 164 | - do_action( 'rtsb/before/save/options', $section_id, $module_id, $options[ $module_id ] ?? [] ); | |
| 165 | 138 | } |
| 166 | 139 | |
| 167 | 140 | DataModel::source()->set_option( $section_id, $options ); |
| 168 | - Cache::clear_asset_cache(); | |
| 169 | 141 | |
| 170 | 142 | wp_send_json_success( |
| 171 | 143 | [ |
| 172 | 144 | 'message' => esc_html__( 'Successfully Saved', 'shopbuilder' ), |
| @@ -173,5 +145,7 @@ | ||
| 173 | 145 | 'sections' => $sections, |
| 174 | 146 | ] |
| 175 | 147 | ); |
| 176 | 148 | } |
| 149 | + | |
| 150 | + | |
| 177 | 151 | } |