Passed
Pull Request — master (#394)
by Brian
05:43
created

get_collection_params()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 26
rs 9.8333
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * REST items controllers.
4
 *
5
 * @version 1.0.19
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
/**
11
 * REST API items controller class.
12
 *
13
 * @package Invoicing
14
 */
15
class WPInv_REST_Items_Controller extends GetPaid_REST_Posts_Controller {
16
17
    /**
18
	 * Post type.
19
	 *
20
	 * @var string
21
	 */
22
	protected $post_type = 'wpi_item';
23
	
24
	/**
25
	 * The base of this controller's route.
26
	 *
27
	 * @since 1.0.13
28
	 * @var string
29
	 */
30
	protected $rest_base = 'items';
31
32
	/** Contains this controller's class name.
33
	 *
34
	 * @var string
35
	 */
36
	public $crud_class = 'WPInv_Item';
37
38
	/**
39
	 * Registers the routes for the objects of the controller.
40
	 *
41
	 * @since 1.0.19
42
	 *
43
	 * @see register_rest_route()
44
	 */
45
	public function register_namespace_routes( $namespace ) {
46
47
		parent::register_namespace_routes( $namespace );
48
49
		register_rest_route(
50
			$this->namespace,
51
			'/' . $this->rest_base . '/item-types',
52
			array(
53
				array(
54
					'methods'             => WP_REST_Server::READABLE,
55
					'callback'            => array( $this, 'get_item_types' ),
56
					'permission_callback' => array( $this, 'get_items_permissions_check' ),
57
				),
58
			)
59
		);
60
61
	}
62
63
	/**
64
	 * Handles rest requests for item types.
65
	 *
66
	 * @since 1.0.13
67
	 * 
68
	 * 
69
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
70
	 */
71
	public function get_item_types() {
72
		return rest_ensure_response( wpinv_get_item_types() );
73
	}
74
75
    /**
76
	 * Retrieves the query params for the items collection.
77
	 *
78
	 * @since 1.0.13
79
	 *
80
	 * @return array Collection parameters.
81
	 */
82
	public function get_collection_params() {
83
84
		$params = array_merge(
85
86
			parent::get_collection_params(),
87
88
        	array(
89
90
				// Item types
91
				'type'                  => array(
92
					'description'       => __( 'Type of items to fetch.', 'invoicing' ),
93
					'type'              => array( 'array', 'string' ),
94
					'default'           => 'any',
95
					'validate_callback' => 'rest_validate_request_arg',
96
					'sanitize_callback' => 'wpinv_parse_list',
97
					'items'             => array(
98
						'enum'          => array_merge( array( 'any' ), wpinv_item_types() ),
99
						'type'          => 'string',
100
					),
101
				),
102
103
			)
104
		);
105
106
		// Filter collection parameters for the items controller.
107
		return apply_filters( 'getpaid_rest_items_collection_params', $params, $this );
108
109
	}
110
111
	/**
112
	 * Determine the allowed query_vars for a get_items() response and
113
	 * prepare for WP_Query.
114
	 *
115
	 * @param array           $prepared_args Prepared arguments.
116
	 * @param WP_REST_Request $request Request object.
117
	 * @return array          $query_args
118
	 */
119
	protected function prepare_items_query( $prepared_args = array(), $request = null ) {
120
121
		$query_args = parent::prepare_items_query( $prepared_args );
122
123
		// Retrieve items by type.
124
		if ( ! in_array( 'any', $request['type'] ) ) {
0 ignored issues
show
Bug introduced by
It seems like $request['type'] can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
		if ( ! in_array( 'any', /** @scrutinizer ignore-type */ $request['type'] ) ) {
Loading history...
125
126
			if ( empty( $query_args['meta_query'] ) ) {
127
				$query_args['meta_query'] = array();
128
			}
129
130
			$query_args['meta_query'][] = array(
131
				'key'     => '_wpinv_type',
132
				'value'   => implode( ',', $request['type'] ),
0 ignored issues
show
Bug introduced by
It seems like $request['type'] can also be of type null; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
				'value'   => implode( ',', /** @scrutinizer ignore-type */ $request['type'] ),
Loading history...
133
				'compare' => 'IN',
134
			);
135
136
		}
137
138
		return apply_filters( 'getpaid_rest_items_prepare_items_query', $query_args, $request, $this );
139
140
	}
141
142
	/**
143
	 * Retrieves a valid list of post statuses.
144
	 *
145
	 * @since 1.0.15
146
	 *
147
	 * @return array A list of registered item statuses.
148
	 */
149
	public function get_post_statuses() {
150
		return array( 'draft', 'pending', 'publish' );
151
	}
152
153
	/**
154
	 * Checks if a key should be included in a response.
155
	 *
156
	 * @since  1.0.19
157
	 * @param  WPInv_Item   $item  Item object.
158
	 * @param  string       $field_key The key to check for.
159
	 * @return bool
160
	 */
161
	public function object_supports_field( $item, $field_key ) {
162
163
		if ( 'minimum_price' == $field_key && ! $item->user_can_set_their_price() ) {
164
			return false;
165
		}
166
167
		foreach( wpinv_parse_list( 'initial_price the_initial_price recurring_price the_recurring_price recurring_period recurring_interval recurring_limit is_free_trial trial_period trial_interval first_renewal_date' ) as $key ) {
168
169
			if ( $key == $field_key && ! $item->is_recurring() ) {
170
				return false;
171
			}
172
173
		}
174
175
		foreach( wpinv_parse_list( 'trial_period trial_interval' ) as $key ) {
176
177
			if ( $key == $field_key && ! $item->has_free_trial() ) {
178
				return false;
179
			}
180
181
		}
182
183
		return parent::object_supports_field( $item, $field_key );
184
	}
185
186
}
187