1
0

Package.php 5.4 KB

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