| 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 |
|
| 72 |
/** |
| 73 |
* Toggle modules |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function toggle_modules() { |
| 78 |
$section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : ''; |
| 79 |
$module_ids = isset( $_POST['module_ids'] ) ? array_map( 'sanitize_text_field', $_POST['module_ids'] ) : []; |
| 80 |
$type = isset( $_POST['type'] ) && $_POST['type'] === 'active' ? 'active' : false; |
| 81 |
// error_log(print_r($module_ids, true)) |
| 82 |
|
| 83 |
if ( ! $section_id || empty( $module_ids ) ) { |
| 84 |
wp_send_json_error( |
| 85 |
[ |
| 86 |
'message' => esc_html__( 'Section , block or options may be empty', 'shopbuilder' ) |
| 87 |
] |
| 88 |
); |
| 89 |
} |
| 90 |
$sections = Settings::instance()->get_sections(); |
| 91 |
if ( empty( $sections[ $section_id ] ) ) { |
| 92 |
wp_send_json_error( |
| 93 |
[ |
| 94 |
'message' => esc_html__( 'No section found with given data', 'shopbuilder' ) |
| 95 |
] |
| 96 |
); |
| 97 |
} |
| 98 |
$options = DataModel::source()->get_option( $section_id, [] ); |
| 99 |
foreach ( $module_ids as $module_id ) { |
| 100 |
if ( isset( $sections[ $section_id ]['list'][ $module_id ] ) ) { |
| 101 |
if ( isset( $sections[ $section_id ]['list'][ $module_id ]['package'] ) && 'pro-disabled' === $sections[ $section_id ]['list'][ $module_id ]['package'] ) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
if ( $type === 'active' ) { |
| 105 |
$options[ $module_id ]['active'] = 'on'; |
| 106 |
$sections[ $section_id ]['list'][ $module_id ]['active'] = 'on'; |
| 107 |
} else { |
| 108 |
$options[ $module_id ]['active'] = ''; |
| 109 |
$sections[ $section_id ]['list'][ $module_id ]['active'] = ''; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
DataModel::source()->set_option( $section_id, $options ); |
| 115 |
|
| 116 |
wp_send_json_success( |
| 117 |
[ |
| 118 |
'message' => esc_html__( 'Successfully Saved', 'shopbuilder' ), |
| 119 |
'sections' => $sections, |
| 120 |
] |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
} |
| 126 |
|