PluginProbe
WooCommerce / 11.0.1
WooCommerce v11.0.1
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / StoreApi / Routes / V1 / ProductAttributesById.php
ProductAttributesById.php
92 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Automattic\WooCommerce\StoreApi\Routes\V1;
3
4 use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
5
6 /**
7 * ProductAttributesById class.
8 */
9 class ProductAttributesById extends AbstractRoute {
10 /**
11 * The route identifier.
12 *
13 * @var string
14 */
15 const IDENTIFIER = 'product-attributes-by-id';
16
17 /**
18 * The routes schema.
19 *
20 * @var string
21 */
22 const SCHEMA_TYPE = 'product-attribute';
23
24 /**
25 * Get the path of this REST route.
26 *
27 * @return string
28 */
29 public function get_path() {
30 return self::get_path_regex();
31 }
32
33 /**
34 * Get the path of this rest route.
35 *
36 * @return string
37 */
38 public static function get_path_regex() {
39 return '/products/attributes/(?P<id>[\d]+)';
40 }
41
42 /**
43 * Get method arguments for this REST route.
44 *
45 * @return array An array of endpoints.
46 */
47 public function get_args() {
48 return [
49 'args' => array(
50 'id' => array(
51 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
52 'type' => 'integer',
53 ),
54 ),
55 [
56 'methods' => \WP_REST_Server::READABLE,
57 'callback' => [ $this, 'get_response' ],
58 'permission_callback' => '__return_true',
59 'args' => array(
60 'context' => $this->get_context_param(
61 array(
62 'default' => 'view',
63 )
64 ),
65 ),
66 'allow_batch' => [ 'v1' => true ],
67 ],
68 'schema' => [ $this->schema, 'get_public_item_schema' ],
69 ];
70 }
71
72 /**
73 * Get a single item.
74 *
75 * @throws RouteException On error.
76 * @param \WP_REST_Request $request Request object.
77 * @return \WP_REST_Response
78 */
79 protected function get_route_response( \WP_REST_Request $request ) {
80 $object = wc_get_attribute( (int) $request['id'] );
81
82 if ( ! $object || 0 === $object->id ) {
83 throw new RouteException( 'woocommerce_rest_attribute_invalid_id', __( 'Invalid attribute ID.', 'woocommerce' ), 404 );
84 }
85
86 $data = $this->prepare_item_for_response( $object, $request );
87 $response = rest_ensure_response( $data );
88
89 return $response;
90 }
91 }
92