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('WOO_SLT_APP_API_URL',      'http://YourDomainWhereSoftwareManagement.com/index.php');
        define('WOO_SLT_PRODUCT_ID',       'Unitque Software ID #@3');
        define('WOO_SLT_VERSION',          '1.0');
        
        define('WOO_SLT_INSTANCE',         str_replace(array ("https://" , "http://"), "", network_site_url()));;
        define('WOO_SLT_THEME_SLUG',        'my-theme-slug' );
        define('WOO_SLT_CHANGELOG_URL',     'http://YourDomainWhereSoftwareManagement.com/changelog/');

            class WOO_SLT_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_theme_update($checked_data)
                        {
                            if ( !is_object( $checked_data ) ||  ! isset ( $checked_data->response ) )
                                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( wp_remote_retrieve_body( $data ) );

                            if(!is_array($response_block) || count($response_block) < 1)
                                return $checked_data;
                             
                             //retrieve the last message within the $response_block
                             $response_block = $response_block[count($response_block) - 1];
                             
                             $response  =   $this->postprocess_response( $response_block );
                             if ( $response ) 
                                {                                    
                                    if ( ! isset ( $response->new_version ) )
                                        return $checked_data;
                                    
                                    //check if the returned version is higher
                                    if ( isset ( $response->new_version )   &&  version_compare( $response->new_version, WOOGC_VERSION, '<'  ) )
                                        return $checked_data;
                                        
                                    $checked_data->response[$this->plugin] = $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;
                             
                            $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 pluguin.' , 'wooslt') . '&lt;/p> &lt;p>&lt;a href=&quot;?&quot; onclick=&quot;document.location.reload(); return false;&quot;>'. __( 'Try again', 'wooslt' ) .'&lt;/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.' , 'wooslt') . '&lt;/p> &lt;p>&lt;a href=&quot;?&quot; onclick=&quot;document.location.reload(); return false;&quot;>'. __( 'Try again', 'wooslt' ) .'&lt;/a>', $data->get_error_message());

                            $response_block = json_decode( wp_remote_retrieve_body( $data ) );
                     
                             if( ! is_array($response_block) || count( $response_block ) < 1 )
                                return $def;
                                
                             //retrieve the last message within the $response_block
                             $response_block = $response_block[ count( $response_block ) - 1 ];
                             
                             $response  =   $this->postprocess_response( $response_block );
                             if ( $response ) 
                                return $response;
                        }

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

                            $license_data = get_site_option('slt_license'); 

                            return array(
                                         'woo_sl_action'        => $action,
                                         'version'              => WOO_SLT_VERSION,
                                         'product_unique_id'    => WOO_SLT_PRODUCT_ID,
                                         'licence_key'          => $license_data['key'],
                                         'domain'               => WOO_SLT_INSTANCE,
                                         
                                         'wp-version'           => $wp_version,
                                         'api_version'          => '1.1'
                                         
                            );
                        }


                    private function postprocess_response( $response_block )
                         {
                             $response = isset( $response_block->update_data ) ? $response_block->update_data : '';
                             
                             if ( is_object( $response ) && ! empty ( $response ) )
                                 {
                                     //include slug and plugin data
                                     $response->slug    =   $this->slug;
                                     $response->plugin  =   $this->plugin;
                                     
                                     //if sections are being set
                                     if ( isset ( $response->sections ) )
                                        $response->sections = (array)$response->sections;
                                     
                                     //if banners are being set
                                     if ( isset ( $response->banners ) )
                                        $response->banners = (array)$response->banners;
                                       
                                     //if icons being set, convert to array
                                     if ( isset ( $response->icons ) )
                                        $response->icons    =   (array)$response->icons;
                                     
                                     return $response;
                                 }
                             
                             return FALSE;
                             
                         }

                }
             
             
        function WOO_SLT_run_updater()
             {
             
                 $wp_theme_auto_update = new WOO_SLT_CodeAutoUpdate( WOO_SLT_APP_API_URL, WOO_SLT_THEME_SLUG );
                 
                 // Take over the update check
                 add_filter('site_transient_update_themes',                                        array($wp_theme_auto_update, 'check_for_theme_update'));
                 
                 // Take over the Plugin info screen
                 add_filter('theme_api',                                                          array($wp_theme_auto_update, 'theme_api_call'), 10, 3);
             
             }
        add_action( 'after_setup_theme', 'WOO_SLT_run_updater' );
    

By woocommerce-sl, posted on August 1, 2017

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments