UpgraderStatus.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace YahnisElsts\PluginUpdateChecker\v5p1;
  3. if ( !class_exists(UpgraderStatus::class, false) ):
  4. /**
  5. * A utility class that helps figure out which plugin or theme WordPress is upgrading.
  6. *
  7. * It may seem strange to have a separate class just for that, but the task is surprisingly complicated.
  8. * Core classes like Plugin_Upgrader don't expose the plugin file name during an in-progress update (AFAICT).
  9. * This class uses a few workarounds and heuristics to get the file name.
  10. */
  11. class UpgraderStatus {
  12. private $currentType = null; //This must be either "plugin" or "theme".
  13. private $currentId = null; //Plugin basename or theme directory name.
  14. public function __construct() {
  15. //Keep track of which plugin/theme WordPress is currently upgrading.
  16. add_filter('upgrader_pre_install', array($this, 'setUpgradedThing'), 10, 2);
  17. add_filter('upgrader_package_options', array($this, 'setUpgradedPluginFromOptions'), 10, 1);
  18. add_filter('upgrader_post_install', array($this, 'clearUpgradedThing'), 10, 1);
  19. add_action('upgrader_process_complete', array($this, 'clearUpgradedThing'), 10, 1);
  20. }
  21. /**
  22. * Is there and update being installed RIGHT NOW, for a specific plugin?
  23. *
  24. * Caution: This method is unreliable. WordPress doesn't make it easy to figure out what it is upgrading,
  25. * and upgrader implementations are liable to change without notice.
  26. *
  27. * @param string $pluginFile The plugin to check.
  28. * @param \WP_Upgrader|null $upgrader The upgrader that's performing the current update.
  29. * @return bool True if the plugin identified by $pluginFile is being upgraded.
  30. */
  31. public function isPluginBeingUpgraded($pluginFile, $upgrader = null) {
  32. return $this->isBeingUpgraded('plugin', $pluginFile, $upgrader);
  33. }
  34. /**
  35. * Is there an update being installed for a specific theme?
  36. *
  37. * @param string $stylesheet Theme directory name.
  38. * @param \WP_Upgrader|null $upgrader The upgrader that's performing the current update.
  39. * @return bool
  40. */
  41. public function isThemeBeingUpgraded($stylesheet, $upgrader = null) {
  42. return $this->isBeingUpgraded('theme', $stylesheet, $upgrader);
  43. }
  44. /**
  45. * Check if a specific theme or plugin is being upgraded.
  46. *
  47. * @param string $type
  48. * @param string $id
  49. * @param \Plugin_Upgrader|\WP_Upgrader|null $upgrader
  50. * @return bool
  51. */
  52. protected function isBeingUpgraded($type, $id, $upgrader = null) {
  53. if ( isset($upgrader) ) {
  54. list($currentType, $currentId) = $this->getThingBeingUpgradedBy($upgrader);
  55. if ( $currentType !== null ) {
  56. $this->currentType = $currentType;
  57. $this->currentId = $currentId;
  58. }
  59. }
  60. return ($this->currentType === $type) && ($this->currentId === $id);
  61. }
  62. /**
  63. * Figure out which theme or plugin is being upgraded by a WP_Upgrader instance.
  64. *
  65. * Returns an array with two items. The first item is the type of the thing that's being
  66. * upgraded: "plugin" or "theme". The second item is either the plugin basename or
  67. * the theme directory name. If we can't determine what the upgrader is doing, both items
  68. * will be NULL.
  69. *
  70. * Examples:
  71. * ['plugin', 'plugin-dir-name/plugin.php']
  72. * ['theme', 'theme-dir-name']
  73. *
  74. * @param \Plugin_Upgrader|\WP_Upgrader $upgrader
  75. * @return array
  76. */
  77. private function getThingBeingUpgradedBy($upgrader) {
  78. if ( !isset($upgrader, $upgrader->skin) ) {
  79. return array(null, null);
  80. }
  81. //Figure out which plugin or theme is being upgraded.
  82. $pluginFile = null;
  83. $themeDirectoryName = null;
  84. $skin = $upgrader->skin;
  85. if ( isset($skin->theme_info) && ($skin->theme_info instanceof \WP_Theme) ) {
  86. $themeDirectoryName = $skin->theme_info->get_stylesheet();
  87. } elseif ( $skin instanceof \Plugin_Upgrader_Skin ) {
  88. if ( isset($skin->plugin) && is_string($skin->plugin) && ($skin->plugin !== '') ) {
  89. $pluginFile = $skin->plugin;
  90. }
  91. } elseif ( $skin instanceof \Theme_Upgrader_Skin ) {
  92. if ( isset($skin->theme) && is_string($skin->theme) && ($skin->theme !== '') ) {
  93. $themeDirectoryName = $skin->theme;
  94. }
  95. } elseif ( isset($skin->plugin_info) && is_array($skin->plugin_info) ) {
  96. //This case is tricky because Bulk_Plugin_Upgrader_Skin (etc) doesn't actually store the plugin
  97. //filename anywhere. Instead, it has the plugin headers in $plugin_info. So the best we can
  98. //do is compare those headers to the headers of installed plugins.
  99. $pluginFile = $this->identifyPluginByHeaders($skin->plugin_info);
  100. }
  101. if ( $pluginFile !== null ) {
  102. return array('plugin', $pluginFile);
  103. } elseif ( $themeDirectoryName !== null ) {
  104. return array('theme', $themeDirectoryName);
  105. }
  106. return array(null, null);
  107. }
  108. /**
  109. * Identify an installed plugin based on its headers.
  110. *
  111. * @param array $searchHeaders The plugin file header to look for.
  112. * @return string|null Plugin basename ("foo/bar.php"), or NULL if we can't identify the plugin.
  113. */
  114. private function identifyPluginByHeaders($searchHeaders) {
  115. if ( !function_exists('get_plugins') ){
  116. require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  117. }
  118. $installedPlugins = get_plugins();
  119. $matches = array();
  120. foreach($installedPlugins as $pluginBasename => $headers) {
  121. $diff1 = array_diff_assoc($headers, $searchHeaders);
  122. $diff2 = array_diff_assoc($searchHeaders, $headers);
  123. if ( empty($diff1) && empty($diff2) ) {
  124. $matches[] = $pluginBasename;
  125. }
  126. }
  127. //It's possible (though very unlikely) that there could be two plugins with identical
  128. //headers. In that case, we can't unambiguously identify the plugin that's being upgraded.
  129. if ( count($matches) !== 1 ) {
  130. return null;
  131. }
  132. return reset($matches);
  133. }
  134. /**
  135. * @access private
  136. *
  137. * @param mixed $input
  138. * @param array $hookExtra
  139. * @return mixed Returns $input unaltered.
  140. */
  141. public function setUpgradedThing($input, $hookExtra) {
  142. if ( !empty($hookExtra['plugin']) && is_string($hookExtra['plugin']) ) {
  143. $this->currentId = $hookExtra['plugin'];
  144. $this->currentType = 'plugin';
  145. } elseif ( !empty($hookExtra['theme']) && is_string($hookExtra['theme']) ) {
  146. $this->currentId = $hookExtra['theme'];
  147. $this->currentType = 'theme';
  148. } else {
  149. $this->currentType = null;
  150. $this->currentId = null;
  151. }
  152. return $input;
  153. }
  154. /**
  155. * @access private
  156. *
  157. * @param array $options
  158. * @return array
  159. */
  160. public function setUpgradedPluginFromOptions($options) {
  161. if ( isset($options['hook_extra']['plugin']) && is_string($options['hook_extra']['plugin']) ) {
  162. $this->currentType = 'plugin';
  163. $this->currentId = $options['hook_extra']['plugin'];
  164. } else {
  165. $this->currentType = null;
  166. $this->currentId = null;
  167. }
  168. return $options;
  169. }
  170. /**
  171. * @access private
  172. *
  173. * @param mixed $input
  174. * @return mixed Returns $input unaltered.
  175. */
  176. public function clearUpgradedThing($input = null) {
  177. $this->currentId = null;
  178. $this->currentType = null;
  179. return $input;
  180. }
  181. }
  182. endif;