# shopbuilder/1.0.0/app/Controllers/Admin/Ajax/AdminSettings.php

ShopBuilder – WooCommerce Builder For Elementor, version 1.0.0. 206 lines.

- Page: https://pluginprobe.com/plugins/shopbuilder/1.0.0/code/app/Controllers/Admin/Ajax/AdminSettings.php
- Raw: https://pluginprobe.com/plugins/shopbuilder/1.0.0/raw/app/Controllers/Admin/Ajax/AdminSettings.php
- Modified: 2023-03-09T09:36:06+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/shopbuilder/1.0.0/code/app/Controllers/Admin/Ajax/AdminSettings.php#L10-L20`.

```php
<?php

namespace RadiusTheme\SB\Controllers\Admin\Ajax;

use RadiusTheme\SB\Helpers\Fns;
use RadiusTheme\SB\Models\DataModel;
use RadiusTheme\SB\Models\Settings;
use RadiusTheme\SB\Traits\SingletonTrait;

// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
	exit( 'This script cannot be accessed directly.' );
}

/**
 * Settings Page.
 */
class AdminSettings {
	/**
	 * Singleton Trait.
	 */
	use SingletonTrait;

	/**
	 * Construct function
	 */
	private function __construct() {
		add_action( 'wp_ajax_rtsb_settings_fields', [ $this, 'get_settings_fields' ] );
		add_action( 'wp_ajax_rtsb_settings_data', [ $this, 'get_settings_data' ] );
		add_action( 'wp_ajax_rtsb_save_settings_data', [ $this, 'save_settings_data' ] );
		add_action( 'wp_ajax_rtsb_toggle_modules_activation', [ $this, 'toggle_modules' ] );
	}

	/**
	 * Get settings fields
	 *
	 * @return void
	 */
	public function get_settings_fields() {
		$data = Settings::instance()->get_fields();
		wp_send_json_success( $data );
	}

	/**
	 * Get settings Data
	 *
	 * @return void
	 */
	public function get_settings_data() {
		$data = Settings::instance()->get_data();
		wp_send_json_success( $data );
	}

	/**
	 * Save settings data
	 *
	 * @return void
	 */
	public function save_settings_data() {
		$section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : '';
		$block_id   = isset( $_POST['block_id'] ) ? sanitize_text_field( wp_unslash( $_POST['block_id'] ) ) : '';
		$rawOptions = isset( $_POST['options'] ) ? $_POST['options'] : [];
        $status = Fns::set_options( $section_id, $block_id, $rawOptions );
        if( boolval( $status['status'] ) ){
            wp_send_json_success  ( $status );
        } else {
            wp_send_json_error( $status );
        }

        /*
		if ( ! $section_id || ! $block_id || empty( $rawOptions ) || ! is_array( $rawOptions ) ) {
			wp_send_json_error( 
				[
					'message'  => esc_html__( 'Section , block or options may be empty', 'shopbuilder' ) 
				]
			);
		}
		$sections = Settings::instance()->get_sections();
		if ( empty( $sections[ $section_id ] ) || empty( $sections[ $section_id ]['list'][ $block_id ] ) ) {
			wp_send_json_error( 
				[ 
					'message'  => esc_html__( 'No section or block found with given data', 'shopbuilder' ) 
				]
			);
		}
		$options = DataModel::source()->get_option( $section_id, [] );
		$changed = false;
		$fields  = [];
		if ( isset( $sections[ $section_id ]['list'][ $block_id ]['fields'] ) && ! empty( $sections[ $section_id ]['list'][ $block_id ]['fields'] ) ) {
			$fields = $sections[ $section_id ]['list'][ $block_id ]['fields'];
		}

		if ( empty( $fields ) ) {
			if ( isset( $rawOptions['active'] ) ) {
				$changed                        = true;
				$options[ $block_id ]['active'] = 'on' === $rawOptions['active'] ? 'on' : '';
			}
		} else {
			foreach ( $rawOptions as $raw_option_key => $raw_value ) {
				if ( $raw_option_key === 'active' ) {
					$changed                        = true;
					$options[ $block_id ]['active'] = $sections[ $section_id ]['list'][ $block_id ]['active'] = 'on' === $raw_value ? 'on' : '';
				} else {
					if ( isset( $fields[ $raw_option_key ] ) ) {
						$field = $fields[ $raw_option_key ];
						if ( $field['type'] === 'switch' ) {
							$value = 'on' === $raw_value ? 'on' : '';
						} else {
							if ( ! empty( $field['multiple'] ) || 'checkbox' === $field['type'] ) {
								if ( isset( $raw_value ) && is_array( $raw_value ) ) {
									if ( ! empty( $field['sanitize_fn'] ) && is_callable( $field['sanitize_fn'] ) ) {
										$value = array_map( $field['sanitize_fn'], $raw_value );
									} else {
										$value = array_map( 'sanitize_text_field', $raw_value );
									}
								} else {
									$value = [];
								}
							} else {
								if ( ! empty( $field['sanitize_fn'] ) && is_callable( $field['sanitize_fn'] ) ) {
									$value = $field['sanitize_fn']( $raw_value );
								} else {
									$value = sanitize_text_field( $raw_value );
								}
							}
						}
						$options[ $block_id ][ $raw_option_key ] = $sections[ $section_id ]['list'][ $block_id ]['fields'][ $raw_option_key ]['value'] = $value;
						$changed                                 = true;
					}
				}
			}
		}
		if ( ! $changed ) {
			wp_send_json_error( 
				[
					'message'  => esc_html__( 'No changes found for update', 'shopbuilder' ) ,
				]
			);
		}

		DataModel::source()->set_option( $section_id, $options );

		wp_send_json_success(
			[
				'message'  => esc_html__( 'Successfully Saved', 'shopbuilder' ),
				'sections' => $sections,
			]
		);
        */
	}

	/**
	 * Toggle modules
	 *
	 * @return void
	 */
	public function toggle_modules() {
		$section_id = isset( $_POST['section_id'] ) ? sanitize_text_field( wp_unslash( $_POST['section_id'] ) ) : '';
		$module_ids = isset( $_POST['module_ids'] ) ? array_map( 'sanitize_text_field', $_POST['module_ids'] ) : [];
		$type       = isset( $_POST['type'] ) && $_POST['type'] === 'active' ? 'active' : false;
		// error_log(print_r($module_ids, true))

		if ( ! $section_id || empty( $module_ids ) ) {
			wp_send_json_error( 
				[
					'message'  => esc_html__( 'Section , block or options may be empty', 'shopbuilder' ) 
				]
			);
		}
		$sections = Settings::instance()->get_sections();
		if ( empty( $sections[ $section_id ] ) ) {
			wp_send_json_error( 
				[ 
					'message'  => esc_html__( 'No section found with given data', 'shopbuilder' ) 
				] 
			);
		}
		$options = DataModel::source()->get_option( $section_id, [] );
		foreach ( $module_ids as $module_id ) {
			if ( isset( $sections[ $section_id ]['list'][ $module_id ] ) ) {
				if ( isset( $sections[ $section_id ]['list'][ $module_id ]['package'] ) && 'pro-disabled' === $sections[ $section_id ]['list'][ $module_id ]['package'] ) {
					continue;
				}
				if ( $type === 'active' ) {
					$options[ $module_id ]['active']                         = 'on';
					$sections[ $section_id ]['list'][ $module_id ]['active'] = 'on';
				} else {
					$options[ $module_id ]['active']                         = '';
					$sections[ $section_id ]['list'][ $module_id ]['active'] = '';
				}
			}
		}

		DataModel::source()->set_option( $section_id, $options );

		wp_send_json_success(
			[
				'message'  => esc_html__( 'Successfully Saved', 'shopbuilder' ),
				'sections' => $sections,
			]
		);
	}


}

```
