PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.0
JetFormBuilder — Dynamic Blocks Form Builder v1.2.0
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / framework / loader.php
jetformbuilder / framework Last commit date
vue-ui 5 years ago loader.php 5 years ago
loader.php
235 lines
1 <?php
2
3 namespace Jet_Form_Builder\Framework;
4
5 /**
6 * Cherry X framework loader class.
7 *
8 * How to use:
9 *
10 * 1. Copy and include this class into your theme/plugin
11 * 2. Add unique prefix for the class name, e.g. - Twentyseventeen_Jet_Engine_CX_Loader
12 * 3. Initialize loader on after_setup_theme hook with priority -20, Example:
13 *
14 * add_action( 'after_setup_theme', 'twentyseventeen_framework_loader', -20 );
15 * function twentyseventeen_framework_loader() {
16 * require get_theme_file_path( 'framework/loader.php' );
17 * new Twentyseventeen_Jet_Engine_CX_Loader(
18 * array(
19 * get_theme_file_path( 'framework/modules/module-1/module-1.php' ),
20 * get_theme_file_path( 'framework/modules/module-2/module-2.php' ),
21 * get_theme_file_path( 'framework/modules/module-3/module-3.php' ),
22 * )
23 * );
24 * }
25 *
26 * Notes:
27 *
28 * 1. This class only select latest version of each module from all Cherry X frameworks loaded in current environment
29 * 2. You should manually initialize selected modules later, when them will be needed you, but not eralier than after_setup_theme hook with priority 0.
30 */
31
32 // If this file is called directly, abort.
33 if ( ! defined( 'WPINC' ) ) {
34 die;
35 }
36
37
38 /**
39 * Define Jet_Engine_CX_Loader class
40 */
41 class CX_Loader {
42
43 /**
44 * Key for object cache where are stored information about all modules in current environment
45 *
46 * @var string
47 */
48 private $key = 'cherry_x_modules';
49
50 /**
51 * Holder for modules list of current loader instance.
52 *
53 * @var array
54 */
55 private $modules = array();
56
57 /**
58 * Holder for modules slugs list of current loader instance.
59 *
60 * @var array
61 */
62 private $modules_slugs = array();
63
64 /**
65 * Included modules paths and URLs
66 *
67 * @var array
68 */
69 private $included_modules = array();
70
71 /**
72 * Loads latest versions of all modules passed into modules array
73 *
74 * @param array $modules List of loaded modules. Format:
75 * array(
76 * get_theme_file_path( 'framework/modules/module-1/module-1.php' ),
77 * get_theme_file_path( 'framework/modules/module-2/module-2.php' ),
78 * get_theme_file_path( 'framework/modules/module-3/module-3.php' ),
79 * )
80 */
81 public function __construct( array $modules = array() ) {
82
83 $this->modules = $modules;
84
85 add_action( 'after_setup_theme', array( $this, 'store_versions' ), - 10 );
86 add_action( 'after_setup_theme', array( $this, 'include_modules' ), - 1 );
87
88 }
89
90 /**
91 * Store versions for modules passed in current instance into global modules versions list
92 *
93 * @return void
94 */
95 public function store_versions() {
96
97 foreach ( $this->modules as $module ) {
98 $this->store_module_version( $module );
99 }
100
101 }
102
103 /**
104 * Include latest versions of modules in current loader instance.
105 * All available version preiously stored by 'store_versions' methods of each loader instance.
106 *
107 * @return boolean
108 */
109 public function include_modules() {
110
111 $modules_data = wp_cache_get( $this->key );
112
113 foreach ( $this->modules_slugs as $slug ) {
114
115 if ( empty( $modules_data[ $slug ] ) ) {
116 continue;
117 }
118
119 $path = $this->get_latest_version_path( $modules_data[ $slug ] );
120
121 if ( file_exists( $path ) ) {
122
123 $dir = pathinfo( $path, PATHINFO_DIRNAME );
124
125 $normalize_dir = wp_normalize_path( $dir );
126 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
127
128 if ( 0 === strpos( $normalize_dir, $plugin_dir ) ) {
129 $url = str_replace(
130 '\\',
131 '/',
132 str_replace( $plugin_dir, plugins_url(), $normalize_dir )
133 );
134 } else {
135 $url = str_replace(
136 '\\',
137 '/',
138 str_replace( wp_normalize_path( WP_CONTENT_DIR ), content_url(), $normalize_dir )
139 );
140 }
141
142 $this->included_modules[ $slug ] = array(
143 'path' => trailingslashit( $dir ),
144 'url' => apply_filters( 'cx_include_module_url', trailingslashit( $url ), $path ),
145 );
146
147 require_once $path;
148
149 }
150
151 }
152
153 return true;
154
155 }
156
157 /**
158 * Retireve path and URL of included module directory
159 *
160 * @param [type] $file [description]
161 *
162 * @return [type] [description]
163 */
164 public function get_included_module_data( $file ) {
165
166 return isset( $this->included_modules[ $file ] ) ? $this->included_modules[ $file ] : false;
167
168 }
169
170 /**
171 * Select latest version path from all available.
172 *
173 * @param array $module_versions All available vaerions paths for selected module
174 *
175 * @return string Module path.
176 */
177 private function get_latest_version_path( array $module_versions = array() ) {
178
179 // Immediately return path if array contain sinle element.
180 if ( 1 === count( $module_versions ) ) {
181 $module_versions = array_values( $module_versions );
182
183 return $module_versions[0];
184 }
185
186 // Sort array by version and return highest
187 uksort( $module_versions, 'version_compare' );
188
189 return end( $module_versions );
190 }
191
192 /**
193 * Store passed module version and path into global modules data.
194 *
195 * @param string $module_path Module path
196 *
197 * @return boolean
198 */
199 private function store_module_version( $module_path = null ) {
200
201 $slug = basename( $module_path );
202 $modules_data = wp_cache_get( $this->key );
203 $modules_data = ! empty( $modules_data ) ? $modules_data : array();
204
205 if ( empty( $modules_data[ $slug ] ) ) {
206 $modules_data[ $slug ] = array();
207 }
208
209 $filedata = get_file_data( $module_path, array(
210 'version' => 'Version',
211 ) );
212
213 if ( empty( $filedata['version'] ) ) {
214 // If version not passed in file header, so module defined not correctly and not be included
215 return false;
216 }
217
218 $current_version = $filedata['version'];
219
220 if ( empty( $modules_data[ $slug ][ $current_version ] ) ) {
221 $modules_data[ $slug ][ $current_version ] = $module_path;
222 }
223
224 $this->modules_slugs[] = $slug;
225
226 wp_cache_set( $this->key, $modules_data, '', 1 );
227
228 return true;
229
230 }
231
232 }
233
234
235