Repository navigation
Expand file tree
/
Copy pathinit.php
More file actions
82 lines (67 loc) · 1.92 KB
/
Copy pathinit.php
File metadata and controls
82 lines (67 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Entry point.
*
* @package Optiz
*/
declare(strict_types=1);
defined( 'ABSPATH' ) || exit;
// Bootstrap class lives in the global namespace so every bundled copy of this
// file (across multiple plugins) shares the same candidate registry without
// relying on a PHP global variable.
if ( ! class_exists( 'OptizBootstrap', false ) ) {
/**
* Bootstrap class for version-election across multiple bundled copies.
*
* @since 1.0.0
*/
final class OptizBootstrap {
/**
* Version-to-directory map for all registered copies.
*
* @var array<string,string>
*/
private static array $candidates = [];
/**
* Registers a copy of the library as a candidate for election.
*
* @since 1.0.0
*
* @param string $version Semantic version string.
* @param string $dir Absolute path to the library root directory.
*/
public static function add_candidate( string $version, string $dir ): void {
self::$candidates[ $version ] = $dir;
}
/**
* Elects the highest-version candidate and defines the three library constants.
*
* Hooked to plugins_loaded at priority 0 by the first init.php that runs.
*
* @since 1.0.0
*/
public static function elect(): void {
uksort( self::$candidates, 'version_compare' );
$winner_dir = end( self::$candidates );
$winner_version = key( self::$candidates );
if ( ! defined( 'OPTIZ_LOADED_VERSION' ) ) {
define( 'OPTIZ_LOADED_VERSION', $winner_version );
}
if ( ! defined( 'OPTIZ_DIR' ) ) {
define( 'OPTIZ_DIR', $winner_dir );
}
if ( ! defined( 'OPTIZ_URL' ) ) {
define( 'OPTIZ_URL', plugin_dir_url( $winner_dir . '/init.php' ) );
}
}
}
}
( static function () {
$version = '1.0.1';
OptizBootstrap::add_candidate( $version, __DIR__ );
if ( defined( 'OPTIZ_ELECTION_HOOKED' ) ) {
return;
}
define( 'OPTIZ_ELECTION_HOOKED', true );
add_action( 'plugins_loaded', [ 'OptizBootstrap', 'elect' ], 0 );
} )();