namespace = ACADEMY_PLUGIN_SLUG . '/v1'; $self->rest_base = 'settings'; add_action( 'rest_api_init', array( $self, 'register_routes' ) ); add_action( 'academy/admin/after_save_settings', array( $self, 'after_save_settings' ) ); } /** * Register the routes for the objects of the controller. */ public function register_routes() { $endpoint = '/settings/'; register_rest_route( $this->namespace, $endpoint, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => array(), ), ) ); // Instructors get a fixed allowlist of display settings only. register_rest_route( $this->namespace, '/settings/instructor', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_instructor_items' ), 'permission_callback' => array( $this, 'get_instructor_items_permissions_check' ), 'args' => array(), ), ) ); } /** * Settings an instructor's course, lesson and quiz screens read. Nothing * else from the global settings is ever sent to a non-administrator. * * @return string[] */ public static function instructor_keys() { $keys = array( 'monetization_engine', 'is_instructor_can_publish_course', 'is_instructor_update_course_price', 'allow_instructor_to_use_chatgpt', 'academy_primary_certificate_id', 'is_enabled_course_review', 'is_enabled_course_popup_review', 'is_enabled_course_coming_soon', 'is_expire_course_enrollment', 'is_enabled_lessons_php_render', 'learn_page_engine', ); /** * Display settings an instructor's screens may read. Add-ons can append * their own non-sensitive keys; credentials are removed regardless. * * @param string[] $keys Setting keys. */ return array_values( array_unique( array_map( 'strval', (array) apply_filters( 'academy/api/settings/instructor_keys', $keys ) ) ) ); } public function get_instructor_items( $request ) { $settings = BaseSettings::get_saved_data(); $settings = apply_filters( 'academy/api/settings/get_settings', $settings ); $allowed = array_intersect_key( (array) $settings, array_flip( self::instructor_keys() ) ); return rest_ensure_response( self::remove_secrets( $allowed ) ); } /** * Instructors and administrators. * * @param \WP_REST_Request $request Request. * @return bool */ public function get_instructor_items_permissions_check( $request ) { return current_user_can( 'manage_options' ) || current_user_can( 'manage_academy_instructor' ); } public function get_items( $request ) { $settings = BaseSettings::get_saved_data(); if ( ! isset( $settings['form_builder'] ) ) { $settings['form_builder'] = FormBuilder::get_saved_data(); } $response = apply_filters( 'academy/api/settings/get_settings', $settings ); return rest_ensure_response( $response ); } /** * The full settings are for site administrators only. Instructors use * /settings/instructor, which returns a fixed allowlist. * * @param \WP_REST_Request $request Request. * @return bool */ public function get_items_permissions_check( $request ) { return current_user_can( 'manage_options' ); } /** * Remove every credential-like entry (API keys, secrets, passwords, * tokens, private/license keys) from a settings array, at any depth. * Page selectors such as password_reset_page are kept. * * @param mixed $settings Settings. * @return mixed */ public static function remove_secrets( $settings ) { if ( ! is_array( $settings ) ) { return $settings; } foreach ( $settings as $key => $value ) { $is_secret = is_string( $key ) && preg_match( '/(api_?key|secret|passw(or)?d|token|private_?key|license_?key)/i', $key ) && ! preg_match( '/_page(_id)?$/i', $key ); if ( $is_secret ) { unset( $settings[ $key ] ); } elseif ( is_array( $value ) ) { $settings[ $key ] = self::remove_secrets( $value ); } } return $settings; } public function after_save_settings( $is_update ) { if ( $is_update ) { update_option( 'academy_flash_role_management', true ); } } }