Completed
Push — add/publicize-rest-api-2 ( 495ee5...dee08a )
by
unknown
07:25
created

WPCOM_REST_API_V2_Field_Controller::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @todo - nicer API for array values?
5
 */
6
abstract class WPCOM_REST_API_V2_Field_Controller {
7
	protected $object_type;
8
	protected $field_name;
9
10
	public function __construct() {
11
		if ( ! $this->object_type ) {
12
			/* translators: %s: object_type */
13
	                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::$object_type', sprintf( __( "Property '%s' must be overridden.", 'jetpack' ), 'object_type' ), 'Jetpack 6.8' );
14
			return;
15
		}
16
17
		if ( ! $this->field_name ) {
18
			/* translators: %s: field_name */
19
	                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::$field_name', sprintf( __( "Property '%s' must be overridden.", 'jetpack' ), 'field_name' ), 'Jetpack 6.8' );
20
			return;
21
		}
22
	}
23
24
	public function register_fields() {
25
		foreach ( (array) $this->object_type as $object_type ) {
26
			register_rest_field( $object_type, $this->field_name, array(
27
				'get_callback' => array( $this, 'get_for_response' ),
28
				'update_callback' => array( $this, 'update_from_request' ),
29
				'schema' => $this->get_schema(),
30
			) );
31
		}
32
	}
33
34
	function prepare_for_response( $value, $request ) {
35
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
36
		$schema = $this->get_schema();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $schema is correct as $this->get_schema() (which targets WPCOM_REST_API_V2_Field_Controller::get_schema()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
37
38
		$is_valid = rest_validate_value_from_schema( $value, $schema, $this->field_name );
39
		if ( is_wp_error( $is_valid ) ) {
40
			return $is_valid;
41
		}
42
43
		return $this->filter_response_by_context( $value, $schema, $context );
44
	}
45
46
	final public function default_value( $schema ) {
47
		if ( isset( $schema['default'] ) ) {
48
			return $schema['default'];
49
		}
50
51
		// If you have something more complicated, use $schema['default'];
52
		switch ( isset( $schema['type'] ) ? $schema['type'] : 'null' ) {
53
		case 'string' :
54
			return '';
55
		case 'integer' :
56
		case 'number' :
57
			return 0;
58
		case 'object' :
59
			return (object) array();
60
		case 'array' :
61
			return array();
62
		case 'boolean' :
63
			return false;
64
		case 'null' :
65
		default :
66
			return null;
67
		}
68
	}
69
70
	final public function get_for_response( $object_data, $field_name, $request, $object_type ) {
71
		$permission_check = $this->get_permission_check( $object_data, $request );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $permission_check is correct as $this->get_permission_ch...$object_data, $request) (which targets WPCOM_REST_API_V2_Field_...:get_permission_check()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
72
73
		if ( ! $permission_check || is_wp_error( $permission_check ) ) {
74
			return $this->default_value( $this->get_schema() );
75
		}
76
77
		$value = $this->get( $object_data, $request );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $this->get($object_data, $request) (which targets WPCOM_REST_API_V2_Field_Controller::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
78
79
		return $this->prepare_for_response( $value, $request );
80
	}
81
82
	final public function update_from_request( $value, $object_data, $field_name, $request, $object_type ) {
83
		$permission_check = $this->update_permission_check( $value, $object_data, $request );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $permission_check is correct as $this->update_permission...$object_data, $request) (which targets WPCOM_REST_API_V2_Field_...date_permission_check()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
84
85
		if ( ! $permission_check ) {
86
			return;
87
		}
88
89
		if ( is_wp_error( $permission_check ) ) {
90
			return $permission_check;
91
		}
92
93
		$updated = $this->update( $value, $object_data, $request );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $updated is correct as $this->update($value, $object_data, $request) (which targets WPCOM_REST_API_V2_Field_Controller::update()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
94
95
		if ( is_wp_error( $updated ) ) {
96
			return $updated;
97
		}
98
	}
99
100
	public function get_permission_check( $object_data, $request ) {
101
		/* translators: %s: get_permission_check() */
102
                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::get_permission_check', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' );
103
	}
104
105
	public function get( $object_data, $request ) {
106
		/* translators: %s: get() */
107
                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::get', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' );
108
	}
109
110
	public function update_permission_check( $value, $object_data, $request ) {
111
		/* translators: %s: update_permission_check() */
112
                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::update_permission_check', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' );
113
	}
114
115
	public function update( $value, $object_data, $request ) {
116
		/* translators: %s: update() */
117
                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::update', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' );
118
	}
119
120
	public function get_schema() {
121
		/* translators: %s: get_schema() */
122
                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::get_schema', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' );
123
	}
124
125
	function is_valid_for_context( $schema, $context ) {
126
		return empty( $schema['context'] ) || in_array( $context, $schema['context'], true );
127
	}
128
129
	final function filter_response_by_context( $value, $schema, $context ) {
130
		if ( ! $this->is_valid_for_context( $schema, $context ) ) {
131
			return new WP_Error( '__wrong-context__' );
132
		}
133
134
		switch ( $schema['type'] ) {
135
		case 'array' :
136
			if ( ! isset( $schema['items'] ) ) {
137
				return $value;
138
			}
139
140
			// Shortcircuit if we know none of the items are valid for this context.
141
			// This would only happen in a strangely written schema.
142
			if ( ! $this->is_valid_for_context( $schema['items'], $context ) ) {
143
				return array();
144
			}
145
146
			// Recurse to prune sub-properties of each item.
147
148
			$keys = array_keys( $value );
149
150
			$items = array_map(
151
				array( $this, 'filter_response_by_context' ),
152
				$value,
153
				array_fill( 0, count( $keys ), $schema['items'] ),
154
				array_fill( 0, count( $keys ), $context )
155
			);
156
157
			return array_combine( $keys, $items );
158
		case 'object' :
159
			if ( ! isset( $schema['properties'] ) ) {
160
				return $value;
161
			}
162
163
			foreach ( $value as $field_name => $field_value ) {
164
				if ( isset( $schema['properties'][$field_name] ) ) {
165
					$field_value = $this->filter_response_by_context( $field_value, $schema['properties'][$field_name], $context );
166
					if ( is_wp_error( $field_value ) && '__wrong-context__' === $field_value->get_error_code() ) {
167
						unset( $value[$field_name] );
168
					} else {
169
						// Respect recursion that pruned sub-properties of each property.
170
						$value[$field_name] = $field_value;
171
					}
172
				}
173
			}
174
175
			return (object) $value;
176
		}
177
178
		return $value;
179
	}
180
}
181