PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.3.3
JetFormBuilder — Dynamic Blocks Form Builder v3.3.3
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 / modules / framework / cx-loader.php
jetformbuilder / modules / framework Last commit date
admin-bar 2 years ago vue-ui 2 years ago cx-loader.php 2 years ago module.php 2 years ago
cx-loader.php
232 lines
1 <?php
2
3 namespace JFB_Modules\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 * Store versions for modules passed in current instance into global modules versions list
91 *
92 * @return void
93 */
94 public function store_versions() {
95
96 foreach ( $this->modules as $module ) {
97 $this->store_module_version( $module );
98 }
99 }
100
101 /**
102 * Include latest versions of modules in current loader instance.
103 * All available version preiously stored by 'store_versions' methods of each loader instance.
104 *
105 * @return boolean
106 */
107 public function include_modules() {
108
109 $modules_data = wp_cache_get( $this->key );
110
111 foreach ( $this->modules_slugs as $slug ) {
112
113 if ( empty( $modules_data[ $slug ] ) ) {
114 continue;
115 }
116
117 $path = $this->get_latest_version_path( $modules_data[ $slug ] );
118
119 if ( file_exists( $path ) ) {
120
121 $dir = pathinfo( $path, PATHINFO_DIRNAME );
122
123 $normalize_dir = wp_normalize_path( $dir );
124 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
125
126 if ( 0 === strpos( $normalize_dir, $plugin_dir ) ) {
127 $url = str_replace(
128 '\\',
129 '/',
130 str_replace( $plugin_dir, plugins_url(), $normalize_dir )
131 );
132 } else {
133 $url = str_replace(
134 '\\',
135 '/',
136 str_replace( wp_normalize_path( WP_CONTENT_DIR ), content_url(), $normalize_dir )
137 );
138 }
139
140 $this->included_modules[ $slug ] = array(
141 'path' => trailingslashit( $dir ),
142 'url' => apply_filters( 'cx_include_module_url', trailingslashit( $url ), $path ),
143 );
144
145 require_once $path;
146
147 }
148 }
149
150 return true;
151 }
152
153 /**
154 * Retireve path and URL of included module directory
155 *
156 * @param [type] $file [description]
157 *
158 * @return [type] [description]
159 */
160 public function get_included_module_data( $file ) {
161
162 return isset( $this->included_modules[ $file ] ) ? $this->included_modules[ $file ] : false;
163 }
164
165 /**
166 * Select latest version path from all available.
167 *
168 * @param array $module_versions All available vaerions paths for selected module
169 *
170 * @return string Module path.
171 */
172 private function get_latest_version_path( array $module_versions = array() ) {
173
174 // Immediately return path if array contain sinle element.
175 if ( 1 === count( $module_versions ) ) {
176 $module_versions = array_values( $module_versions );
177
178 return $module_versions[0];
179 }
180
181 // Sort array by version and return highest
182 uksort( $module_versions, 'version_compare' );
183
184 return end( $module_versions );
185 }
186
187 /**
188 * Store passed module version and path into global modules data.
189 *
190 * @param string $module_path Module path
191 *
192 * @return boolean
193 */
194 private function store_module_version( $module_path = null ) {
195
196 $slug = basename( $module_path );
197 $modules_data = wp_cache_get( $this->key );
198 $modules_data = ! empty( $modules_data ) ? $modules_data : array();
199
200 if ( empty( $modules_data[ $slug ] ) ) {
201 $modules_data[ $slug ] = array();
202 }
203
204 $filedata = get_file_data(
205 $module_path,
206 array(
207 'version' => 'Version',
208 )
209 );
210
211 if ( empty( $filedata['version'] ) ) {
212 // If version not passed in file header, so module defined not correctly and not be included
213 return false;
214 }
215
216 $current_version = $filedata['version'];
217
218 if ( empty( $modules_data[ $slug ][ $current_version ] ) ) {
219 $modules_data[ $slug ][ $current_version ] = $module_path;
220 }
221
222 $this->modules_slugs[] = $slug;
223
224 wp_cache_set( $this->key, $modules_data, '', 1 );
225
226 return true;
227 }
228
229 }
230
231
232