PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.56
Timetics – Appointment Booking Calendar & Scheduling v1.0.56
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 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.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / vendor / arraytics / tools-sdk / src / Extension.php

Extension.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.56, at vendor/arraytics/tools-sdk/src/Extension.php

245 lines 6.9 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 $parent_key = isset($extension['parent']) ? $extension['parent'] : '';
54
55 if ( ! empty( $parent_key ) ) {
56 $parent_status = isset($settings[$parent_key]) ? $settings[$parent_key] : $this->extensions[$parent_key]['status'];
57
58 $extension['status'] = $status === 'on' && $parent_status === 'on' ? 'on' : 'off';
59 }
60
61 $slug = isset($extension['slug']) ? $extension['slug'] : '';
62 $deps = isset($extension['deps']) ? $extension['deps'] : [];
63
64 if ($extension['type'] === 'module' && !empty($deps)) {
65 $dep = $deps[0];
66
67 if (!PluginManager::is_installed($dep)) {
68 $extension['status'] = 'upgrade';
69 } elseif (!PluginManager::is_activated($dep)) {
70 $extension['status'] = 'install';
71 } else {
72 $extension['status'] = 'activate';
73 }
74 } elseif (in_array($extension['type'], ['plugin', 'addon'])) {
75 if (!PluginManager::is_installed($slug)) {
76 $extension['status'] = 'install';
77 } elseif (!PluginManager::is_activated($slug)) {
78 $extension['status'] = 'activate';
79 } else {
80 $extension['status'] = 'deactivate';
81 }
82 } elseif ($extension['type'] === 'arraytics-plugin') {
83 if (!PluginManager::is_activated($slug)) {
84 $extension['status'] = 'install';
85 } else {
86 $extension['status'] = 'activate';
87 }
88 }
89
90 $resolved[$key] = $extension;
91 }
92
93 return $resolved;
94 }
95
96 /**
97 * Update the status of a given extension.
98 *
99 * @param string $key The extension key.
100 * @param string $status The status to set ('on' or 'off').
101 * @return bool True if update successful, false otherwise.
102 */
103 public function update($key, $status) {
104 if (!isset($this->extensions[$key])) {
105 return false;
106 }
107
108 $settings = get_option($this->option_name, []);
109 $settings[$key] = $status === 'on' ? 'on' : 'off';
110
111 return update_option($this->option_name, $settings);
112 }
113
114 /**
115 * Get all extensions that are currently enabled (status = 'on').
116 *
117 * @return array
118 */
119 public function enabled() {
120 return array_filter($this->get(), function ($ext) {
121 return $ext['status'] === 'on';
122 });
123 }
124
125 /**
126 * Find an extension by its key.
127 *
128 * @param string $key The extension key.
129 * @return array|null
130 */
131 public function find($key) {
132 return isset($this->extensions[$key]) ? $this->extensions[$key] : null;
133 }
134
135 /**
136 * Find the parent of a given extension.
137 *
138 * @param string $key The extension key.
139 * @return array|null
140 */
141 public function find_parent($key) {
142 return isset($this->extensions[$key]['parent']) ? $this->extensions[$key]['parent'] : null;
143 }
144
145 /**
146 * Get the current option name used for storing extension settings.
147 *
148 * @return string
149 */
150 public function get_option_name() {
151 return $this->option_name;
152 }
153
154 /**
155 * Get all registered extensions as initially provided.
156 *
157 * @return array
158 */
159 public function get_extensions() {
160 return $this->extensions;
161 }
162
163 /**
164 * Get all extensions of type "plugin".
165 *
166 * @return array
167 */
168 public function get_plugins() {
169 return array_filter($this->get(), function ($e) {
170 return $e['type'] === 'plugin';
171 });
172 }
173
174 /**
175 * Get all extensions of type "addon".
176 *
177 * @return array
178 */
179 public function get_addons() {
180 return array_filter($this->get(), function ($e) {
181 return $e['type'] === 'addon';
182 });
183 }
184
185 /**
186 * Get all extensions of type "module".
187 *
188 * @return array
189 */
190 public function get_modules() {
191 return array_filter($this->get(), function ($e) {
192 return $e['type'] === 'module';
193 });
194 }
195
196 /**
197 * Get all extensions of type "arraytics-plugin".
198 *
199 * @return array
200 */
201 public function get_our_plugins() {
202 return array_filter($this->get(), function ($e) {
203 return $e['type'] === 'arraytics-plugin';
204 });
205 }
206
207 /**
208 * Get all submodules of a given module.
209 *
210 * @param string $module
211 * @return array
212 */
213 public function get_submodule($module) {
214 return array_filter($this->get(), function ($extension) use ($module) {
215 return isset($extension['parent']) && $extension['parent'] === $module;
216 });
217 }
218
219 /**
220 * Check whether a module or submodule is enabled.
221 *
222 * @param string $key The extension key (module or submodule).
223 * @return bool
224 */
225 public function is_enabled($key) {
226 $extensions = $this->get();
227
228 if (!isset($extensions[$key])) {
229 return false;
230 }
231
232 $extension = $extensions[$key];
233 $status = isset($extension['status']) ? $extension['status'] : 'off';
234
235 if (empty($extension['parent'])) {
236 return $status === 'on';
237 }
238
239 $parent_key = $extension['parent'];
240 $parent_status = isset($extensions[$parent_key]['status']) ? $extensions[$parent_key]['status'] : 'off';
241
242 return $status === 'on' && $parent_status === 'on';
243 }
244 }
245