Completed
Push — master ( 9a7f47...7f2ea5 )
by Mike
05:50
created

AbstractController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register_items_route() 0 27 3
A add_additional_fields_schema() 0 5 1
A register_item_route() 0 54 4
1
<?php
2
/**
3
 * REST Controller
4
 *
5
 * It's required to follow "Controller Classes" guide before extending this class:
6
 * <https://developer.wordpress.org/rest-api/extending-the-rest-api/controller-classes/>
7
 *
8
 * @class   \WC_REST_Controller
9
 * @see     https://developer.wordpress.org/rest-api/extending-the-rest-api/controller-classes/
10
 * @package WooCommerce/RestApi
11
 */
12
13
namespace WooCommerce\RestApi\Controllers\Version4;
14
15
defined( 'ABSPATH' ) || exit;
16
17
use \WP_REST_Controller;
18
19
/**
20
 * Abstract Rest Controller Class
21
 *
22
 * @package WooCommerce/RestApi
23
 * @extends  WP_REST_Controller
24
 * @version  2.6.0
25
 */
26
abstract class AbstractController extends WP_REST_Controller {
27
	use BatchTrait;
28
29
	/**
30
	 * Endpoint namespace.
31
	 *
32
	 * @var string
33
	 */
34
	protected $namespace = 'wc/v4';
35
36
	/**
37
	 * Route base.
38
	 *
39
	 * @var string
40
	 */
41
	protected $rest_base = '';
42
43
	/**
44
	 * Register route for items requests.
45
	 *
46
	 * @param array $methods Supported methods. read, create.
47
	 */
48
	protected function register_items_route( $methods = [ 'read', 'create' ] ) {
49
		$routes           = [];
50
		$routes['schema'] = [ $this, 'get_public_item_schema' ];
51
52
		if ( in_array( 'read', $methods, true ) ) {
53
			$routes[] = array(
54
				'methods'             => \WP_REST_Server::READABLE,
55
				'callback'            => array( $this, 'get_items' ),
56
				'permission_callback' => array( $this, 'get_items_permissions_check' ),
57
				'args'                => $this->get_collection_params(),
58
			);
59
		}
60
61
		if ( in_array( 'create', $methods, true ) ) {
62
			$routes[] = array(
63
				'methods'             => \WP_REST_Server::CREATABLE,
64
				'callback'            => array( $this, 'create_item' ),
65
				'permission_callback' => array( $this, 'create_item_permissions_check' ),
66
				'args'                => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ),
67
			);
68
		}
69
70
		register_rest_route(
71
			$this->namespace,
72
			'/' . $this->rest_base,
73
			$routes,
74
			true
75
		);
76
	}
77
78
	/**
79
	 * Register route for item create/get/delete/update requests.
80
	 *
81
	 * @param array $methods Supported methods. read, create.
82
	 */
83
	protected function register_item_route( $methods = [ 'read', 'edit', 'delete' ] ) {
84
		$routes           = [];
85
		$routes['schema'] = [ $this, 'get_public_item_schema' ];
86
		$routes['args']   = [
87
			'id' => [
88
				'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
89
				'type'        => 'integer',
90
			],
91
		];
92
93
		if ( in_array( 'read', $methods, true ) ) {
94
			$routes[] = array(
95
				'methods'             => \WP_REST_Server::READABLE,
96
				'callback'            => array( $this, 'get_item' ),
97
				'permission_callback' => array( $this, 'get_item_permissions_check' ),
98
				'args'                => array(
99
					'context' => $this->get_context_param(
100
						array(
101
							'default' => 'view',
102
						)
103
					),
104
				),
105
			);
106
		}
107
108
		if ( in_array( 'edit', $methods, true ) ) {
109
			$routes[] = array(
110
				'methods'             => \WP_REST_Server::EDITABLE,
111
				'callback'            => array( $this, 'update_item' ),
112
				'permission_callback' => array( $this, 'update_item_permissions_check' ),
113
				'args'                => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ),
114
			);
115
		}
116
117
		if ( in_array( 'delete', $methods, true ) ) {
118
			$routes[] = array(
119
				'methods'             => \WP_REST_Server::DELETABLE,
120
				'callback'            => array( $this, 'delete_item' ),
121
				'permission_callback' => array( $this, 'delete_item_permissions_check' ),
122
				'args'                => array(
123
					'force' => array(
124
						'default'     => false,
125
						'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
126
						'type'        => 'boolean',
127
					),
128
				),
129
			);
130
		}
131
132
		register_rest_route(
133
			$this->namespace,
134
			'/' . $this->rest_base . '/(?P<id>[\d]+)',
135
			$routes,
136
			true
137
		);
138
	}
139
140
	/**
141
	 * Add the schema from additional fields to an schema array.
142
	 *
143
	 * @param array $schema Schema array.
144
	 * @return array
145
	 */
146
	protected function add_additional_fields_schema( $schema ) {
147
		$schema               = parent::add_additional_fields_schema( $schema );
148
		$object_type          = $schema['title'];
149
		$schema['properties'] = apply_filters( 'woocommerce_rest_' . $object_type . '_schema', $schema['properties'] );
150
		return $schema;
151
	}
152
}
153