1
0

PluginInfo.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace YahnisElsts\PluginUpdateChecker\v5p1\Plugin;
  3. use YahnisElsts\PluginUpdateChecker\v5p1\Metadata;
  4. if ( !class_exists(PluginInfo::class, false) ):
  5. /**
  6. * A container class for holding and transforming various plugin metadata.
  7. *
  8. * @author Janis Elsts
  9. * @copyright 2016
  10. * @access public
  11. */
  12. class PluginInfo extends Metadata {
  13. //Most fields map directly to the contents of the plugin's info.json file.
  14. //See the relevant docs for a description of their meaning.
  15. public $name;
  16. public $slug;
  17. public $version;
  18. public $homepage;
  19. public $sections = array();
  20. public $download_url;
  21. public $banners;
  22. public $icons = array();
  23. public $translations = array();
  24. public $author;
  25. public $author_homepage;
  26. public $requires;
  27. public $tested;
  28. public $requires_php;
  29. public $upgrade_notice;
  30. public $rating;
  31. public $num_ratings;
  32. public $downloaded;
  33. public $active_installs;
  34. public $last_updated;
  35. public $id = 0; //The native WP.org API returns numeric plugin IDs, but they're not used for anything.
  36. public $filename; //Plugin filename relative to the plugins directory.
  37. /**
  38. * Create a new instance of Plugin Info from JSON-encoded plugin info
  39. * returned by an external update API.
  40. *
  41. * @param string $json Valid JSON string representing plugin info.
  42. * @return self|null New instance of Plugin Info, or NULL on error.
  43. */
  44. public static function fromJson($json){
  45. $instance = new self();
  46. if ( !parent::createFromJson($json, $instance) ) {
  47. return null;
  48. }
  49. //json_decode decodes assoc. arrays as objects. We want them as arrays.
  50. $instance->sections = (array)$instance->sections;
  51. $instance->icons = (array)$instance->icons;
  52. return $instance;
  53. }
  54. /**
  55. * Very, very basic validation.
  56. *
  57. * @param \StdClass $apiResponse
  58. * @return bool|\WP_Error
  59. */
  60. protected function validateMetadata($apiResponse) {
  61. if (
  62. !isset($apiResponse->name, $apiResponse->version)
  63. || empty($apiResponse->name)
  64. || empty($apiResponse->version)
  65. ) {
  66. return new \WP_Error(
  67. 'puc-invalid-metadata',
  68. "The plugin metadata file does not contain the required 'name' and/or 'version' keys."
  69. );
  70. }
  71. return true;
  72. }
  73. /**
  74. * Transform plugin info into the format used by the native WordPress.org API
  75. *
  76. * @return object
  77. */
  78. public function toWpFormat(){
  79. $info = new \stdClass;
  80. //The custom update API is built so that many fields have the same name and format
  81. //as those returned by the native WordPress.org API. These can be assigned directly.
  82. $sameFormat = array(
  83. 'name', 'slug', 'version', 'requires', 'tested', 'rating', 'upgrade_notice',
  84. 'num_ratings', 'downloaded', 'active_installs', 'homepage', 'last_updated',
  85. 'requires_php',
  86. );
  87. foreach($sameFormat as $field){
  88. if ( isset($this->$field) ) {
  89. $info->$field = $this->$field;
  90. } else {
  91. $info->$field = null;
  92. }
  93. }
  94. //Other fields need to be renamed and/or transformed.
  95. $info->download_link = $this->download_url;
  96. $info->author = $this->getFormattedAuthor();
  97. $info->sections = array_merge(array('description' => ''), $this->sections);
  98. if ( !empty($this->banners) ) {
  99. //WP expects an array with two keys: "high" and "low". Both are optional.
  100. //Docs: https://wordpress.org/plugins/about/faq/#banners
  101. $info->banners = is_object($this->banners) ? get_object_vars($this->banners) : $this->banners;
  102. $info->banners = array_intersect_key($info->banners, array('high' => true, 'low' => true));
  103. }
  104. return $info;
  105. }
  106. protected function getFormattedAuthor() {
  107. if ( !empty($this->author_homepage) ){
  108. /** @noinspection HtmlUnknownTarget */
  109. return sprintf('<a href="%s">%s</a>', $this->author_homepage, $this->author);
  110. }
  111. return $this->author;
  112. }
  113. }
  114. endif;