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.

90 lines
2.4 KiB

3 years ago
  1. <?php
  2. if ( !class_exists('Puc_v4p1_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_v4p1_Plugin_Update extends Puc_v4p1_Update {
  11. public $id = 0;
  12. public $homepage;
  13. public $upgrade_notice;
  14. public $tested;
  15. public $filename; //Plugin filename relative to the plugins directory.
  16. protected static $extraFields = array(
  17. 'id', 'homepage', 'tested', 'upgrade_notice', 'filename',
  18. );
  19. /**
  20. * Create a new instance of PluginUpdate from its JSON-encoded representation.
  21. *
  22. * @param string $json
  23. * @return Puc_v4p1_Plugin_Update|null
  24. */
  25. public static function fromJson($json){
  26. //Since update-related information is simply a subset of the full plugin info,
  27. //we can parse the update JSON as if it was a plugin info string, then copy over
  28. //the parts that we care about.
  29. $pluginInfo = Puc_v4p1_Plugin_Info::fromJson($json);
  30. if ( $pluginInfo != null ) {
  31. return self::fromPluginInfo($pluginInfo);
  32. } else {
  33. return null;
  34. }
  35. }
  36. /**
  37. * Create a new instance of PluginUpdate based on an instance of PluginInfo.
  38. * Basically, this just copies a subset of fields from one object to another.
  39. *
  40. * @param Puc_v4p1_Plugin_Info $info
  41. * @return Puc_v4p1_Plugin_Update
  42. */
  43. public static function fromPluginInfo($info){
  44. return self::fromObject($info);
  45. }
  46. /**
  47. * Create a new instance by copying the necessary fields from another object.
  48. *
  49. * @param StdClass|Puc_v4p1_Plugin_Info|Puc_v4p1_Plugin_Update $object The source object.
  50. * @return Puc_v4p1_Plugin_Update The new copy.
  51. */
  52. public static function fromObject($object) {
  53. $update = new self();
  54. $update->copyFields($object, $update);
  55. return $update;
  56. }
  57. /**
  58. * @return string[]
  59. */
  60. protected function getFieldNames() {
  61. return array_merge(parent::getFieldNames(), self::$extraFields);
  62. }
  63. /**
  64. * Transform the update into the format used by WordPress native plugin API.
  65. *
  66. * @return object
  67. */
  68. public function toWpFormat(){
  69. $update = parent::toWpFormat();
  70. $update->id = $this->id;
  71. $update->url = $this->homepage;
  72. $update->tested = $this->tested;
  73. $update->plugin = $this->filename;
  74. if ( !empty($this->upgrade_notice) ){
  75. $update->upgrade_notice = $this->upgrade_notice;
  76. }
  77. return $update;
  78. }
  79. }
  80. endif;