1
0

Info.php 3.7 KB

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