summaryrefslogtreecommitdiff
blob: 20f8895c1c7cb852a19d1c62f0013ea337dd5276 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

class WPCOM_JSON_API_Update_Option_Endpoint extends WPCOM_JSON_API_Get_Option_Endpoint {
	public $option_value;

	function result() {
		if ( $this->site_option ) {
			update_site_option( $this->option_name, $this->option_value );
		} else {
			update_option( $this->option_name, $this->option_value );
		}
		return parent::result();
	}

	function validate_input( $object ) {
		$input = $this->input();
		$query_args = $this->query_args();
		if ( ! isset( $input['option_value'] ) || is_array( $input['option_value'] ) ) {
			return new WP_Error( 'option_value_not_set', __( 'You must specify an option_value', 'jetpack' ) );
		}
		if ( $query_args['is_array'] ) {
			// When converted back from JSON, the value is an object.
			// Cast it to an array for options that expect arrays.
			$this->option_value = (array) $input['option_value'];
		} else {
			$this->option_value = $input['option_value'];
		}

		return parent::validate_input( $object );
	}
}