Update.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. if ( !class_exists('Puc_v4p11_Plugin_Update', false) ):
  3. /**
  4. * A simple container class for holding information about an available update.
  5. *
  6. * @author Janis Elsts
  7. * @copyright 2016
  8. * @access public
  9. */
  10. class Puc_v4p11_Plugin_Update extends Puc_v4p11_Update {
  11. public $id = 0;
  12. public $homepage;
  13. public $upgrade_notice;
  14. public $tested;
  15. public $requires_php = false;
  16. public $icons = array();
  17. public $filename; //Plugin filename relative to the plugins directory.
  18. protected static $extraFields = array(
  19. 'id', 'homepage', 'tested', 'requires_php', 'upgrade_notice', 'icons', 'filename',
  20. );
  21. /**
  22. * Create a new instance of PluginUpdate from its JSON-encoded representation.
  23. *
  24. * @param string $json
  25. * @return Puc_v4p11_Plugin_Update|null
  26. */
  27. public static function fromJson($json){
  28. //Since update-related information is simply a subset of the full plugin info,
  29. //we can parse the update JSON as if it was a plugin info string, then copy over
  30. //the parts that we care about.
  31. $pluginInfo = Puc_v4p11_Plugin_Info::fromJson($json);
  32. if ( $pluginInfo !== null ) {
  33. return self::fromPluginInfo($pluginInfo);
  34. } else {
  35. return null;
  36. }
  37. }
  38. /**
  39. * Create a new instance of PluginUpdate based on an instance of PluginInfo.
  40. * Basically, this just copies a subset of fields from one object to another.
  41. *
  42. * @param Puc_v4p11_Plugin_Info $info
  43. * @return Puc_v4p11_Plugin_Update
  44. */
  45. public static function fromPluginInfo($info){
  46. return self::fromObject($info);
  47. }
  48. /**
  49. * Create a new instance by copying the necessary fields from another object.
  50. *
  51. * @param StdClass|Puc_v4p11_Plugin_Info|Puc_v4p11_Plugin_Update $object The source object.
  52. * @return Puc_v4p11_Plugin_Update The new copy.
  53. */
  54. public static function fromObject($object) {
  55. $update = new self();
  56. $update->copyFields($object, $update);
  57. return $update;
  58. }
  59. /**
  60. * @return string[]
  61. */
  62. protected function getFieldNames() {
  63. return array_merge(parent::getFieldNames(), self::$extraFields);
  64. }
  65. /**
  66. * Transform the update into the format used by WordPress native plugin API.
  67. *
  68. * @return object
  69. */
  70. public function toWpFormat() {
  71. $update = parent::toWpFormat();
  72. $update->id = $this->id;
  73. $update->url = $this->homepage;
  74. $update->tested = $this->tested;
  75. $update->requires_php = $this->requires_php;
  76. $update->plugin = $this->filename;
  77. if ( !empty($this->upgrade_notice) ) {
  78. $update->upgrade_notice = $this->upgrade_notice;
  79. }
  80. if ( !empty($this->icons) && is_array($this->icons) ) {
  81. //This should be an array with up to 4 keys: 'svg', '1x', '2x' and 'default'.
  82. //Docs: https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons
  83. $icons = array_intersect_key(
  84. $this->icons,
  85. array('svg' => true, '1x' => true, '2x' => true, 'default' => true)
  86. );
  87. if ( !empty($icons) ) {
  88. $update->icons = $icons;
  89. //It appears that the 'default' icon isn't used anywhere in WordPress 4.9,
  90. //but lets set it just in case a future release needs it.
  91. if ( !isset($update->icons['default']) ) {
  92. $update->icons['default'] = current($update->icons);
  93. }
  94. }
  95. }
  96. return $update;
  97. }
  98. }
  99. endif;