plug_update_plugin.php 3.3 KB

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