1
0

Package.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace YahnisElsts\PluginUpdateChecker\v5p1\Plugin;
  3. use YahnisElsts\PluginUpdateChecker\v5p1\InstalledPackage;
  4. use YahnisElsts\PluginUpdateChecker\v5p1\PucFactory;
  5. if ( !class_exists(Package::class, false) ):
  6. class Package extends InstalledPackage {
  7. /**
  8. * @var UpdateChecker
  9. */
  10. protected $updateChecker;
  11. /**
  12. * @var string Full path of the main plugin file.
  13. */
  14. protected $pluginAbsolutePath = '';
  15. /**
  16. * @var string Plugin basename.
  17. */
  18. private $pluginFile;
  19. /**
  20. * @var string|null
  21. */
  22. private $cachedInstalledVersion = null;
  23. public function __construct($pluginAbsolutePath, $updateChecker) {
  24. $this->pluginAbsolutePath = $pluginAbsolutePath;
  25. $this->pluginFile = plugin_basename($this->pluginAbsolutePath);
  26. parent::__construct($updateChecker);
  27. //Clear the version number cache when something - anything - is upgraded or WP clears the update cache.
  28. add_filter('upgrader_post_install', array($this, 'clearCachedVersion'));
  29. add_action('delete_site_transient_update_plugins', array($this, 'clearCachedVersion'));
  30. }
  31. public function getInstalledVersion() {
  32. if ( isset($this->cachedInstalledVersion) ) {
  33. return $this->cachedInstalledVersion;
  34. }
  35. $pluginHeader = $this->getPluginHeader();
  36. if ( isset($pluginHeader['Version']) ) {
  37. $this->cachedInstalledVersion = $pluginHeader['Version'];
  38. return $pluginHeader['Version'];
  39. } else {
  40. //This can happen if the filename points to something that is not a plugin.
  41. $this->updateChecker->triggerError(
  42. sprintf(
  43. "Cannot read the Version header for '%s'. The filename is incorrect or is not a plugin.",
  44. $this->updateChecker->pluginFile
  45. ),
  46. E_USER_WARNING
  47. );
  48. return null;
  49. }
  50. }
  51. /**
  52. * Clear the cached plugin version. This method can be set up as a filter (hook) and will
  53. * return the filter argument unmodified.
  54. *
  55. * @param mixed $filterArgument
  56. * @return mixed
  57. */
  58. public function clearCachedVersion($filterArgument = null) {
  59. $this->cachedInstalledVersion = null;
  60. return $filterArgument;
  61. }
  62. public function getAbsoluteDirectoryPath() {
  63. return dirname($this->pluginAbsolutePath);
  64. }
  65. /**
  66. * Get the value of a specific plugin or theme header.
  67. *
  68. * @param string $headerName
  69. * @param string $defaultValue
  70. * @return string Either the value of the header, or $defaultValue if the header doesn't exist or is empty.
  71. */
  72. public function getHeaderValue($headerName, $defaultValue = '') {
  73. $headers = $this->getPluginHeader();
  74. if ( isset($headers[$headerName]) && ($headers[$headerName] !== '') ) {
  75. return $headers[$headerName];
  76. }
  77. return $defaultValue;
  78. }
  79. protected function getHeaderNames() {
  80. return array(
  81. 'Name' => 'Plugin Name',
  82. 'PluginURI' => 'Plugin URI',
  83. 'Version' => 'Version',
  84. 'Description' => 'Description',
  85. 'Author' => 'Author',
  86. 'AuthorURI' => 'Author URI',
  87. 'TextDomain' => 'Text Domain',
  88. 'DomainPath' => 'Domain Path',
  89. 'Network' => 'Network',
  90. //The newest WordPress version that this plugin requires or has been tested with.
  91. //We support several different formats for compatibility with other libraries.
  92. 'Tested WP' => 'Tested WP',
  93. 'Requires WP' => 'Requires WP',
  94. 'Tested up to' => 'Tested up to',
  95. 'Requires at least' => 'Requires at least',
  96. );
  97. }
  98. /**
  99. * Get the translated plugin title.
  100. *
  101. * @return string
  102. */
  103. public function getPluginTitle() {
  104. $title = '';
  105. $header = $this->getPluginHeader();
  106. if ( $header && !empty($header['Name']) && isset($header['TextDomain']) ) {
  107. $title = translate($header['Name'], $header['TextDomain']);
  108. }
  109. return $title;
  110. }
  111. /**
  112. * Get plugin's metadata from its file header.
  113. *
  114. * @return array
  115. */
  116. public function getPluginHeader() {
  117. if ( !is_file($this->pluginAbsolutePath) ) {
  118. //This can happen if the plugin filename is wrong.
  119. $this->updateChecker->triggerError(
  120. sprintf(
  121. "Can't to read the plugin header for '%s'. The file does not exist.",
  122. $this->updateChecker->pluginFile
  123. ),
  124. E_USER_WARNING
  125. );
  126. return array();
  127. }
  128. if ( !function_exists('get_plugin_data') ) {
  129. require_once(ABSPATH . '/wp-admin/includes/plugin.php');
  130. }
  131. return get_plugin_data($this->pluginAbsolutePath, false, false);
  132. }
  133. public function removeHooks() {
  134. remove_filter('upgrader_post_install', array($this, 'clearCachedVersion'));
  135. remove_action('delete_site_transient_update_plugins', array($this, 'clearCachedVersion'));
  136. }
  137. /**
  138. * Check if the plugin file is inside the mu-plugins directory.
  139. *
  140. * @return bool
  141. */
  142. public function isMuPlugin() {
  143. static $cachedResult = null;
  144. if ( $cachedResult === null ) {
  145. if ( !defined('WPMU_PLUGIN_DIR') || !is_string(WPMU_PLUGIN_DIR) ) {
  146. $cachedResult = false;
  147. return $cachedResult;
  148. }
  149. //Convert both paths to the canonical form before comparison.
  150. $muPluginDir = realpath(WPMU_PLUGIN_DIR);
  151. $pluginPath = realpath($this->pluginAbsolutePath);
  152. //If realpath() fails, just normalize the syntax instead.
  153. if (($muPluginDir === false) || ($pluginPath === false)) {
  154. $muPluginDir = PucFactory::normalizePath(WPMU_PLUGIN_DIR);
  155. $pluginPath = PucFactory::normalizePath($this->pluginAbsolutePath);
  156. }
  157. $cachedResult = (strpos($pluginPath, $muPluginDir) === 0);
  158. }
  159. return $cachedResult;
  160. }
  161. }
  162. endif;