Update.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace YahnisElsts\PluginUpdateChecker\v5p1\Theme;
  3. use YahnisElsts\PluginUpdateChecker\v5p1\Update as BaseUpdate;
  4. if ( !class_exists(Update::class, false) ):
  5. class Update extends BaseUpdate {
  6. public $details_url = '';
  7. protected static $extraFields = array('details_url');
  8. /**
  9. * Transform the metadata into the format used by WordPress core.
  10. * Note the inconsistency: WP stores plugin updates as objects and theme updates as arrays.
  11. *
  12. * @return array
  13. */
  14. public function toWpFormat() {
  15. $update = array(
  16. 'theme' => $this->slug,
  17. 'new_version' => $this->version,
  18. 'url' => $this->details_url,
  19. );
  20. if ( !empty($this->download_url) ) {
  21. $update['package'] = $this->download_url;
  22. }
  23. return $update;
  24. }
  25. /**
  26. * Create a new instance of Theme_Update from its JSON-encoded representation.
  27. *
  28. * @param string $json Valid JSON string representing a theme information object.
  29. * @return self New instance of ThemeUpdate, or NULL on error.
  30. */
  31. public static function fromJson($json) {
  32. $instance = new self();
  33. if ( !parent::createFromJson($json, $instance) ) {
  34. return null;
  35. }
  36. return $instance;
  37. }
  38. /**
  39. * Create a new instance by copying the necessary fields from another object.
  40. *
  41. * @param \StdClass|self $object The source object.
  42. * @return self The new copy.
  43. */
  44. public static function fromObject($object) {
  45. $update = new self();
  46. $update->copyFields($object, $update);
  47. return $update;
  48. }
  49. /**
  50. * Basic validation.
  51. *
  52. * @param \StdClass $apiResponse
  53. * @return bool|\WP_Error
  54. */
  55. protected function validateMetadata($apiResponse) {
  56. $required = array('version', 'details_url');
  57. foreach($required as $key) {
  58. if ( !isset($apiResponse->$key) || empty($apiResponse->$key) ) {
  59. return new \WP_Error(
  60. 'tuc-invalid-metadata',
  61. sprintf('The theme metadata is missing the required "%s" key.', $key)
  62. );
  63. }
  64. }
  65. return true;
  66. }
  67. protected function getFieldNames() {
  68. return array_merge(parent::getFieldNames(), self::$extraFields);
  69. }
  70. protected function getPrefixedFilter($tag) {
  71. return parent::getPrefixedFilter($tag) . '_theme';
  72. }
  73. }
  74. endif;