PluginProbe
WooCommerce / 11.1.0-beta.1
WooCommerce v11.1.0-beta.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 / Admin / API / MarketingRecommendations.php
MarketingRecommendations.php
229 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API MarketingRecommendations Controller
4 *
5 * Handles requests to /marketing/recommendations.
6 */
7
8 namespace Automattic\WooCommerce\Admin\API;
9
10 use Automattic\WooCommerce\Admin\Features\MarketingRecommendations\Init as MarketingRecommendationsInit;
11 use WC_REST_Controller;
12 use WP_Error;
13 use WP_REST_Request;
14 use WP_REST_Response;
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * MarketingRecommendations Controller.
20 *
21 * @internal
22 * @extends WC_REST_Controller
23 * @since x.x.x
24 */
25 class MarketingRecommendations extends WC_REST_Controller {
26
27 /**
28 * Endpoint namespace.
29 *
30 * @var string
31 */
32 protected $namespace = 'wc-admin';
33
34 /**
35 * Route base.
36 *
37 * @var string
38 */
39 protected $rest_base = 'marketing/recommendations';
40
41 /**
42 * Register routes.
43 */
44 public function register_routes() {
45 register_rest_route(
46 $this->namespace,
47 '/' . $this->rest_base,
48 [
49 [
50 'methods' => \WP_REST_Server::READABLE,
51 'callback' => [ $this, 'get_items' ],
52 'permission_callback' => [ $this, 'get_items_permissions_check' ],
53 'args' => [
54 'category' => [
55 'type' => 'string',
56 'validate_callback' => 'rest_validate_request_arg',
57 'sanitize_callback' => 'sanitize_title_with_dashes',
58 'enum' => [ 'channels', 'extensions' ],
59 'required' => true,
60 ],
61 ],
62 ],
63 'schema' => [ $this, 'get_public_item_schema' ],
64 ]
65 );
66 }
67
68 /**
69 * Check whether a given request has permission to view marketing recommendations.
70 *
71 * @param WP_REST_Request $request Full details about the request.
72 *
73 * @return WP_Error|boolean
74 */
75 public function get_items_permissions_check( $request ) {
76 if ( ! current_user_can( 'install_plugins' ) ) {
77 return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view marketing channels.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
78 }
79
80 return true;
81 }
82
83 /**
84 * Retrieves a collection of recommendations.
85 *
86 * @param WP_REST_Request $request Full details about the request.
87 *
88 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
89 */
90 public function get_items( $request ) {
91 $category = $request->get_param( 'category' );
92 if ( 'channels' === $category ) {
93 $items = MarketingRecommendationsInit::get_recommended_marketing_channels();
94 } elseif ( 'extensions' === $category ) {
95 $items = MarketingRecommendationsInit::get_recommended_marketing_extensions_excluding_channels();
96 } else {
97 return new WP_Error( 'woocommerce_rest_invalid_category', __( 'The specified category for recommendations is invalid. Allowed values: "channels", "extensions".', 'woocommerce' ), array( 'status' => 400 ) );
98 }
99
100 $responses = [];
101 foreach ( $items as $item ) {
102 $response = $this->prepare_item_for_response( $item, $request );
103 $responses[] = $this->prepare_response_for_collection( $response );
104 }
105
106 return rest_ensure_response( $responses );
107 }
108
109 /**
110 * Prepares the item for the REST response.
111 *
112 * @param array $item WordPress representation of the item.
113 * @param WP_REST_Request $request Request object.
114 *
115 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
116 */
117 public function prepare_item_for_response( $item, $request ) {
118 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
119 $data = $this->add_additional_fields_to_object( $item, $request );
120 $data = $this->filter_response_by_context( $data, $context );
121
122 return rest_ensure_response( $data );
123 }
124
125 /**
126 * Retrieves the item's schema, conforming to JSON Schema.
127 *
128 * @return array Item schema data.
129 */
130 public function get_item_schema() {
131 $schema = [
132 '$schema' => 'http://json-schema.org/draft-04/schema#',
133 'title' => 'marketing_recommendation',
134 'type' => 'object',
135 'properties' => [
136 'title' => [
137 'type' => 'string',
138 'context' => [ 'view' ],
139 'readonly' => true,
140 ],
141 'description' => [
142 'type' => 'string',
143 'context' => [ 'view' ],
144 'readonly' => true,
145 ],
146 'url' => [
147 'type' => 'string',
148 'context' => [ 'view' ],
149 'readonly' => true,
150 ],
151 'direct_install' => [
152 'type' => 'string',
153 'context' => [ 'view' ],
154 'readonly' => true,
155 ],
156 'icon' => [
157 'type' => 'string',
158 'context' => [ 'view' ],
159 'readonly' => true,
160 ],
161 'product' => [
162 'type' => 'string',
163 'context' => [ 'view' ],
164 'readonly' => true,
165 ],
166 'plugin' => [
167 'type' => 'string',
168 'context' => [ 'view' ],
169 'readonly' => true,
170 ],
171 'categories' => [
172 'type' => 'array',
173 'context' => [ 'view' ],
174 'readonly' => true,
175 'items' => [
176 'type' => 'string',
177 ],
178 ],
179 'subcategories' => [
180 'type' => 'array',
181 'context' => [ 'view' ],
182 'readonly' => true,
183 'items' => [
184 'type' => 'object',
185 'context' => [ 'view' ],
186 'readonly' => true,
187 'properties' => [
188 'slug' => [
189 'type' => 'string',
190 'context' => [ 'view' ],
191 'readonly' => true,
192 ],
193 'name' => [
194 'type' => 'string',
195 'context' => [ 'view' ],
196 'readonly' => true,
197 ],
198 ],
199 ],
200 ],
201 'tags' => [
202 'type' => 'array',
203 'context' => [ 'view' ],
204 'readonly' => true,
205 'items' => [
206 'type' => 'object',
207 'context' => [ 'view' ],
208 'readonly' => true,
209 'properties' => [
210 'slug' => [
211 'type' => 'string',
212 'context' => [ 'view' ],
213 'readonly' => true,
214 ],
215 'name' => [
216 'type' => 'string',
217 'context' => [ 'view' ],
218 'readonly' => true,
219 ],
220 ],
221 ],
222 ],
223 ],
224 ];
225
226 return $this->add_additional_fields_schema( $schema );
227 }
228 }
229