Passed
Push — master ( 08c185...0abe41 )
by Brian
10:06 queued 05:09
created

get_allowed_query_vars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
rs 10
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
					'items'             => array(
96
						'enum'          => array_merge( array( 'any' ), wpinv_item_types() ),
97
						'type'          => 'string',
98
					),
99
				),
100
101
			)
102
		);
103
104
		// Filter collection parameters for the items controller.
105
		return apply_filters( 'getpaid_rest_items_collection_params', $params, $this );
106
107
	}
108
	
109
	/**
110
	 * Get all the WP Query vars that are allowed for the API request.
111
	 *
112
	 * @return array
113
	 */
114
	protected function get_allowed_query_vars() {
115
		$vars = array_merge( array( 'type' ), parent::get_allowed_query_vars() );
116
		return apply_filters( 'getpaid_rest_items_allowed_query_vars', $vars, $this );
117
	}
118
119
	/**
120
	 * Determine the allowed query_vars for a get_items() response and
121
	 * prepare for WP_Query.
122
	 *
123
	 * @param array           $prepared_args Prepared arguments.
124
	 * @param WP_REST_Request $request Request object.
125
	 * @return array          $query_args
126
	 */
127
	protected function prepare_items_query( $prepared_args = array(), $request = null ) {
128
129
		$query_args = parent::prepare_items_query( $prepared_args );
130
131
		// Retrieve items by type.
132
		if (  isset( $query_args['type'] ) && 'any' != $query_args['type'] ) {
133
134
			if ( empty( $query_args['meta_query'] ) ) {
135
				$query_args['meta_query'] = array();
136
			}
137
138
			$types = wpinv_parse_list( $query_args['type'] );
139
			$query_args['meta_query'][] = array(
140
				'key'     => '_wpinv_type',
141
				'value'   => implode( ',', $types ),
142
				'compare' => 'IN',
143
			);
144
			unset( $query_args['type'] );
145
146
		}
147
148
		return apply_filters( 'getpaid_rest_items_prepare_items_query', $query_args, $request, $this );
149
150
	}
151
152
	/**
153
	 * Retrieves a valid list of post statuses.
154
	 *
155
	 * @since 1.0.15
156
	 *
157
	 * @return array A list of registered item statuses.
158
	 */
159
	public function get_post_statuses() {
160
		return array( 'draft', 'pending', 'publish' );
161
	}
162
163
}
164