plug_update_plugin.php 3.3 KB

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