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