| 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 |
|
| 34 |
/** |
| 35 |
* Get settings fields |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function get_settings_fields() { |
| 40 |
$data = Settings::instance()->get_fields(); |
| 41 |
wp_send_json_success( $data ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get settings Data |
| 46 |
* |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function get_settings_data() { |
| 50 |
$data = Settings::instance()->get_data(); |
| 51 |
wp_send_json_success( $data ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Save settings data |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function save_settings_data() { |
| 60 |
$section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : ''; |
| 61 |
$block_id = isset( $_POST['block_id'] ) ? sanitize_text_field( wp_unslash( $_POST['block_id'] ) ) : ''; |
| 62 |
$rawOptions = isset( $_POST['options'] ) ? $_POST['options'] : []; |
| 63 |
$status = Fns::set_options( $section_id, $block_id, $rawOptions ); |
| 64 |
if( boolval( $status['status'] ) ){ |
| 65 |
wp_send_json_success ( $status ); |
| 66 |
} else { |
| 67 |
wp_send_json_error( $status ); |
| 68 |
} |
| 69 |
|
| 70 |
/* |
| 71 |
if ( ! $section_id || ! $block_id || empty( $rawOptions ) || ! is_array( $rawOptions ) ) { |
| 72 |
wp_send_json_error( |
| 73 |
[ |
| 74 |
'message' => esc_html__( 'Section , block or options may be empty', 'shopbuilder' ) |
| 75 |
] |
| 76 |
); |
| 77 |
} |
| 78 |
$sections = Settings::instance()->get_sections(); |
| 79 |
if ( empty( $sections[ $section_id ] ) || empty( $sections[ $section_id ]['list'][ $block_id ] ) ) { |
| 80 |
wp_send_json_error( |
| 81 |
[ |
| 82 |
'message' => esc_html__( 'No section or block found with given data', 'shopbuilder' ) |
| 83 |
] |
| 84 |
); |
| 85 |
} |
| 86 |
$options = DataModel::source()->get_option( $section_id, [] ); |
| 87 |
$changed = false; |
| 88 |
$fields = []; |
| 89 |
if ( isset( $sections[ $section_id ]['list'][ $block_id ]['fields'] ) && ! empty( $sections[ $section_id ]['list'][ $block_id ]['fields'] ) ) { |
| 90 |
$fields = $sections[ $section_id ]['list'][ $block_id ]['fields']; |
| 91 |
} |
| 92 |
|
| 93 |
if ( empty( $fields ) ) { |
| 94 |
if ( isset( $rawOptions['active'] ) ) { |
| 95 |
$changed = true; |
| 96 |
$options[ $block_id ]['active'] = 'on' === $rawOptions['active'] ? 'on' : ''; |
| 97 |
} |
| 98 |
} else { |
| 99 |
foreach ( $rawOptions as $raw_option_key => $raw_value ) { |
| 100 |
if ( $raw_option_key === 'active' ) { |
| 101 |
$changed = true; |
| 102 |
$options[ $block_id ]['active'] = $sections[ $section_id ]['list'][ $block_id ]['active'] = 'on' === $raw_value ? 'on' : ''; |
| 103 |
} else { |
| 104 |
if ( isset( $fields[ $raw_option_key ] ) ) { |
| 105 |
$field = $fields[ $raw_option_key ]; |
| 106 |
if ( $field['type'] === 'switch' ) { |
| 107 |
$value = 'on' === $raw_value ? 'on' : ''; |
| 108 |
} else { |
| 109 |
if ( ! empty( $field['multiple'] ) || 'checkbox' === $field['type'] ) { |
| 110 |
if ( isset( $raw_value ) && is_array( $raw_value ) ) { |
| 111 |
if ( ! empty( $field['sanitize_fn'] ) && is_callable( $field['sanitize_fn'] ) ) { |
| 112 |
$value = array_map( $field['sanitize_fn'], $raw_value ); |
| 113 |
} else { |
| 114 |
$value = array_map( 'sanitize_text_field', $raw_value ); |
| 115 |
} |
| 116 |
} else { |
| 117 |
$value = []; |
| 118 |
} |
| 119 |
} else { |
| 120 |
if ( ! empty( $field['sanitize_fn'] ) && is_callable( $field['sanitize_fn'] ) ) { |
| 121 |
$value = $field['sanitize_fn']( $raw_value ); |
| 122 |
} else { |
| 123 |
$value = sanitize_text_field( $raw_value ); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
$options[ $block_id ][ $raw_option_key ] = $sections[ $section_id ]['list'][ $block_id ]['fields'][ $raw_option_key ]['value'] = $value; |
| 128 |
$changed = true; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
if ( ! $changed ) { |
| 134 |
wp_send_json_error( |
| 135 |
[ |
| 136 |
'message' => esc_html__( 'No changes found for update', 'shopbuilder' ) , |
| 137 |
] |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
DataModel::source()->set_option( $section_id, $options ); |
| 142 |
|
| 143 |
wp_send_json_success( |
| 144 |
[ |
| 145 |
'message' => esc_html__( 'Successfully Saved', 'shopbuilder' ), |
| 146 |
'sections' => $sections, |
| 147 |
] |
| 148 |
); |
| 149 |
*/ |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Toggle modules |
| 154 |
* |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
public function toggle_modules() { |
| 158 |
$section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : ''; |
| 159 |
$module_ids = isset( $_POST['module_ids'] ) ? array_map( 'sanitize_text_field', $_POST['module_ids'] ) : []; |
| 160 |
$type = isset( $_POST['type'] ) && $_POST['type'] === 'active' ? 'active' : false; |
| 161 |
// error_log(print_r($module_ids, true)) |
| 162 |
|
| 163 |
if ( ! $section_id || empty( $module_ids ) ) { |
| 164 |
wp_send_json_error( |
| 165 |
[ |
| 166 |
'message' => esc_html__( 'Section , block or options may be empty', 'shopbuilder' ) |
| 167 |
] |
| 168 |
); |
| 169 |
} |
| 170 |
$sections = Settings::instance()->get_sections(); |
| 171 |
if ( empty( $sections[ $section_id ] ) ) { |
| 172 |
wp_send_json_error( |
| 173 |
[ |
| 174 |
'message' => esc_html__( 'No section found with given data', 'shopbuilder' ) |
| 175 |
] |
| 176 |
); |
| 177 |
} |
| 178 |
$options = DataModel::source()->get_option( $section_id, [] ); |
| 179 |
foreach ( $module_ids as $module_id ) { |
| 180 |
if ( isset( $sections[ $section_id ]['list'][ $module_id ] ) ) { |
| 181 |
if ( isset( $sections[ $section_id ]['list'][ $module_id ]['package'] ) && 'pro-disabled' === $sections[ $section_id ]['list'][ $module_id ]['package'] ) { |
| 182 |
continue; |
| 183 |
} |
| 184 |
if ( $type === 'active' ) { |
| 185 |
$options[ $module_id ]['active'] = 'on'; |
| 186 |
$sections[ $section_id ]['list'][ $module_id ]['active'] = 'on'; |
| 187 |
} else { |
| 188 |
$options[ $module_id ]['active'] = ''; |
| 189 |
$sections[ $section_id ]['list'][ $module_id ]['active'] = ''; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
DataModel::source()->set_option( $section_id, $options ); |
| 195 |
|
| 196 |
wp_send_json_success( |
| 197 |
[ |
| 198 |
'message' => esc_html__( 'Successfully Saved', 'shopbuilder' ), |
| 199 |
'sections' => $sections, |
| 200 |
] |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
} |
| 206 |
|