Wordpress插件更新

时间:2014-10-20 01:19:29

标签: php wordpress

我需要为我的插件实现更新,而不是在wordpress存储库上。

我需要知道的是两个过滤器之间的区别:

'site_transient_update_plugins' and 'pre_set_site_transient_update_plugins'

我使用' pre_set_site_transient_update_plugins'实现了一个用于更新的课程。过滤器:

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;
    }

}

将从插件的主文件创建此类的实例:

 $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);

唯一的问题是我使用&#39; pre_set_site_transient_update_plugins&#39;过滤器,当新版本的插件可用时,当我第一次访问插件页面时,没有关于新版本的信息。然后当我刷新页面时会有更新选项,每次下一次访问插件页面都会有更新选项,而不是第一次访问。

当我使用&#39; site_transient_update_plugins&#39;首次访问时将显示过滤器更新信息,所有工作正常。我很担心,因为过滤器'pre_set_site_transient_update_plugins&#39;将被召唤两次,并且#site; transient_update_plugins&#39;将被召唤11次。

因此,如果我使用&#39; site_transient_update_plugins&#39;一切都会正常,但我在这个钩子上的函数,包含wp_remote_post()函数,将在每个访问插件页面上调用11次。

这样做是否聪明,是否需要昂贵的操作。有没有人有一些建议。

0 个答案:

没有答案
相关问题