| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class ActionScheduler_Versions |
| 5 |
*/ |
| 6 |
class ActionScheduler_Versions { |
| 7 |
/** |
| 8 |
* ActionScheduler_Versions instance. |
| 9 |
* |
| 10 |
* @var ActionScheduler_Versions |
| 11 |
*/ |
| 12 |
private static $instance = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* Versions. |
| 16 |
* |
| 17 |
* @var array<string, callable> |
| 18 |
*/ |
| 19 |
private $versions = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* Registered sources. |
| 23 |
* |
| 24 |
* @var array<string, string> |
| 25 |
*/ |
| 26 |
private $sources = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* Register version's callback. |
| 30 |
* |
| 31 |
* @param string $version_string Action Scheduler version. |
| 32 |
* @param callable $initialization_callback Callback to initialize the version. |
| 33 |
*/ |
| 34 |
public function register( $version_string, $initialization_callback ) { |
| 35 |
if ( isset( $this->versions[ $version_string ] ) ) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 40 |
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); |
| 41 |
$source = $backtrace[0]['file']; |
| 42 |
|
| 43 |
$this->versions[ $version_string ] = $initialization_callback; |
| 44 |
$this->sources[ $source ] = $version_string; |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get all versions. |
| 50 |
*/ |
| 51 |
public function get_versions() { |
| 52 |
return $this->versions; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get registered sources. |
| 57 |
* |
| 58 |
* Use with caution: this method is only available as of Action Scheduler's 3.9.1 |
| 59 |
* release and, owing to the way Action Scheduler is loaded, it's possible that the |
| 60 |
* class definition used at runtime will belong to an earlier version. |
| 61 |
* |
| 62 |
* @since 3.9.1 |
| 63 |
* |
| 64 |
* @return array<string, string> |
| 65 |
*/ |
| 66 |
public function get_sources() { |
| 67 |
return $this->sources; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get latest version registered. |
| 72 |
*/ |
| 73 |
public function latest_version() { |
| 74 |
$keys = array_keys( $this->versions ); |
| 75 |
if ( empty( $keys ) ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
uasort( $keys, 'version_compare' ); |
| 79 |
return end( $keys ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get callback for latest registered version. |
| 84 |
*/ |
| 85 |
public function latest_version_callback() { |
| 86 |
$latest = $this->latest_version(); |
| 87 |
|
| 88 |
if ( empty( $latest ) || ! isset( $this->versions[ $latest ] ) ) { |
| 89 |
return '__return_null'; |
| 90 |
} |
| 91 |
|
| 92 |
return $this->versions[ $latest ]; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get instance. |
| 97 |
* |
| 98 |
* @return ActionScheduler_Versions |
| 99 |
* @codeCoverageIgnore |
| 100 |
*/ |
| 101 |
public static function instance() { |
| 102 |
if ( empty( self::$instance ) ) { |
| 103 |
self::$instance = new self(); |
| 104 |
} |
| 105 |
return self::$instance; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Initialize. |
| 110 |
* |
| 111 |
* @codeCoverageIgnore |
| 112 |
*/ |
| 113 |
public static function initialize_latest_version() { |
| 114 |
$self = self::instance(); |
| 115 |
call_user_func( $self->latest_version_callback() ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns information about the plugin or theme which contains the current active version |
| 120 |
* of Action Scheduler. |
| 121 |
* |
| 122 |
* If this cannot be determined, or if Action Scheduler is being loaded via some other |
| 123 |
* method, then it will return an empty array. Otherwise, if populated, the array will |
| 124 |
* look like the following: |
| 125 |
* |
| 126 |
* [ |
| 127 |
* 'type' => 'plugin', # or 'theme' |
| 128 |
* 'name' => 'Name', |
| 129 |
* ] |
| 130 |
* |
| 131 |
* @deprecated 3.9.2 Use ActionScheduler_SystemInformation::active_source(). |
| 132 |
* |
| 133 |
* @return array |
| 134 |
*/ |
| 135 |
public function active_source(): array { |
| 136 |
_deprecated_function( __METHOD__, '3.9.2', 'ActionScheduler_SystemInformation::active_source()' ); |
| 137 |
return ActionScheduler_SystemInformation::active_source(); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns the directory path for the currently active installation of Action Scheduler. |
| 142 |
* |
| 143 |
* @deprecated 3.9.2 Use ActionScheduler_SystemInformation::active_source_path(). |
| 144 |
* |
| 145 |
* @return string |
| 146 |
*/ |
| 147 |
public function active_source_path(): string { |
| 148 |
_deprecated_function( __METHOD__, '3.9.2', 'ActionScheduler_SystemInformation::active_source_path()' ); |
| 149 |
return ActionScheduler_SystemInformation::active_source_path(); |
| 150 |
} |
| 151 |
} |
| 152 |
|