plug_update_plugin.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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' ) ) return false;
  33. $response = wp_remote_get( $this->ns->update_check_url );
  34. list($version, $url) = explode('|', $response['body']);
  35. $version = trim($version);
  36. $urlx = "https://git.tum.dk/tum.dk/dagsplug/archive/".$version.".zip";
  37. $this->status = 'Remote version: '.$version." URL: ".$url."::: ".$urlx.":::";
  38. $this->possible = false;
  39. if($this->plugin_get("Version") == $version) return false;
  40. $this->possible = true;
  41. $plugin_transient = get_site_transient('update_plugins');
  42. $a = array(
  43. 'slug' => $plugin_folder,
  44. 'new_version' => $version,
  45. 'url' => $this->plugin_get("AuthorURI"),
  46. 'package' => $urlx
  47. );
  48. $o = (object) $a;
  49. $plugin_transient->response[$plugin_folder.'/'.$plugin_file] = $o;
  50. set_site_transient('update_plugins', $plugin_transient);
  51. }
  52. function updates_exclude( $r, $url ) {
  53. if ( 0 !== strpos( $url, 'http://api.wordpress.org/plugins/update-check' ) )
  54. return $r; // Not a plugin update request. Bail immediately.
  55. $plugins = unserialize( $r['body']['plugins'] );
  56. unset( $plugins->plugins[ plugin_basename( $this->file ) ] );
  57. unset( $plugins->active[ array_search( plugin_basename( $this->file ), $plugins->active ) ] );
  58. $r['body']['plugins'] = serialize( $plugins );
  59. return $r;
  60. }
  61. function plugin_get($i) {
  62. if ( ! function_exists( 'get_plugins' ) )
  63. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  64. $plugin_folder = get_plugins( '/' . plugin_basename( dirname( $this->file ) ) );
  65. $plugin_file = basename( ( $this->file ) );
  66. return $plugin_folder[$plugin_file][$i];
  67. }
  68. function admin_line(){
  69. ?>
  70. <a href="/wp-admin/admin.php?page=dagsopt-options&checkforupdates"><?php echo(__("Check for updates","dagsopt")) ?></a>
  71. <br>
  72. <?php echo($this->status); ?>
  73. <?php
  74. if ( current_user_can('update_plugins') && $this->possible) {
  75. $url = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=' . $this->ns->filename), 'upgrade-plugin_' .$this->ns->filename);
  76. echo('<a href="'.$url.'">'.__("Update","dagsopt").'</a>');
  77. }
  78. ?>
  79. <?php
  80. }
  81. }
  82. global $plug_update_plugin;
  83. $plug_update_plugin = new plug_update_plugin($this);
  84. $this->dagsopt['plug_update_plugin'] = $plug_update_plugin;
  85. }