Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-inclu.../rest-api/endpoint...
File: class-wp-rest-settings-controller.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* REST API: WP_REST_Settings_Controller class
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage REST_API
[5] Fix | Delete
* @since 4.7.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Core class used to manage a site's settings via the REST API.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 4.7.0
[12] Fix | Delete
*
[13] Fix | Delete
* @see WP_REST_Controller
[14] Fix | Delete
*/
[15] Fix | Delete
class WP_REST_Settings_Controller extends WP_REST_Controller {
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Constructor.
[19] Fix | Delete
*
[20] Fix | Delete
* @since 4.7.0
[21] Fix | Delete
*/
[22] Fix | Delete
public function __construct() {
[23] Fix | Delete
$this->namespace = 'wp/v2';
[24] Fix | Delete
$this->rest_base = 'settings';
[25] Fix | Delete
}
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Registers the routes for the site's settings.
[29] Fix | Delete
*
[30] Fix | Delete
* @since 4.7.0
[31] Fix | Delete
*
[32] Fix | Delete
* @see register_rest_route()
[33] Fix | Delete
*/
[34] Fix | Delete
public function register_routes() {
[35] Fix | Delete
[36] Fix | Delete
register_rest_route(
[37] Fix | Delete
$this->namespace,
[38] Fix | Delete
'/' . $this->rest_base,
[39] Fix | Delete
array(
[40] Fix | Delete
array(
[41] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[42] Fix | Delete
'callback' => array( $this, 'get_item' ),
[43] Fix | Delete
'args' => array(),
[44] Fix | Delete
'permission_callback' => array( $this, 'get_item_permissions_check' ),
[45] Fix | Delete
),
[46] Fix | Delete
array(
[47] Fix | Delete
'methods' => WP_REST_Server::EDITABLE,
[48] Fix | Delete
'callback' => array( $this, 'update_item' ),
[49] Fix | Delete
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
[50] Fix | Delete
'permission_callback' => array( $this, 'get_item_permissions_check' ),
[51] Fix | Delete
),
[52] Fix | Delete
'schema' => array( $this, 'get_public_item_schema' ),
[53] Fix | Delete
)
[54] Fix | Delete
);
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Checks if a given request has access to read and manage settings.
[59] Fix | Delete
*
[60] Fix | Delete
* @since 4.7.0
[61] Fix | Delete
*
[62] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[63] Fix | Delete
* @return bool True if the request has read access for the item, otherwise false.
[64] Fix | Delete
*/
[65] Fix | Delete
public function get_item_permissions_check( $request ) {
[66] Fix | Delete
return current_user_can( 'manage_options' );
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Retrieves the settings.
[71] Fix | Delete
*
[72] Fix | Delete
* @since 4.7.0
[73] Fix | Delete
*
[74] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[75] Fix | Delete
* @return array|WP_Error Array on success, or WP_Error object on failure.
[76] Fix | Delete
*/
[77] Fix | Delete
public function get_item( $request ) {
[78] Fix | Delete
$options = $this->get_registered_options();
[79] Fix | Delete
$response = array();
[80] Fix | Delete
[81] Fix | Delete
foreach ( $options as $name => $args ) {
[82] Fix | Delete
/**
[83] Fix | Delete
* Filters the value of a setting recognized by the REST API.
[84] Fix | Delete
*
[85] Fix | Delete
* Allow hijacking the setting value and overriding the built-in behavior by returning a
[86] Fix | Delete
* non-null value. The returned value will be presented as the setting value instead.
[87] Fix | Delete
*
[88] Fix | Delete
* @since 4.7.0
[89] Fix | Delete
*
[90] Fix | Delete
* @param mixed $result Value to use for the requested setting. Can be a scalar
[91] Fix | Delete
* matching the registered schema for the setting, or null to
[92] Fix | Delete
* follow the default get_option() behavior.
[93] Fix | Delete
* @param string $name Setting name (as shown in REST API responses).
[94] Fix | Delete
* @param array $args Arguments passed to register_setting() for this setting.
[95] Fix | Delete
*/
[96] Fix | Delete
$response[ $name ] = apply_filters( 'rest_pre_get_setting', null, $name, $args );
[97] Fix | Delete
[98] Fix | Delete
if ( is_null( $response[ $name ] ) ) {
[99] Fix | Delete
// Default to a null value as "null" in the response means "not set".
[100] Fix | Delete
$response[ $name ] = get_option( $args['option_name'], $args['schema']['default'] );
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
/*
[104] Fix | Delete
* Because get_option() is lossy, we have to
[105] Fix | Delete
* cast values to the type they are registered with.
[106] Fix | Delete
*/
[107] Fix | Delete
$response[ $name ] = $this->prepare_value( $response[ $name ], $args['schema'] );
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
return $response;
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
/**
[114] Fix | Delete
* Prepares a value for output based off a schema array.
[115] Fix | Delete
*
[116] Fix | Delete
* @since 4.7.0
[117] Fix | Delete
*
[118] Fix | Delete
* @param mixed $value Value to prepare.
[119] Fix | Delete
* @param array $schema Schema to match.
[120] Fix | Delete
* @return mixed The prepared value.
[121] Fix | Delete
*/
[122] Fix | Delete
protected function prepare_value( $value, $schema ) {
[123] Fix | Delete
/*
[124] Fix | Delete
* If the value is not valid by the schema, set the value to null.
[125] Fix | Delete
* Null values are specifically non-destructive, so this will not cause
[126] Fix | Delete
* overwriting the current invalid value to null.
[127] Fix | Delete
*/
[128] Fix | Delete
if ( is_wp_error( rest_validate_value_from_schema( $value, $schema ) ) ) {
[129] Fix | Delete
return null;
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
return rest_sanitize_value_from_schema( $value, $schema );
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* Updates settings for the settings object.
[137] Fix | Delete
*
[138] Fix | Delete
* @since 4.7.0
[139] Fix | Delete
*
[140] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[141] Fix | Delete
* @return array|WP_Error Array on success, or error object on failure.
[142] Fix | Delete
*/
[143] Fix | Delete
public function update_item( $request ) {
[144] Fix | Delete
$options = $this->get_registered_options();
[145] Fix | Delete
[146] Fix | Delete
$params = $request->get_params();
[147] Fix | Delete
[148] Fix | Delete
foreach ( $options as $name => $args ) {
[149] Fix | Delete
if ( ! array_key_exists( $name, $params ) ) {
[150] Fix | Delete
continue;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Filters whether to preempt a setting value update via the REST API.
[155] Fix | Delete
*
[156] Fix | Delete
* Allows hijacking the setting update logic and overriding the built-in behavior by
[157] Fix | Delete
* returning true.
[158] Fix | Delete
*
[159] Fix | Delete
* @since 4.7.0
[160] Fix | Delete
*
[161] Fix | Delete
* @param bool $result Whether to override the default behavior for updating the
[162] Fix | Delete
* value of a setting.
[163] Fix | Delete
* @param string $name Setting name (as shown in REST API responses).
[164] Fix | Delete
* @param mixed $value Updated setting value.
[165] Fix | Delete
* @param array $args Arguments passed to register_setting() for this setting.
[166] Fix | Delete
*/
[167] Fix | Delete
$updated = apply_filters( 'rest_pre_update_setting', false, $name, $request[ $name ], $args );
[168] Fix | Delete
[169] Fix | Delete
if ( $updated ) {
[170] Fix | Delete
continue;
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/*
[174] Fix | Delete
* A null value for an option would have the same effect as
[175] Fix | Delete
* deleting the option from the database, and relying on the
[176] Fix | Delete
* default value.
[177] Fix | Delete
*/
[178] Fix | Delete
if ( is_null( $request[ $name ] ) ) {
[179] Fix | Delete
/*
[180] Fix | Delete
* A null value is returned in the response for any option
[181] Fix | Delete
* that has a non-scalar value.
[182] Fix | Delete
*
[183] Fix | Delete
* To protect clients from accidentally including the null
[184] Fix | Delete
* values from a response object in a request, we do not allow
[185] Fix | Delete
* options with values that don't pass validation to be updated to null.
[186] Fix | Delete
* Without this added protection a client could mistakenly
[187] Fix | Delete
* delete all options that have invalid values from the
[188] Fix | Delete
* database.
[189] Fix | Delete
*/
[190] Fix | Delete
if ( is_wp_error( rest_validate_value_from_schema( get_option( $args['option_name'], false ), $args['schema'] ) ) ) {
[191] Fix | Delete
return new WP_Error(
[192] Fix | Delete
'rest_invalid_stored_value',
[193] Fix | Delete
/* translators: %s: Property name. */
[194] Fix | Delete
sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' ), $name ),
[195] Fix | Delete
array( 'status' => 500 )
[196] Fix | Delete
);
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
delete_option( $args['option_name'] );
[200] Fix | Delete
} else {
[201] Fix | Delete
update_option( $args['option_name'], $request[ $name ] );
[202] Fix | Delete
}
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
return $this->get_item( $request );
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
/**
[209] Fix | Delete
* Retrieves all of the registered options for the Settings API.
[210] Fix | Delete
*
[211] Fix | Delete
* @since 4.7.0
[212] Fix | Delete
*
[213] Fix | Delete
* @return array Array of registered options.
[214] Fix | Delete
*/
[215] Fix | Delete
protected function get_registered_options() {
[216] Fix | Delete
$rest_options = array();
[217] Fix | Delete
[218] Fix | Delete
foreach ( get_registered_settings() as $name => $args ) {
[219] Fix | Delete
if ( empty( $args['show_in_rest'] ) ) {
[220] Fix | Delete
continue;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
$rest_args = array();
[224] Fix | Delete
[225] Fix | Delete
if ( is_array( $args['show_in_rest'] ) ) {
[226] Fix | Delete
$rest_args = $args['show_in_rest'];
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
$defaults = array(
[230] Fix | Delete
'name' => ! empty( $rest_args['name'] ) ? $rest_args['name'] : $name,
[231] Fix | Delete
'schema' => array(),
[232] Fix | Delete
);
[233] Fix | Delete
[234] Fix | Delete
$rest_args = array_merge( $defaults, $rest_args );
[235] Fix | Delete
[236] Fix | Delete
$default_schema = array(
[237] Fix | Delete
'type' => empty( $args['type'] ) ? null : $args['type'],
[238] Fix | Delete
'title' => empty( $args['label'] ) ? '' : $args['label'],
[239] Fix | Delete
'description' => empty( $args['description'] ) ? '' : $args['description'],
[240] Fix | Delete
'default' => isset( $args['default'] ) ? $args['default'] : null,
[241] Fix | Delete
);
[242] Fix | Delete
[243] Fix | Delete
$rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] );
[244] Fix | Delete
$rest_args['option_name'] = $name;
[245] Fix | Delete
[246] Fix | Delete
// Skip over settings that don't have a defined type in the schema.
[247] Fix | Delete
if ( empty( $rest_args['schema']['type'] ) ) {
[248] Fix | Delete
continue;
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
/*
[252] Fix | Delete
* Allow the supported types for settings, as we don't want invalid types
[253] Fix | Delete
* to be updated with arbitrary values that we can't do decent sanitizing for.
[254] Fix | Delete
*/
[255] Fix | Delete
if ( ! in_array( $rest_args['schema']['type'], array( 'number', 'integer', 'string', 'boolean', 'array', 'object' ), true ) ) {
[256] Fix | Delete
continue;
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
$rest_args['schema'] = rest_default_additional_properties_to_false( $rest_args['schema'] );
[260] Fix | Delete
[261] Fix | Delete
$rest_options[ $rest_args['name'] ] = $rest_args;
[262] Fix | Delete
}
[263] Fix | Delete
[264] Fix | Delete
return $rest_options;
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
/**
[268] Fix | Delete
* Retrieves the site setting schema, conforming to JSON Schema.
[269] Fix | Delete
*
[270] Fix | Delete
* @since 4.7.0
[271] Fix | Delete
*
[272] Fix | Delete
* @return array Item schema data.
[273] Fix | Delete
*/
[274] Fix | Delete
public function get_item_schema() {
[275] Fix | Delete
if ( $this->schema ) {
[276] Fix | Delete
return $this->add_additional_fields_schema( $this->schema );
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
$options = $this->get_registered_options();
[280] Fix | Delete
[281] Fix | Delete
$schema = array(
[282] Fix | Delete
'$schema' => 'http://json-schema.org/draft-04/schema#',
[283] Fix | Delete
'title' => 'settings',
[284] Fix | Delete
'type' => 'object',
[285] Fix | Delete
'properties' => array(),
[286] Fix | Delete
);
[287] Fix | Delete
[288] Fix | Delete
foreach ( $options as $option_name => $option ) {
[289] Fix | Delete
$schema['properties'][ $option_name ] = $option['schema'];
[290] Fix | Delete
$schema['properties'][ $option_name ]['arg_options'] = array(
[291] Fix | Delete
'sanitize_callback' => array( $this, 'sanitize_callback' ),
[292] Fix | Delete
);
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
$this->schema = $schema;
[296] Fix | Delete
[297] Fix | Delete
return $this->add_additional_fields_schema( $this->schema );
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
/**
[301] Fix | Delete
* Custom sanitize callback used for all options to allow the use of 'null'.
[302] Fix | Delete
*
[303] Fix | Delete
* By default, the schema of settings will throw an error if a value is set to
[304] Fix | Delete
* `null` as it's not a valid value for something like "type => string". We
[305] Fix | Delete
* provide a wrapper sanitizer to allow the use of `null`.
[306] Fix | Delete
*
[307] Fix | Delete
* @since 4.7.0
[308] Fix | Delete
*
[309] Fix | Delete
* @param mixed $value The value for the setting.
[310] Fix | Delete
* @param WP_REST_Request $request The request object.
[311] Fix | Delete
* @param string $param The parameter name.
[312] Fix | Delete
* @return mixed|WP_Error
[313] Fix | Delete
*/
[314] Fix | Delete
public function sanitize_callback( $value, $request, $param ) {
[315] Fix | Delete
if ( is_null( $value ) ) {
[316] Fix | Delete
return $value;
[317] Fix | Delete
}
[318] Fix | Delete
[319] Fix | Delete
return rest_parse_request_arg( $value, $request, $param );
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
/**
[323] Fix | Delete
* Recursively add additionalProperties = false to all objects in a schema
[324] Fix | Delete
* if no additionalProperties setting is specified.
[325] Fix | Delete
*
[326] Fix | Delete
* This is needed to restrict properties of objects in settings values to only
[327] Fix | Delete
* registered items, as the REST API will allow additional properties by
[328] Fix | Delete
* default.
[329] Fix | Delete
*
[330] Fix | Delete
* @since 4.9.0
[331] Fix | Delete
* @deprecated 6.1.0 Use {@see rest_default_additional_properties_to_false()} instead.
[332] Fix | Delete
*
[333] Fix | Delete
* @param array $schema The schema array.
[334] Fix | Delete
* @return array
[335] Fix | Delete
*/
[336] Fix | Delete
protected function set_additional_properties_to_false( $schema ) {
[337] Fix | Delete
_deprecated_function( __METHOD__, '6.1.0', 'rest_default_additional_properties_to_false()' );
[338] Fix | Delete
[339] Fix | Delete
return rest_default_additional_properties_to_false( $schema );
[340] Fix | Delete
}
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function