UpdateChecker.php 4.6 KB

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