PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.0
Booktics – Appointment Booking Calendar for Service Businesses v1.0.0
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.0, at core/extensions/controllers/extension-controller.php

175 lines 4.7 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 WP_HTTP_Response;
8
9 class Extension_Controller extends Base_Rest_Controller {
10 /**
11 * Store namespace of api
12 *
13 * @var string
14 */
15 protected $namespace = 'booktics/v1';
16
17 /**
18 * Store base url
19 *
20 * @var string
21 */
22 protected $base = 'extensions';
23
24 /**
25 *
26 *
27 * @return void
28 */
29 public function register_routes(): void {
30 register_rest_route(
31 $this->namespace, $this->base, array(
32 array(
33 'methods' => \WP_REST_Server::READABLE,
34 'callback' => array( $this, 'get_items' ),
35 'permission_callback' => array(
36 $this,
37 'get_items_permissions_check',
38 ),
39 ),
40 )
41 );
42
43 register_rest_route(
44 $this->namespace, $this->base, array(
45 array(
46 'methods' => \WP_REST_Server::EDITABLE,
47 'callback' => array( $this, 'update_item' ),
48 'permission_callback' => array(
49 $this,
50 'update_item_permissions_check',
51 ),
52 ),
53 )
54 );
55 }
56
57 /**
58 * Check if a given request has access to get items
59 *
60 * @param $request
61 *
62 * @return bool|\WP_Error
63 */
64 public function get_items_permissions_check( $request ) {
65 return true;
66 }
67
68 /**
69 * Get all extensions
70 *
71 * @param $request
72 *
73 * @return WP_HTTP_Response
74 */
75 public function get_items( $request ) {
76 $type = ! empty( $request['type'] ) ? $request['type'] : 'addon';
77
78 $extensions = array_values(
79 array_filter(
80 Plugin_Manager::extensions(),
81 function ( $extension ) use ( $type ) {
82 return $extension['type'] === $type;
83 }
84 )
85 );
86
87 $extensions = array_map(
88 function ( $extension ) {
89 if ( Plugin_Manager::is_installed( $extension['name'] ) ) {
90 $extension['status'] = 'installed';
91 }
92 if ( Plugin_Manager::is_activated( $extension['name'] ) ) {
93 $extension['status'] = 'activated';
94 }
95
96 return $extension;
97 }, $extensions
98 );
99
100 return $this->response( $extensions );
101 }
102
103 /**
104 * Check if a given request has access to get items
105 *
106 * @param $request
107 *
108 * @return bool|\WP_Error
109 */
110 public function update_item_permissions_check( $request ) {
111 return true;
112 }
113
114 /**
115 * Enable or disable extension
116 *
117 * @param $request
118 *
119 * @return WP_HTTP_Response
120 */
121 public function update_item( $request ) {
122 $input_data = json_decode( $request->get_body(), true );
123 $name = $input_data['name'];
124 $status = $input_data['status'];
125
126 $statuses = array( 'off', 'on', 'install', 'activate', 'deactivate' );
127 $extensions = Plugin_Manager::extensions();
128 if ( ! $name ) {
129 return $this->error( __( 'Please enter extension name', 'booktics' ) );
130 }
131
132 if ( ! $status ) {
133 return $this->error( __( 'Please enter status', 'booktics' ) );
134 }
135
136 if ( ! in_array( $status, $statuses ) ) {
137 return $this->error( __( 'Please enter status on/off', 'booktics' ) );
138 }
139
140 if ( ! isset( $extensions[ $name ] ) ) {
141 return $this->error( __( 'Invalid extension', 'booktics' ) );
142 }
143 $extension = $extensions[ $name ];
144 if ( 'addon' === $extension['type'] ) {
145 $result = null;
146
147 if ( in_array( $status, array( 'install', 'activate' ) ) ) {
148 $result = Plugin_Manager::install_plugin( $name );
149 $result = Plugin_Manager::activate_plugin( $name );
150 }
151
152 if ( 'deactivate' === $status && Plugin_Manager::is_activated( $name ) ) {
153 $result = Plugin_Manager::deactivate_plugin( $name );
154 }
155
156 if ( is_wp_error( $result ) ) {
157 return $this->error( __( 'Extension installation error', 'booktics' ) );
158 }
159
160 if ( ! $result ) {
161 return $this->error( __( "Extension couldn't " . $status, 'booktics' ) );
162 }
163 }
164 $response = Plugin_Manager::extensions()[ $name ];
165 if ( Plugin_Manager::is_installed( $response['name'] ) ) {
166 $response['status'] = 'installed';
167 }
168 if ( Plugin_Manager::is_activated( $response['name'] ) ) {
169 $response['status'] = 'activated';
170 }
171
172 return $this->response( $response, __( 'Successfully updated', 'booktics' ) );
173 }
174 }
175