ThemeUpdateChecker.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. if ( !class_exists('Puc_v4p11_Vcs_ThemeUpdateChecker', false) ):
  3. class Puc_v4p11_Vcs_ThemeUpdateChecker extends Puc_v4p11_Theme_UpdateChecker implements Puc_v4p11_Vcs_BaseChecker {
  4. /**
  5. * @var string The branch where to look for updates. Defaults to "master".
  6. */
  7. protected $branch = 'master';
  8. /**
  9. * @var Puc_v4p11_Vcs_Api Repository API client.
  10. */
  11. protected $api = null;
  12. /**
  13. * Puc_v4p11_Vcs_ThemeUpdateChecker constructor.
  14. *
  15. * @param Puc_v4p11_Vcs_Api $api
  16. * @param null $stylesheet
  17. * @param null $customSlug
  18. * @param int $checkPeriod
  19. * @param string $optionName
  20. */
  21. public function __construct($api, $stylesheet = null, $customSlug = null, $checkPeriod = 12, $optionName = '') {
  22. $this->api = $api;
  23. $this->api->setHttpFilterName($this->getUniqueName('request_update_options'));
  24. parent::__construct($api->getRepositoryUrl(), $stylesheet, $customSlug, $checkPeriod, $optionName);
  25. $this->api->setSlug($this->slug);
  26. }
  27. public function requestUpdate() {
  28. $api = $this->api;
  29. $api->setLocalDirectory($this->package->getAbsoluteDirectoryPath());
  30. $update = new Puc_v4p11_Theme_Update();
  31. $update->slug = $this->slug;
  32. //Figure out which reference (tag or branch) we'll use to get the latest version of the theme.
  33. $updateSource = $api->chooseReference($this->branch);
  34. if ( $updateSource ) {
  35. $ref = $updateSource->name;
  36. $update->download_url = $updateSource->downloadUrl;
  37. } else {
  38. do_action(
  39. 'puc_api_error',
  40. new WP_Error(
  41. 'puc-no-update-source',
  42. 'Could not retrieve version information from the repository. '
  43. . 'This usually means that the update checker either can\'t connect '
  44. . 'to the repository or it\'s configured incorrectly.'
  45. ),
  46. null, null, $this->slug
  47. );
  48. $ref = $this->branch;
  49. }
  50. //Get headers from the main stylesheet in this branch/tag. Its "Version" header and other metadata
  51. //are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags.
  52. $remoteHeader = $this->package->getFileHeader($api->getRemoteFile('style.css', $ref));
  53. $update->version = Puc_v4p11_Utils::findNotEmpty(array(
  54. $remoteHeader['Version'],
  55. Puc_v4p11_Utils::get($updateSource, 'version'),
  56. ));
  57. //The details URL defaults to the Theme URI header or the repository URL.
  58. $update->details_url = Puc_v4p11_Utils::findNotEmpty(array(
  59. $remoteHeader['ThemeURI'],
  60. $this->package->getHeaderValue('ThemeURI'),
  61. $this->metadataUrl,
  62. ));
  63. if ( empty($update->version) ) {
  64. //It looks like we didn't find a valid update after all.
  65. $update = null;
  66. }
  67. $update = $this->filterUpdateResult($update);
  68. return $update;
  69. }
  70. //FIXME: This is duplicated code. Both theme and plugin subclasses that use VCS share these methods.
  71. public function setBranch($branch) {
  72. $this->branch = $branch;
  73. return $this;
  74. }
  75. public function setAuthentication($credentials) {
  76. $this->api->setAuthentication($credentials);
  77. return $this;
  78. }
  79. public function getVcsApi() {
  80. return $this->api;
  81. }
  82. public function getUpdate() {
  83. $update = parent::getUpdate();
  84. if ( isset($update) && !empty($update->download_url) ) {
  85. $update->download_url = $this->api->signDownloadUrl($update->download_url);
  86. }
  87. return $update;
  88. }
  89. public function onDisplayConfiguration($panel) {
  90. parent::onDisplayConfiguration($panel);
  91. $panel->row('Branch', $this->branch);
  92. $panel->row('Authentication enabled', $this->api->isAuthenticationEnabled() ? 'Yes' : 'No');
  93. $panel->row('API client', get_class($this->api));
  94. }
  95. }
  96. endif;