radishconcepts / WordPress-GitHub-Plugin-Updater

This class is meant to be used with your Github hosted WordPress plugins. The purpose of the class is to allow your WordPress plugin to be updated whenever you push out a new version of your plugin; similarly to the experience users know and love with the WordPress.org plugin repository.
https://github.com/jkudish/WordPress-GitHub-Plugin-Updater
822 stars 195 forks source link

Fetching README.md when not needed #62

Closed bradmkjr closed 9 years ago

bradmkjr commented 10 years ago

The get_new_version function loads up the base file and then also loads up README.md without checking if a valid version number was found in base file.

I recommend replacing get_new_version with the following:

    /**
     * Get New Version from github
     *
     * @since 1.0
     * @return int $version the version number
     */
    public function get_new_version() {
        $version = get_site_transient( $this->config['slug'].'_new_version' );

        if ( $this->overrule_transients() || ( !isset( $version ) || !$version || '' == $version ) ) {

            $raw_response = $this->remote_api_get( trailingslashit( $this->config['api_url'] ). basename( $this->config['slug'] ) );

            if ( is_wp_error( $raw_response ) )
                $version = false;

            if (is_array($raw_response)) {
                if (!empty($raw_response['body']))
                    preg_match( '#^\s*Version\:\s*(.*)$#im', $raw_response['body'], $matches );
            }

            if ( empty( $matches[1] ) )
                $version = false;
            else
                $version = $matches[1];

            if( ! $version ){
                // back compat for older readme version handling
                $raw_response = $this->remote_api_get( trailingslashit( $this->config['api_url'] ) . $this->config['readme'] );

                if ( is_wp_error( $raw_response ) )
                    return $version;

                preg_match( '#^\s*`*~Current Version\:\s*([^~]*)~#im', $raw_response['body'], $__version );

                if ( isset( $__version[1] ) ) {
                    $version_readme = $__version[1];
                    if ( -1 == version_compare( $version, $version_readme ) )
                        $version = $version_readme;
                }

            }

            // refresh every 6 hours
            if ( false !== $version )
                set_site_transient( $this->config['slug'].'_new_version', $version, 60*60*6 );
        }

        return $version;
    }

This should speed up every site using this class, removing an unneeded call to the GitHub API.

I'm working on a proper pull request but wanted to share this to get feedback.