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

class-wc-rest-product-categories-v2-controller.php (1 issue)

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 Categories controller
4
 *
5
 * Handles requests to the products/categories endpoint.
6
 *
7
 * @package WooCommerce/API
8
 * @since   2.6.0
9
 */
10
11
defined( 'ABSPATH' ) || exit;
12
13
/**
14
 * REST API Product Categories controller class.
15
 *
16
 * @package WooCommerce/API
17
 * @extends WC_REST_Product_Categories_V1_Controller
18
 */
19
class WC_REST_Product_Categories_V2_Controller extends WC_REST_Product_Categories_V1_Controller {
20
21
	/**
22
	 * Endpoint namespace.
23
	 *
24
	 * @var string
25
	 */
26
	protected $namespace = 'wc/v2';
27
28
	/**
29
	 * Prepare a single product category output for response.
30
	 *
31
	 * @param WP_Term         $item    Term object.
32
	 * @param WP_REST_Request $request Request instance.
33
	 * @return WP_REST_Response
34
	 */
35 View Code Duplication
	public function prepare_item_for_response( $item, $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...
36
		// Get category display type.
37
		$display_type = get_term_meta( $item->term_id, 'display_type', true );
38
39
		// Get category order.
40
		$menu_order = get_term_meta( $item->term_id, 'order', true );
41
42
		$data = array(
43
			'id'          => (int) $item->term_id,
44
			'name'        => $item->name,
45
			'slug'        => $item->slug,
46
			'parent'      => (int) $item->parent,
47
			'description' => $item->description,
48
			'display'     => $display_type ? $display_type : 'default',
49
			'image'       => null,
50
			'menu_order'  => (int) $menu_order,
51
			'count'       => (int) $item->count,
52
		);
53
54
		// Get category image.
55
		$image_id = get_term_meta( $item->term_id, 'thumbnail_id', true );
56
		if ( $image_id ) {
57
			$attachment = get_post( $image_id );
58
59
			$data['image'] = array(
60
				'id'                => (int) $image_id,
61
				'date_created'      => wc_rest_prepare_date_response( $attachment->post_date ),
62
				'date_created_gmt'  => wc_rest_prepare_date_response( $attachment->post_date_gmt ),
63
				'date_modified'     => wc_rest_prepare_date_response( $attachment->post_modified ),
64
				'date_modified_gmt' => wc_rest_prepare_date_response( $attachment->post_modified_gmt ),
65
				'src'               => wp_get_attachment_url( $image_id ),
66
				'title'             => get_the_title( $attachment ),
67
				'alt'               => get_post_meta( $image_id, '_wp_attachment_image_alt', true ),
68
			);
69
		}
70
71
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
72
		$data    = $this->add_additional_fields_to_object( $data, $request );
73
		$data    = $this->filter_response_by_context( $data, $context );
74
75
		$response = rest_ensure_response( $data );
76
77
		$response->add_links( $this->prepare_links( $item, $request ) );
78
79
		/**
80
		 * Filter a term item returned from the API.
81
		 *
82
		 * Allows modification of the term data right before it is returned.
83
		 *
84
		 * @param WP_REST_Response  $response  The response object.
85
		 * @param object            $item      The original term object.
86
		 * @param WP_REST_Request   $request   Request used to generate the response.
87
		 */
88
		return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
89
	}
90
91
	/**
92
	 * Get the Category schema, conforming to JSON Schema.
93
	 *
94
	 * @return array
95
	 */
96 426
	public function get_item_schema() {
97
		$schema = array(
98 426
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
99 426
			'title'      => $this->taxonomy,
100 426
			'type'       => 'object',
101
			'properties' => array(
102
				'id'          => array(
103 426
					'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
104 426
					'type'        => 'integer',
105
					'context'     => array( 'view', 'edit' ),
106
					'readonly'    => true,
107
				),
108
				'name'        => array(
109 426
					'description' => __( 'Category name.', 'woocommerce' ),
110 426
					'type'        => 'string',
111
					'context'     => array( 'view', 'edit' ),
112
					'arg_options' => array(
113
						'sanitize_callback' => 'sanitize_text_field',
114
					),
115
				),
116
				'slug'        => array(
117 426
					'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woocommerce' ),
118 426
					'type'        => 'string',
119
					'context'     => array( 'view', 'edit' ),
120
					'arg_options' => array(
121
						'sanitize_callback' => 'sanitize_title',
122
					),
123
				),
124
				'parent'      => array(
125 426
					'description' => __( 'The ID for the parent of the resource.', 'woocommerce' ),
126 426
					'type'        => 'integer',
127
					'context'     => array( 'view', 'edit' ),
128
				),
129
				'description' => array(
130 426
					'description' => __( 'HTML description of the resource.', 'woocommerce' ),
131 426
					'type'        => 'string',
132
					'context'     => array( 'view', 'edit' ),
133
					'arg_options' => array(
134
						'sanitize_callback' => 'wp_filter_post_kses',
135
					),
136
				),
137
				'display'     => array(
138 426
					'description' => __( 'Category archive display type.', 'woocommerce' ),
139 426
					'type'        => 'string',
140 426
					'default'     => 'default',
141
					'enum'        => array( 'default', 'products', 'subcategories', 'both' ),
142
					'context'     => array( 'view', 'edit' ),
143
				),
144
				'image'       => array(
145 426
					'description' => __( 'Image data.', 'woocommerce' ),
146 426
					'type'        => 'object',
147
					'context'     => array( 'view', 'edit' ),
148
					'properties'  => array(
149
						'id'                => array(
150 426
							'description' => __( 'Image ID.', 'woocommerce' ),
151 426
							'type'        => 'integer',
152
							'context'     => array( 'view', 'edit' ),
153
						),
154
						'date_created'      => array(
155 426
							'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ),
156 426
							'type'        => 'date-time',
157
							'context'     => array( 'view', 'edit' ),
158
							'readonly'    => true,
159
						),
160
						'date_created_gmt'  => array(
161 426
							'description' => __( 'The date the image was created, as GMT.', 'woocommerce' ),
162 426
							'type'        => 'date-time',
163
							'context'     => array( 'view', 'edit' ),
164
							'readonly'    => true,
165
						),
166
						'date_modified'     => array(
167 426
							'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ),
168 426
							'type'        => 'date-time',
169
							'context'     => array( 'view', 'edit' ),
170
							'readonly'    => true,
171
						),
172
						'date_modified_gmt' => array(
173 426
							'description' => __( 'The date the image was last modified, as GMT.', 'woocommerce' ),
174 426
							'type'        => 'date-time',
175
							'context'     => array( 'view', 'edit' ),
176
							'readonly'    => true,
177
						),
178
						'src'               => array(
179 426
							'description' => __( 'Image URL.', 'woocommerce' ),
180 426
							'type'        => 'string',
181 426
							'format'      => 'uri',
182
							'context'     => array( 'view', 'edit' ),
183
						),
184
						'title'             => array(
185 426
							'description' => __( 'Image name.', 'woocommerce' ),
186 426
							'type'        => 'string',
187
							'context'     => array( 'view', 'edit' ),
188
						),
189
						'alt'               => array(
190 426
							'description' => __( 'Image alternative text.', 'woocommerce' ),
191 426
							'type'        => 'string',
192
							'context'     => array( 'view', 'edit' ),
193
						),
194
					),
195
				),
196
				'menu_order'  => array(
197 426
					'description' => __( 'Menu order, used to custom sort the resource.', 'woocommerce' ),
198 426
					'type'        => 'integer',
199
					'context'     => array( 'view', 'edit' ),
200
				),
201
				'count'       => array(
202 426
					'description' => __( 'Number of published products for the resource.', 'woocommerce' ),
203 426
					'type'        => 'integer',
204
					'context'     => array( 'view', 'edit' ),
205
					'readonly'    => true,
206
				),
207
			),
208
		);
209
210 426
		return $this->add_additional_fields_schema( $schema );
211
	}
212
}
213