WordPress Theme autoupdate API integration code example

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPrint this page

This is an API example on how to use the Theme AutoUpdate functionality.

define('SL_APP_API_URL',      'http://YourDomainWhereSoftwareManagement.com/index.php');
    define('SL_PRODUCT_ID',           'Unitque Software title #@3');
    define('SL_VERSION',           '1.2');
    define('SL_INSTANCE',           'http://mywebsitename.com');

    class APTO_theme_CodeAutoUpdate
        {
            # URL to check for updates, this is where the index.php script goes
            public $api_url;
            private $slug;

            public function __construct($api_url, $slug)
                {
                    $this->api_url = $api_url;

                    $this->slug = $slug;
     
                }


            public function check_for_update($checked_data)
                {
                    if (empty($checked_data->checked) || !isset($checked_data->checked[$this->slug]))
                        return $checked_data;

                    $request_string = $this->prepare_request('theme_update');
                    if($request_string === FALSE)
                        return $checked_data;

                    // Start checking for an update
                    $request_uri = $this->api_url . '?' . http_build_query( $request_string , '', '&');
                    $data = wp_remote_get( $request_uri );

                    if(is_wp_error( $data ) || $data['response']['code'] != 200)
                        return $checked_data;

                    $response_block = json_decode($data['body']);
                    
                    if(!is_array($response_block) || count($response_block) < 1)
                        {
                            unset ( $checked_data->response[$this->slug] );
                            return $checked_data; 
                        }
                    
                    //retrieve the last message within the $response_block
                    $response_block = $response_block[count($response_block) - 1];
                    $response = isset($response_block->message) ? $response_block->message : '';

                    if (is_object($response) && !empty($response)) // Feed the update data into WP updater
                        {
                            $checked_data->response[$this->slug] = (array)$response;
                        }

                    return $checked_data;
                }


            public function theme_api_call($def, $action, $args)
                {
                    if (!is_object($args) || !isset($args->slug) || $args->slug != $this->slug)
                        return $def;


                    //$args->package_type = $this->package_type;

                    $request_string = $this->prepare_request($action, $args);
                    if($request_string === FALSE)
                    return new WP_Error('theme_api_failed', __('An error occour when try to identify the theme.' , 'apto') . '</p> <p><a href="?" onclick="document.location.reload(); return false;">'. __( 'Try again', 'apto' ) .'</a>');;

                    $request_uri = $this->api_url . '?' . http_build_query( $request_string , '', '&');
                    $data = wp_remote_get( $request_uri );

                    if(is_wp_error( $data ) || $data['response']['code'] != 200)
                    return new WP_Error('theme_api_failed', __('An Unexpected HTTP Error occurred during the API request.' , 'apto') . '</p> <p><a href="?" onclick="document.location.reload(); return false;">'. __( 'Try again', 'apto' ) .'</a>', $data->get_error_message());

                    $response_block = json_decode($data['body']);
                    //retrieve the last message within the $response_block
                    $response_block = $response_block[count($response_block) - 1];
                    $response = $response_block->message;

                    if (is_object($response) && !empty($response)) // Feed the update data into WP updater
                        {
                            //include slug and theme data
                            $response->theme = $this->slug;
    
                            return $response;
                        }
                }

            public function prepare_request($action, $args = array())
                {

                    global $wp_version;

                    return array(
                                    'woo_sl_action' => $action,
                                    'version' => SL_VERSION,
                                    'product_unique_id' => SL_PRODUCT_ID,
                                    'licence_key' => $licence_key,
                                    'domain' => SL_INSTANCE,

                                    'wp-version' => $wp_version,

                    );
                }
        }

    function APTO_theme_run_updater()
        {
            if(function_exists('wp_get_theme'))
                {
                    $theme_data = wp_get_theme(get_option('template'));
                    $theme_version = $theme_data->Version;  
                } 
            else 
                {
                    $theme_data = get_theme_data( TEMPLATEPATH . '/style.css');
                    $theme_version = $theme_data['Version'];
                }    
            $theme_base = get_option('template');
            
            $APTO_theme_CodeAutoUpdate = new APTO_theme_CodeAutoUpdate(SL_APP_API_URL, $theme_slug);
            
            add_filter('pre_set_site_transient_update_themes', array($APTO_theme_CodeAutoUpdate, 'check_for_update'));
            add_filter('themes_api', array($APTO_theme_CodeAutoUpdate,  'theme_api_call'), 10, 3);

        }
        
    if (is_admin())
        {
            APTO_theme_run_updater();
        }

By woocommerce-sl,posted on August 1, 2017