| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
defined( 'ABSPATH' ) || exit; |
| 7 |
|
| 8 |
/** |
| 9 |
* Elects a single "leader" among the add-ons' updaters. |
| 10 |
* |
| 11 |
* Several add-ons can run at once, each carrying its own namespaced copy of the updater, isolated from the others. |
| 12 |
* This registry lives in core as the single point they all share, and only arbitrates which updater is the leader. |
| 13 |
* HTTP and license logic stay in the package. |
| 14 |
* |
| 15 |
* @since 3.9 |
| 16 |
*/ |
| 17 |
class PLL_Updater_Registry { |
| 18 |
|
| 19 |
/** |
| 20 |
* The registered updaters. On equal versions, the first registered wins. |
| 21 |
* |
| 22 |
* @var PLL_Updater_Interface[] |
| 23 |
*/ |
| 24 |
private static $candidates = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* Announces an updater to the registry. Called from each add-on's Updater constructor. |
| 28 |
* |
| 29 |
* @since 3.9 |
| 30 |
* |
| 31 |
* @param PLL_Updater_Interface $updater The add-on's updater. |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public static function register( PLL_Updater_Interface $updater ): void { |
| 35 |
add_action( 'pll_init', array( self::class, 'elect' ), 0 ); |
| 36 |
|
| 37 |
self::$candidates[] = $updater; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Elects the highest-version updater and runs its setup once. Hooked to `pll_init`. |
| 42 |
* |
| 43 |
* @since 3.9 |
| 44 |
* |
| 45 |
* @param PLL_Base $polylang Polylang object. |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public static function elect( PLL_Base $polylang ): void { |
| 49 |
$elected = null; |
| 50 |
|
| 51 |
foreach ( self::$candidates as $candidate ) { |
| 52 |
if ( null === $elected || version_compare( $candidate::get_version(), $elected::get_version(), '>' ) ) { |
| 53 |
$elected = $candidate; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if ( null !== $elected ) { |
| 58 |
$elected->init( $polylang ); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|