Update.php 3.2 KB

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