Completed
Push — update/media-extractor-summary... ( 317788...ea9570 )
by Jeremy
34:42 queued 06:44
created

WPCOM_JSON_API_Get_Option_Endpoint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A result() 0 6 2
B validate_input() 0 22 5
1
<?php
2
3
class WPCOM_JSON_API_Get_Option_Endpoint extends Jetpack_JSON_API_Endpoint {
4
5
	protected $needed_capabilities = 'manage_options';
6
7
	public $option_name;
8
	public $site_option;
9
10
	function result() {
11
		if ( $this->site_option ) {
12
			return array( 'option_value' => get_site_option( $this->option_name ) );
13
		}
14
		return array( 'option_value' => get_option( $this->option_name ) );
15
	}
16
17
	function validate_input( $object ) {
18
		$query_args = $this->query_args();
19
		$this->option_name = isset( $query_args['option_name'] ) ? $query_args['option_name'] : false;
20
		if ( ! $this->option_name ) {
21
			return new WP_Error( 'option_name_not_set', __( 'You must specify an option_name', 'jetpack' ) );
22
		}
23
		$this->site_option = isset( $query_args['site_option'] ) ? $query_args['site_option'] : false;
24
		/**
25
		 * Filter the list of options that are manageable via the JSON API.
26
		 *
27
		 * @module json-api
28
		 *
29
		 * @since 3.8.2
30
		 *
31
		 * @param array The default list of site options.
32
		 * @param bool Is the option a site option.
33
		 */
34
		if ( ! in_array( $this->option_name, apply_filters( 'jetpack_options_whitelist', array(), $this->site_option ) ) ) {
35
			return new WP_Error( 'option_name_not_in_whitelist', __( 'You must specify a whitelisted option_name', 'jetpack' ) );
36
		}
37
		return true;
38
	}
39
}
40