| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Extensions\Controllers; |
| 4 |
|
| 5 |
use Booktics\Abstracts\Base_Rest_Controller; |
| 6 |
use Arraytics\ToolsSdk\Extension as ExtensionManager; |
| 7 |
use Arraytics\ToolsSdk\PluginManager; |
| 8 |
use WP_HTTP_Response; |
| 9 |
use WP_REST_Request; |
| 10 |
|
| 11 |
/** |
| 12 |
* Module Controller |
| 13 |
* |
| 14 |
* @package Booktics/Module |
| 15 |
*/ |
| 16 |
class Module_Controller extends Base_Rest_Controller { |
| 17 |
/** |
| 18 |
* The namespace of this controller's route. |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $namespace = 'booktics/v1'; |
| 24 |
|
| 25 |
/** |
| 26 |
* The base of this controller's route. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $rest_base = 'module'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Extension manager instance |
| 35 |
* |
| 36 |
* @var ExtensionManager |
| 37 |
*/ |
| 38 |
protected $extensions; |
| 39 |
|
| 40 |
/** |
| 41 |
* Initialize the class and set its properties. |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
$this->extensions = new ExtensionManager('booktics_available_modules', booktics_modules_list()); |
| 45 |
$this->plugin_manager = new PluginManager(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Register the routes for the objects of the controller. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function register_routes(): void { |
| 56 |
register_rest_route($this->namespace, '/' . $this->rest_base, [ |
| 57 |
[ |
| 58 |
'methods' => \WP_REST_Server::READABLE, |
| 59 |
'callback' => [$this, 'get_items'], |
| 60 |
'permission_callback' => [$this, 'get_items_permissions_check'], |
| 61 |
'args' => [ |
| 62 |
'type' => [ |
| 63 |
'description' => __('Type of extensions to retrieve (module, addon, plugin, all).', 'booktics'), |
| 64 |
'type' => 'string', |
| 65 |
'enum' => ['module', 'addon', 'plugin', 'all'], |
| 66 |
'default' => 'all', |
| 67 |
], |
| 68 |
], |
| 69 |
], |
| 70 |
[ |
| 71 |
'methods' => \WP_REST_Server::EDITABLE, |
| 72 |
'callback' => [$this, 'update_item'], |
| 73 |
'permission_callback' => [$this, 'update_item_permissions_check'], |
| 74 |
], |
| 75 |
]); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Check if a given request has permission to get items. |
| 80 |
* |
| 81 |
* @since 1.0.0 |
| 82 |
* |
| 83 |
* @param WP_REST_Request $request Full data about the request. |
| 84 |
* @return bool True if the request has read access, false otherwise. |
| 85 |
*/ |
| 86 |
public function get_items_permissions_check($request) { |
| 87 |
return current_user_can('manage_options'); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Check if a given request has permission to update items. |
| 92 |
* |
| 93 |
* @since 1.0.0 |
| 94 |
* |
| 95 |
* @param WP_REST_Request $request Full data about the request. |
| 96 |
* @return bool True if the request has update access, false otherwise. |
| 97 |
*/ |
| 98 |
public function update_item_permissions_check($request) { |
| 99 |
return current_user_can('manage_options'); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get all extensions |
| 104 |
* |
| 105 |
* @since 1.0.0 |
| 106 |
* |
| 107 |
* @param WP_REST_Request $request Full data about the request. |
| 108 |
* @return WP_HTTP_Response Response object on success, or WP_Error object on failure. |
| 109 |
*/ |
| 110 |
public function get_items($request) { |
| 111 |
$type = ! empty( $request['type'] ) ? sanitize_key( $request['type'] ) : 'all'; |
| 112 |
|
| 113 |
$types = [ |
| 114 |
'module' => $this->extensions->get_modules(), |
| 115 |
'addon' => $this->extensions->get_addons(), |
| 116 |
'plugin' => $this->extensions->get_plugins(), |
| 117 |
'all' => $this->extensions->get() |
| 118 |
]; |
| 119 |
|
| 120 |
if (!isset($types[$type])) { |
| 121 |
return $this->error( |
| 122 |
__('Invalid extension type', 'booktics'), |
| 123 |
400, |
| 124 |
'invalid_extension_type' |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
foreach ($types[$type] as $data) { |
| 129 |
$modules_list[] = $data; |
| 130 |
} |
| 131 |
|
| 132 |
return $this->response($modules_list); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Update extension status |
| 137 |
* |
| 138 |
* @since 1.0.0 |
| 139 |
* |
| 140 |
* @param WP_REST_Request $request Full data about the request. |
| 141 |
* @return WP_HTTP_Response Response object on success, or WP_Error object on failure. |
| 142 |
*/ |
| 143 |
public function update_item($request) { |
| 144 |
$params = json_decode( $request->get_body(), true); |
| 145 |
|
| 146 |
$name = isset($params['name']) ? sanitize_text_field($params['name']) : ''; |
| 147 |
$status = isset($params['status']) ? sanitize_text_field($params['status']) : ''; |
| 148 |
|
| 149 |
$valid_statuses = ['off', 'on', 'install', 'activate', 'deactivate', 'upgrade']; |
| 150 |
|
| 151 |
if (empty($name)) { |
| 152 |
return $this->error( |
| 153 |
__('Please enter extension name', 'booktics'), |
| 154 |
422, |
| 155 |
'extension_name_error' |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
if (empty($status)) { |
| 160 |
return $this->error( |
| 161 |
/* translators: %s: extension name */ |
| 162 |
sprintf(__('Please provide status for extension %s', 'booktics'), $name), |
| 163 |
422, |
| 164 |
'extension_status_error' |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
if (!in_array($status, $valid_statuses, true)) { |
| 169 |
return $this->error( |
| 170 |
/* translators: %s: extension status */ |
| 171 |
sprintf(__('Invalid status %s provided', 'booktics'), $status), |
| 172 |
422, |
| 173 |
'invalid_status' |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
$extension = $this->extensions->find($name); |
| 178 |
|
| 179 |
if (!$extension) { |
| 180 |
return $this->error( |
| 181 |
/* translators: %s: extension name */ |
| 182 |
sprintf(__('Extension %s not found', 'booktics'), $name), |
| 183 |
404, |
| 184 |
'extension_not_found' |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
if ( 'module' == $extension['type'] ) { |
| 189 |
$result = $this->extensions->update($name, $status); |
| 190 |
|
| 191 |
if (false === $result) { |
| 192 |
return $this->error( |
| 193 |
/* translators: %s: extension name */ |
| 194 |
sprintf(__('Could not turn %s the module', 'booktics'), $status), |
| 195 |
500, |
| 196 |
'operation_failed' |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
$this->update_relevant_settings($name, $status); |
| 201 |
|
| 202 |
return $this->response( [ |
| 203 |
'name' => $name, |
| 204 |
'status' => $status |
| 205 |
], |
| 206 |
/* translators: %s: extension name */ |
| 207 |
sprintf( __('Module turned %s successfully', 'booktics'), $status) ); |
| 208 |
} |
| 209 |
|
| 210 |
// When updating plugin status using tools-sdk change actual plugin status |
| 211 |
if ( 'plugin' === $extension['type'] ) { |
| 212 |
switch ( $status ) { |
| 213 |
case 'install': |
| 214 |
$result = $this->plugin_manager->install_plugin($name); |
| 215 |
break; |
| 216 |
case 'activate': |
| 217 |
$result = $this->plugin_manager->activate_plugin($name); |
| 218 |
break; |
| 219 |
case 'deactivate': |
| 220 |
$result = $this->plugin_manager->deactivate_plugin($name); |
| 221 |
break; |
| 222 |
case 'upgrade': |
| 223 |
return $this->response( [ 'redirect_url' => $extension['upgrade_link'] ] ); |
| 224 |
break; |
| 225 |
} |
| 226 |
|
| 227 |
if ( false === $result ) { |
| 228 |
return $this->error( |
| 229 |
/* translators: %s: extension name */ |
| 230 |
sprintf(__('Could not %s the extension', 'booktics'), $status), |
| 231 |
500, |
| 232 |
'operation_failed' |
| 233 |
); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
return $this->response([ |
| 238 |
'name' => $name, |
| 239 |
'status' => $status, |
| 240 |
], |
| 241 |
/* translators: %s: extension name */ |
| 242 |
sprintf(__('Extension %s successfully', 'booktics'), $status) |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Update relevant settings based on extension status |
| 248 |
* |
| 249 |
* @since 1.0.0 |
| 250 |
* |
| 251 |
* @param string $extension_name Extension name. |
| 252 |
* @param string $extension_status Extension status. |
| 253 |
* @return void |
| 254 |
*/ |
| 255 |
public function update_relevant_settings($extension_name, $extension_status) { |
| 256 |
$is_enabled = $extension_status === 'on'; |
| 257 |
|
| 258 |
switch ($extension_name) { |
| 259 |
case 'stripe': |
| 260 |
booktics_update_option('stripe_addon_install_status', $is_enabled ? 1 : 0); |
| 261 |
booktics_update_option('enable_stripe', $is_enabled); |
| 262 |
break; |
| 263 |
|
| 264 |
case 'paypal': |
| 265 |
booktics_update_option('paypal_connected', $is_enabled); |
| 266 |
booktics_update_option('enable_paypal', $is_enabled); |
| 267 |
break; |
| 268 |
|
| 269 |
case 'google-calendar': |
| 270 |
booktics_update_option('google_redirect_url', booktics_get_auth_redirect_uri('google-auth')); |
| 271 |
booktics_update_option('google_calendar_connected', false); |
| 272 |
booktics_update_option('enable_google_calendar', $is_enabled); |
| 273 |
break; |
| 274 |
|
| 275 |
case 'outlook-calendar': |
| 276 |
booktics_update_option('microsoft_redirect_url', booktics_get_auth_redirect_uri('microsoft-auth')); |
| 277 |
booktics_update_option('enable_outlook_calendar', $is_enabled); |
| 278 |
break; |
| 279 |
|
| 280 |
case 'fluentcrm': |
| 281 |
booktics_update_option('enable_fluentcrm', $is_enabled); |
| 282 |
break; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|