UpdateChecker.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. if ( !class_exists('Puc_v4p11_Theme_UpdateChecker', false) ):
  3. class Puc_v4p11_Theme_UpdateChecker extends Puc_v4p11_UpdateChecker {
  4. protected $filterSuffix = 'theme';
  5. protected $updateTransient = 'update_themes';
  6. protected $translationType = 'theme';
  7. /**
  8. * @var string Theme directory name.
  9. */
  10. protected $stylesheet;
  11. public function __construct($metadataUrl, $stylesheet = null, $customSlug = null, $checkPeriod = 12, $optionName = '') {
  12. if ( $stylesheet === null ) {
  13. $stylesheet = get_stylesheet();
  14. }
  15. $this->stylesheet = $stylesheet;
  16. parent::__construct(
  17. $metadataUrl,
  18. $stylesheet,
  19. $customSlug ?: $stylesheet,
  20. $checkPeriod,
  21. $optionName
  22. );
  23. }
  24. /**
  25. * For themes, the update array is indexed by theme directory name.
  26. *
  27. * @return string
  28. */
  29. protected function getUpdateListKey() {
  30. return $this->directoryName;
  31. }
  32. /**
  33. * Retrieve the latest update (if any) from the configured API endpoint.
  34. *
  35. * @return Puc_v4p11_Update|null An instance of Update, or NULL when no updates are available.
  36. */
  37. public function requestUpdate() {
  38. list($themeUpdate, $result) = $this->requestMetadata('Puc_v4p11_Theme_Update', 'request_update');
  39. if ( $themeUpdate !== null ) {
  40. /** @var Puc_v4p11_Theme_Update $themeUpdate */
  41. $themeUpdate->slug = $this->slug;
  42. }
  43. $themeUpdate = $this->filterUpdateResult($themeUpdate, $result);
  44. return $themeUpdate;
  45. }
  46. protected function getNoUpdateItemFields() {
  47. return array_merge(
  48. parent::getNoUpdateItemFields(),
  49. array(
  50. 'theme' => $this->directoryName,
  51. 'requires' => '',
  52. )
  53. );
  54. }
  55. public function userCanInstallUpdates() {
  56. return current_user_can('update_themes');
  57. }
  58. /**
  59. * Create an instance of the scheduler.
  60. *
  61. * @param int $checkPeriod
  62. * @return Puc_v4p11_Scheduler
  63. */
  64. protected function createScheduler($checkPeriod) {
  65. return new Puc_v4p11_Scheduler($this, $checkPeriod, array('load-themes.php'));
  66. }
  67. /**
  68. * Is there an update being installed right now for this theme?
  69. *
  70. * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update.
  71. * @return bool
  72. */
  73. public function isBeingUpgraded($upgrader = null) {
  74. return $this->upgraderStatus->isThemeBeingUpgraded($this->stylesheet, $upgrader);
  75. }
  76. protected function createDebugBarExtension() {
  77. return new Puc_v4p11_DebugBar_Extension($this, 'Puc_v4p11_DebugBar_ThemePanel');
  78. }
  79. /**
  80. * Register a callback for filtering query arguments.
  81. *
  82. * The callback function should take one argument - an associative array of query arguments.
  83. * It should return a modified array of query arguments.
  84. *
  85. * @param callable $callback
  86. * @return void
  87. */
  88. public function addQueryArgFilter($callback){
  89. $this->addFilter('request_update_query_args', $callback);
  90. }
  91. /**
  92. * Register a callback for filtering arguments passed to wp_remote_get().
  93. *
  94. * The callback function should take one argument - an associative array of arguments -
  95. * and return a modified array or arguments. See the WP documentation on wp_remote_get()
  96. * for details on what arguments are available and how they work.
  97. *
  98. * @uses add_filter() This method is a convenience wrapper for add_filter().
  99. *
  100. * @param callable $callback
  101. * @return void
  102. */
  103. public function addHttpRequestArgFilter($callback) {
  104. $this->addFilter('request_update_options', $callback);
  105. }
  106. /**
  107. * Register a callback for filtering theme updates retrieved from the external API.
  108. *
  109. * The callback function should take two arguments. If the theme update was retrieved
  110. * successfully, the first argument passed will be an instance of Theme_Update. Otherwise,
  111. * it will be NULL. The second argument will be the corresponding return value of
  112. * wp_remote_get (see WP docs for details).
  113. *
  114. * The callback function should return a new or modified instance of Theme_Update or NULL.
  115. *
  116. * @uses add_filter() This method is a convenience wrapper for add_filter().
  117. *
  118. * @param callable $callback
  119. * @return void
  120. */
  121. public function addResultFilter($callback) {
  122. $this->addFilter('request_update_result', $callback, 10, 2);
  123. }
  124. /**
  125. * Create a package instance that represents this plugin or theme.
  126. *
  127. * @return Puc_v4p11_InstalledPackage
  128. */
  129. protected function createInstalledPackage() {
  130. return new Puc_v4p11_Theme_Package($this->stylesheet, $this);
  131. }
  132. }
  133. endif;