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.

131 lines
3.3 KiB

3 years ago
  1. <?php
  2. if ( !class_exists('Puc_v4p1_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_v4p1_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. trigger_error(
  31. "Failed to parse update metadata. Try validating your .json file with http://jsonlint.com/",
  32. E_USER_NOTICE
  33. );
  34. return false;
  35. }
  36. $valid = $target->validateMetadata($apiResponse);
  37. if ( is_wp_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;