| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorOne; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Class \ElementorOne\Versions |
| 11 |
*/ |
| 12 |
class Versions { |
| 13 |
/** |
| 14 |
* \ElementorOne\Versions instance. |
| 15 |
* |
| 16 |
* @var Versions |
| 17 |
*/ |
| 18 |
private static $instance = null; |
| 19 |
|
| 20 |
/** |
| 21 |
* Versions. |
| 22 |
* |
| 23 |
* @var array<string, callable> |
| 24 |
*/ |
| 25 |
private $versions = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* Registered sources. |
| 29 |
* |
| 30 |
* @var array<string, string> |
| 31 |
*/ |
| 32 |
private $sources = []; |
| 33 |
|
| 34 |
/** |
| 35 |
* The determined highest version source directory. |
| 36 |
* |
| 37 |
* @var string|null |
| 38 |
*/ |
| 39 |
private static $active_source_dir = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* Register version's callback. |
| 43 |
* |
| 44 |
* @param string $version_string Elementor One version. |
| 45 |
* @param callable $initialization_callback Callback to initialize the version. |
| 46 |
*/ |
| 47 |
public function register( $version_string, $initialization_callback ) { |
| 48 |
if ( isset( $this->versions[ $version_string ] ) ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 53 |
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); |
| 54 |
$source = $backtrace[0]['file']; |
| 55 |
|
| 56 |
$this->versions[ $version_string ] = $initialization_callback; |
| 57 |
$this->sources[ $source ] = $version_string; |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get all versions. |
| 63 |
*/ |
| 64 |
public function get_versions() { |
| 65 |
return $this->versions; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get registered sources. |
| 70 |
* |
| 71 |
* @return array<string, string> |
| 72 |
*/ |
| 73 |
public function get_sources() { |
| 74 |
return $this->sources; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get latest version registered. |
| 79 |
*/ |
| 80 |
public function latest_version() { |
| 81 |
$keys = array_keys( $this->versions ); |
| 82 |
if ( empty( $keys ) ) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
uasort( $keys, 'version_compare' ); |
| 86 |
return end( $keys ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get callback for latest registered version. |
| 91 |
*/ |
| 92 |
public function latest_version_callback() { |
| 93 |
$latest = $this->latest_version(); |
| 94 |
|
| 95 |
if ( empty( $latest ) || ! isset( $this->versions[ $latest ] ) ) { |
| 96 |
return '__return_null'; |
| 97 |
} |
| 98 |
|
| 99 |
return $this->versions[ $latest ]; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Custom autoloader that loads classes from the highest version source. |
| 104 |
* |
| 105 |
* @param string $class_name Fully qualified class name |
| 106 |
*/ |
| 107 |
public static function autoloader( $class_name ) { |
| 108 |
// Only handle ElementorOne namespace classes |
| 109 |
if ( strpos( $class_name, 'ElementorOne\\' ) !== 0 ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
// Don't handle Versions class itself (already loaded) |
| 114 |
if ( 'ElementorOne\Versions' === $class_name ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
// Wait until we have determined the active source directory |
| 119 |
if ( empty( self::$active_source_dir ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
// Convert class name to file path |
| 124 |
$relative_class = str_replace( 'ElementorOne\\', '', $class_name ); |
| 125 |
$file_path = str_replace( '\\', '/', $relative_class ) . '.php'; |
| 126 |
$full_path = self::$active_source_dir . '/src/' . $file_path; |
| 127 |
|
| 128 |
// Load the file if it exists |
| 129 |
if ( file_exists( $full_path ) ) { |
| 130 |
require_once $full_path; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get instance. |
| 136 |
* |
| 137 |
* @return \ElementorOne\Versions |
| 138 |
* @codeCoverageIgnore |
| 139 |
*/ |
| 140 |
public static function instance() { |
| 141 |
if ( empty( self::$instance ) ) { |
| 142 |
self::$instance = new self(); |
| 143 |
// Register our custom autoloader with high priority (prepend=true) |
| 144 |
spl_autoload_register( [ __CLASS__, 'autoloader' ], true, true ); |
| 145 |
} |
| 146 |
return self::$instance; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Initialize. |
| 151 |
* |
| 152 |
* @codeCoverageIgnore |
| 153 |
*/ |
| 154 |
public static function initialize_latest_version() { |
| 155 |
$self = self::instance(); |
| 156 |
|
| 157 |
// Determine the highest version source directory from the callback function |
| 158 |
// The callback will be defined in the runner.php of the highest version |
| 159 |
$callback = $self->latest_version_callback(); |
| 160 |
|
| 161 |
// Get the source directory by inspecting where the callback function is defined |
| 162 |
if ( is_string( $callback ) && function_exists( $callback ) ) { |
| 163 |
$reflection = new \ReflectionFunction( $callback ); |
| 164 |
$callback_file = $reflection->getFileName(); |
| 165 |
// The callback is defined in runner.php, so parent dir is the package dir |
| 166 |
self::$active_source_dir = dirname( $callback_file ); |
| 167 |
} |
| 168 |
|
| 169 |
// Call the initialization callback |
| 170 |
call_user_func( $callback ); |
| 171 |
} |
| 172 |
} |
| 173 |
|