Search code examples
wordpresshttpserverwordpress-themingauto-update

How wordpress theme and plugin update work from remote server


Actually how wordpress theme and plugin update working. What is it's core class to handle this system. How wordpress handle this HTTP request from remote server.

Can you help me to give some basic guide?


Solution

  • Core Class:

    ...\wp-includes\update.php
    

    update.php file Checks against the WordPress server at api.wordpress.org server. Will only check if WordPress isn't installing.

    //Every 12 hours it check version
    function wp_version_check(){}
    //If Available new plugin version than call
    function wp_update_plugins(){}
    //If Available new theme version than call
    function wp_update_themes(){}
    

    How It Works:

    Every 12 hours, your WordPress blog will check for new plugin versions and store the request sent and response received in a site transient called 'update_plugins'. The following code will display the content of this transient:

    add_filter ('pre_set_site_transient_update_plugins', 'display_transient_update_plugins');
    function display_transient_update_plugins ($transient)
    {
        var_dump($transient);
    }
    

    After 12 hours or so, refresh your blog plugins page and you should get a similar output to the following:

    object(stdClass)[18]
      public 'last_checked' => int 1333808132
      public 'checked' => 
        array
          'access/access.php' => string '1.0' (length=3)
          'adpress/wp-adpress.php' => string '3.1' (length=3)
              ...
          'wp-paypal/plug.php' => string '1.0' (length=3)
      public 'response' => 
        array
          'akismet/akismet.php' => 
            object(stdClass)[13]
              public 'id' => string '15' (length=2)
              public 'slug' => string 'akismet' (length=7)
              public 'new_version' => string '2.5.5' (length=5)
              public 'url' => string 'http://wordpress.org/extend/plugins/akismet/' (length=44)
              public 'package' => string 'http://downloads.wordpress.org/plugin/akismet.2.5.5.zip' (length=55)
          'update.tmp/plugin.php' => 
            object(stdClass)[12]
              public 'slug' => string 'plugin' (length=6)
              public 'new_version' => string '1.1' (length=3)
              public 'url' => string 'http://localhost/update.php' (length=27)
              public 'package' => string 'http://localhost/update.php' (length=27)
    

    The 12 hour waiting period is defined in WordPress core on Line 223 of the wp-includes/update.php file, it's the $timeout variable.

    Version compare and check for new version:

    // If a newer version is available, add the update
        if (version_compare(1.2, 1.3, '<')) {
            $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;
        }
    

    After check version if have new version. Wordpress handle update by this filter-

    add_filter ('pre_set_site_transient_update_plugins', 'display_transient_update_plugins');
    function display_transient_update_plugins ($transient)
    {
        $obj = new stdClass();
        $obj->slug = 'hello.php';
        $obj->new_version = '1.9';
        $obj->url = 'http://anyurl.com';
        $obj->package = 'http://anyurl.com';
        $transient['hello.php'] => $obj;
        return $transient;
    }
    

    Handle this HTTP request from remote server:

    There have three tutorial for update wordpress plugin and theme from remote server- https://code.tutsplus.com/tutorials/a-guide-to-the-wordpress-http-api-automatic-plugin-updates--wp-25181

    http://w-shadow.com/blog/2010/09/02/automatic-updates-for-any-plugin/ [Only for Apache server]

    https://kaspars.net/blog/wordpress/automatic-updates-for-plugins-and-themes-hosted-outside-wordpress-extend

    Hope it would be helpful.