PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.2.9
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.2.9
1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 1.3.44 All 176 releases
disco / engine / Initialize.php

Initialize.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.2.9, at engine/Initialize.php

246 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Disco
5 *
6 * @package Disco
7 * @author Ohidul Islam <wahid0003@gmail.com>
8 * @link http://domain.tld
9 * @license GPL 2.0+
10 * @copyright 2022 WebAppick
11 */
12
13 namespace Disco\Engine;
14
15 use Disco\Engine;
16 use function apply_filters;
17
18 /**
19 * Disco Initializer
20 */
21 class Initialize {
22
23 /**
24 * List of class to initialize.
25 *
26 * @var array
27 */
28 public $classes = array();
29
30 /**
31 * Instance of this Context.
32 *
33 * @var object
34 */
35 protected $content = null;
36
37 /**
38 * Composer autoload file list.
39 *
40 * @var \Composer\Autoload\ClassLoader
41 */
42 private $composer;
43
44 /**
45 * The Constructor that load the entry classes
46 *
47 * @param \Composer\Autoload\ClassLoader $composer Composer autoload output.
48 * @since 1.0.0
49 */
50 public function __construct( \Composer\Autoload\ClassLoader $composer ) {
51 $this->content = new Engine\Context;
52 $this->composer = $composer;
53
54 // $this->get_classes( 'Internals' );
55 // $this->get_classes( 'Integrations' );
56
57 if ( $this->content->request( 'rest' ) ) {
58 $this->get_classes( 'Rest' );
59 }
60
61 // if ( $this->content->request( 'cli' ) ) {
62 // $this->get_classes( 'Cli' );
63 // }
64
65 // if ( $this->content->request( 'ajax' ) ) {
66 // $this->get_classes( 'Ajax' );
67 // }
68
69 if ( $this->content->request( 'backend' ) ) {
70 $this->get_classes( 'Backend' );
71 }
72
73 if ( $this->content->request( 'frontend' ) ) {
74 $this->get_classes( 'Frontend' );
75 }
76
77 $this->load_classes();
78 }
79
80 /**
81 * Initialize all the classes.
82 *
83 * @return void
84 * @throws \Exception If the class does not have the initialize method.
85 * @since 1.0.0
86 */
87 private function load_classes() {
88 $this->classes = apply_filters( 'disco_classes_to_execute', $this->classes );
89
90 foreach ( $this->classes as $class ) {
91 try {
92 $this->initialize_plugin_class( $class );
93 } catch ( \Throwable $err ) {
94 \do_action( 'disco_initialize_failed', $err );
95
96 if ( WP_DEBUG ) {
97 throw new \Exception( esc_html( $err->getMessage() ) );
98 }
99 }
100 }
101 }
102
103 /**
104 * Validate the class and initialize it.
105 *
106 * @param class-string $classtovalidate Class name to validate.
107 * @return void
108 * @since 1.0.0
109 * @SuppressWarnings("MissingImport")
110 */
111 private function initialize_plugin_class( $classtovalidate ) {
112 $reflection = new \ReflectionClass( $classtovalidate );
113
114 if ( $reflection->isAbstract() ) {
115 return;
116 }
117
118 $temp = new $classtovalidate;
119
120 if ( ! \method_exists( $temp, 'initialize' ) ) {
121 return;
122 }
123
124 $temp->initialize();
125 }
126
127 /**
128 * Based on the folder loads the classes automatically using the Composer autoload to detect the classes of a
129 * Namespace.
130 *
131 * @param string $namespacetofind Class name to find.
132 * @return array Return the classes.
133 * @since 1.0.0
134 */
135 private function get_classes( string $namespacetofind ) {
136 $prefix = $this->composer->getPrefixesPsr4();
137 $classmap = $this->composer->getClassMap();
138 $namespacetofind = 'Disco\\' . $namespacetofind;
139
140 // In case composer has autoload optimized
141 if ( isset( $classmap['Disco\\Engine\\Initialize'] ) ) {
142 $classes = \array_keys( $classmap );
143
144 foreach ( $classes as $class ) {
145 if ( 0 !== \strncmp( (string) $class, $namespacetofind, \strlen( $namespacetofind ) ) ) {
146 continue;
147 }
148
149 $this->classes[] = $class;
150 }
151
152 return $this->classes;
153 }
154
155 $namespacetofind .= '\\';
156
157 // In case composer is not optimized
158 if ( isset( $prefix[ $namespacetofind ] ) ) {
159 $folder = $prefix[ $namespacetofind ][0];
160 $php_files = $this->scandir( $folder );
161 $this->find_classes( $php_files, $folder, $namespacetofind );
162
163 if ( ! WP_DEBUG ) {
164 \wp_die( \esc_html__( 'Disco is on production environment with missing `composer dumpautoload -o` that will improve the performance on autoloading itself.', 'disco' ) );
165 }
166
167 return $this->classes;
168 }
169
170 return $this->classes;
171 }
172
173 /**
174 * Get php files inside the folder/subfolder that will be loaded.
175 * This class is used only when Composer is not optimized.
176 *
177 * @param string $folder Path.
178 * @param string $exclude_str Exclude all files whose filename contain this. Defaults to `~`.
179 * @return array List of files.
180 * @since 1.0.0
181 */
182 private function scandir( string $folder, string $exclude_str = '~' ) {
183 // Also exclude these specific scandir findings.
184 $blacklist = array(
185 '..',
186 '.',
187 'index.php',
188 );
189 // Scan for files.
190 $temp_files = \scandir( $folder );
191
192 $files = array();
193
194 if ( \is_array( $temp_files ) ) {
195 foreach ( $temp_files as $temp_file ) {
196 // Only include filenames that DO NOT contain the excluded string and ARE NOT on the scandir result blacklist.
197 if ( \is_string( $exclude_str ) && false !== \mb_strpos( $temp_file, $exclude_str )
198 || $temp_file[0] === '.'
199 || \in_array( $temp_file, $blacklist, true )
200 ) {
201 continue;
202 }
203
204 $files[] = $temp_file;
205 }
206 }
207
208 return $files;
209 }
210
211 /**
212 * Load namespace classes by files.
213 *
214 * @param array $php_files List of files with the Class.
215 * @param string $folder Path of the folder.
216 * @param string $base Namespace base.
217 * @return void
218 * @since 1.0.0
219 */
220 private function find_classes( array $php_files, string $folder, string $base ) {
221 foreach ( $php_files as $php_file ) {
222 $class_name = \substr( $php_file, 0, -4 );
223 $path = $folder . '/' . $php_file;
224
225 if ( \is_file( $path ) ) {
226 $this->classes[] = $base . $class_name;
227
228 continue;
229 }
230
231 // Verify the Namespace level
232 if ( \substr_count( $base . $class_name, '\\' ) < 2 ) {
233 continue;
234 }
235
236 if ( ! \is_dir( $path ) || \strtolower( $php_file ) === $php_file ) {
237 continue;
238 }
239
240 $sub_php_files = $this->scandir( $folder . '/' . $php_file );
241 $this->find_classes( $sub_php_files, $folder . '/' . $php_file, $base . $php_file . '\\' );
242 }//end foreach
243 }
244
245 }
246