| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Controllers\Admin\Ajax; |
| 4 |
|
| 5 |
use RadiusTheme\SB\Helpers\Fns; |
| 6 |
use RadiusTheme\SB\Helpers\Cache; |
| 7 |
use RadiusTheme\SB\Models\Settings; |
| 8 |
use RadiusTheme\SB\Models\DataModel; |
| 9 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 10 |
|
| 11 |
// Do not allow directly accessing this file. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit( 'This script cannot be accessed directly.' ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Settings Page. |
| 18 |
*/ |
| 19 |
class AdminSettings { |
| 20 |
/** |
| 21 |
* Singleton Trait. |
| 22 |
*/ |
| 23 |
use SingletonTrait; |
| 24 |
|
| 25 |
/** |
| 26 |
* Construct function |
| 27 |
*/ |
| 28 |
private function __construct() { |
| 29 |
add_action( 'wp_ajax_rtsb_save_settings_data', [ $this, 'save_settings_data' ] ); |
| 30 |
add_action( 'wp_ajax_rtsb_toggle_modules_activation', [ $this, 'toggle_modules' ] ); |
| 31 |
add_action( 'wp_ajax_rtsb_get_multiselect_data', [ $this, 'get_multiselect_data' ] ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get settings fields |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
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 |
| 46 |
$decodedString = stripslashes( $func_with_param ); |
| 47 |
$functionArray = json_decode( $decodedString, true ); |
| 48 |
$className = $functionArray[0]; |
| 49 |
$methodName = $functionArray[1]; |
| 50 |
$argument = $functionArray[2] ?? []; |
| 51 |
$result = []; |
| 52 |
$params = []; |
| 53 |
if ( class_exists( $className ) && method_exists( $className, $methodName ) ) { |
| 54 |
$params[] = $s; |
| 55 |
$params[] = $argument; |
| 56 |
$result = call_user_func_array( [ $className, $methodName ], $params ); |
| 57 |
} |
| 58 |
wp_send_json_success( $result ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get settings Data |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function get_settings_data() { |
| 67 |
$data = Settings::instance()->get_data(); |
| 68 |
wp_send_json_success( $data ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Save settings data |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
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 |
$section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : ''; |
| 82 |
$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 |
|
| 103 |
if ( boolval( $status['status'] ) ) { |
| 104 |
do_action( 'rtsb/after/saved/settings/success/' . $section_id . '/' . $block_id, $rawOptions ); |
| 105 |
do_action( 'rtsb/after/saved/settings/success', $section_id, $block_id, $rawOptions ); |
| 106 |
|
| 107 |
Cache::clear_asset_cache(); |
| 108 |
|
| 109 |
wp_send_json_success( $status ); |
| 110 |
} else { |
| 111 |
wp_send_json_error( $status ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Toggle modules |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
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 |
$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; |
| 133 |
|
| 134 |
if ( ! $section_id || empty( $module_ids ) ) { |
| 135 |
wp_send_json_error( |
| 136 |
[ |
| 137 |
'message' => esc_html__( 'Section , block or options may be empty', 'shopbuilder' ), |
| 138 |
] |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
$sections = Settings::instance()->get_sections(); |
| 143 |
if ( empty( $sections[ $section_id ] ) ) { |
| 144 |
wp_send_json_error( |
| 145 |
[ |
| 146 |
'message' => esc_html__( 'No section found with given data', 'shopbuilder' ), |
| 147 |
] |
| 148 |
); |
| 149 |
} |
| 150 |
$options = DataModel::source()->get_option( $section_id, [] ); |
| 151 |
foreach ( $module_ids as $module_id ) { |
| 152 |
if ( isset( $sections[ $section_id ]['list'][ $module_id ] ) ) { |
| 153 |
if ( isset( $sections[ $section_id ]['list'][ $module_id ]['package'] ) && 'pro-disabled' === $sections[ $section_id ]['list'][ $module_id ]['package'] ) { |
| 154 |
continue; |
| 155 |
} |
| 156 |
if ( 'active' === $type ) { |
| 157 |
$options[ $module_id ]['active'] = 'on'; |
| 158 |
$sections[ $section_id ]['list'][ $module_id ]['active'] = 'on'; |
| 159 |
} else { |
| 160 |
$options[ $module_id ]['active'] = ''; |
| 161 |
$sections[ $section_id ]['list'][ $module_id ]['active'] = ''; |
| 162 |
} |
| 163 |
} |
| 164 |
do_action( 'rtsb/before/save/options', $section_id, $module_id, $options[ $module_id ] ?? [] ); |
| 165 |
} |
| 166 |
|
| 167 |
DataModel::source()->set_option( $section_id, $options ); |
| 168 |
Cache::clear_asset_cache(); |
| 169 |
|
| 170 |
wp_send_json_success( |
| 171 |
[ |
| 172 |
'message' => esc_html__( 'Successfully Saved', 'shopbuilder' ), |
| 173 |
'sections' => $sections, |
| 174 |
] |
| 175 |
); |
| 176 |
} |
| 177 |
} |
| 178 |
|