An updated theme (based on Atticus Finch) for ClassicPress
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.0 KiB

3 years ago
  1. <?php
  2. if ( !class_exists('Puc_v4p1_Vcs_ThemeUpdateChecker', false) ):
  3. class Puc_v4p1_Vcs_ThemeUpdateChecker extends Puc_v4p1_Theme_UpdateChecker implements Puc_v4p1_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_v4p1_Vcs_Api Repository API client.
  10. */
  11. protected $api = null;
  12. /**
  13. * Puc_v4p1_Vcs_ThemeUpdateChecker constructor.
  14. *
  15. * @param Puc_v4p1_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. }
  26. public function requestUpdate() {
  27. $api = $this->api;
  28. $update = new Puc_v4p1_Theme_Update();
  29. $update->slug = $this->slug;
  30. //Figure out which reference (tag or branch) we'll use to get the latest version of the theme.
  31. $updateSource = $api->chooseReference($this->branch);
  32. if ( $updateSource ) {
  33. $ref = $updateSource->name;
  34. $update->download_url = $updateSource->downloadUrl;
  35. } else {
  36. $ref = $this->branch;
  37. }
  38. //Get headers from the main stylesheet in this branch/tag. Its "Version" header and other metadata
  39. //are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags.
  40. $remoteHeader = $this->getFileHeader($api->getRemoteFile('style.css', $ref));
  41. $update->version = Puc_v4p1_Utils::findNotEmpty(array(
  42. $remoteHeader['Version'],
  43. Puc_v4p1_Utils::get($updateSource, 'version'),
  44. ));
  45. //The details URL defaults to the Theme URI header or the repository URL.
  46. $update->details_url = Puc_v4p1_Utils::findNotEmpty(array(
  47. $remoteHeader['ThemeURI'],
  48. $this->theme->get('ThemeURI'),
  49. $this->metadataUrl,
  50. ));
  51. if ( empty($update->version) ) {
  52. //It looks like we didn't find a valid update after all.
  53. $update = null;
  54. }
  55. $update = $this->filterUpdateResult($update);
  56. return $update;
  57. }
  58. //FIXME: This is duplicated code. Both theme and plugin subclasses that use VCS share these methods.
  59. public function setBranch($branch) {
  60. $this->branch = $branch;
  61. return $this;
  62. }
  63. public function setAuthentication($credentials) {
  64. $this->api->setAuthentication($credentials);
  65. return $this;
  66. }
  67. public function getUpdate() {
  68. $update = parent::getUpdate();
  69. if ( isset($update) && !empty($update->download_url) ) {
  70. $update->download_url = $this->api->signDownloadUrl($update->download_url);
  71. }
  72. return $update;
  73. }
  74. public function onDisplayConfiguration($panel) {
  75. parent::onDisplayConfiguration($panel);
  76. $panel->row('Branch', $this->branch);
  77. $panel->row('Authentication enabled', $this->api->isAuthenticationEnabled() ? 'Yes' : 'No');
  78. $panel->row('API client', get_class($this->api));
  79. }
  80. }
  81. endif;