/ wordpress / restai / includes / class-restai-settings.php
class-restai-settings.php
  1  <?php
  2  /**
  3   * Settings screen + Settings API integration.
  4   *
  5   * @package RESTai
  6   */
  7  
  8  namespace RESTai;
  9  
 10  if ( ! defined( 'ABSPATH' ) ) {
 11  	exit;
 12  }
 13  
 14  class Settings {
 15  
 16  	/** @var Client */
 17  	private $client;
 18  
 19  	public function __construct( Client $client ) {
 20  		$this->client = $client;
 21  		add_action( 'admin_menu', array( $this, 'register_menu' ) );
 22  		add_action( 'admin_init', array( $this, 'register_settings' ) );
 23  	}
 24  
 25  	public function register_menu() {
 26  		add_options_page(
 27  			__( 'RESTai', 'restai' ),
 28  			__( 'RESTai', 'restai' ),
 29  			'manage_options',
 30  			'restai',
 31  			array( $this, 'render_settings_page' )
 32  		);
 33  		add_menu_page(
 34  			__( 'RESTai Analytics', 'restai' ),
 35  			__( 'RESTai', 'restai' ),
 36  			'manage_options',
 37  			'restai-analytics',
 38  			array( $this, 'render_analytics_page' ),
 39  			Icon::data_url(),
 40  			76
 41  		);
 42  	}
 43  
 44  	public function register_settings() {
 45  		register_setting(
 46  			'restai_settings_group',
 47  			'restai_settings',
 48  			array(
 49  				'sanitize_callback' => array( $this, 'sanitize_settings' ),
 50  				'default'           => array(),
 51  			)
 52  		);
 53  		register_setting(
 54  			'restai_settings_group',
 55  			'restai_project_map',
 56  			array(
 57  				'default'           => array(),
 58  				'sanitize_callback' => array( $this, 'sanitize_project_map' ),
 59  			)
 60  		);
 61  		register_setting(
 62  			'restai_settings_group',
 63  			'restai_widget_settings',
 64  			array( 'default' => array() )
 65  		);
 66  	}
 67  
 68  	/**
 69  	 * Sanitise the entire settings array.
 70  	 */
 71  	public function sanitize_settings( $input ) {
 72  		$out = array();
 73  
 74  		$out['url']     = isset( $input['url'] ) ? esc_url_raw( trim( $input['url'] ) ) : '';
 75  		$out['api_key'] = isset( $input['api_key'] ) ? sanitize_text_field( $input['api_key'] ) : '';
 76  		$out['team_id'] = isset( $input['team_id'] ) ? absint( $input['team_id'] ) : 0;
 77  		$out['image_generator'] = isset( $input['image_generator'] ) ? sanitize_text_field( $input['image_generator'] ) : '';
 78  
 79  		foreach ( array(
 80  			'enable_widget',
 81  			'enable_search',
 82  			'enable_knowledge',
 83  			'enable_moderation',
 84  			'enable_email_ai',
 85  			'auto_alt_text',
 86  		) as $bool_key ) {
 87  			$out[ $bool_key ] = ! empty( $input[ $bool_key ] );
 88  		}
 89  
 90  		return $out;
 91  	}
 92  
 93  	/**
 94  	 * Sanitise the task -> project_id map.
 95  	 */
 96  	public function sanitize_project_map( $input ) {
 97  		if ( ! is_array( $input ) ) {
 98  			return array();
 99  		}
100  		$out = array();
101  		foreach ( $input as $task => $value ) {
102  			$task_key = sanitize_key( $task );
103  			$id       = absint( $value );
104  			if ( $task_key !== '' && $id > 0 ) {
105  				$out[ $task_key ] = $id;
106  			}
107  		}
108  		return $out;
109  	}
110  
111  	public function render_settings_page() {
112  		if ( ! current_user_can( 'manage_options' ) ) {
113  			wp_die( esc_html__( 'You do not have permission to access this page.', 'restai' ) );
114  		}
115  		require RESTAI_PLUGIN_DIR . 'admin/views/settings.php';
116  	}
117  
118  	public function render_analytics_page() {
119  		if ( ! current_user_can( 'manage_options' ) ) {
120  			wp_die( esc_html__( 'You do not have permission to access this page.', 'restai' ) );
121  		}
122  		require RESTAI_PLUGIN_DIR . 'admin/views/analytics.php';
123  	}
124  }