ThemeUpdateChecker.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace YahnisElsts\PluginUpdateChecker\v5p1\Vcs;
  3. use YahnisElsts\PluginUpdateChecker\v5p1\Theme;
  4. use YahnisElsts\PluginUpdateChecker\v5p1\Utils;
  5. if ( !class_exists(ThemeUpdateChecker::class, false) ):
  6. class ThemeUpdateChecker extends Theme\UpdateChecker implements BaseChecker {
  7. use VcsCheckerMethods;
  8. /**
  9. * ThemeUpdateChecker constructor.
  10. *
  11. * @param Api $api
  12. * @param null $stylesheet
  13. * @param null $customSlug
  14. * @param int $checkPeriod
  15. * @param string $optionName
  16. */
  17. public function __construct($api, $stylesheet = null, $customSlug = null, $checkPeriod = 12, $optionName = '') {
  18. $this->api = $api;
  19. parent::__construct($api->getRepositoryUrl(), $stylesheet, $customSlug, $checkPeriod, $optionName);
  20. $this->api->setHttpFilterName($this->getUniqueName('request_update_options'));
  21. $this->api->setStrategyFilterName($this->getUniqueName('vcs_update_detection_strategies'));
  22. $this->api->setSlug($this->slug);
  23. }
  24. public function requestUpdate() {
  25. $api = $this->api;
  26. $api->setLocalDirectory($this->package->getAbsoluteDirectoryPath());
  27. $update = new Theme\Update();
  28. $update->slug = $this->slug;
  29. //Figure out which reference (tag or branch) we'll use to get the latest version of the theme.
  30. $updateSource = $api->chooseReference($this->branch);
  31. if ( $updateSource ) {
  32. $ref = $updateSource->name;
  33. $update->download_url = $updateSource->downloadUrl;
  34. } else {
  35. do_action(
  36. 'puc_api_error',
  37. new \WP_Error(
  38. 'puc-no-update-source',
  39. 'Could not retrieve version information from the repository. '
  40. . 'This usually means that the update checker either can\'t connect '
  41. . 'to the repository or it\'s configured incorrectly.'
  42. ),
  43. null, null, $this->slug
  44. );
  45. $ref = $this->branch;
  46. }
  47. //Get headers from the main stylesheet in this branch/tag. Its "Version" header and other metadata
  48. //are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags.
  49. $remoteHeader = $this->package->getFileHeader($api->getRemoteFile('style.css', $ref));
  50. $update->version = Utils::findNotEmpty(array(
  51. $remoteHeader['Version'],
  52. Utils::get($updateSource, 'version'),
  53. ));
  54. //The details URL defaults to the Theme URI header or the repository URL.
  55. $update->details_url = Utils::findNotEmpty(array(
  56. $remoteHeader['ThemeURI'],
  57. $this->package->getHeaderValue('ThemeURI'),
  58. $this->metadataUrl,
  59. ));
  60. if ( empty($update->version) ) {
  61. //It looks like we didn't find a valid update after all.
  62. $update = null;
  63. }
  64. $update = $this->filterUpdateResult($update);
  65. return $update;
  66. }
  67. }
  68. endif;