| 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 |
return true; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Retireve path and URL of included module directory |
| 158 |
* |
| 159 |
* @param [type] $file [description] |
| 160 |
* |
| 161 |
* @return [type] [description] |
| 162 |
*/ |
| 163 |
public function get_included_module_data( $file ) { |
| 164 |
|
| 165 |
return isset( $this->included_modules[ $file ] ) ? $this->included_modules[ $file ] : false; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Select latest version path from all available. |
| 171 |
* |
| 172 |
* @param array $module_versions All available vaerions paths for selected module |
| 173 |
* |
| 174 |
* @return string Module path. |
| 175 |
*/ |
| 176 |
private function get_latest_version_path( array $module_versions = array() ) { |
| 177 |
|
| 178 |
// Immediately return path if array contain sinle element. |
| 179 |
if ( 1 === count( $module_versions ) ) { |
| 180 |
$module_versions = array_values( $module_versions ); |
| 181 |
|
| 182 |
return $module_versions[0]; |
| 183 |
} |
| 184 |
|
| 185 |
// Sort array by version and return highest |
| 186 |
uksort( $module_versions, 'version_compare' ); |
| 187 |
|
| 188 |
return end( $module_versions ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Store passed module version and path into global modules data. |
| 193 |
* |
| 194 |
* @param string $module_path Module path |
| 195 |
* |
| 196 |
* @return boolean |
| 197 |
*/ |
| 198 |
private function store_module_version( $module_path = null ) { |
| 199 |
|
| 200 |
$slug = basename( $module_path ); |
| 201 |
$modules_data = wp_cache_get( $this->key ); |
| 202 |
$modules_data = ! empty( $modules_data ) ? $modules_data : array(); |
| 203 |
|
| 204 |
if ( empty( $modules_data[ $slug ] ) ) { |
| 205 |
$modules_data[ $slug ] = array(); |
| 206 |
} |
| 207 |
|
| 208 |
$filedata = get_file_data( |
| 209 |
$module_path, |
| 210 |
array( |
| 211 |
'version' => 'Version', |
| 212 |
) |
| 213 |
); |
| 214 |
|
| 215 |
if ( empty( $filedata['version'] ) ) { |
| 216 |
// If version not passed in file header, so module defined not correctly and not be included |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
$current_version = $filedata['version']; |
| 221 |
|
| 222 |
if ( empty( $modules_data[ $slug ][ $current_version ] ) ) { |
| 223 |
$modules_data[ $slug ][ $current_version ] = $module_path; |
| 224 |
} |
| 225 |
|
| 226 |
$this->modules_slugs[] = $slug; |
| 227 |
|
| 228 |
wp_cache_set( $this->key, $modules_data, '', 1 ); |
| 229 |
|
| 230 |
return true; |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|