PluginUpdateChecker.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace YahnisElsts\PluginUpdateChecker\v5p0\Vcs;
  3. use YahnisElsts\PluginUpdateChecker\v5p0\Plugin;
  4. if ( !class_exists(PluginUpdateChecker::class, false) ):
  5. class PluginUpdateChecker extends Plugin\UpdateChecker implements BaseChecker {
  6. use VcsCheckerMethods;
  7. /**
  8. * PluginUpdateChecker constructor.
  9. *
  10. * @param Api $api
  11. * @param string $pluginFile
  12. * @param string $slug
  13. * @param int $checkPeriod
  14. * @param string $optionName
  15. * @param string $muPluginFile
  16. */
  17. public function __construct($api, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '') {
  18. $this->api = $api;
  19. parent::__construct($api->getRepositoryUrl(), $pluginFile, $slug, $checkPeriod, $optionName, $muPluginFile);
  20. $this->api->setHttpFilterName($this->getUniqueName('request_info_options'));
  21. $this->api->setStrategyFilterName($this->getUniqueName('vcs_update_detection_strategies'));
  22. $this->api->setSlug($this->slug);
  23. }
  24. public function requestInfo($unusedParameter = null) {
  25. //We have to make several remote API requests to gather all the necessary info
  26. //which can take a while on slow networks.
  27. if ( function_exists('set_time_limit') ) {
  28. @set_time_limit(60);
  29. }
  30. $api = $this->api;
  31. $api->setLocalDirectory($this->package->getAbsoluteDirectoryPath());
  32. $info = new Plugin\PluginInfo();
  33. $info->filename = $this->pluginFile;
  34. $info->slug = $this->slug;
  35. $this->setInfoFromHeader($this->package->getPluginHeader(), $info);
  36. $this->setIconsFromLocalAssets($info);
  37. $this->setBannersFromLocalAssets($info);
  38. //Pick a branch or tag.
  39. $updateSource = $api->chooseReference($this->branch);
  40. if ( $updateSource ) {
  41. $ref = $updateSource->name;
  42. $info->version = $updateSource->version;
  43. $info->last_updated = $updateSource->updated;
  44. $info->download_url = $updateSource->downloadUrl;
  45. if ( !empty($updateSource->changelog) ) {
  46. $info->sections['changelog'] = $updateSource->changelog;
  47. }
  48. if ( isset($updateSource->downloadCount) ) {
  49. $info->downloaded = $updateSource->downloadCount;
  50. }
  51. } else {
  52. //There's probably a network problem or an authentication error.
  53. do_action(
  54. 'puc_api_error',
  55. new \WP_Error(
  56. 'puc-no-update-source',
  57. 'Could not retrieve version information from the repository. '
  58. . 'This usually means that the update checker either can\'t connect '
  59. . 'to the repository or it\'s configured incorrectly.'
  60. ),
  61. null, null, $this->slug
  62. );
  63. return null;
  64. }
  65. //Get headers from the main plugin file in this branch/tag. Its "Version" header and other metadata
  66. //are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags.
  67. $mainPluginFile = basename($this->pluginFile);
  68. $remotePlugin = $api->getRemoteFile($mainPluginFile, $ref);
  69. if ( !empty($remotePlugin) ) {
  70. $remoteHeader = $this->package->getFileHeader($remotePlugin);
  71. $this->setInfoFromHeader($remoteHeader, $info);
  72. }
  73. //Try parsing readme.txt. If it's formatted according to WordPress.org standards, it will contain
  74. //a lot of useful information like the required/tested WP version, changelog, and so on.
  75. if ( $this->readmeTxtExistsLocally() ) {
  76. $this->setInfoFromRemoteReadme($ref, $info);
  77. }
  78. //The changelog might be in a separate file.
  79. if ( empty($info->sections['changelog']) ) {
  80. $info->sections['changelog'] = $api->getRemoteChangelog($ref, $this->package->getAbsoluteDirectoryPath());
  81. if ( empty($info->sections['changelog']) ) {
  82. $info->sections['changelog'] = __('There is no changelog available.', 'plugin-update-checker');
  83. }
  84. }
  85. if ( empty($info->last_updated) ) {
  86. //Fetch the latest commit that changed the tag or branch and use it as the "last_updated" date.
  87. $latestCommitTime = $api->getLatestCommitTime($ref);
  88. if ( $latestCommitTime !== null ) {
  89. $info->last_updated = $latestCommitTime;
  90. }
  91. }
  92. $info = apply_filters($this->getUniqueName('request_info_result'), $info, null);
  93. return $info;
  94. }
  95. /**
  96. * Check if the currently installed version has a readme.txt file.
  97. *
  98. * @return bool
  99. */
  100. protected function readmeTxtExistsLocally() {
  101. return $this->package->fileExists($this->api->getLocalReadmeName());
  102. }
  103. /**
  104. * Copy plugin metadata from a file header to a Plugin Info object.
  105. *
  106. * @param array $fileHeader
  107. * @param Plugin\PluginInfo $pluginInfo
  108. */
  109. protected function setInfoFromHeader($fileHeader, $pluginInfo) {
  110. $headerToPropertyMap = array(
  111. 'Version' => 'version',
  112. 'Name' => 'name',
  113. 'PluginURI' => 'homepage',
  114. 'Author' => 'author',
  115. 'AuthorName' => 'author',
  116. 'AuthorURI' => 'author_homepage',
  117. 'Requires WP' => 'requires',
  118. 'Tested WP' => 'tested',
  119. 'Requires at least' => 'requires',
  120. 'Tested up to' => 'tested',
  121. 'Requires PHP' => 'requires_php',
  122. );
  123. foreach ($headerToPropertyMap as $headerName => $property) {
  124. if ( isset($fileHeader[$headerName]) && !empty($fileHeader[$headerName]) ) {
  125. $pluginInfo->$property = $fileHeader[$headerName];
  126. }
  127. }
  128. if ( !empty($fileHeader['Description']) ) {
  129. $pluginInfo->sections['description'] = $fileHeader['Description'];
  130. }
  131. }
  132. /**
  133. * Copy plugin metadata from the remote readme.txt file.
  134. *
  135. * @param string $ref GitHub tag or branch where to look for the readme.
  136. * @param Plugin\PluginInfo $pluginInfo
  137. */
  138. protected function setInfoFromRemoteReadme($ref, $pluginInfo) {
  139. $readme = $this->api->getRemoteReadme($ref);
  140. if ( empty($readme) ) {
  141. return;
  142. }
  143. if ( isset($readme['sections']) ) {
  144. $pluginInfo->sections = array_merge($pluginInfo->sections, $readme['sections']);
  145. }
  146. if ( !empty($readme['tested_up_to']) ) {
  147. $pluginInfo->tested = $readme['tested_up_to'];
  148. }
  149. if ( !empty($readme['requires_at_least']) ) {
  150. $pluginInfo->requires = $readme['requires_at_least'];
  151. }
  152. if ( !empty($readme['requires_php']) ) {
  153. $pluginInfo->requires_php = $readme['requires_php'];
  154. }
  155. if ( isset($readme['upgrade_notice'], $readme['upgrade_notice'][$pluginInfo->version]) ) {
  156. $pluginInfo->upgrade_notice = $readme['upgrade_notice'][$pluginInfo->version];
  157. }
  158. }
  159. /**
  160. * Add icons from the currently installed version to a Plugin Info object.
  161. *
  162. * The icons should be in a subdirectory named "assets". Supported image formats
  163. * and file names are described here:
  164. * @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons
  165. *
  166. * @param Plugin\PluginInfo $pluginInfo
  167. */
  168. protected function setIconsFromLocalAssets($pluginInfo) {
  169. $icons = $this->getLocalAssetUrls(array(
  170. 'icon.svg' => 'svg',
  171. 'icon-256x256.png' => '2x',
  172. 'icon-256x256.jpg' => '2x',
  173. 'icon-128x128.png' => '1x',
  174. 'icon-128x128.jpg' => '1x',
  175. ));
  176. if ( !empty($icons) ) {
  177. //The "default" key seems to be used only as last-resort fallback in WP core (5.8/5.9),
  178. //but we'll set it anyway in case some code somewhere needs it.
  179. reset($icons);
  180. $firstKey = key($icons);
  181. $icons['default'] = $icons[$firstKey];
  182. $pluginInfo->icons = $icons;
  183. }
  184. }
  185. /**
  186. * Add banners from the currently installed version to a Plugin Info object.
  187. *
  188. * The banners should be in a subdirectory named "assets". Supported image formats
  189. * and file names are described here:
  190. * @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-headers
  191. *
  192. * @param Plugin\PluginInfo $pluginInfo
  193. */
  194. protected function setBannersFromLocalAssets($pluginInfo) {
  195. $banners = $this->getLocalAssetUrls(array(
  196. 'banner-772x250.png' => 'high',
  197. 'banner-772x250.jpg' => 'high',
  198. 'banner-1544x500.png' => 'low',
  199. 'banner-1544x500.jpg' => 'low',
  200. ));
  201. if ( !empty($banners) ) {
  202. $pluginInfo->banners = $banners;
  203. }
  204. }
  205. /**
  206. * @param array<string, string> $filesToKeys
  207. * @return array<string, string>
  208. */
  209. protected function getLocalAssetUrls($filesToKeys) {
  210. $assetDirectory = $this->package->getAbsoluteDirectoryPath() . DIRECTORY_SEPARATOR . 'assets';
  211. if ( !is_dir($assetDirectory) ) {
  212. return array();
  213. }
  214. $assetBaseUrl = trailingslashit(plugins_url('', $assetDirectory . '/imaginary.file'));
  215. $foundAssets = array();
  216. foreach ($filesToKeys as $fileName => $key) {
  217. $fullBannerPath = $assetDirectory . DIRECTORY_SEPARATOR . $fileName;
  218. if ( !isset($icons[$key]) && is_file($fullBannerPath) ) {
  219. $foundAssets[$key] = $assetBaseUrl . $fileName;
  220. }
  221. }
  222. return $foundAssets;
  223. }
  224. }
  225. endif;