PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.19
Booktics – Appointment Booking Calendar for Service Businesses v1.0.19
1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 All 27 releases
booktics / core / extensions / controllers / extension-controller.php

extension-controller.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.19, at core/extensions/controllers/extension-controller.php

206 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics\Extensions\Controllers;
4
5 use Booktics\Abstracts\Base_Rest_Controller;
6 use Booktics\Extensions\Plugin_Manager;
7 use Arraytics\ToolsSdk\PluginManager as SdkPluginManager;
8 use WP_HTTP_Response;
9
10 class Extension_Controller extends Base_Rest_Controller {
11 /**
12 * Store namespace of api
13 *
14 * @var string
15 */
16 protected $namespace = 'booktics/v1';
17
18 /**
19 * Store base url
20 *
21 * @var string
22 */
23 protected $base = 'extensions';
24
25 /**
26 *
27 *
28 * @return void
29 */
30 public function register_routes(): void {
31 register_rest_route(
32 $this->namespace, $this->base, array(
33 array(
34 'methods' => \WP_REST_Server::READABLE,
35 'callback' => array( $this, 'get_items' ),
36 'permission_callback' => array(
37 $this,
38 'get_items_permissions_check',
39 ),
40 ),
41 )
42 );
43
44 register_rest_route(
45 $this->namespace, $this->base, array(
46 array(
47 'methods' => \WP_REST_Server::EDITABLE,
48 'callback' => array( $this, 'update_item' ),
49 'permission_callback' => array(
50 $this,
51 'update_item_permissions_check',
52 ),
53 ),
54 )
55 );
56 }
57
58 /**
59 * Check if a given request has access to get items
60 *
61 * @param $request
62 *
63 * @return bool|\WP_Error
64 */
65 public function get_items_permissions_check( $request ) {
66 return current_user_can( 'manage_options' );
67 }
68
69 /**
70 * Get all extensions
71 *
72 * @param $request
73 *
74 * @return WP_HTTP_Response
75 */
76 public function get_items( $request ) {
77 $type = ! empty( $request['type'] ) ? sanitize_key( $request['type'] ) : 'addon';
78
79 $extensions = array_values(
80 array_filter(
81 Plugin_Manager::extensions(),
82 function ( $extension ) use ( $type ) {
83 return $extension['type'] === $type;
84 }
85 )
86 );
87
88 $extensions = array_map(
89 function ( $extension ) {
90 $extension['status'] = 'install';
91 if ( SdkPluginManager::is_installed( $extension['slug'] ) ) {
92 $extension['status'] = 'activate';
93 }
94 if ( SdkPluginManager::is_activated( $extension['slug'] ) ) {
95 $extension['status'] = 'deactivate';
96 }
97
98 return $extension;
99 }, $extensions
100 );
101
102 return $this->response( $extensions );
103 }
104
105 /**
106 * Check if a given request has access to get items
107 *
108 * @param $request
109 *
110 * @return bool|\WP_Error
111 */
112 public function update_item_permissions_check( $request ) {
113 return current_user_can( 'manage_options' );
114 }
115
116 /**
117 * Enable or disable extension
118 *
119 * @param $request
120 *
121 * @return WP_HTTP_Response
122 */
123 public function update_item( $request ) {
124 $input_data = json_decode( $request->get_body(), true );
125 $name = $input_data['name'];
126 $status = $input_data['status'];
127
128 $statuses = array( 'off', 'on', 'install', 'activate', 'deactivate' );
129 $extensions = Plugin_Manager::extensions();
130 if ( ! $name ) {
131 return $this->error( __( 'Please enter extension name', 'booktics' ) );
132 }
133
134 if ( ! $status ) {
135 return $this->error( __( 'Please enter status', 'booktics' ) );
136 }
137
138 if ( ! in_array( $status, $statuses ) ) {
139 return $this->error( __( 'Please enter status on/off', 'booktics' ) );
140 }
141
142 if ( ! isset( $extensions[ $name ] ) ) {
143 return $this->error( __( 'Invalid extension', 'booktics' ) );
144 }
145 $extension = $extensions[ $name ];
146 if ( 'addon' === $extension['type'] ) {
147 $result = null;
148
149 if ( in_array( $status, array( 'install', 'activate' ) ) ) {
150 $result = Plugin_Manager::install_plugin( $extension['slug'] );
151 if ( $result ) {
152 $result = Plugin_Manager::activate_plugin( $extension['slug'] );
153 }
154 }
155
156 if ( 'deactivate' === $status && Plugin_Manager::is_activated( $extension['slug'] ) ) {
157 $result = Plugin_Manager::deactivate_plugin( $extension['slug'] );
158 }
159
160 if ( is_wp_error( $result ) ) {
161 return $this->error( __( 'Extension installation error', 'booktics' ) );
162 }
163
164 if ( ! $result ) {
165 // translators: %s is the action performed on the extension (install, activate, deactivate, etc.)
166 return $this->error( sprintf( __( 'Extension couldn\'t %s', 'booktics' ), $status ) );
167 }
168 }
169
170 if ( 'plugin' === $extension['type'] ) {
171 $result = null;
172
173 if ( 'install' === $status ) {
174 $result = SdkPluginManager::install_plugin( $extension['slug'] );
175 if ( $result ) {
176 $result = SdkPluginManager::activate_plugin( $extension['slug'] );
177 }
178 } elseif ( 'activate' === $status ) {
179 $result = SdkPluginManager::activate_plugin( $extension['slug'] );
180 } elseif ( 'deactivate' === $status ) {
181 $result = SdkPluginManager::deactivate_plugin( $extension['slug'] );
182 }
183
184 if ( is_wp_error( $result ) ) {
185 return $this->error( __( 'Plugin operation error', 'booktics' ) );
186 }
187
188 if ( false === $result ) {
189 // translators: %s is the action performed on the plugin (install, activate, deactivate, etc.)
190 return $this->error( sprintf( __( 'Plugin couldn\'t %s', 'booktics' ), $status ) );
191 }
192 }
193
194 $response = Plugin_Manager::extensions()[ $name ];
195 $response['status'] = 'install';
196 if ( SdkPluginManager::is_installed( $response['slug'] ) ) {
197 $response['status'] = 'activate';
198 }
199 if ( SdkPluginManager::is_activated( $response['slug'] ) ) {
200 $response['status'] = 'deactivate';
201 }
202
203 return $this->response( $response, __( 'Successfully updated', 'booktics' ) );
204 }
205 }
206