Metadata.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. if ( !class_exists('Puc_v4p11_Metadata', false) ):
  3. /**
  4. * A base container for holding information about updates and plugin metadata.
  5. *
  6. * @author Janis Elsts
  7. * @copyright 2016
  8. * @access public
  9. */
  10. abstract class Puc_v4p11_Metadata {
  11. /**
  12. * Create an instance of this class from a JSON document.
  13. *
  14. * @abstract
  15. * @param string $json
  16. * @return self
  17. */
  18. public static function fromJson(/** @noinspection PhpUnusedParameterInspection */ $json) {
  19. throw new LogicException('The ' . __METHOD__ . ' method must be implemented by subclasses');
  20. }
  21. /**
  22. * @param string $json
  23. * @param self $target
  24. * @return bool
  25. */
  26. protected static function createFromJson($json, $target) {
  27. /** @var StdClass $apiResponse */
  28. $apiResponse = json_decode($json);
  29. if ( empty($apiResponse) || !is_object($apiResponse) ){
  30. $errorMessage = "Failed to parse update metadata. Try validating your .json file with http://jsonlint.com/";
  31. do_action('puc_api_error', new WP_Error('puc-invalid-json', $errorMessage));
  32. trigger_error($errorMessage, E_USER_NOTICE);
  33. return false;
  34. }
  35. $valid = $target->validateMetadata($apiResponse);
  36. if ( is_wp_error($valid) ){
  37. do_action('puc_api_error', $valid);
  38. trigger_error($valid->get_error_message(), E_USER_NOTICE);
  39. return false;
  40. }
  41. foreach(get_object_vars($apiResponse) as $key => $value){
  42. $target->$key = $value;
  43. }
  44. return true;
  45. }
  46. /**
  47. * No validation by default! Subclasses should check that the required fields are present.
  48. *
  49. * @param StdClass $apiResponse
  50. * @return bool|WP_Error
  51. */
  52. protected function validateMetadata(/** @noinspection PhpUnusedParameterInspection */ $apiResponse) {
  53. return true;
  54. }
  55. /**
  56. * Create a new instance by copying the necessary fields from another object.
  57. *
  58. * @abstract
  59. * @param StdClass|self $object The source object.
  60. * @return self The new copy.
  61. */
  62. public static function fromObject(/** @noinspection PhpUnusedParameterInspection */ $object) {
  63. throw new LogicException('The ' . __METHOD__ . ' method must be implemented by subclasses');
  64. }
  65. /**
  66. * Create an instance of StdClass that can later be converted back to an
  67. * update or info container. Useful for serialization and caching, as it
  68. * avoids the "incomplete object" problem if the cached value is loaded
  69. * before this class.
  70. *
  71. * @return StdClass
  72. */
  73. public function toStdClass() {
  74. $object = new stdClass();
  75. $this->copyFields($this, $object);
  76. return $object;
  77. }
  78. /**
  79. * Transform the metadata into the format used by WordPress core.
  80. *
  81. * @return object
  82. */
  83. abstract public function toWpFormat();
  84. /**
  85. * Copy known fields from one object to another.
  86. *
  87. * @param StdClass|self $from
  88. * @param StdClass|self $to
  89. */
  90. protected function copyFields($from, $to) {
  91. $fields = $this->getFieldNames();
  92. if ( property_exists($from, 'slug') && !empty($from->slug) ) {
  93. //Let plugins add extra fields without having to create subclasses.
  94. $fields = apply_filters($this->getPrefixedFilter('retain_fields') . '-' . $from->slug, $fields);
  95. }
  96. foreach ($fields as $field) {
  97. if ( property_exists($from, $field) ) {
  98. $to->$field = $from->$field;
  99. }
  100. }
  101. }
  102. /**
  103. * @return string[]
  104. */
  105. protected function getFieldNames() {
  106. return array();
  107. }
  108. /**
  109. * @param string $tag
  110. * @return string
  111. */
  112. protected function getPrefixedFilter($tag) {
  113. return 'puc_' . $tag;
  114. }
  115. }
  116. endif;