|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + */ |
| 5 | + |
| 6 | +// Exit if accessed directly. |
| 7 | +if ( ! defined( 'ABSPATH' ) ) { |
| 8 | + exit; |
| 9 | +} |
| 10 | + |
| 11 | +if ( ! class_exists( 'Stackable_Admin_Settings' ) ) { |
| 12 | + |
| 13 | + class Stackable_Admin_Settings extends WP_REST_Settings_Controller { |
| 14 | + |
| 15 | + /** |
| 16 | + * Constructor. |
| 17 | + * |
| 18 | + */ |
| 19 | + public function __construct() { |
| 20 | + $this->namespace = 'stackable/v3'; |
| 21 | + $this->rest_base = 'settings'; |
| 22 | + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 23 | + } |
| 24 | + |
| 25 | + public function get_item_permissions_check( $request ) { |
| 26 | + return current_user_can( 'edit_posts' ); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Updates settings for the settings object. |
| 31 | + */ |
| 32 | + public function update_item( $request ) { |
| 33 | + |
| 34 | + if ( $request->has_param( '_locale' ) ) { |
| 35 | + unset( $request[ '_locale' ] ); |
| 36 | + } |
| 37 | + |
| 38 | + $params = $request->get_params(); |
| 39 | + |
| 40 | + return parent::update_item( $request ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Retrieves all of the registered stackable options. |
| 45 | + * |
| 46 | + * @return array Array of registered options. |
| 47 | + */ |
| 48 | + protected function get_registered_options() { |
| 49 | + $rest_options = array(); |
| 50 | + |
| 51 | + foreach ( get_registered_settings() as $name => $args ) { |
| 52 | + if ( empty( $args['show_in_rest'] ) ) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + $rest_args = array(); |
| 57 | + |
| 58 | + if ( is_array( $args['show_in_rest'] ) ) { |
| 59 | + $rest_args = $args['show_in_rest']; |
| 60 | + } |
| 61 | + |
| 62 | + $defaults = array( |
| 63 | + 'name' => ! empty( $rest_args['name'] ) ? $rest_args['name'] : $name, |
| 64 | + 'schema' => array(), |
| 65 | + ); |
| 66 | + |
| 67 | + $rest_args = array_merge( $defaults, $rest_args ); |
| 68 | + |
| 69 | + // Skip over settings not from Stackable |
| 70 | + if ( strpos( $rest_args[ 'name' ], 'stackable' ) !== 0 ) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + $default_schema = array( |
| 75 | + 'type' => empty( $args['type'] ) ? null : $args['type'], |
| 76 | + 'title' => empty( $args['label'] ) ? '' : $args['label'], |
| 77 | + 'description' => empty( $args['description'] ) ? '' : $args['description'], |
| 78 | + 'default' => isset( $args['default'] ) ? $args['default'] : null, |
| 79 | + ); |
| 80 | + |
| 81 | + $rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] ); |
| 82 | + $rest_args['option_name'] = $name; |
| 83 | + |
| 84 | + // Skip over settings that don't have a defined type in the schema. |
| 85 | + if ( empty( $rest_args['schema']['type'] ) ) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + /* |
| 90 | + * Allow the supported types for settings, as we don't want invalid types |
| 91 | + * to be updated with arbitrary values that we can't do decent sanitizing for. |
| 92 | + */ |
| 93 | + if ( ! in_array( $rest_args['schema']['type'], array( 'number', 'integer', 'string', 'boolean', 'array', 'object' ), true ) ) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + $rest_args['schema'] = rest_default_additional_properties_to_false( $rest_args['schema'] ); |
| 98 | + |
| 99 | + $rest_options[ $rest_args['name'] ] = $rest_args; |
| 100 | + } |
| 101 | + |
| 102 | + return $rest_options; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + new Stackable_Admin_Settings(); |
| 107 | +} |
0 commit comments