PluginProbe
Blocks – Reusable Content, Shortcodes & Site Variables / trunk
Blocks – Reusable Content, Shortcodes & Site Variables vtrunk
26.09.03.15 26.08.31.21 26.08.22.20 26.08.22.22 26.08.22.17 26.08.22.13 26.08.21.19 26.08.07.23 26.07.19.14 26.07.13.21 26.07.13.17 26.07.12.13 026.07.07.21 026.07.05.18 026.06.26.20 026.06.26.21 026.06.08.20 026.05.13.14 026.04.29.10 trunk 026.02.22.22 026.03.16.23 026.04.23.13
blocks / includes / signls / loader.php

loader.php in Blocks – Reusable Content, Shortcodes & Site Variables trunk, at includes/signls/loader.php

150 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 * Collision-safe loader for vendored Signls SDK copies.
4 *
5 * @package signls-sdk
6 * @license GPL-3.0-or-later
7 */
8
9 defined( 'ABSPATH' ) || exit;
10
11 if ( ! class_exists( 'Signls_Sdk_Loader', false ) ) {
12 final class Signls_Sdk_Loader {
13
14 private static $candidates = array();
15
16 private static $descriptors = array();
17
18 private static $booted = false;
19
20 private static $hooked = false;
21
22 public static function add_candidate( $version, $bootstrap, $minimum_php = '7.4.0' ) {
23 if ( ! is_string( $version ) || ! is_string( $bootstrap ) || ! is_string( $minimum_php ) ) {
24 return;
25 }
26
27 if ( ! is_file( $bootstrap ) || version_compare( PHP_VERSION, $minimum_php, '<' ) ) {
28 return;
29 }
30
31 self::$candidates[ $version . '|' . $bootstrap ] = array(
32 'version' => $version,
33 'bootstrap' => $bootstrap,
34 );
35 }
36
37 public static function register( array $descriptor ) {
38 $product = isset( $descriptor['product_slug'] ) ? (string) $descriptor['product_slug'] : '';
39 if ( ! preg_match( '/^[a-z0-9][a-z0-9-]{1,63}$/', $product ) ) {
40 return false;
41 }
42
43 self::$descriptors[ $product ] = $descriptor;
44 if ( self::$booted ) {
45 return self::boot_descriptor( $descriptor );
46 }
47 self::schedule_boot();
48 return true;
49 }
50
51 public static function boot() {
52 if ( self::$booted ) {
53 return;
54 }
55
56 self::$booted = true;
57 $candidate = self::best_candidate();
58 if ( null === $candidate ) {
59 return;
60 }
61
62 if ( ! self::require_file( $candidate['bootstrap'] ) ) {
63 return;
64 }
65 if ( ! class_exists( 'Signls\\Sdk\\V1\\Runtime' ) ) {
66 return;
67 }
68
69 foreach ( self::$descriptors as $descriptor ) {
70 self::boot_descriptor( $descriptor );
71 }
72 }
73
74 public static function selected_version() {
75 $candidate = self::best_candidate();
76 return null === $candidate ? null : $candidate['version'];
77 }
78
79 private static function schedule_boot() {
80 if ( self::$booted ) {
81 return;
82 }
83
84 if ( function_exists( 'did_action' ) && did_action( 'plugins_loaded' ) ) {
85 self::boot();
86 return;
87 }
88
89 if ( ! self::$hooked && function_exists( 'add_action' ) ) {
90 self::$hooked = true;
91 add_action( 'plugins_loaded', array( __CLASS__, 'boot' ), PHP_INT_MAX );
92 }
93 }
94
95 private static function best_candidate() {
96 if ( empty( self::$candidates ) ) {
97 return null;
98 }
99
100 $candidates = array_values( self::$candidates );
101 usort(
102 $candidates,
103 static function ( $left, $right ) {
104 return version_compare( $right['version'], $left['version'] );
105 }
106 );
107
108 return $candidates[0];
109 }
110
111 private static function boot_descriptor( array $descriptor ) {
112 try {
113 $adapter_file = isset( $descriptor['adapter_file'] ) ? (string) $descriptor['adapter_file'] : '';
114 if ( '' !== $adapter_file && ! self::require_file( $adapter_file ) ) {
115 return false;
116 }
117 return \Signls\Sdk\V1\Runtime::boot( $descriptor );
118 } catch ( \Throwable $error ) {
119 // A product's observations must never break the host plugin.
120 return false;
121 }
122 }
123
124 private static function require_file( $path ) {
125 if ( ! is_string( $path ) || ! is_file( $path ) ) {
126 return false;
127 }
128 set_error_handler( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- Contain a transient missing-file warning during plugin replacement.
129 static function ( $severity, $message ) use ( $path ) {
130 return E_WARNING === $severity && false !== strpos( $message, $path );
131 }
132 );
133 try {
134 require_once $path;
135 return true;
136 } catch ( \Throwable $error ) {
137 return false;
138 } finally {
139 restore_error_handler();
140 }
141 }
142 }
143 }
144
145 require_once __DIR__ . '/bridge-1-1-5.php';
146 require_once __DIR__ . '/bridge-1-1-6.php';
147 require_once __DIR__ . '/bridge-1-1-7.php';
148
149 Signls_Sdk_Loader::add_candidate( '1.1.10', __DIR__ . '/sdk.php', '7.4.0' );
150