Updating Wordpress Plugin

I need to implement an update for my plugin that is not in the Wordpress repository.

I need to know the difference between the two filters:

'site_transient_update_plugins' and 'pre_set_site_transient_update_plugins'

      

I have implemented one update class 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 plugin file:

 $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 that I am using the pre_set_site_transient_update_plugins filter and when a new version of the plugin is available, when I visit the plugin page for the first time, there is no information about the new version. And then when I refresh the page, the refresh option will appear, and every next visit on the plugin page will be the refresh option, just not for the first visit.

And when I use 'site_transient_update_plugins' the filter update information will be shown for the first visit and everything works fine. I'm just worried because the "pre_set_site_transient_update_plugins" filter will be called twice, and "site_transient_update_plugins" will be called 11 times.

So if I use 'site_transient_update_plugins' everything works fine, but my function on this hook, which contains the wp_remote_post () function, will be called 11 times on each visit plugin page.

Whether it is smart, it is a very expensive operation. Does anyone have any tips.

+3


source to share





All Articles