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

...c-rest-blocks-product-attributes-controller.php (5 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 Attributes controller customized for Products Block.
4
 *
5
 * Handles requests to the /products/attributes 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 Attributes controller class.
17
 *
18
 * @package WooCommerce/API
19
 */
20
class WC_REST_Blocks_Product_Attributes_Controller extends WC_REST_Product_Attributes_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 if a given request has access to read the attributes.
78
	 *
79
	 * @param  WP_REST_Request $request Full details about the request.
80
	 * @return WP_Error|boolean
81
	 */
82 1 View Code Duplication
	public function get_items_permissions_check( $request ) {
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...
83 1
		if ( ! current_user_can( 'edit_posts' ) ) {
84
			return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
85
		}
86
87 1
		return true;
88
	}
89
	/**
90
	 * Check if a given request has access to read a attribute.
91
	 *
92
	 * @param  WP_REST_Request $request Full details about the request.
93
	 * @return WP_Error|boolean
94
	 */
95 3 View Code Duplication
	public function get_item_permissions_check( $request ) {
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...
96 3
		$taxonomy = $this->get_taxonomy( $request );
97
98 3
		if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) {
99 1
			return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
100
		}
101
102 2
		if ( ! current_user_can( 'edit_posts' ) ) {
103 1
			return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
104
		}
105
106 1
		return true;
107
	}
108
109
	/**
110
	 * Check permissions.
111
	 *
112
	 * @param WP_REST_Request $request Full details about the request.
113
	 * @param string          $context Request context.
114
	 * @return bool|WP_Error
115
	 */
116 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...
117
		// Get taxonomy.
118
		$taxonomy = $this->get_taxonomy( $request );
119
		if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) {
120
			return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Taxonomy does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
121
		}
122
123
		// Check permissions for a single term.
124
		$id = intval( $request['id'] );
125
		if ( $id ) {
126
			$term = get_term( $id, $taxonomy );
127
128
			if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) {
129
				return new WP_Error( 'woocommerce_rest_term_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
130
			}
131
		}
132
133
		return current_user_can( 'edit_posts' );
134
	}
135
136
	/**
137
	 * Prepare a single product category output for response.
138
	 *
139
	 * @param WP_Term         $item    Term object.
140
	 * @param WP_REST_Request $request Request instance.
141
	 * @return WP_REST_Response
142
	 */
143 2
	public function prepare_item_for_response( $item, $request ) {
144 2
		$taxonomy = wc_attribute_taxonomy_name( $item->attribute_name );
145
		$data     = array(
146 2
			'id'    => (int) $item->attribute_id,
147 2
			'name'  => $item->attribute_label,
148 2
			'slug'  => $taxonomy,
149 2
			'count' => wp_count_terms( $taxonomy ),
150
		);
151
152 2
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
153 2
		$data    = $this->add_additional_fields_to_object( $data, $request );
154 2
		$data    = $this->filter_response_by_context( $data, $context );
155
156 2
		$response = rest_ensure_response( $data );
157
158 2
		$response->header( 'X-Woo-Notice', __( 'Private REST API for use by block editor only.', 'woocommerce' ) );
159 2
		$response->add_links( $this->prepare_links( $item ) );
160
161 2
		return $response;
162
	}
163
164
	/**
165
	 * Get the Product's schema, conforming to JSON Schema.
166
	 *
167
	 * @return array
168
	 */
169 426
	public function get_item_schema() {
170 426
		$raw_schema = parent::get_item_schema();
171
		$schema     = array(
172 426
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
173
			'title'      => 'product_block_attribute',
174
			'type'       => 'object',
175
			'properties' => array(),
176
		);
177
178 426
		$schema['properties']['id']    = $raw_schema['properties']['id'];
179 426
		$schema['properties']['name']  = $raw_schema['properties']['name'];
180 426
		$schema['properties']['slug']  = $raw_schema['properties']['slug'];
181 426
		$schema['properties']['count'] = array(
182 426
			'description' => __( 'Number of terms in the attribute taxonomy.', 'woocommerce' ),
183 426
			'type'        => 'integer',
184
			'context'     => array( 'view', 'edit' ),
185
			'readonly'    => true,
186
		);
187
188 426
		return $this->add_additional_fields_schema( $schema );
189
	}
190
}
191