modules = $modules; add_action( 'after_setup_theme', array( $this, 'store_versions' ), -10 ); add_action( 'after_setup_theme', array( $this, 'include_modules' ), -1 ); } /** * Store versions for modules passed in current instance into global modules versions list * * @return void */ public function store_versions() { foreach ( $this->modules as $module ) { $this->store_module_version( $module ); } } /** * Include latest versions of modules in current loader instance. * All available version preiously stored by 'store_versions' methods of each loader instance. * * @return boolean */ public function include_modules() { $modules_data = wp_cache_get( $this->key ); foreach ( $this->modules_slugs as $slug ) { if ( empty( $modules_data[ $slug ] ) ) { continue; } $path = $this->get_latest_version_path( $modules_data[ $slug ] ); if ( file_exists( $path ) ) { $dir = pathinfo( $path, PATHINFO_DIRNAME ); $url = str_replace( '\\', '/', str_replace( wp_normalize_path( WP_CONTENT_DIR ), content_url(), wp_normalize_path( $dir ) ) ); $this->included_modules[ $slug ] = array( 'path' => trailingslashit( $dir ), 'url' => apply_filters( 'cx_include_module_url', trailingslashit( $url ), $path ), ); require_once $path; } } return true; } /** * Retireve path and URL of included module directory * * @param [type] $file [description] * @return [type] [description] */ public function get_included_module_data( $file ) { return isset( $this->included_modules[ $file ] ) ? $this->included_modules[ $file ] : false; } /** * Select latest version path from all available. * * @param array $module_versions All available vaerions paths for selected module * @return string Module path. */ private function get_latest_version_path( array $module_versions = array() ) { // Immediately return path if array contain sinle element. if ( 1 === count( $module_versions ) ) { $module_versions = array_values( $module_versions ); return $module_versions[0]; } // Sort array by version and return highest uksort( $module_versions, 'version_compare' ); return end( $module_versions ); } /** * Store passed module version and path into global modules data. * * @param string $module_path Module path * @return boolean */ private function store_module_version( $module_path = null ) { $slug = basename( $module_path ); $modules_data = wp_cache_get( $this->key ); $modules_data = ! empty( $modules_data ) ? $modules_data : array(); if ( empty( $modules_data[ $slug ] ) ) { $modules_data[ $slug ] = array(); } $filedata = get_file_data( $module_path, array( 'version' => 'Version', ) ); if ( empty( $filedata['version'] ) ) { // If version not passed in file header, so module defined not correctly and not be included return false; } $current_version = $filedata['version']; if ( empty( $modules_data[ $slug ][ $current_version ] ) ) { $modules_data[ $slug ][ $current_version ] = $module_path; } $this->modules_slugs[] = $slug; wp_cache_set( $this->key, $modules_data, '', 1 ); return true; } } }