Completed
Push — add/publicize-rest-api-2 ( 78c211...304115 )
by
unknown
13:03
created

WPCOM_REST_API_V2_Field_Controller::get()   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 2
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
		register_rest_field( 'post', 'jetpack_publicize_connections', array(
24
			'get_callback' => array( $this, 'get_for_response' ),
25
			'update_callback' => array( $this, 'update_from_request' ),
26
			'schema' => $this->get_schema(),
27
		) );
28
	}
29
30
	function prepare_for_response( $value, $request ) {
31
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
32
		$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...
33
34
		$is_valid = rest_validate_value_from_schema( $value, $schema, $this->field_name );
35
		if ( is_wp_error( $is_valid ) ) {
36
			return $is_valid;
37
		}
38
39
		return $this->filter_response_by_context( $value, $schema, $context );
40
	}
41
42
	public function get_for_response( $object_data, $field_name, $request, $object_type ) {
43
		$permission_check = $this->get_permission_check( $request );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $permission_check is correct as $this->get_permission_check($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...
44
45
		if ( ! $permission_check || is_wp_error( $permission_check ) ) {
46
			return;
47
		}
48
49
		$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...
50
51
		return $this->prepare_for_response( $value, $request );
52
	}
53
54
	public function update_from_request( $value, $object_data, $field_name, $request, $object_type ) {
55
		$permission_check = $this->update_permission_check( $value, $request );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $permission_check is correct as $this->update_permission_check($value, $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...
56
57
		if ( ! $permission_check ) {
58
			return;
59
		}
60
61
		if ( is_wp_error( $permission_check ) ) {
62
			return $permission_check;
63
		}
64
65
		$new_value = $this->update( $value, $object_data, $request );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $new_value 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...
66
67
		$this->prepare_for_response( $new_value, $request );
68
	}
69
70
	public function get_permission_check( $request ) {
71
		/* translators: %s: get_permission_check() */
72
                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::get_permission_check', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' );
73
	}
74
75
	public function get( $object_data, $request ) {
76
		/* translators: %s: get() */
77
                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::get', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' );
78
	}
79
80
	public function update_permission_check( $value, $request ) {
81
		/* translators: %s: update_permission_check() */
82
                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::update_permission_check', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' );
83
	}
84
85
	public function update( $value, $object_data, $request ) {
86
		/* translators: %s: update() */
87
                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::update', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' );
88
	}
89
90
	public function get_schema() {
91
		/* translators: %s: get_schema() */
92
                _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::get_schema', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' );
93
	}
94
95
	function is_valid_for_context( $schema, $context ) {
96
		return empty( $schema['context'] ) || in_array( $context, $schema['context'], true );
97
	}
98
99
	function filter_response_by_context( $value, $schema, $context ) {
100
		if ( ! $this->is_valid_for_context( $schema, $context ) ) {
101
			return new WP_Error( '__wrong-context__' );
102
		}
103
104
		switch ( $schema['type'] ) {
105
		case 'array' :
106
			if ( ! isset( $schema['items'] ) ) {
107
				return $value;
108
			}
109
110
			// Shortcircuit if we know none of the items are valid for this context.
111
			// This would only happen in a strangely written schema.
112
			if ( ! $this->is_valid_for_context( $schema['items'], $context ) ) {
113
				return array();
114
			}
115
116
			// Recurse to prune sub-properties of each item.
117
118
			$keys = array_keys( $value );
119
120
			$items = array_map(
121
				array( $this, 'filter_response_by_context' ),
122
				$value,
123
				array_fill( 0, count( $keys ), $schema['items'] ),
124
				array_fill( 0, count( $keys ), $context )
125
			);
126
127
			return array_combine( $keys, $items );
128
		case 'object' :
129
			if ( ! isset( $schema['properties'] ) ) {
130
				return $value;
131
			}
132
133
			foreach ( $value as $field_name => $field_value ) {
134
				if ( isset( $schema['properties'][$field_name] ) ) {
135
					$field_value = $this->filter_response_by_context( $field_value, $schema['properties'][$field_name], $context );
136
					if ( is_wp_error( $field_value ) && '__wrong-context__' === $field_value->get_error_code() ) {
137
						unset( $value[$field_name] );
138
					} else {
139
						// Respect recursion that pruned sub-properties of each property.
140
						$value[$field_name] = $field_value;
141
					}
142
				}
143
			}
144
145
			return (object) $value;
146
		}
147
148
		return $value;
149
	}
150
}
151