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 / Marketing.php
Marketing.php
173 lines 4.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 Marketing Controller
4 *
5 * Handles requests to /marketing.
6 */
7
8 namespace Automattic\WooCommerce\Admin\API;
9
10 use Automattic\WooCommerce\Admin\PluginsHelper;
11 use Automattic\WooCommerce\Internal\Admin\Marketing\MarketingSpecs;
12 use Automattic\WooCommerce\Admin\Features\MarketingRecommendations\Init as MarketingRecommendationsInit;
13
14 defined( 'ABSPATH' ) || exit;
15
16 /**
17 * Marketing Controller.
18 *
19 * @internal
20 * @extends WC_REST_Data_Controller
21 */
22 class Marketing extends \WC_REST_Data_Controller {
23
24 /**
25 * Endpoint namespace.
26 *
27 * @var string
28 */
29 protected $namespace = 'wc-admin';
30
31 /**
32 * Route base.
33 *
34 * @var string
35 */
36 protected $rest_base = 'marketing';
37
38 /**
39 * Register routes.
40 */
41 public function register_routes() {
42 register_rest_route(
43 $this->namespace,
44 '/' . $this->rest_base . '/recommended',
45 array(
46 array(
47 'methods' => \WP_REST_Server::READABLE,
48 'callback' => array( $this, 'get_recommended_plugins' ),
49 'permission_callback' => array( $this, 'get_recommended_plugins_permissions_check' ),
50 'args' => array(
51 'per_page' => $this->get_collection_params()['per_page'],
52 'category' => array(
53 'type' => 'string',
54 'validate_callback' => 'rest_validate_request_arg',
55 'sanitize_callback' => 'sanitize_title_with_dashes',
56 ),
57 ),
58 ),
59 'schema' => array( $this, 'get_public_item_schema' ),
60 )
61 );
62
63 register_rest_route(
64 $this->namespace,
65 '/' . $this->rest_base . '/knowledge-base',
66 array(
67 array(
68 'methods' => \WP_REST_Server::READABLE,
69 'callback' => array( $this, 'get_knowledge_base_posts' ),
70 'permission_callback' => array( $this, 'get_items_permissions_check' ),
71 'args' => array(
72 'category' => array(
73 'type' => 'string',
74 'validate_callback' => 'rest_validate_request_arg',
75 'sanitize_callback' => 'sanitize_title_with_dashes',
76 ),
77 ),
78 ),
79 'schema' => array( $this, 'get_public_item_schema' ),
80 )
81 );
82
83 register_rest_route(
84 $this->namespace,
85 '/' . $this->rest_base . '/misc-recommendations',
86 array(
87 array(
88 'methods' => \WP_REST_Server::READABLE,
89 'callback' => array( $this, 'get_misc_recommendations' ),
90 'permission_callback' => array( $this, 'get_recommended_plugins_permissions_check' ),
91 ),
92 'schema' => array( $this, 'get_public_item_schema' ),
93 )
94 );
95 }
96
97 /**
98 * Check whether a given request has permission to install plugins.
99 *
100 * @param WP_REST_Request $request Full details about the request.
101 * @return WP_Error|boolean
102 */
103 public function get_recommended_plugins_permissions_check( $request ) {
104 if ( ! current_user_can( 'install_plugins' ) ) {
105 return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'You do not have permissions to manage plugins. Please contact your site administrator.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
106 }
107
108 return true;
109 }
110
111
112 /**
113 * Return installed marketing extensions data.
114 *
115 * @param \WP_REST_Request $request Request data.
116 *
117 * @return \WP_Error|\WP_REST_Response
118 */
119 public function get_recommended_plugins( $request ) {
120 // Default to marketing category (if no category set).
121 $category = ( ! empty( $request->get_param( 'category' ) ) ) ? $request->get_param( 'category' ) : 'marketing';
122 $all_plugins = MarketingRecommendationsInit::get_recommended_plugins();
123 $valid_plugins = [];
124 $per_page = $request->get_param( 'per_page' );
125
126 foreach ( $all_plugins as $plugin ) {
127
128 // default to marketing if 'categories' is empty on the plugin object (support for legacy api while testing).
129 $plugin_categories = ( ! empty( $plugin['categories'] ) ) ? $plugin['categories'] : [ 'marketing' ];
130
131 if ( ! PluginsHelper::is_plugin_installed( $plugin['plugin'] ) && in_array( $category, $plugin_categories, true ) ) {
132 $valid_plugins[] = $plugin;
133 }
134 }
135
136 return rest_ensure_response( array_slice( $valid_plugins, 0, $per_page ) );
137 }
138
139 /**
140 * Return installed marketing extensions data.
141 *
142 * @param \WP_REST_Request $request Request data.
143 *
144 * @return \WP_Error|\WP_REST_Response
145 */
146 public function get_knowledge_base_posts( $request ) {
147 /**
148 * MarketingSpecs class.
149 *
150 * @var MarketingSpecs $marketing_specs
151 */
152 $marketing_specs = wc_get_container()->get( MarketingSpecs::class );
153
154 $category = $request->get_param( 'category' );
155 return rest_ensure_response( $marketing_specs->get_knowledge_base_posts( $category ) );
156 }
157
158 /**
159 * Return misc recommendations.
160 *
161 * @param \WP_REST_Request $request Request data.
162 *
163 * @since 9.5.0
164 *
165 * @return \WP_Error|\WP_REST_Response
166 */
167 public function get_misc_recommendations( $request ) {
168 $misc_recommendations = MarketingRecommendationsInit::get_misc_recommendations();
169
170 return rest_ensure_response( $misc_recommendations );
171 }
172 }
173