Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Check update: take #1 (disabled)

  • Loading branch information...
commit 0814c641ca3d7d730ab7ce25ed6e1df7cb9f1acb 1 parent 3b2bb88
Dzikri Aziz authored

Showing 2 changed files with 148 additions and 1 deletion. Show diff stats Hide diff stats

  1. +139 0 kc-essentials-inc/_update.php
  2. +9 1 kc-essentials.php
139 kc-essentials-inc/_update.php
... ... @@ -0,0 +1,139 @@
  1 +<?php
  2 +
  3 +/*
  4 + * Plugin update handler
  5 + * Based on http://wp.tutsplus.com/tutorials/plugins/a-guide-to-the-wordpress-http-api-automatic-plugin-updates/
  6 + */
  7 +
  8 +class kcUpdate {
  9 + /**
  10 + * The plugin current version
  11 + * @var string
  12 + */
  13 + public $current_version;
  14 +
  15 + /**
  16 + * The plugin remote update path
  17 + * @var string
  18 + */
  19 + public $update_path;
  20 +
  21 + /**
  22 + * Plugin Slug (plugin_directory/plugin_file.php)
  23 + * @var string
  24 + */
  25 + public $plugin_slug;
  26 +
  27 + /**
  28 + * Plugin name (plugin_file)
  29 + * @var string
  30 + */
  31 + public $slug;
  32 +
  33 +
  34 + /**
  35 + * Initialize a new instance of the WordPress Auto-Update class
  36 + * @param string $current_version
  37 + * @param string $update_path
  38 + * @param string $plugin_slug
  39 + */
  40 + function __construct( $current_version, $update_path, $plugin_slug ) {
  41 + // Set the class public variables
  42 + $this->current_version = $current_version;
  43 + $this->update_path = $update_path;
  44 + $this->plugin_slug = $plugin_slug;
  45 + list ($t1, $t2) = explode('/', $plugin_slug);
  46 + $this->slug = str_replace('.php', '', $t2);
  47 +
  48 + // define the alternative API for updating checking
  49 + add_filter('pre_set_site_transient_update_plugins', array($this, 'check_update'));
  50 +
  51 + // Define the alternative response for information checking
  52 + add_filter('plugins_api', array($this, 'check_info'), 10, 3);
  53 + }
  54 +
  55 +
  56 + /**
  57 + * Add our self-hosted autoupdate plugin to the filter transient
  58 + *
  59 + * @param $transient
  60 + * @return object $ transient
  61 + */
  62 + public function check_update( $transient ) {
  63 + if (empty($transient->checked)) {
  64 + return $transient;
  65 + }
  66 +
  67 + // Get the remote version
  68 + $remote_version = $this->getRemote_version();
  69 +
  70 + // If a newer version is available, add the update
  71 + if (version_compare($this->current_version, $remote_version, '<')) {
  72 + $obj = new stdClass();
  73 + $obj->slug = $this->slug;
  74 + $obj->new_version = $remote_version;
  75 + $obj->url = $this->update_path;
  76 + $obj->package = $this->update_path;
  77 + $transient->response[$this->plugin_slug] = $obj;
  78 + }
  79 + return $transient;
  80 + }
  81 +
  82 +
  83 + /**
  84 + * Add our self-hosted description to the filter
  85 + *
  86 + * @param boolean $false
  87 + * @param array $action
  88 + * @param object $arg
  89 + * @return bool|object
  90 + */
  91 + public function check_info( $false, $action, $arg ) {
  92 + if ($arg->slug === $this->slug) {
  93 + $information = $this->getRemote_information();
  94 + return $information;
  95 + }
  96 + return false;
  97 + }
  98 +
  99 +
  100 + /**
  101 + * Return the remote version
  102 + * @return string $remote_version
  103 + */
  104 + public function getRemote_version() {
  105 + $request = wp_remote_post($this->update_path, array('body' => array('action' => 'version')));
  106 + if (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) {
  107 + return $request['body'];
  108 + }
  109 + return false;
  110 + }
  111 +
  112 +
  113 + /**
  114 + * Get information about the remote version
  115 + * @return bool|object
  116 + */
  117 + public function getRemote_information() {
  118 + $request = wp_remote_post($this->update_path, array('body' => array('action' => 'info')));
  119 + if (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) {
  120 + return unserialize($request['body']);
  121 + }
  122 + return false;
  123 + }
  124 +
  125 +
  126 + /**
  127 + * Return the status of the plugin licensing
  128 + * @return boolean $remote_license
  129 + */
  130 + public function getRemote_license() {
  131 + $request = wp_remote_post($this->update_path, array('body' => array('action' => 'license')));
  132 + if (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) {
  133 + return $request['body'];
  134 + }
  135 + return false;
  136 + }
  137 +}
  138 +
  139 +?>
10 kc-essentials.php
@@ -43,6 +43,9 @@ static function init() {
43 43 if ( file_exists("{$paths['inc']}/{$component}.php") )
44 44 require_once "{$paths['inc']}/{$component}.php";
45 45
  46 + # Auto update
  47 + //self::check_update();
  48 +
46 49 # Scripts n styles
47 50 self::_sns();
48 51 }
@@ -90,9 +93,14 @@ public static function _deactivate() {
90 93 unset( $kcs['kids']['kc_essentials'] );
91 94 update_option( 'kc_settings', $kcs );
92 95 }
93   -}
94 96
95 97
  98 + public static function check_update() {
  99 + if ( !class_exists('kcUpdate') )
  100 + require_once self::$pdata['paths']['inc'] . '/_update.php';
  101 + new kcUpdate( '0.1', 'http://repo.kucrut.org/api.php', self::$pdata['paths']['p_file'] );
  102 + }
  103 +}
96 104 require_once dirname(__FILE__) . '/kc-essentials-inc/_options.php';
97 105 add_action( 'init', array('kcEssentials', 'init'), 100 );
98 106

0 comments on commit 0814c64

Please sign in to comment.
Something went wrong with that request. Please try again.