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 Categories controller customized for Products Block. |
||
4 | * |
||
5 | * Handles requests to the /products/categories 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 Categories controller class. |
||
17 | * |
||
18 | * @package WooCommerce/API |
||
19 | */ |
||
20 | class WC_REST_Blocks_Product_Categories_Controller extends WC_REST_Product_Categories_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
|
|||
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' ), |
|
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 | 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 | 4 | $id = intval( $request['id'] ); |
|
92 | 4 | if ( $id ) { |
|
93 | 3 | $term = get_term( $id, $taxonomy ); |
|
94 | |||
95 | 3 | if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) { |
|
96 | 1 | 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 | $data = array( |
||
112 | 2 | 'id' => (int) $item->term_id, |
|
113 | 2 | 'name' => $item->name, |
|
114 | 2 | 'slug' => $item->slug, |
|
115 | 2 | 'parent' => (int) $item->parent, |
|
116 | 2 | 'count' => (int) $item->count, |
|
117 | ); |
||
118 | |||
119 | 2 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
120 | 2 | $data = $this->add_additional_fields_to_object( $data, $request ); |
|
121 | 2 | $data = $this->filter_response_by_context( $data, $context ); |
|
122 | |||
123 | 2 | $response = rest_ensure_response( $data ); |
|
124 | |||
125 | 2 | $response->header( 'X-Woo-Notice', __( 'Private REST API for use by block editor only.', 'woocommerce' ) ); |
|
126 | 2 | $response->add_links( $this->prepare_links( $item, $request ) ); |
|
127 | |||
128 | 2 | return $response; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Get the Product's schema, conforming to JSON Schema. |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 426 | public function get_item_schema() { |
|
137 | 426 | $raw_schema = parent::get_item_schema(); |
|
138 | $schema = array( |
||
139 | 426 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
140 | 'title' => 'product_block_category', |
||
141 | 'type' => 'object', |
||
142 | 'properties' => array(), |
||
143 | ); |
||
144 | |||
145 | 426 | $schema['properties']['id'] = $raw_schema['properties']['id']; |
|
146 | 426 | $schema['properties']['name'] = $raw_schema['properties']['name']; |
|
147 | 426 | $schema['properties']['slug'] = $raw_schema['properties']['slug']; |
|
148 | 426 | $schema['properties']['parent'] = $raw_schema['properties']['parent']; |
|
149 | 426 | $schema['properties']['count'] = $raw_schema['properties']['count']; |
|
150 | |||
151 | 426 | return $this->add_additional_fields_schema( $schema ); |
|
152 | } |
||
153 | } |
||
154 |
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.