admin-bar
2 years ago
blocks-style-manager
2 months ago
vue-ui
2 years ago
cx-loader.php
2 months ago
module.php
2 months ago
cx-loader.php
274 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 = $this->cache_get(); |
| 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 = $this->cache_get(); |
| 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 | $this->cache_set( $modules_data ); |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Set modules data cache. |
| 231 | * |
| 232 | * @return void |
| 233 | */ |
| 234 | public function cache_set( $modules_data = array() ) { |
| 235 | global $cx_modules_data; |
| 236 | $cx_modules_data = $modules_data; |
| 237 | wp_cache_set( $this->key, $modules_data, '', 1 ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Get modules data cache. |
| 242 | * |
| 243 | * @return array |
| 244 | */ |
| 245 | public function cache_get() { |
| 246 | |
| 247 | $modules_data = wp_cache_get( $this->key ); |
| 248 | |
| 249 | if ( ! is_array( $modules_data ) ) { |
| 250 | $modules_data = array(); |
| 251 | } |
| 252 | |
| 253 | global $cx_modules_data; |
| 254 | |
| 255 | if ( ! empty( $cx_modules_data ) ) { |
| 256 | foreach ( $cx_modules_data as $slug => $versions ) { |
| 257 | if ( empty( $modules_data[ $slug ] ) ) { |
| 258 | $modules_data[ $slug ] = $versions; |
| 259 | } else { |
| 260 | foreach ( $versions as $version => $path ) { |
| 261 | if ( empty( $modules_data[ $slug ][ $version ] ) ) { |
| 262 | $modules_data[ $slug ][ $version ] = $path; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return $modules_data; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | |
| 274 |