plug_update_plugin.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. if (!class_exists("plug_update_plugin")) {
  3. class plug_update_plugin {
  4. function __construct($ns) {
  5. $this->title = __("Update plugin", "dagsopt");
  6. $this->pluginname = $ns->pluginname;
  7. $this->file = $ns->file;
  8. $this->ns = $ns;
  9. $this->status = "";
  10. $this->possible = false;
  11. }
  12. function start() {
  13. register_activation_hook(plugin_basename($this->file), array(&$this, 'activatePlugin'));
  14. register_deactivation_hook(plugin_basename($this->file), array(&$this, 'deactivatePlugin'));
  15. add_action($this->pluginname . 'UpdateCheck', array(&$this, 'check_update'));
  16. add_filter('http_request_args', array(&$this, 'updates_exclude'), 5, 2);
  17. if (isset($_REQUEST["checkforupdates"])) {
  18. $this->check_update();
  19. }
  20. }
  21. function activatePlugin() {
  22. wp_schedule_event(time(), 'hourly', $this->pluginname . 'UpdateCheck');
  23. }
  24. function deactivatePlugin() {
  25. wp_clear_scheduled_hook($this->pluginname . 'UpdateCheck');
  26. }
  27. /*SELF UPDATE*/
  28. function check_update() {
  29. global $wp_version;
  30. $plugin_folder = plugin_basename(dirname($this->file));
  31. $plugin_file = basename(($this->file));
  32. if (defined('WP_INSTALLING')) {
  33. return false;
  34. }
  35. $response = wp_remote_get($this->ns->update_check_url);
  36. list($version) = explode('|', $response['body']);
  37. $version = trim($version);
  38. $urlx = "https://git.tum.dk/tum.dk/dagsplug/archive/" . $version . ".zip";
  39. $this->status = 'Remote version: ' . $version . " URL: " . $urlx . ":::";
  40. $this->possible = false;
  41. if ($this->plugin_get("Version") == $version) {
  42. return false;
  43. }
  44. $this->possible = true;
  45. $plugin_transient = get_site_transient('update_plugins');
  46. $a = array(
  47. 'slug' => $plugin_folder,
  48. 'new_version' => $version,
  49. 'url' => $this->plugin_get("AuthorURI"),
  50. 'package' => $urlx,
  51. );
  52. $o = (object) $a;
  53. $plugin_transient->response[$plugin_folder . '/' . $plugin_file] = $o;
  54. set_site_transient('update_plugins', $plugin_transient);
  55. }
  56. function updates_exclude($r, $url) {
  57. if (0 !== strpos($url, 'http://api.wordpress.org/plugins/update-check')) {
  58. return $r;
  59. }
  60. // Not a plugin update request. Bail immediately.
  61. $plugins = unserialize($r['body']['plugins']);
  62. unset($plugins->plugins[plugin_basename($this->file)]);
  63. unset($plugins->active[array_search(plugin_basename($this->file), $plugins->active)]);
  64. $r['body']['plugins'] = serialize($plugins);
  65. return $r;
  66. }
  67. function plugin_get($i) {
  68. if (!function_exists('get_plugins')) {
  69. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  70. }
  71. $plugin_folder = get_plugins('/' . plugin_basename(dirname($this->file)));
  72. $plugin_file = basename(($this->file));
  73. return $plugin_folder[$plugin_file][$i];
  74. }
  75. function admin_line() {
  76. ?>
  77. <a href="/wp-admin/admin.php?page=dagsopt-options&checkforupdates"><?php echo (__("Check for updates", "dagsopt")) ?></a>
  78. <br>
  79. <?php echo ($this->status); ?>
  80. <?php
  81. if (current_user_can('update_plugins') && $this->possible) {
  82. $url = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=' . $this->ns->filename), 'upgrade-plugin_' . $this->ns->filename);
  83. echo ('<a href="' . $url . '">' . __("Update", "dagsopt") . '</a>');
  84. }
  85. ?>
  86. <?php
  87. }
  88. }
  89. global $plug_update_plugin;
  90. $plug_update_plugin = new plug_update_plugin($this);
  91. $this->dagsopt['plug_update_plugin'] = $plug_update_plugin;
  92. }