Completed
Push — master ( 15aa29...17da96 )
by Claudio
18:39 queued 11s
created

...t-blocks-product-attribute-terms-controller.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * REST API Product Attribute Terms controller customized for Products Block.
4
 *
5
 * Handles requests to the /products/attributes/<attribute_id/terms endpoint.
6
 *
7
 * @internal This API is used internally by the block post editor--it is still in flux. It should not be used outside of wc-blocks.
8
 * @package WooCommerce\Blocks\Products\Rest\Controller
9
 */
10
11 1
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * REST API Product Attribute Terms controller class.
17
 *
18
 * @package WooCommerce/API
19
 */
20
class WC_REST_Blocks_Product_Attribute_Terms_Controller extends WC_REST_Product_Attribute_Terms_Controller {
21
22
	/**
23
	 * Endpoint namespace.
24
	 *
25
	 * @var string
26
	 */
27
	protected $namespace = 'wc-blocks/v1';
28
29
	/**
30
	 * Register the routes for products.
31
	 */
32 426 View Code Duplication
	public function register_routes() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33 426
		register_rest_route(
34 426
			$this->namespace,
35 426
			'/' . $this->rest_base,
36
			array(
37
				array(
38 426
					'methods'             => WP_REST_Server::READABLE,
39 426
					'callback'            => array( $this, 'get_items' ),
40 426
					'permission_callback' => array( $this, 'get_items_permissions_check' ),
41 426
					'args'                => $this->get_collection_params(),
42
				),
43 426
				'schema' => array( $this, 'get_public_item_schema' ),
0 ignored issues
show
Key specified for array entry; first entry has no key
Loading history...
44
			),
45 426
			true
46
		);
47
48 426
		register_rest_route(
49 426
			$this->namespace,
50 426
			'/' . $this->rest_base . '/(?P<id>[\d]+)',
51
			array(
52
				'args'   => array(
53
					'id' => array(
54 426
						'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
55 426
						'type'        => 'integer',
56
					),
57
				),
58
				array(
59
					'methods'             => WP_REST_Server::READABLE,
60 426
					'callback'            => array( $this, 'get_item' ),
61 426
					'permission_callback' => array( $this, 'get_item_permissions_check' ),
62
					'args'                => array(
63 426
						'context' => $this->get_context_param(
64
							array(
65 426
								'default' => 'view',
66
							)
67
						),
68
					),
69
				),
70 426
				'schema' => array( $this, 'get_public_item_schema' ),
71
			),
72 426
			true
73
		);
74
	}
75
76
	/**
77
	 * Check permissions.
78
	 *
79
	 * @param WP_REST_Request $request Full details about the request.
80
	 * @param string          $context Request context.
81
	 * @return bool|WP_Error
82
	 */
83 4 View Code Duplication
	protected function check_permissions( $request, $context = 'read' ) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
		// Get taxonomy.
85 4
		$taxonomy = $this->get_taxonomy( $request );
86 4
		if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) {
87 1
			return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Taxonomy does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
88
		}
89
90
		// Check permissions for a single term.
91 3
		$id = intval( $request['id'] );
92 3
		if ( $id ) {
93
			$term = get_term( $id, $taxonomy );
94
95
			if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) {
96
				return new WP_Error( 'woocommerce_rest_term_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
97
			}
98
		}
99
100 3
		return current_user_can( 'edit_posts' );
101
	}
102
103
	/**
104
	 * Prepare a single product category output for response.
105
	 *
106
	 * @param WP_Term         $item    Term object.
107
	 * @param WP_REST_Request $request Request instance.
108
	 * @return WP_REST_Response
109
	 */
110 2
	public function prepare_item_for_response( $item, $request ) {
111
		// Get the attribute slug.
112 2
		$attribute_id = absint( $request->get_param( 'attribute_id' ) );
113 2
		$attribute    = wc_get_attribute( $attribute_id );
114
115
		$data = array(
116 2
			'id'        => (int) $item->term_id,
117 2
			'name'      => $item->name,
118 2
			'slug'      => $item->slug,
119 2
			'count'     => (int) $item->count,
120
			'attribute' => array(
121 2
				'id'    => $attribute->id,
122 2
				'name'  => $attribute->name,
123 2
				'slug'  => $attribute->slug,
124
			),
125
		);
126
127 2
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
128 2
		$data    = $this->add_additional_fields_to_object( $data, $request );
129 2
		$data    = $this->filter_response_by_context( $data, $context );
130
131 2
		$response = rest_ensure_response( $data );
132
133 2
		$response->header( 'X-Woo-Notice', __( 'Private REST API for use by block editor only.', 'woocommerce' ) );
134
135 2
		$response->add_links( $this->prepare_links( $item, $request ) );
136
137 2
		return $response;
138
	}
139
140
	/**
141
	 * Get the Product's schema, conforming to JSON Schema.
142
	 *
143
	 * @return array
144
	 */
145 426
	public function get_item_schema() {
146 426
		$raw_schema = parent::get_item_schema();
147
		$schema     = array(
148 426
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
149
			'title'      => 'product_attribute_term',
150
			'type'       => 'object',
151
			'properties' => array(),
152
		);
153
154 426
		$schema['properties']['id']        = $raw_schema['properties']['id'];
155 426
		$schema['properties']['name']      = $raw_schema['properties']['name'];
156 426
		$schema['properties']['slug']      = $raw_schema['properties']['slug'];
157 426
		$schema['properties']['count']     = $raw_schema['properties']['count'];
158 426
		$schema['properties']['attribute'] = array(
159 426
			'description' => __( 'Attribute.', 'woocommerce' ),
160 426
			'type'        => 'object',
161
			'context'     => array( 'view', 'edit' ),
162
			'readonly'    => true,
163
			'properties' => array(
164
				'id' => array(
165 426
					'description' => __( 'Attribute ID.', 'woocommerce' ),
166 426
					'type'        => 'integer',
167
					'context'     => array( 'view', 'edit' ),
168
					'readonly'    => true,
169
				),
170
				'name' => array(
171 426
					'description' => __( 'Attribute name.', 'woocommerce' ),
172 426
					'type'        => 'string',
173
					'context'     => array( 'view', 'edit' ),
174
					'readonly'    => true,
175
				),
176
				'slug' => array(
177 426
					'description' => __( 'Attribute slug.', 'woocommerce' ),
178 426
					'type'        => 'string',
179
					'context'     => array( 'view', 'edit' ),
180
					'readonly'    => true,
181
				),
182
			),
183
		);
184
185 426
		return $this->add_additional_fields_schema( $schema );
186
	}
187
}
188