← All changes
|
vendor/jetpack-autoloader/class-latest-autoloader-guard.php
+165
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,165 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * This file was automatically generated by automattic/jetpack-autoloader. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-autoloader | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack\Autoloader\jpf11009ded9fc4592b6a05b61ce272b3c_jetpackⓥ16_3\al6_0_1; | |
| 9 | + | |
| 10 | + // phpcs:ignore | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * This class ensures that we're only executing the latest autoloader. | |
| 14 | + */ | |
| 15 | +class Latest_Autoloader_Guard { | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * The Plugins_Handler instance. | |
| 19 | + * | |
| 20 | + * @var Plugins_Handler | |
| 21 | + */ | |
| 22 | + private $plugins_handler; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * The Autoloader_Handler instance. | |
| 26 | + * | |
| 27 | + * @var Autoloader_Handler | |
| 28 | + */ | |
| 29 | + private $autoloader_handler; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * The Autoloader_locator instance. | |
| 33 | + * | |
| 34 | + * @var Autoloader_Locator | |
| 35 | + */ | |
| 36 | + private $autoloader_locator; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * The constructor. | |
| 40 | + * | |
| 41 | + * @param Plugins_Handler $plugins_handler The Plugins_Handler instance. | |
| 42 | + * @param Autoloader_Handler $autoloader_handler The Autoloader_Handler instance. | |
| 43 | + * @param Autoloader_Locator $autoloader_locator The Autoloader_Locator instance. | |
| 44 | + */ | |
| 45 | + public function __construct( $plugins_handler, $autoloader_handler, $autoloader_locator ) { | |
| 46 | + $this->plugins_handler = $plugins_handler; | |
| 47 | + $this->autoloader_handler = $autoloader_handler; | |
| 48 | + $this->autoloader_locator = $autoloader_locator; | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Indicates whether or not the autoloader should be initialized. Note that this function | |
| 53 | + * has the side-effect of actually loading the latest autoloader in the event that this | |
| 54 | + * is not it. | |
| 55 | + * | |
| 56 | + * @param string $current_plugin The current plugin we're checking. | |
| 57 | + * @param string[] $plugins The active plugins to check for autoloaders in. | |
| 58 | + * @param bool $was_included_by_autoloader Indicates whether or not this autoloader was included by another. | |
| 59 | + * | |
| 60 | + * @return bool True if we should stop initialization, otherwise false. | |
| 61 | + */ | |
| 62 | + public function should_stop_init( $current_plugin, $plugins, $was_included_by_autoloader ) { | |
| 63 | + global $jetpack_autoloader_latest_version; | |
| 64 | + | |
| 65 | + // We need to reset the autoloader when the plugins change because | |
| 66 | + // that means the autoloader was generated with a different list. | |
| 67 | + if ( $this->plugins_handler->have_plugins_changed( $plugins ) ) { | |
| 68 | + $this->autoloader_handler->reset_autoloader(); | |
| 69 | + } | |
| 70 | + | |
| 71 | + // When the latest autoloader has already been found we don't need to search for it again. | |
| 72 | + // We should take care however because this will also trigger if the autoloader has been | |
| 73 | + // included by an older one. | |
| 74 | + if ( isset( $jetpack_autoloader_latest_version ) && ! $was_included_by_autoloader ) { | |
| 75 | + return true; | |
| 76 | + } | |
| 77 | + | |
| 78 | + $latest_plugin = $this->autoloader_locator->find_latest_autoloader( $plugins, $jetpack_autoloader_latest_version ); | |
| 79 | + if ( isset( $latest_plugin ) && $latest_plugin !== $current_plugin ) { | |
| 80 | + require $this->autoloader_locator->get_autoloader_path( $latest_plugin ); | |
| 81 | + return true; | |
| 82 | + } | |
| 83 | + | |
| 84 | + return false; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Check for conflicting autoloaders. | |
| 89 | + * | |
| 90 | + * A common source of strange and confusing problems is when another plugin | |
| 91 | + * registers a Composer autoloader at a higher priority that us. If enabled, | |
| 92 | + * check for this problem and warn about it. | |
| 93 | + * | |
| 94 | + * Called from the plugins_loaded hook. | |
| 95 | + * | |
| 96 | + * @since 3.1.0 | |
| 97 | + * @return void | |
| 98 | + */ | |
| 99 | + public function check_for_conflicting_autoloaders() { | |
| 100 | + if ( ! defined( 'JETPACK_AUTOLOAD_DEBUG_CONFLICTING_LOADERS' ) || ! JETPACK_AUTOLOAD_DEBUG_CONFLICTING_LOADERS ) { | |
| 101 | + return; | |
| 102 | + } | |
| 103 | + | |
| 104 | + global $jetpack_autoloader_loader; | |
| 105 | + if ( ! isset( $jetpack_autoloader_loader ) ) { | |
| 106 | + return; | |
| 107 | + } | |
| 108 | + $prefixes = array(); | |
| 109 | + foreach ( ( $jetpack_autoloader_loader->get_class_map() ?? array() ) as $classname => $data ) { | |
| 110 | + $parts = explode( '\\', trim( $classname, '\\' ) ); | |
| 111 | + array_pop( $parts ); | |
| 112 | + while ( $parts ) { | |
| 113 | + $prefixes[ implode( '\\', $parts ) . '\\' ] = true; | |
| 114 | + array_pop( $parts ); | |
| 115 | + } | |
| 116 | + } | |
| 117 | + foreach ( ( $jetpack_autoloader_loader->get_psr4_map() ?? array() ) as $prefix => $data ) { | |
| 118 | + $parts = explode( '\\', trim( $prefix, '\\' ) ); | |
| 119 | + while ( $parts ) { | |
| 120 | + $prefixes[ implode( '\\', $parts ) . '\\' ] = true; | |
| 121 | + array_pop( $parts ); | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + $autoload_chain = spl_autoload_functions(); | |
| 126 | + if ( ! $autoload_chain ) { | |
| 127 | + return; | |
| 128 | + } | |
| 129 | + | |
| 130 | + foreach ( $autoload_chain as $autoloader ) { | |
| 131 | + // No need to check anything after us. | |
| 132 | + if ( is_array( $autoloader ) && is_string( $autoloader[0] ) && substr( $autoloader[0], 0, strlen( __NAMESPACE__ ) + 1 ) === __NAMESPACE__ . '\\' ) { | |
| 133 | + break; | |
| 134 | + } | |
| 135 | + | |
| 136 | + // We can check Composer autoloaders easily enough. | |
| 137 | + if ( is_array( $autoloader ) && $autoloader[0] instanceof \Composer\Autoload\ClassLoader && is_callable( array( $autoloader[0], 'getPrefixesPsr4' ) ) ) { | |
| 138 | + $composer_autoloader = $autoloader[0]; | |
| 139 | + foreach ( $composer_autoloader->getClassMap() as $classname => $path ) { | |
| 140 | + if ( $jetpack_autoloader_loader->find_class_file( $classname ) ) { | |
| 141 | + $msg = "A Composer autoloader is registered with a higher priority than the Jetpack Autoloader and would also handle some of the classes we handle (e.g. $classname => $path). This may cause strange and confusing problems."; | |
| 142 | + wp_trigger_error( '', $msg ); | |
| 143 | + continue 2; | |
| 144 | + } | |
| 145 | + } | |
| 146 | + foreach ( $composer_autoloader->getPrefixesPsr4() as $prefix => $paths ) { | |
| 147 | + if ( isset( $prefixes[ $prefix ] ) ) { | |
| 148 | + $path = array_pop( $paths ); | |
| 149 | + $msg = "A Composer autoloader is registered with a higher priority than the Jetpack Autoloader and would also handle some of the namespaces we handle (e.g. $prefix => $path). This may cause strange and confusing problems."; | |
| 150 | + wp_trigger_error( '', $msg ); | |
| 151 | + continue 2; | |
| 152 | + } | |
| 153 | + } | |
| 154 | + foreach ( $composer_autoloader->getPrefixes() as $prefix => $paths ) { | |
| 155 | + if ( isset( $prefixes[ $prefix ] ) ) { | |
| 156 | + $path = array_pop( $paths ); | |
| 157 | + $msg = "A Composer autoloader is registered with a higher priority than the Jetpack Autoloader and would also handle some of the namespaces we handle (e.g. $prefix => $path). This may cause strange and confusing problems."; | |
| 158 | + wp_trigger_error( '', $msg ); | |
| 159 | + continue 2; | |
| 160 | + } | |
| 161 | + } | |
| 162 | + } | |
| 163 | + } | |
| 164 | + } | |
| 165 | +} | |