PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.2
Booktics – Appointment Booking Calendar for Service Businesses v1.0.2
1.0.29 1.0.28 1.0.27 1.0.26 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 All 31 releases
booktics / vendor / arraytics / tools-sdk / src / Extension.php

Extension.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.2, at vendor/arraytics/tools-sdk/src/Extension.php

227 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 * Extensions class for managing modules, addons, and plugins.
4 *
5 * @package Arraytics\ToolsSdk
6 */
7 namespace Arraytics\ToolsSdk;
8
9 /**
10 * Class Extension
11 *
12 * Handles registration and retrieval of plugin-related extensions such as modules, plugins, and addons.
13 */
14 class Extension {
15 /**
16 * The option name used to store extension statuses in WordPress options table.
17 *
18 * @var string
19 */
20 protected $option_name = 'extensions';
21
22 /**
23 * List of registered extensions.
24 *
25 * @var array
26 */
27 protected $extensions = [];
28
29 /**
30 * Boot the extension system with provided option name and extensions list.
31 *
32 * @param string $option_name The option key used to save extension settings.
33 * @param array $extensions List of available extensions.
34 */
35 public function __construct($option_name, $extensions) {
36 $this->option_name = $option_name;
37 $this->extensions = apply_filters('plugin_utility_manager/extensions', $extensions);
38 }
39
40 /**
41 * Get all registered extensions with resolved status (on, off, upgrade, install, activate).
42 *
43 * @return array
44 */
45 public function get() {
46 $settings = get_option($this->option_name, []);
47 $resolved = [];
48
49 foreach ($this->extensions as $key => $extension) {
50 $status = isset($settings[$key]) ? $settings[$key] : $extension['status'];
51 $extension['status'] = $status;
52
53 $slug = isset($extension['slug']) ? $extension['slug'] : '';
54 $deps = isset($extension['deps']) ? $extension['deps'] : [];
55
56 if ($extension['type'] === 'module' && !empty($deps)) {
57 $dep = $deps[0];
58
59 if (!PluginManager::is_installed($dep)) {
60 $extension['status'] = 'upgrade';
61 } elseif (!PluginManager::is_activated($dep)) {
62 $extension['status'] = 'install';
63 } else {
64 $extension['status'] = 'activate';
65 }
66 } elseif (in_array($extension['type'], ['plugin', 'addon'])) {
67 if (!PluginManager::is_installed($slug)) {
68 $extension['status'] = 'upgrade';
69 } elseif (!PluginManager::is_activated($slug)) {
70 $extension['status'] = 'install';
71 } else {
72 $extension['status'] = 'activate';
73 }
74 } elseif ($extension['type'] === 'arraytics-plugin') {
75 if (!PluginManager::is_activated($slug)) {
76 $extension['status'] = 'install';
77 } else {
78 $extension['status'] = 'activate';
79 }
80 }
81
82 $resolved[$key] = $extension;
83 }
84
85 return $resolved;
86 }
87
88 /**
89 * Update the status of a given extension.
90 *
91 * @param string $key The extension key.
92 * @param string $status The status to set ('on' or 'off').
93 * @return bool True if update successful, false otherwise.
94 */
95 public function update($key, $status) {
96 if (!isset($this->extensions[$key])) {
97 return false;
98 }
99
100 $settings = get_option($this->option_name, []);
101 $settings[$key] = $status === 'on' ? 'on' : 'off';
102
103 return update_option($this->option_name, $settings);
104 }
105
106 /**
107 * Get all extensions that are currently enabled (status = 'on').
108 *
109 * @return array
110 */
111 public function enabled() {
112 return array_filter($this->get(), function ($ext) {
113 return $ext['status'] === 'on';
114 });
115 }
116
117 /**
118 * Find an extension by its key.
119 *
120 * @param string $key The extension key.
121 * @return array|null
122 */
123 public function find($key) {
124 return isset($this->extensions[$key]) ? $this->extensions[$key] : null;
125 }
126
127 /**
128 * Get the current option name used for storing extension settings.
129 *
130 * @return string
131 */
132 public function get_option_name() {
133 return $this->option_name;
134 }
135
136 /**
137 * Get all registered extensions as initially provided.
138 *
139 * @return array
140 */
141 public function get_extensions() {
142 return $this->extensions;
143 }
144
145 /**
146 * Get all extensions of type "plugin".
147 *
148 * @return array
149 */
150 public function get_plugins() {
151 return array_filter($this->get(), function ($e) {
152 return $e['type'] === 'plugin';
153 });
154 }
155
156 /**
157 * Get all extensions of type "addon".
158 *
159 * @return array
160 */
161 public function get_addons() {
162 return array_filter($this->get(), function ($e) {
163 return $e['type'] === 'addon';
164 });
165 }
166
167 /**
168 * Get all extensions of type "module".
169 *
170 * @return array
171 */
172 public function get_modules() {
173 return array_filter($this->get(), function ($e) {
174 return $e['type'] === 'module';
175 });
176 }
177
178 /**
179 * Get all extensions of type "arraytics-plugin".
180 *
181 * @return array
182 */
183 public function get_our_plugins() {
184 return array_filter($this->get(), function ($e) {
185 return $e['type'] === 'arraytics-plugin';
186 });
187 }
188
189 /**
190 * Get all submodules of a given module.
191 *
192 * @param string $module
193 * @return array
194 */
195 public function get_submodule($module) {
196 return array_filter($this->get(), function ($extension) use ($module) {
197 return isset($extension['parent']) && $extension['parent'] === $module;
198 });
199 }
200
201 /**
202 * Check whether a module or submodule is enabled.
203 *
204 * @param string $key The extension key (module or submodule).
205 * @return bool
206 */
207 public function is_enabled($key) {
208 $extensions = $this->get();
209
210 if (!isset($extensions[$key])) {
211 return false;
212 }
213
214 $extension = $extensions[$key];
215 $status = isset($extension['status']) ? $extension['status'] : 'off';
216
217 if (empty($extension['parent'])) {
218 return $status === 'on';
219 }
220
221 $parent_key = $extension['parent'];
222 $parent_status = isset($extensions[$parent_key]['status']) ? $extensions[$parent_key]['status'] : 'off';
223
224 return $status === 'on' && $parent_status === 'on';
225 }
226 }
227