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.

126 lines
3.5 KiB

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