| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit( 0 ); |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Update option endpoint. |
| 9 |
* |
| 10 |
* @phan-constructor-used-for-side-effects |
| 11 |
*/ |
| 12 |
class WPCOM_JSON_API_Update_Option_Endpoint extends WPCOM_JSON_API_Get_Option_Endpoint { |
| 13 |
/** |
| 14 |
* The option value. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
public $option_value; |
| 19 |
|
| 20 |
/** |
| 21 |
* Endpoint callback. |
| 22 |
* |
| 23 |
* @return array |
| 24 |
*/ |
| 25 |
public function result() { |
| 26 |
if ( $this->site_option ) { |
| 27 |
update_site_option( $this->option_name, $this->option_value ); |
| 28 |
} else { |
| 29 |
update_option( $this->option_name, $this->option_value ); |
| 30 |
} |
| 31 |
return parent::result(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Validate the input. |
| 36 |
* |
| 37 |
* @param object $object - the object we're validating. |
| 38 |
* |
| 39 |
* @return bool|WP_Error |
| 40 |
*/ |
| 41 |
public function validate_input( $object ) { |
| 42 |
$input = $this->input(); |
| 43 |
$query_args = $this->query_args(); |
| 44 |
if ( ! isset( $input['option_value'] ) || is_array( $input['option_value'] ) ) { |
| 45 |
return new WP_Error( 'option_value_not_set', __( 'You must specify an option_value', 'jetpack' ) ); |
| 46 |
} |
| 47 |
if ( $query_args['is_array'] ) { |
| 48 |
// When converted back from JSON, the value is an object. |
| 49 |
// Cast it to an array for options that expect arrays. |
| 50 |
$this->option_value = (array) $input['option_value']; |
| 51 |
} else { |
| 52 |
$this->option_value = $input['option_value']; |
| 53 |
} |
| 54 |
|
| 55 |
return parent::validate_input( $object ); |
| 56 |
} |
| 57 |
} |
| 58 |
|