| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
use Automattic\Jetpack\Sync\Defaults; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit( 0 ); |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Get option endpoint. |
| 11 |
* |
| 12 |
* @phan-constructor-used-for-side-effects |
| 13 |
*/ |
| 14 |
class WPCOM_JSON_API_Get_Option_Endpoint extends Jetpack_JSON_API_Endpoint { |
| 15 |
/** |
| 16 |
* This endpoint allows authentication both via a blog and a user token. |
| 17 |
* If a user token is used, that user should have `manage_options` capability. |
| 18 |
* |
| 19 |
* @var array|string |
| 20 |
*/ |
| 21 |
protected $needed_capabilities = 'manage_options'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Options name. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
public $option_name; |
| 29 |
|
| 30 |
/** |
| 31 |
* Site option. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
public $site_option; |
| 36 |
|
| 37 |
/** |
| 38 |
* Endpoint callback. |
| 39 |
* |
| 40 |
* @return array |
| 41 |
*/ |
| 42 |
public function result() { |
| 43 |
if ( $this->site_option ) { |
| 44 |
return array( 'option_value' => get_site_option( $this->option_name ) ); |
| 45 |
} |
| 46 |
return array( 'option_value' => get_option( $this->option_name ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Validate the input. |
| 51 |
* |
| 52 |
* @param object $object - unused, for parent class compatability. |
| 53 |
* |
| 54 |
* @return bool|WP_Error |
| 55 |
*/ |
| 56 |
public function validate_input( $object ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 57 |
$query_args = $this->query_args(); |
| 58 |
$this->option_name = $query_args['option_name'] ?? false; |
| 59 |
if ( ! $this->option_name ) { |
| 60 |
return new WP_Error( 'option_name_not_set', __( 'You must specify an option_name', 'jetpack' ) ); |
| 61 |
} |
| 62 |
$this->site_option = $query_args['site_option'] ?? false; |
| 63 |
|
| 64 |
/** |
| 65 |
* Filter the list of options that are manageable via the JSON API. |
| 66 |
* |
| 67 |
* @module json-api |
| 68 |
* |
| 69 |
* @since 3.8.2 |
| 70 |
* |
| 71 |
* @param array The default list of site options. |
| 72 |
* @param bool Is the option a site option. |
| 73 |
*/ |
| 74 |
if ( ! in_array( $this->option_name, apply_filters( 'jetpack_options_whitelist', Defaults::$default_options_whitelist, $this->site_option ), true ) ) { |
| 75 |
return new WP_Error( 'option_name_not_in_whitelist', __( 'You must specify a whitelisted option_name', 'jetpack' ) ); |
| 76 |
} |
| 77 |
return true; |
| 78 |
} |
| 79 |
} |
| 80 |
|