PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / trunk
WCPOS – Point of Sale (POS) plugin for WooCommerce vtrunk
1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 1.9.13 1.9.12 1.9.11 1.9.10 1.9.9 All 158 releases
woocommerce-pos / includes / API / V1 / Extensions.php

Extensions.php in WCPOS – Point of Sale (POS) plugin for WooCommerce trunk, at includes/API/V1/Extensions.php

119 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Extensions REST API controller.
4 *
5 * Serves the extension catalog with local plugin status.
6 *
7 * @package WCPOS\WooCommercePOS\API\V1
8 */
9
10 namespace WCPOS\WooCommercePOS\API\V1;
11
12 use WCPOS\WooCommercePOS\Services\Extensions as ExtensionsService;
13 use WP_REST_Controller;
14 use WP_REST_Request;
15 use WP_REST_Response;
16 use WP_REST_Server;
17
18 use const WCPOS\WooCommercePOS\SHORT_NAME;
19
20 /**
21 * Extensions controller class.
22 */
23 class Extensions extends WP_REST_Controller {
24
25 /**
26 * Route namespace.
27 *
28 * @var string
29 */
30 protected $namespace = SHORT_NAME . '/v1';
31
32 /**
33 * Route base.
34 *
35 * @var string
36 */
37 protected $rest_base = 'extensions';
38
39 /**
40 * Register routes.
41 *
42 * @return void
43 */
44 public function register_routes(): void {
45 register_rest_route(
46 $this->namespace,
47 '/' . $this->rest_base,
48 array(
49 'methods' => WP_REST_Server::READABLE,
50 'callback' => array( $this, 'get_items' ),
51 'permission_callback' => array( $this, 'check_permissions' ),
52 'args' => array(
53 'force' => array(
54 'type' => 'boolean',
55 'default' => false,
56 'sanitize_callback' => 'rest_sanitize_boolean',
57 ),
58 ),
59 )
60 );
61 }
62
63 /**
64 * Get all extensions with status.
65 *
66 * @param WP_REST_Request $request Request object.
67 *
68 * @return WP_REST_Response
69 */
70 public function get_items( $request ): WP_REST_Response {
71 $service = ExtensionsService::instance();
72 $force = $request->get_param( 'force' );
73 if ( $force && ! $service->clear_cache() ) {
74 return new WP_REST_Response(
75 array(
76 'code' => 'wcpos_extensions_cache_clear_failed',
77 'message' => __( 'Could not clear the extensions cache.', 'woocommerce-pos' ),
78 ),
79 500
80 );
81 }
82
83 $extensions = $service->get_extensions();
84 if ( $force && empty( $extensions ) ) {
85 return new WP_REST_Response(
86 array(
87 'code' => 'wcpos_extensions_refresh_failed',
88 'message' => __( 'Could not refresh extensions. Please try again.', 'woocommerce-pos' ),
89 ),
90 502
91 );
92 }
93
94 $response = new WP_REST_Response( $extensions );
95 $response->header( 'X-WP-Total', (string) \count( $extensions ) );
96
97 return $response;
98 }
99
100 /**
101 * Check if the current user has permission.
102 *
103 * @param WP_REST_Request $request Request object.
104 *
105 * @return bool|\WP_Error
106 */
107 public function check_permissions( WP_REST_Request $request ) {
108 if ( ! current_user_can( 'manage_woocommerce_pos' ) ) {
109 return new \WP_Error(
110 'rest_forbidden',
111 __( 'You do not have permission to view extensions.', 'woocommerce-pos' ),
112 array( 'status' => rest_authorization_required_code() )
113 );
114 }
115
116 return true;
117 }
118 }
119