WordPress plugin update

I need to implement update for my plugin which is not on wordpress repository.

What I need to know is difference between two filters:

Read More
'site_transient_update_plugins' and 'pre_set_site_transient_update_plugins'

I implemented one class for update with ‘pre_set_site_transient_update_plugins’ filter:

class WPAutoUpdate{

    public $current_version;
    public $update_path;
    public $plugin_slug;
    public $slug;

    function __construct($current_version, $update_path, $plugin_slug){
        $this->current_version = $current_version;
        $this->update_path = $update_path;
        $this->plugin_slug = $plugin_slug;
        list ($t1, $t2) = explode('/', $plugin_slug);
        $this->slug = str_replace('.php', '', $t2);

        add_filter('pre_set_site_transient_update_plugins', array(&$this, 'check_update'));
    }

    public function check_update($transient){
        if (empty($transient->checked)) {
            return $transient;
        }

        $remote_version = $this->getRemote_version();

        if (version_compare($this->current_version, $remote_version, '<')) {
            $obj = new stdClass();
            $obj->slug = $this->slug;
            $obj->new_version = $remote_version;
            $obj->url = $this->update_path;
            $obj->package = $this->update_path;
            $transient->response[$this->plugin_slug] = $obj;
        }

        return $transient;
    }

    public function getRemote_version(){
        $request = wp_remote_post($this->update_path, array('body' => array('action' => 'version')));
        if (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) {
            return $request['body'];
        }
        return false;
    }

}

An instance of this class will be created from the main file of plugin:

 $pluginData = get_plugin_data(__FILE__);
 $plugin_current_version = $pluginData['Version'];
 $plugin_remote_path = 'https://mysite.com/myplugin';  
 $plugin_slug = plugin_basename(__FILE__);  
 new WPAutoUpdate ($plugin_current_version, $plugin_remote_path, $plugin_slug);

The only problem is when I use ‘pre_set_site_transient_update_plugins’ filter and when new version of plugin is available, when I visit plugin page for the first time there is no information about new version. And then when I refresh the page there will be update option and each next visit on plugin page there will be update option, just not for the first visit.

And when I use ‘site_transient_update_plugins’ filter update information will be shown for the first visit and all works fine. Just I am worried because filter ‘pre_set_site_transient_update_plugins’ will be called just twice and ‘site_transient_update_plugins’ will be called 11 times.

So if I use ‘site_transient_update_plugins’ all will works fine but my function on this hook, which contain wp_remote_post() function, will be called 11 times on each visit plugin page.

Is that smart to do, is that to much expensive operation. Does anyone has some advice.

Related posts

Leave a Reply