Geen omschrijving

class-plugin-locator.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 scans the WordPress installation to find active plugins.
  11. */
  12. class Plugin_Locator {
  13. /**
  14. * The path processor for finding plugin paths.
  15. *
  16. * @var Path_Processor
  17. */
  18. private $path_processor;
  19. /**
  20. * The constructor.
  21. *
  22. * @param Path_Processor $path_processor The Path_Processor instance.
  23. */
  24. public function __construct( $path_processor ) {
  25. $this->path_processor = $path_processor;
  26. }
  27. /**
  28. * Finds the path to the current plugin.
  29. *
  30. * @return string $path The path to the current plugin.
  31. *
  32. * @throws \RuntimeException If the current plugin does not have an autoloader.
  33. */
  34. public function find_current_plugin() {
  35. // Escape from `vendor/__DIR__` to root plugin directory.
  36. $plugin_directory = dirname( dirname( __DIR__ ) );
  37. // Use the path processor to ensure that this is an autoloader we're referencing.
  38. $path = $this->path_processor->find_directory_with_autoloader( $plugin_directory, array() );
  39. if ( false === $path ) {
  40. throw new \RuntimeException( 'Failed to locate plugin ' . $plugin_directory );
  41. }
  42. return $path;
  43. }
  44. /**
  45. * Checks a given option for plugin paths.
  46. *
  47. * @param string $option_name The option that we want to check for plugin information.
  48. * @param bool $site_option Indicates whether or not we want to check the site option.
  49. *
  50. * @return array $plugin_paths The list of absolute paths we've found.
  51. */
  52. public function find_using_option( $option_name, $site_option = false ) {
  53. $raw = $site_option ? get_site_option( $option_name ) : get_option( $option_name );
  54. if ( false === $raw ) {
  55. return array();
  56. }
  57. return $this->convert_plugins_to_paths( $raw );
  58. }
  59. /**
  60. * Checks for plugins in the `action` request parameter.
  61. *
  62. * @param string[] $allowed_actions The actions that we're allowed to return plugins for.
  63. *
  64. * @return array $plugin_paths The list of absolute paths we've found.
  65. */
  66. public function find_using_request_action( $allowed_actions ) {
  67. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  68. /**
  69. * Note: we're not actually checking the nonce here because it's too early
  70. * in the execution. The pluggable functions are not yet loaded to give
  71. * plugins a chance to plug their versions. Therefore we're doing the bare
  72. * minimum: checking whether the nonce exists and it's in the right place.
  73. * The request will fail later if the nonce doesn't pass the check.
  74. */
  75. if ( empty( $_REQUEST['_wpnonce'] ) ) {
  76. return array();
  77. }
  78. $action = isset( $_REQUEST['action'] ) ? wp_unslash( $_REQUEST['action'] ) : false;
  79. if ( ! in_array( $action, $allowed_actions, true ) ) {
  80. return array();
  81. }
  82. $plugin_slugs = array();
  83. switch ( $action ) {
  84. case 'activate':
  85. case 'deactivate':
  86. if ( empty( $_REQUEST['plugin'] ) ) {
  87. break;
  88. }
  89. $plugin_slugs[] = wp_unslash( $_REQUEST['plugin'] );
  90. break;
  91. case 'activate-selected':
  92. case 'deactivate-selected':
  93. if ( empty( $_REQUEST['checked'] ) ) {
  94. break;
  95. }
  96. $plugin_slugs = wp_unslash( $_REQUEST['checked'] );
  97. break;
  98. }
  99. // phpcs:enable WordPress.Security.NonceVerification.Recommended
  100. return $this->convert_plugins_to_paths( $plugin_slugs );
  101. }
  102. /**
  103. * Given an array of plugin slugs or paths, this will convert them to absolute paths and filter
  104. * out the plugins that are not directory plugins. Note that array keys will also be included
  105. * if they are plugin paths!
  106. *
  107. * @param string[] $plugins Plugin paths or slugs to filter.
  108. *
  109. * @return string[]
  110. */
  111. private function convert_plugins_to_paths( $plugins ) {
  112. if ( ! is_array( $plugins ) || empty( $plugins ) ) {
  113. return array();
  114. }
  115. // We're going to look for plugins in the standard directories.
  116. $path_constants = array( WP_PLUGIN_DIR, WPMU_PLUGIN_DIR );
  117. $plugin_paths = array();
  118. foreach ( $plugins as $key => $value ) {
  119. $path = $this->path_processor->find_directory_with_autoloader( $key, $path_constants );
  120. if ( $path ) {
  121. $plugin_paths[] = $path;
  122. }
  123. $path = $this->path_processor->find_directory_with_autoloader( $value, $path_constants );
  124. if ( $path ) {
  125. $plugin_paths[] = $path;
  126. }
  127. }
  128. return $plugin_paths;
  129. }
  130. }