Changeset 39030
- Timestamp:
- 10/30/2016 06:00:24 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php
r38982 r39030 1 1 <?php 2 3 2 /** 4 * Manage a WordPress site's settings. 3 * REST API: WP_REST_Settings_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core class used to manage a site's settings via the REST API. 12 * 13 * @since 4.7.0 14 * 15 * @see WP_REST_Controller 5 16 */ 6 17 class WP_REST_Settings_Controller extends WP_REST_Controller { … … 18 29 19 30 /** 20 * Register the routes for the objects of the controller. 31 * Registers the routes for the objects of the controller. 32 * 33 * @since 4.7.0 34 * @access public 35 * 36 * @see register_rest_route() 21 37 */ 22 38 public function register_routes() { 39 23 40 register_rest_route( $this->namespace, '/' . $this->rest_base, array( 24 41 array( … … 36 53 'schema' => array( $this, 'get_public_item_schema' ), 37 54 ) ); 38 } 39 40 /** 41 * Check if a given request has access to read and manage settings. 42 * 43 * @param WP_REST_Request $request Full details about the request. 44 * @return boolean 55 56 } 57 58 /** 59 * Checks if a given request has access to read and manage settings. 60 * 61 * @since 4.7.0 62 * @access public 63 * 64 * @param WP_REST_Request $request Full details about the request. 65 * @return bool True if the request has read access for the item, otherwise false. 45 66 */ 46 67 public function get_item_permissions_check( $request ) { … … 49 70 50 71 /** 51 * Get the settings. 72 * Retrieves the settings. 73 * 74 * @since 4.7.0 75 * @access public 52 76 * 53 77 * @param WP_REST_Request $request Full details about the request. 54 * @return WP_Error|array78 * @return array|WP_Error Array on success, or WP_Error object on failure. 55 79 */ 56 80 public function get_item( $request ) { … … 67 91 * @since 4.7.0 68 92 * 69 * @param mixed $result 70 * 71 * follow the default `get_option`behavior.72 * @param string $name 73 * @param array $args Arguments passed to `register_setting()`for this setting.93 * @param mixed $result Value to use for the requested setting. Can be a scalar 94 * matching the registered schema for the setting, or null to 95 * follow the default get_option() behavior. 96 * @param string $name Setting name (as shown in REST API responses). 97 * @param array $args Arguments passed to register_setting() for this setting. 74 98 */ 75 99 $response[ $name ] = apply_filters( 'rest_pre_get_setting', null, $name, $args ); … … 80 104 } 81 105 82 // Because get_option() is lossy, we have to 83 // cast values to the type they are registered with. 106 /* 107 * Because get_option() is lossy, we have to 108 * cast values to the type they are registered with. 109 */ 84 110 $response[ $name ] = $this->prepare_value( $response[ $name ], $args['schema'] ); 85 111 } … … 89 115 90 116 /** 91 * Prepare a value for output based off a schema array. 92 * 93 * @param mixed $value 94 * @param array $schema 95 * @return mixed 117 * Prepares a value for output based off a schema array. 118 * 119 * @since 4.7.0 120 * @access protected 121 * 122 * @param mixed $value Value to prepare. 123 * @param array $schema Schema to match. 124 * @return mixed The prepared value. 96 125 */ 97 126 protected function prepare_value( $value, $schema ) { 98 // If the value is not a scalar, it's not possible to cast it to 99 // anything. 127 // If the value is not a scalar, it's not possible to cast it to anything. 100 128 if ( ! is_scalar( $value ) ) { 101 129 return null; … … 115 143 116 144 /** 117 * Update settings for the settings object. 118 * 119 * @param WP_REST_Request $request Full detail about the request. 120 * @return WP_Error|array 145 * Updates settings for the settings object. 146 * 147 * @since 4.7.0 148 * @access public 149 * 150 * @param WP_REST_Request $request Full details about the request. 151 * @return array|WP_Error Array on success, or error object on failure. 121 152 */ 122 153 public function update_item( $request ) { 123 154 $options = $this->get_registered_options(); 124 $params = $request->get_params();155 $params = $request->get_params(); 125 156 126 157 foreach ( $options as $name => $args ) { … … 132 163 * Filters whether to preempt a setting value update. 133 164 * 134 * Allow hijacking the setting update logic and overriding the built-in behavior by165 * Allows hijacking the setting update logic and overriding the built-in behavior by 135 166 * returning true. 136 167 * 137 168 * @since 4.7.0 138 169 * 139 * @param bool ean$result Whether to override the default behavior for updating the140 * 141 * @param string 142 * @param mixed 143 * @param array $args Arguments passed to `register_setting()`for this setting.170 * @param bool $result Whether to override the default behavior for updating the 171 * value of a setting. 172 * @param string $name Setting name (as shown in REST API responses). 173 * @param mixed $value Updated setting value. 174 * @param array $args Arguments passed to register_setting() for this setting. 144 175 */ 145 176 $updated = apply_filters( 'rest_pre_update_setting', false, $name, $request[ $name ], $args ); 177 146 178 if ( $updated ) { 147 179 continue; 148 180 } 149 181 150 /* *151 * A `null`value for an option would have the same effect as152 * deleting the option from the database, and relying on the153 * default value.154 */182 /* 183 * A null value for an option would have the same effect as 184 * deleting the option from the database, and relying on the 185 * default value. 186 */ 155 187 if ( is_null( $request[ $name ] ) ) { 156 /* *157 * A `null`value is returned in the response for any option188 /* 189 * A null value is returned in the response for any option 158 190 * that has a non-scalar value. 159 191 * 160 * To protect clients from accidentally including the `null`192 * To protect clients from accidentally including the null 161 193 * values from a response object in a request, we do not allow 162 * options with non-scalar values to be updated to `null`.194 * options with non-scalar values to be updated to null. 163 195 * Without this added protection a client could mistakenly 164 196 * delete all options that have non-scalar values from the … … 181 213 182 214 /** 183 * Get all the registered options for the Settings API 184 * 185 * @return array 215 * Retrieves all of the registered options for the Settings API. 216 * 217 * @since 4.7.0 218 * @access protected 219 * 220 * @return array Array of registered options. 186 221 */ 187 222 protected function get_registered_options() { … … 194 229 195 230 $rest_args = array(); 231 196 232 if ( is_array( $args['show_in_rest'] ) ) { 197 233 $rest_args = $args['show_in_rest']; … … 202 238 'schema' => array(), 203 239 ); 240 204 241 $rest_args = array_merge( $defaults, $rest_args ); 205 242 … … 218 255 } 219 256 220 // Whitelist the supported types for settings, as we don't want invalid types 221 // to be updated with arbitrary values that we can't do decent sanitizing for. 257 /* 258 * Whitelist the supported types for settings, as we don't want invalid types 259 * to be updated with arbitrary values that we can't do decent sanitizing for. 260 */ 222 261 if ( ! in_array( $rest_args['schema']['type'], array( 'number', 'string', 'boolean' ), true ) ) { 223 262 continue; … … 231 270 232 271 /** 233 * Get the site setting schema, conforming to JSON Schema. 234 * 235 * @return array 272 * Retrieves the site setting schema, conforming to JSON Schema. 273 * 274 * @since 4.7.0 275 * @access public 276 * 277 * @return array Item schema data. 236 278 */ 237 279 public function get_item_schema() {
Note: See TracChangeset
for help on using the changeset viewer.