PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / loader.php

loader.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/loader.php

280 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Description of Loader
5 *
6 * @author Ali2Woo Team
7 */
8
9 namespace AliNext_Lite;;
10 use DI\Container;
11
12 #[\AllowDynamicProperties]
13 class Loader {
14
15 const DEFAULT_INCLUDE_POSITION = 1000;
16 const DEFAULT_INCLIDE_ACTION = 'global';
17
18 static protected ?Loader $_instance = null;
19
20 private function __construct() {
21
22 }
23
24 static public function getInstance() {
25 if (!self::$_instance) {
26 self::$_instance = new self();
27 }
28 return self::$_instance;
29 }
30
31 static public function classes($classpath, $default_load_action, Container $DI)
32 {
33 $this_class = Loader::getInstance();
34
35 $result = $this_class->load_classpath($classpath, $default_load_action);
36
37 foreach ($result['delay_include'] as $action => $files) {
38
39 if ('global' === $action) {
40 $include_array = array();
41 foreach ($files as $file) {
42 $tmp = explode("###", $file);
43 $include_array[$tmp[0]] = $tmp[1];
44 }
45 asort($include_array);
46 foreach ($include_array as $file => $p) {
47 include_once($file);
48 }
49 } else {
50 $this_class->add_method($action . "_inclide", function () use (&$this_class, $action, $files) {
51 $include_array = array();
52 foreach ($files as $file) {
53 $tmp = explode("###", $file);
54 $include_array[$tmp[0]] = $tmp[1];
55 }
56 asort($include_array);
57 foreach ($include_array as $file => $p) {
58 include_once($file);
59 }
60 });
61 add_action($action, array($this_class, $action . "_inclide"), 10);
62 }
63 }
64
65 foreach ($result['autoload'] as $action => $class_array) {
66 if ('global' === $action) {
67 foreach ($class_array as $className) {
68 if (
69 (defined('DOING_AJAX') && in_array($className, $result['skip_ajax'])) ||
70 (defined('DOING_CRON') && in_array($className, $result['skip_cron']))
71 ) {
72 continue;
73 }
74
75 $this_class->createClassInstance($className, $DI);
76 }
77 } else {
78 $this_class->add_method($action, function () use (&$this_class, $action, $class_array, $result, $DI) {
79 foreach ($class_array as $className) {
80 if (
81 (defined('DOING_AJAX') && in_array($className, $result['skip_ajax'])) ||
82 (defined('DOING_CRON') && in_array($className, $result['skip_cron']))
83 ) {
84 continue;
85 }
86
87 $this_class->createClassInstance($className, $DI);
88 }
89 });
90 add_action($action, array($this_class, $action), 20);
91 }
92 }
93
94 /* foreach ($result['hook'] as $hook => $class_array_1) {
95 foreach ($class_array_1 as $className1) {
96 if (
97 (defined('DOING_AJAX') && in_array($className1, $result['skip_ajax'])) ||
98 (defined('DOING_CRON') && in_array($className1, $result['skip_cron']))
99 ) {
100 continue;
101 }
102 $this_class->debug("Registering hook '{$hook}' with class '{$className1}'");
103 $instance = $this_class->createClassInstance($className1, $DI);
104 }
105 }*/
106
107 $this_class->add_method('finalize_hooks', function () use ($result, $DI, $this_class) {
108 foreach ($result['hook'] as $hook => $classList) {
109 foreach ($classList as $className) {
110 if (
111 (defined('DOING_AJAX') && in_array($className, $result['skip_ajax'])) ||
112 (defined('DOING_CRON') && in_array($className, $result['skip_cron']))
113 ) {
114 continue;
115 }
116 $this_class->debug("Registering hook '{$hook}' with class '{$className}'");
117 $instance = $this_class->createClassInstance($className, $DI);
118 if (method_exists($instance, '__invoke')) {
119 add_action($hook, $instance);
120 }
121 }
122 }
123 });
124
125 // And finally register a single WP hook to initialize this
126 add_action('plugins_loaded', [$this_class, 'finalize_hooks']);
127 }
128
129 static public function addons($classpath) {
130 if (substr($classpath, -1) !== "/") {
131 $classpath.='/';
132 }
133 $dirs = glob($classpath . '*', GLOB_ONLYDIR);
134 if ($dirs && is_array($dirs)) {
135 foreach (glob($classpath . '*', GLOB_ONLYDIR) as $dir) {
136 $file_list = scandir($dir . '/');
137 foreach ($file_list as $f) {
138 if (is_file($dir . '/' . $f)) {
139 $file_info = pathinfo($f);
140 if ($file_info["extension"] == "php") {
141 include_once($dir . '/' . $f);
142 }
143 }
144 }
145 }
146 }
147 }
148
149 private function debug(string $message): void
150 {
151 if (defined('A2WL_LOADER_DEBUG') && A2WL_LOADER_DEBUG === true) {
152 error_log("[A2W Loader] " . $message);
153 }
154 }
155
156 private function createClassInstance(string $className, Container $DI): object
157 {
158 $fqcn = 'AliNext_Lite\\' . $className;
159
160 $this->debug("Instantiating: {$fqcn} via " . ($DI->has($fqcn) ? "DI container" : "constructor"));
161
162 if ($DI->has($fqcn)) {
163 //load class using DI id it has DI definition in config
164 return $DI->get($fqcn);
165 }
166
167 return new $fqcn();
168 }
169
170 private function load_classpath($classpath, $default_load_action) {
171 $result = array(
172 'delay_include' => array(),
173 'autoload' => array(),
174 'skip_ajax' => array(),
175 'skip_cron' => array(),
176 'hook' => array()
177 );
178
179 if ($classpath) {
180 $classpath .= substr($classpath, -1) === "/" ? "" : "/";
181
182 $include_array = $subdir_array = array();
183
184 foreach (glob($classpath . "*") as $f) {
185 if (is_file($f)) {
186 $this->debug("Scanning file: {$f}");
187
188 $file_info = pathinfo($f);
189 if ($file_info["extension"] == "php") {
190 $file_data = get_file_data($f, array(
191 'position' => '@position',
192 'autoload' => '@autoload',
193 'include_action' => '@include_action',
194 'hook' => '@hook',
195 'ajax' => '@ajax',
196 'cron' => '@cron',
197 ));
198 if (isset($file_data['autoload']) && $file_data['autoload']) {
199 $action = (!is_null(filter_var($file_data['autoload'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))) ? $default_load_action : $file_data['autoload'];
200 if (!isset($result['autoload'][$action])) {
201 $result['autoload'][$action] = array();
202 }
203
204 $this->debug("Autoloading: {$file_info['filename']} for action: {$action}");
205 $result['autoload'][$action][] = $file_info['filename'];
206
207 if(isset($file_data['ajax'])){
208 if(!filter_var($file_data['ajax'], FILTER_VALIDATE_BOOLEAN)){
209 $result['skip_ajax'][] = $file_info['filename'];
210 }
211 }
212
213 if(isset($file_data['cron'])){
214 if(!filter_var($file_data['cron'], FILTER_VALIDATE_BOOLEAN)){
215 $result['skip_cron'][] = $file_info['filename'];
216 }
217 }
218 }
219
220 if (!empty($file_data['hook'])) {
221 $action = $file_data['hook'];
222 if (!isset($result['hook'][$action])) {
223 $result['hook'][$action] = array();
224 }
225
226 $this->debug("Detected hook: {$file_data['hook']} for {$file_info['filename']}");
227 $result['hook'][$action][] = $file_info['filename'];
228
229 if(isset($file_data['ajax'])){
230 if(!filter_var($file_data['ajax'], FILTER_VALIDATE_BOOLEAN)){
231 $result['skip_ajax'][] = $file_info['filename'];
232 }
233 }
234
235 if(isset($file_data['cron'])){
236 if(!filter_var($file_data['cron'], FILTER_VALIDATE_BOOLEAN)){
237 $result['skip_cron'][] = $file_info['filename'];
238 }
239 }
240 }
241
242 if (isset($file_data['include_action']) && $file_data['include_action']) {
243 $include_action = (!is_null(filter_var($file_data['include_action'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))) ? (filter_var($file_data['include_action'], FILTER_VALIDATE_BOOLEAN)?self::DEFAULT_INCLIDE_ACTION:"a2wl_fake_action") : $file_data['include_action'];
244 if (!isset($result['delay_include'][$include_action])) {
245 $result['delay_include'][$include_action] = array();
246 }
247
248 $this->debug("Including delayed file: {$f} for action: {$include_action}");
249 $result['delay_include'][$include_action][] = $f . "###" . (IntVal($file_data['position']) ? IntVal($file_data['position']) : self::DEFAULT_INCLUDE_POSITION);
250 } else {
251 $include_array[$f] = IntVal($file_data['position']) ? IntVal($file_data['position']) : self::DEFAULT_INCLUDE_POSITION;
252 }
253 }
254 } else if (is_dir($f)) {
255 $subdir_array[] = $f;
256 }
257 }
258 asort($include_array);
259 foreach ($include_array as $file => $p) {
260 include_once($file);
261 }
262
263 foreach ($subdir_array as $subdir) {
264 $result = array_merge_recursive($result, $this->load_classpath($subdir, $default_load_action));
265 }
266 }
267
268 return $result;
269 }
270
271 private function add_method($name, $method) {
272 $this->{$name} = $method;
273 }
274
275 public function __call($name, $arguments) {
276 return call_user_func($this->{$name}, $arguments);
277 }
278
279 }
280