Нет описания

class-shutdown-handler.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * This file was automatically generated by automattic/jetpack-autoloader.
  4. *
  5. * @package automattic/jetpack-autoloader
  6. */
  7. namespace Automattic\Jetpack\Autoloader\jpf11009ded9fc4592b6a05b61ce272b3c_jetpackⓥ10_2;
  8. // phpcs:ignore
  9. /**
  10. * This class handles the shutdown of the autoloader.
  11. */
  12. class Shutdown_Handler {
  13. /**
  14. * The Plugins_Handler instance.
  15. *
  16. * @var Plugins_Handler
  17. */
  18. private $plugins_handler;
  19. /**
  20. * The plugins cached by this autoloader.
  21. *
  22. * @var string[]
  23. */
  24. private $cached_plugins;
  25. /**
  26. * Indicates whether or not this autoloader was included by another.
  27. *
  28. * @var bool
  29. */
  30. private $was_included_by_autoloader;
  31. /**
  32. * Constructor.
  33. *
  34. * @param Plugins_Handler $plugins_handler The Plugins_Handler instance to use.
  35. * @param string[] $cached_plugins The plugins cached by the autoloaer.
  36. * @param bool $was_included_by_autoloader Indicates whether or not the autoloader was included by another.
  37. */
  38. public function __construct( $plugins_handler, $cached_plugins, $was_included_by_autoloader ) {
  39. $this->plugins_handler = $plugins_handler;
  40. $this->cached_plugins = $cached_plugins;
  41. $this->was_included_by_autoloader = $was_included_by_autoloader;
  42. }
  43. /**
  44. * Handles the shutdown of the autoloader.
  45. */
  46. public function __invoke() {
  47. // Don't save a broken cache if an error happens during some plugin's initialization.
  48. if ( ! did_action( 'plugins_loaded' ) ) {
  49. // Ensure that the cache is emptied to prevent consecutive failures if the cache is to blame.
  50. if ( ! empty( $this->cached_plugins ) ) {
  51. $this->plugins_handler->cache_plugins( array() );
  52. }
  53. return;
  54. }
  55. // Load the active plugins fresh since the list we pulled earlier might not contain
  56. // plugins that were activated but did not reset the autoloader. This happens
  57. // when a plugin is in the cache but not "active" when the autoloader loads.
  58. // We also want to make sure that plugins which are deactivating are not
  59. // considered "active" so that they will be removed from the cache now.
  60. try {
  61. $active_plugins = $this->plugins_handler->get_active_plugins( false, ! $this->was_included_by_autoloader );
  62. } catch ( \Exception $ex ) {
  63. // When the package is deleted before shutdown it will throw an exception.
  64. // In the event this happens we should erase the cache.
  65. if ( ! empty( $this->cached_plugins ) ) {
  66. $this->plugins_handler->cache_plugins( array() );
  67. }
  68. return;
  69. }
  70. // The paths should be sorted for easy comparisons with those loaded from the cache.
  71. // Note we don't need to sort the cached entries because they're already sorted.
  72. sort( $active_plugins );
  73. // We don't want to waste time saving a cache that hasn't changed.
  74. if ( $this->cached_plugins === $active_plugins ) {
  75. return;
  76. }
  77. $this->plugins_handler->cache_plugins( $active_plugins );
  78. }
  79. }