| 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 WP_REST_Request; |
| 8 |
use Booktics\Extensions\Extension_Icon; |
| 9 |
|
| 10 |
/** |
| 11 |
* Module Controller |
| 12 |
* |
| 13 |
* @package Booktics/Module |
| 14 |
*/ |
| 15 |
class Module_Controller extends Base_Rest_Controller { |
| 16 |
/** |
| 17 |
* The namespace of this controller's route. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $namespace = 'booktics/v1'; |
| 23 |
|
| 24 |
/** |
| 25 |
* The base of this controller's route. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $rest_base = 'module'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Extension manager instance |
| 34 |
* |
| 35 |
* @var ExtensionManager |
| 36 |
*/ |
| 37 |
protected $extensions; |
| 38 |
|
| 39 |
/** |
| 40 |
* Initialize the class and set its properties. |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
$available_modules = [ |
| 44 |
'stripe-addon' => [ |
| 45 |
'name' => 'stripe-addon', |
| 46 |
'slug' => 'stripe-addon', |
| 47 |
'type' => 'module', |
| 48 |
'upgrade' => true, |
| 49 |
'upgrade_link' => 'https://themewinter.com/purchase-history', |
| 50 |
'status' => 'off', |
| 51 |
'is_pro' => false, |
| 52 |
'title' => __( 'Stripe Addon', 'booktics' ), |
| 53 |
'description' => __( 'It allows you to use stripe payment.', 'booktics' ), |
| 54 |
'icon' => Extension_Icon::get( 'booktics-stripe-addon' ), |
| 55 |
'notice' => '', |
| 56 |
'demo_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-stripe-addon', |
| 57 |
'settings_link' => '', |
| 58 |
'doc_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-stripe-addon', |
| 59 |
'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-stripe-addon-1.0.0.zip', |
| 60 |
], |
| 61 |
'google-calendar' => [ |
| 62 |
'name' => 'google-calendar', |
| 63 |
'slug' => 'google-calendar', |
| 64 |
'type' => 'module', |
| 65 |
'upgrade' => true, |
| 66 |
'upgrade_link' => 'https://themewinter.com/purchase-history', |
| 67 |
'status' => 'off', |
| 68 |
'is_pro' => false, |
| 69 |
'title' => __( 'Google Calendar', 'booktics' ), |
| 70 |
'description' => __( 'It allows you to use google calendar.', 'booktics' ), |
| 71 |
'icon' => Extension_Icon::get( 'booktics-google-calendar' ), |
| 72 |
'notice' => '', |
| 73 |
'demo_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-google-calendar', |
| 74 |
'settings_link' => '', |
| 75 |
'doc_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-google-calendar', |
| 76 |
'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-google-calendar-1.0.0.zip', |
| 77 |
], |
| 78 |
]; |
| 79 |
$available_modules = apply_filters('booktics_available_modules', $available_modules); |
| 80 |
|
| 81 |
$this->extensions = new ExtensionManager('booktics_extension_settings', $available_modules); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Register the routes for the objects of the controller. |
| 86 |
* |
| 87 |
* @since 1.0.0 |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
public function register_routes(): void { |
| 92 |
register_rest_route($this->namespace, '/' . $this->rest_base, [ |
| 93 |
[ |
| 94 |
'methods' => \WP_REST_Server::READABLE, |
| 95 |
'callback' => [$this, 'get_items'], |
| 96 |
'permission_callback' => [$this, 'get_items_permissions_check'], |
| 97 |
'args' => [ |
| 98 |
'type' => [ |
| 99 |
'description' => __('Type of extensions to retrieve (module, addon, plugin, all).', 'booktics'), |
| 100 |
'type' => 'string', |
| 101 |
'enum' => ['module', 'addon', 'plugin', 'all'], |
| 102 |
'default' => 'all', |
| 103 |
], |
| 104 |
], |
| 105 |
], |
| 106 |
[ |
| 107 |
'methods' => \WP_REST_Server::EDITABLE, |
| 108 |
'callback' => [$this, 'update_item'], |
| 109 |
'permission_callback' => [$this, 'update_item_permissions_check'], |
| 110 |
], |
| 111 |
]); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Check if a given request has permission to get items. |
| 116 |
* |
| 117 |
* @since 1.0.0 |
| 118 |
* |
| 119 |
* @param WP_REST_Request $request Full data about the request. |
| 120 |
* @return bool True if the request has read access, false otherwise. |
| 121 |
*/ |
| 122 |
public function get_items_permissions_check($request) { |
| 123 |
return current_user_can('manage_options'); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Check if a given request has permission to update items. |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
* |
| 131 |
* @param WP_REST_Request $request Full data about the request. |
| 132 |
* @return bool True if the request has update access, false otherwise. |
| 133 |
*/ |
| 134 |
public function update_item_permissions_check($request) { |
| 135 |
return current_user_can('manage_options'); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get all extensions |
| 140 |
* |
| 141 |
* @since 1.0.0 |
| 142 |
* |
| 143 |
* @param WP_REST_Request $request Full data about the request. |
| 144 |
* @return WP_HTTP_Response Response object on success, or WP_Error object on failure. |
| 145 |
*/ |
| 146 |
public function get_items($request) { |
| 147 |
$type = !empty($request['type']) ? $request['type'] : 'all'; |
| 148 |
|
| 149 |
$types = [ |
| 150 |
'module' => $this->extensions->get_modules(), |
| 151 |
'addon' => $this->extensions->get_addons(), |
| 152 |
'plugin' => $this->extensions->get_plugins(), |
| 153 |
'all' => $this->extensions->get() |
| 154 |
]; |
| 155 |
|
| 156 |
if (!isset($types[$type])) { |
| 157 |
return $this->error( |
| 158 |
__('Invalid extension type', 'booktics'), |
| 159 |
400, |
| 160 |
'invalid_extension_type' |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
foreach ($types[$type] as $data) { |
| 165 |
$modules_list[] = $data; |
| 166 |
} |
| 167 |
|
| 168 |
return $this->response($modules_list); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Update extension status |
| 173 |
* |
| 174 |
* @since 1.0.0 |
| 175 |
* |
| 176 |
* @param WP_REST_Request $request Full data about the request. |
| 177 |
* @return WP_HTTP_Response Response object on success, or WP_Error object on failure. |
| 178 |
*/ |
| 179 |
public function update_item($request) { |
| 180 |
$params = json_decode($request->get_body(), true); |
| 181 |
|
| 182 |
$name = isset($params['name']) ? sanitize_text_field($params['name']) : ''; |
| 183 |
$status = isset($params['status']) ? sanitize_text_field($params['status']) : ''; |
| 184 |
|
| 185 |
$valid_statuses = ['off', 'on', 'install', 'activate', 'deactivate']; |
| 186 |
|
| 187 |
if (empty($name)) { |
| 188 |
return $this->error( |
| 189 |
__('Please enter extension name', 'booktics'), |
| 190 |
422, |
| 191 |
'extension_name_error' |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
if (empty($status)) { |
| 196 |
return $this->error( |
| 197 |
/* translators: %s: extension name */ |
| 198 |
sprintf(__('Please provide status for extension %s', 'booktics'), $name), |
| 199 |
422, |
| 200 |
'extension_status_error' |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
if (!in_array($status, $valid_statuses, true)) { |
| 205 |
return $this->error( |
| 206 |
/* translators: %s: extension status */ |
| 207 |
sprintf(__('Invalid status %s provided', 'booktics'), $status), |
| 208 |
422, |
| 209 |
'invalid_status' |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
$extension = $this->extensions->find($name); |
| 214 |
|
| 215 |
if (!$extension) { |
| 216 |
return $this->error( |
| 217 |
/* translators: %s: extension name */ |
| 218 |
sprintf(__('Extension %s not found', 'booktics'), $name), |
| 219 |
404, |
| 220 |
'extension_not_found' |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
$result = $this->extensions->update($name, $status); |
| 225 |
|
| 226 |
if (false === $result) { |
| 227 |
return $this->error( |
| 228 |
/* translators: %s: extension name */ |
| 229 |
sprintf(__('Could not %s the extension', 'booktics'), $status), |
| 230 |
500, |
| 231 |
'operation_failed' |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
return $this->response([ |
| 236 |
'name' => $name, |
| 237 |
'status' => $status, |
| 238 |
], |
| 239 |
/* translators: %s: extension name */ |
| 240 |
sprintf(__('Extension %s successfully', 'booktics'), $status) |
| 241 |
); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|