PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.3
Elementor Website Builder – more than just a page builder v4.3.3
4.3.3 4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 All 456 releases
elementor / vendor / elementor / elementor-mcp-composer / src / Versions.php

Versions.php in Elementor Website Builder – more than just a page builder 4.3.3, at vendor/elementor/elementor-mcp-composer/src/Versions.php

179 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\MCP\Composer;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 /**
10 * Coordinates multiple bundled copies of this package and loads the highest version.
11 */
12 class Versions {
13 /**
14 * Versions instance.
15 *
16 * @var Versions|null
17 */
18 private static $instance = null;
19
20 /**
21 * Registered version callbacks.
22 *
23 * @var array<string, callable>
24 */
25 private $versions = [];
26
27 /**
28 * Registered source files for each version.
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 a version initialization callback.
43 *
44 * @param string $version_string Package version.
45 * @param callable $initialization_callback Callback to initialize the version.
46 * @return bool
47 */
48 public function register( $version_string, $initialization_callback ) {
49 if ( isset( $this->versions[ $version_string ] ) ) {
50 return false;
51 }
52
53 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
54 $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
55 $source = $backtrace[0]['file'];
56
57 $this->versions[ $version_string ] = $initialization_callback;
58 $this->sources[ $source ] = $version_string;
59
60 return true;
61 }
62
63 /**
64 * Get all registered versions.
65 *
66 * @return array<string, callable>
67 */
68 public function get_versions() {
69 return $this->versions;
70 }
71
72 /**
73 * Get registered source files.
74 *
75 * @return array<string, string>
76 */
77 public function get_sources() {
78 return $this->sources;
79 }
80
81 /**
82 * Get the latest registered version string.
83 *
84 * @return string|false
85 */
86 public function latest_version() {
87 $keys = array_keys( $this->versions );
88
89 if ( empty( $keys ) ) {
90 return false;
91 }
92
93 usort( $keys, 'version_compare' );
94
95 return end( $keys );
96 }
97
98 /**
99 * Get the callback for the latest registered version.
100 *
101 * @return callable
102 */
103 public function latest_version_callback() {
104 $latest = $this->latest_version();
105
106 if ( empty( $latest ) || ! isset( $this->versions[ $latest ] ) ) {
107 return '__return_null';
108 }
109
110 return $this->versions[ $latest ];
111 }
112
113 /**
114 * Custom autoloader that loads classes from the highest version source.
115 *
116 * @param string $class_name Fully qualified class name.
117 * @return void
118 */
119 public static function autoloader( $class_name ) {
120 if ( strpos( $class_name, 'Elementor\\MCP\\Composer\\' ) !== 0 ) {
121 return;
122 }
123
124 if ( 'Elementor\\MCP\\Composer\\Versions' === $class_name ) {
125 return;
126 }
127
128 if ( empty( self::$active_source_dir ) ) {
129 return;
130 }
131
132 $relative_class = str_replace( 'Elementor\\MCP\\Composer\\', '', $class_name );
133 $file_path = str_replace( '\\', '/', $relative_class ) . '.php';
134 $full_path = self::$active_source_dir . '/src/' . $file_path;
135
136 if ( file_exists( $full_path ) ) {
137 require_once $full_path;
138 }
139 }
140
141 /**
142 * Get the singleton instance.
143 *
144 * @return Versions
145 * @codeCoverageIgnore
146 */
147 public static function instance() {
148 if ( empty( self::$instance ) ) {
149 self::$instance = new self();
150 spl_autoload_register( [ __CLASS__, 'autoloader' ], true, true );
151 }
152
153 return self::$instance;
154 }
155
156 /**
157 * Initialize the latest registered package version.
158 *
159 * @return void
160 * @codeCoverageIgnore
161 */
162 public static function initialize_latest_version() {
163 $self = self::instance();
164 $callback = $self->latest_version_callback();
165
166 if ( false === $self->latest_version() ) {
167 return;
168 }
169
170 if ( is_string( $callback ) && function_exists( $callback ) ) {
171 $reflection = new \ReflectionFunction( $callback );
172 $callback_file = $reflection->getFileName();
173 self::$active_source_dir = dirname( $callback_file );
174 }
175
176 call_user_func( $callback );
177 }
178 }
179