Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Update_Option_Endpoint
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 result
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 validate_input
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7/**
8 * Update option endpoint.
9 *
10 * @phan-constructor-used-for-side-effects
11 */
12class 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}