PluginUpdateChecker.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace YahnisElsts\PluginUpdateChecker\v5p1\Vcs;
  3. use YahnisElsts\PluginUpdateChecker\v5p1\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. //Sanity check: Reject updates that don't have a version number.
  74. //This can happen when we're using a branch, and we either fail to retrieve the main plugin
  75. //file or the file doesn't have a "Version" header.
  76. if ( empty($info->version) ) {
  77. do_action(
  78. 'puc_api_error',
  79. new \WP_Error(
  80. 'puc-no-plugin-version',
  81. 'Could not find the version number in the repository.'
  82. ),
  83. null, null, $this->slug
  84. );
  85. return null;
  86. }
  87. //Try parsing readme.txt. If it's formatted according to WordPress.org standards, it will contain
  88. //a lot of useful information like the required/tested WP version, changelog, and so on.
  89. if ( $this->readmeTxtExistsLocally() ) {
  90. $this->setInfoFromRemoteReadme($ref, $info);
  91. }
  92. //The changelog might be in a separate file.
  93. if ( empty($info->sections['changelog']) ) {
  94. $info->sections['changelog'] = $api->getRemoteChangelog($ref, $this->package->getAbsoluteDirectoryPath());
  95. if ( empty($info->sections['changelog']) ) {
  96. $info->sections['changelog'] = __('There is no changelog available.', 'plugin-update-checker');
  97. }
  98. }
  99. if ( empty($info->last_updated) ) {
  100. //Fetch the latest commit that changed the tag or branch and use it as the "last_updated" date.
  101. $latestCommitTime = $api->getLatestCommitTime($ref);
  102. if ( $latestCommitTime !== null ) {
  103. $info->last_updated = $latestCommitTime;
  104. }
  105. }
  106. $info = apply_filters($this->getUniqueName('request_info_result'), $info, null);
  107. return $info;
  108. }
  109. /**
  110. * Check if the currently installed version has a readme.txt file.
  111. *
  112. * @return bool
  113. */
  114. protected function readmeTxtExistsLocally() {
  115. return $this->package->fileExists($this->api->getLocalReadmeName());
  116. }
  117. /**
  118. * Copy plugin metadata from a file header to a Plugin Info object.
  119. *
  120. * @param array $fileHeader
  121. * @param Plugin\PluginInfo $pluginInfo
  122. */
  123. protected function setInfoFromHeader($fileHeader, $pluginInfo) {
  124. $headerToPropertyMap = array(
  125. 'Version' => 'version',
  126. 'Name' => 'name',
  127. 'PluginURI' => 'homepage',
  128. 'Author' => 'author',
  129. 'AuthorName' => 'author',
  130. 'AuthorURI' => 'author_homepage',
  131. 'Requires WP' => 'requires',
  132. 'Tested WP' => 'tested',
  133. 'Requires at least' => 'requires',
  134. 'Tested up to' => 'tested',
  135. 'Requires PHP' => 'requires_php',
  136. );
  137. foreach ($headerToPropertyMap as $headerName => $property) {
  138. if ( isset($fileHeader[$headerName]) && !empty($fileHeader[$headerName]) ) {
  139. $pluginInfo->$property = $fileHeader[$headerName];
  140. }
  141. }
  142. if ( !empty($fileHeader['Description']) ) {
  143. $pluginInfo->sections['description'] = $fileHeader['Description'];
  144. }
  145. }
  146. /**
  147. * Copy plugin metadata from the remote readme.txt file.
  148. *
  149. * @param string $ref GitHub tag or branch where to look for the readme.
  150. * @param Plugin\PluginInfo $pluginInfo
  151. */
  152. protected function setInfoFromRemoteReadme($ref, $pluginInfo) {
  153. $readme = $this->api->getRemoteReadme($ref);
  154. if ( empty($readme) ) {
  155. return;
  156. }
  157. if ( isset($readme['sections']) ) {
  158. $pluginInfo->sections = array_merge($pluginInfo->sections, $readme['sections']);
  159. }
  160. if ( !empty($readme['tested_up_to']) ) {
  161. $pluginInfo->tested = $readme['tested_up_to'];
  162. }
  163. if ( !empty($readme['requires_at_least']) ) {
  164. $pluginInfo->requires = $readme['requires_at_least'];
  165. }
  166. if ( !empty($readme['requires_php']) ) {
  167. $pluginInfo->requires_php = $readme['requires_php'];
  168. }
  169. if ( isset($readme['upgrade_notice'], $readme['upgrade_notice'][$pluginInfo->version]) ) {
  170. $pluginInfo->upgrade_notice = $readme['upgrade_notice'][$pluginInfo->version];
  171. }
  172. }
  173. /**
  174. * Add icons from the currently installed version to a Plugin Info object.
  175. *
  176. * The icons should be in a subdirectory named "assets". Supported image formats
  177. * and file names are described here:
  178. * @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons
  179. *
  180. * @param Plugin\PluginInfo $pluginInfo
  181. */
  182. protected function setIconsFromLocalAssets($pluginInfo) {
  183. $icons = $this->getLocalAssetUrls(array(
  184. 'icon.svg' => 'svg',
  185. 'icon-256x256.png' => '2x',
  186. 'icon-256x256.jpg' => '2x',
  187. 'icon-128x128.png' => '1x',
  188. 'icon-128x128.jpg' => '1x',
  189. ));
  190. if ( !empty($icons) ) {
  191. //The "default" key seems to be used only as last-resort fallback in WP core (5.8/5.9),
  192. //but we'll set it anyway in case some code somewhere needs it.
  193. reset($icons);
  194. $firstKey = key($icons);
  195. $icons['default'] = $icons[$firstKey];
  196. $pluginInfo->icons = $icons;
  197. }
  198. }
  199. /**
  200. * Add banners from the currently installed version to a Plugin Info object.
  201. *
  202. * The banners should be in a subdirectory named "assets". Supported image formats
  203. * and file names are described here:
  204. * @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-headers
  205. *
  206. * @param Plugin\PluginInfo $pluginInfo
  207. */
  208. protected function setBannersFromLocalAssets($pluginInfo) {
  209. $banners = $this->getLocalAssetUrls(array(
  210. 'banner-772x250.png' => 'high',
  211. 'banner-772x250.jpg' => 'high',
  212. 'banner-1544x500.png' => 'low',
  213. 'banner-1544x500.jpg' => 'low',
  214. ));
  215. if ( !empty($banners) ) {
  216. $pluginInfo->banners = $banners;
  217. }
  218. }
  219. /**
  220. * @param array<string, string> $filesToKeys
  221. * @return array<string, string>
  222. */
  223. protected function getLocalAssetUrls($filesToKeys) {
  224. $assetDirectory = $this->package->getAbsoluteDirectoryPath() . DIRECTORY_SEPARATOR . 'assets';
  225. if ( !is_dir($assetDirectory) ) {
  226. return array();
  227. }
  228. $assetBaseUrl = trailingslashit(plugins_url('', $assetDirectory . '/imaginary.file'));
  229. $foundAssets = array();
  230. foreach ($filesToKeys as $fileName => $key) {
  231. $fullBannerPath = $assetDirectory . DIRECTORY_SEPARATOR . $fileName;
  232. if ( !isset($icons[$key]) && is_file($fullBannerPath) ) {
  233. $foundAssets[$key] = $assetBaseUrl . $fileName;
  234. }
  235. }
  236. return $foundAssets;
  237. }
  238. }
  239. endif;