]> _ Git - physioassist-wordpress.git/blob
98cef52134f1406ebb89ecb5bf50c707e1f88db1
[physioassist-wordpress.git] /
1 <?php
2 /**
3  * Installer Class for Theme Support
4  *
5  * Supports automatic updates and installation of Toolset/WPML Themes
6  *
7  * @class       Installer_Theme_Class
8  * @version     1.6
9  * @category    Class
10  * @author      OnTheGoSystems
11  */
12
13 if ( !defined( 'ABSPATH' ) ) {
14     exit;
15 }
16
17 /**
18  * Installer_Theme_Class
19  */
20 class Installer_Theme_Class {
21
22     /** Theme Repository */
23     private $theme_repo;
24
25     /** Repository API */
26     private $repository_api;
27
28     /** Repository Theme Products */
29     private $repository_theme_products;
30
31     /** Site URL */
32     private $installer_site_url;
33
34     /** Site Key */
35     private $installer_site_key;
36
37     /** The Themes Option */
38     protected $installer_themes_option;
39
40     /** Update settings */
41     protected $installer_themes_available_updates;
42
43     /** The Themes */
44     protected $installer_themes = array();
45
46     /** Repository with themes */
47     protected $installer_repo_with_themes;
48
49     /** Active tab */
50     protected $installer_theme_active_tab;
51
52     /** Theme user registration */
53     protected $theme_user_registration;
54
55     /** Client active subscription */
56     protected $installer_theme_subscription_type;
57
58     public function __construct() {
59
60         /** Properties */
61
62         //Get installer repositories
63         $installer_repositories = WP_Installer()->get_repositories();
64
65         //Get repos with themes
66         $repos_with_themes = $this->installer_theme_reposities_that_has_themes( $installer_repositories );
67
68         if ( is_array( $repos_with_themes ) ) {
69             //Assign to property
70             $this->installer_repo_with_themes = $repos_with_themes;
71
72             //Let's looped through repos with themes
73             foreach ( $repos_with_themes as $k => $repo ) {
74
75                 //$repo could be 'toolset' or 'wpml'
76                 //Assign each repo with theme to property
77                 $this->theme_repo[] = $repo;
78
79                 if ( (isset($installer_repositories[$repo]['api-url'])) && (isset($installer_repositories[$repo]['products'])) ) {
80
81                     //Define the rest of the properties based on the given repo
82                     $this->repository_api[$repo] = $installer_repositories[$repo]['api-url'];
83                     $this->repository_theme_products[$repo] = $installer_repositories[$repo]['products'];
84                     $this->installer_site_url[$repo] = WP_Installer()->get_installer_site_url( $repo );
85                     $this->installer_site_key[$repo] = WP_Installer()->get_site_key( $repo );
86                     $this->theme_user_registration[$repo] = false;
87
88                     if ( WP_Installer()->repository_has_valid_subscription( $repo ) ) {
89
90                         $this->installer_theme_subscription_type = WP_Installer()->get_subscription_type_for_repository( $repo );
91                         $this->installer_themes_option[$repo] = 'wp_installer_' . $repo . '_themes';
92                         $this->installer_themes_available_updates[$repo] = 'wp_installer_' . $repo . '_updated_themes';
93                         $this->installer_theme_active_tab = '';
94
95                         //We only set themes available to this validated subscription
96                         $this->installer_theme_available( $repo, $this->installer_theme_subscription_type );
97
98                         add_action( 'installer_themes_support_set_up', array($this, 'installer_theme_sets_active_tab_on_init'), 10 );
99                         $this->theme_user_registration[$repo] = true;
100                     }
101
102                     /** We are ready.. let's initialize .... */
103                     $this->init();
104                 }
105             }
106             add_action( 'installer_themes_support_set_up', array($this, 'installer_theme_loaded_hooks') );
107         }
108     }
109
110     /** Init */
111     public function init() {
112         add_action( 'admin_enqueue_scripts', array($this, 'installer_theme_enqueue_scripts') );
113         add_filter( 'themes_api', array($this, 'installer_theme_api_override'), 10, 3 );
114         add_filter( 'themes_api_result', array($this, 'installer_theme_api_override_response'), 10, 3 );
115         add_filter( 'site_transient_update_themes', array($this, 'installer_theme_upgrade_check'), 10, 1 );
116         add_action( 'http_api_debug', array($this, 'installer_theme_sync_native_wp_api'), 10, 5 );
117         add_filter( 'installer_theme_hook_response_theme', array($this, 'installer_theme_add_num_ratings'), 10, 1 );
118         add_filter( 'themes_update_check_locales', array($this, 'installer_theme_sync_call_wp_theme_api'), 10, 1 );
119         add_filter( 'admin_url', array($this, 'installer_theme_add_query_arg_tab'), 10, 3 );
120         add_filter( 'network_admin_url', array($this, 'installer_theme_add_query_arg_tab'), 10, 2 );
121         add_action( 'wp_ajax_installer_theme_frontend_selected_tab', array($this, 'installer_theme_frontend_selected_tab'), 0 );
122         add_action( 'wp_loaded', array($this, 'installer_themes_support_set_up_func') );
123     }
124
125     /** Enqueue scripts */
126     public function installer_theme_enqueue_scripts() {
127         $current_screen = $this->installer_theme_current_screen();
128         $commercial_plugin_screen = $this->installer_theme_is_commercial_plugin_screen( $current_screen );
129         if ( ('theme-install' == $current_screen) || ($commercial_plugin_screen) || ('theme-install-network' == $current_screen) ) {
130             $repo_with_themes = $this->installer_repo_with_themes;
131             $js_array = array();
132             if ( is_array( $repo_with_themes ) ) {
133                 foreach ( $repo_with_themes as $k => $v ) {
134
135                     //Hyperlink text
136                     $theme_repo_name = $this->installer_theme_get_repo_product_name( $v );
137                     $the_hyperlink_text = esc_js( $theme_repo_name );
138
139                     if ( is_multisite() ) {
140                         $admin_url_passed = network_admin_url();
141                     } else {
142                         $admin_url_passed = admin_url();
143                     }
144
145                     //Define
146                     $js_array[$v] = array(
147                         'the_hyperlink_text' => $the_hyperlink_text,
148                         'registration_status' => $this->theme_user_registration[$v],
149                         'is_commercial_plugin_tab' => $commercial_plugin_screen,
150                         'registration_url' => $admin_url_passed . 'plugin-install.php?tab=commercial#installer_repo_' . $v
151                     );
152
153                 }
154             }
155
156             if ( !(empty($js_array)) ) {
157                 wp_enqueue_script( 'installer-theme-install', WP_Installer()->res_url() . '/res/js/installer_theme_install.js', array('jquery', 'installer-admin'), WP_Installer()->version() );
158                 $installer_ajax_url = admin_url( 'admin-ajax.php' );
159
160                 if ( is_ssl() ) {
161                     $installer_ajax_url = str_replace( 'http://', 'https://', $installer_ajax_url );
162                 } else {
163                     $installer_ajax_url = str_replace( 'https://', 'http://', $installer_ajax_url );
164                 }
165
166                 //Case where user is subscribed to a subscription that does not have themes
167                 $subscription_js_check = $this->installer_theme_subscription_does_not_have_theme( $js_array );
168
169                 wp_localize_script( 'installer-theme-install', 'installer_theme_install_localize',
170                     array(
171                         'js_array_installer' => $js_array,
172                         'ajaxurl' => $installer_ajax_url,
173                         'no_associated_themes' => $subscription_js_check,
174                         'installer_theme_frontend_selected_tab_nonce' => wp_create_nonce( 'installer_theme_frontend_selected_tab' )
175                     )
176                 );
177             }
178         }
179     }
180
181     /** Case where user is subscribed to a subscription that does not have themes */
182     protected function installer_theme_subscription_does_not_have_theme( $js_array ) {
183
184         $any_subscription_has_theme = array();
185         $number_of_registrations = array();
186
187         //Step1, we looped through JS array
188         foreach ( $js_array as $repo_slug => $js_details ) {
189
190             //Step2, checked if user is registered
191             if ( isset($this->theme_user_registration[$repo_slug]) ) {
192                 $registration_status = $this->theme_user_registration[$repo_slug];
193                 if ( $registration_status ) {
194
195                     //Registered
196                     $number_of_registrations[] = $repo_slug;
197
198                     //Step3, we checked if the $repo_slug has available theme
199                     $themes_available = false;
200                     if ( isset($this->installer_themes[$repo_slug]) ) {
201                         $themes_available = $this->installer_themes[$repo_slug];
202                         if ( !(empty($themes_available)) ) {
203                             //This subscription has theme
204                             $themes_available = true;
205                         }
206                     }
207
208                     if ( $themes_available ) {
209                         $any_subscription_has_theme[] = $repo_slug;
210                     }
211                 }
212             }
213
214         }
215
216         //Step4, we are done looping, check if there are any repos that have themes
217         if ( empty($registration_status) ) {
218
219             //No registration on any repos
220             return FALSE;
221
222         } elseif ( !(empty($registration_status)) ) {
223
224             //Has some registration on some repos
225             //We then checked if this user has any active subscriptions
226             if ( empty($any_subscription_has_theme) ) {
227                 //No subscription
228                 return TRUE;
229             } else {
230                 //Has subscription found
231                 return FALSE;
232             }
233         }
234     }
235
236     /** Check if its the commercial plugin screen */
237     private function installer_theme_is_commercial_plugin_screen( $current_screen ) {
238         $commercial = false;
239         if ( ('plugin-install' == $current_screen) || ('plugin-install-network' == $current_screen) ) {
240             if ( isset($_GET['tab']) ) {
241                 $tab = sanitize_text_field( $_GET['tab'] );
242                 if ( 'commercial' == $tab ) {
243                     $commercial = true;
244                 }
245             }
246         }
247         return $commercial;
248     }
249
250     /** Current screen */
251     private function installer_theme_current_screen() {
252
253         $current_screen_loaded = false;
254
255         if ( function_exists( 'get_current_screen' ) ) {
256
257             $screen_output = get_current_screen();
258             $current_screen_loaded = $screen_output->id;
259
260         }
261
262         return $current_screen_loaded;
263
264     }
265
266     /** Override WordPress Themes API */
267     public function installer_theme_api_override( $api_boolean, $action, $args ) {
268
269         //Let's checked if user is browsing our themes
270         if ( isset($args->browse) ) {
271             $browse = $args->browse;
272             if ( in_array( $browse, $this->theme_repo ) ) {
273                 //Uniquely validated for our Themes
274                 if ( 'query_themes' == $action ) {
275                     //User is querying or asking information about our themes, let's override
276                     $api_boolean = true;
277                 }
278             }
279         } elseif ( isset($args->slug) ) {
280             //We are installing our themes
281             $theme_to_install = $args->slug;
282
283             //Lets uniquely validate if this belongs to us
284             //Check if this is OTGS theme
285             $validate_check = $this->installer_themes_belong_to_us( $theme_to_install );
286             if ( $validate_check ) {
287                 //Belongs to us
288                 if ( !(empty($theme_to_install)) ) {
289                     $api_boolean = true;
290                 }
291             }
292         }
293
294         return $api_boolean;
295     }
296
297     /** Override WordPress Themes API response with our own themes API*/
298     public function installer_theme_api_override_response( $res, $action, $args ) {
299
300         if ( true === $res ) {
301             if ( isset($args->browse) ) {
302                 $browse = $args->browse;
303                 if ( in_array( $browse, $this->theme_repo ) ) {
304                     //Uniquely validated for our themes
305                     if ( 'query_themes' == $action ) {
306                         //Client querying OTGS themes
307                         //Check for registration status
308                         if ( isset($this->theme_user_registration[$browse]) ) {
309                             //Set
310                             if ( !($this->theme_user_registration[$browse]) ) {
311                                 //Not registered yet
312                                 $res = new stdClass();
313                                 $res->info = array();
314                                 $res->themes = array();
315                                 return $res;
316                             } else {
317                                 //Registered
318                                 $themes = $this->installer_theme_get_themes( '', $browse );
319                                 $res = $this->installer_theme_format_response( $themes, $action );
320                             }
321                         }
322                     }
323                 }
324             } elseif ( isset($args->slug) ) {
325                 //We are installing theme
326                 //Lets uniquely validate if this belongs to our theme
327                 $theme_to_install = $args->slug;
328
329                 //Lets uniquely validate if this belongs to us
330                 //Check if this is OTGS theme
331                 $validate_check = $this->installer_themes_belong_to_us( $theme_to_install );
332                 if ( $validate_check ) {
333                     //Belongs to us
334                     if ( ($res) && ('theme_information' == $action) ) {
335                         $themes = $this->installer_theme_get_themes( '', $this->installer_theme_active_tab );
336                         $res = $this->installer_theme_format_response( $themes, $action, $args->slug );
337                     }
338                 }
339             }
340             return $res;
341         } else {
342             //Default WP Themes here
343             $client_side_active_tab = get_option( 'wp_installer_clientside_active_tab' );
344             if ( $client_side_active_tab ) {
345                 if ( !(in_array( $client_side_active_tab, $this->theme_repo )) ) {
346                     //Not OTGS tab
347                     return $res;
348                 }
349             }
350
351         }
352     }
353
354     /** Get Themes */
355     private function installer_theme_get_themes( $product_url = '', $repo_source = '' ) {
356
357         //Query API
358         if ( empty($product_url) ) {
359             //Not set
360             if ( isset($this->repository_theme_products[$this->installer_theme_active_tab]) ) {
361                 $query_remote_url = $this->repository_theme_products[$this->installer_theme_active_tab];
362             }
363
364         } else {
365             $query_remote_url = $product_url;
366         }
367
368         //Let's retrieved current installer settings so we won't be querying all the time
369         $current_installer_settings = WP_Installer()->get_settings();
370
371         //Set $themes to FALSE by default
372         $themes = false;
373
374         if ( (is_array( $current_installer_settings )) && (!(empty($current_installer_settings))) ) {
375
376             //Set and already defined, retrieved $products
377             if ( isset($current_installer_settings['repositories'][$repo_source]['data']) ) {
378                 $products = $current_installer_settings['repositories'][$repo_source]['data'];
379                 if ( isset($products['downloads']['themes']) ) {
380                     $themes = $products['downloads']['themes'];
381                 }
382             }
383
384         } else {
385
386             //Call API
387             $response = wp_remote_get( $query_remote_url );
388
389             if ( is_wp_error( $response ) ) {
390                 //Error detected: http fallback
391                 $query_remote_url = preg_replace( "@^https://@", 'http://', $query_remote_url );
392                 $response = wp_remote_get( $query_remote_url );
393             }
394
395             if ( !(is_wp_error( $response )) ) {
396                 //Not WP error
397                 //Evaluate response
398                 if ( $response && isset($response['response']['code']) && $response['response']['code'] == 200 ) {
399                     //In this case, response is set and defined, proceed...
400                     $body = wp_remote_retrieve_body( $response );
401                     if ( $body ) {
402                         $products = json_decode( $body, true );
403                         if ( isset($products['downloads']['themes']) ) {
404                             $themes = $products['downloads']['themes'];
405                         }
406                     }
407
408                 }
409             }
410         }
411
412         //Return themes, can be filtered by user subscription type
413         return apply_filters( 'installer_theme_get_themes', $themes, $this->installer_theme_active_tab );
414     }
415
416     /** Format response in compatibility with WordPress Theme API response */
417     private function installer_theme_format_response( $themes, $action, $slug = '' ) {
418
419         //Let's append download link only when retrieving theme information for installation
420         if ( ('theme_information' == $action) && (!(empty($slug))) ) {
421
422             //Only return one result -> the theme to be installed
423             foreach ( $themes as $k => $theme ) {
424                 if ( $slug == $theme['basename'] ) {
425                     $theme['download_link'] = WP_Installer()->append_site_key_to_download_url( $theme['url'], $this->installer_site_key[$this->installer_theme_active_tab], $this->installer_theme_active_tab );
426                     $theme = json_decode( json_encode( $theme ), FALSE );
427                     return $theme;
428                 }
429             }
430
431         } else {
432
433             $res = new stdClass();
434             $res->info = array();
435             $res->themes = array();
436
437             //Define info
438             $res->info['page'] = 1;
439             $res->info['pages'] = 10;
440
441             //Let's count available themes              ;
442             $res->info['results'] = count( $themes );
443
444             //Let's saved themes for easy access later on
445             $this->installer_theme_savethemes_by_slug( $themes );
446
447             //Let's defined available themes
448             if ( isset($this->installer_theme_subscription_type) ) {
449                 //Has subscription type defined, let's saved what is associated with this subscription
450                 $this->installer_theme_available( $this->installer_theme_active_tab, $this->installer_theme_subscription_type );
451             } else {
452                 $this->installer_theme_available( $this->installer_theme_active_tab );
453             }
454
455             //Let's add themes to the overriden WordPress API Theme response
456             /** Installer 1.7.6: Update to compatible data format response from WP Theme API */
457             $theme_compatible_array=array();
458             if ((is_array($themes))) {
459                 foreach ($themes as $k=>$v) {
460                         $theme_compatible_array[]=(object)($v);
461                 }
462             }
463             $res->themes = $theme_compatible_array;
464             $res->themes = apply_filters( 'installer_theme_hook_response_theme', $res->themes );
465             return $res;
466         }
467     }
468
469     /** Let's save all available themes by its slug after any latest API query */
470     private function installer_theme_savethemes_by_slug( $themes, $doing_query = false ) {
471
472         if ( !($doing_query) ) {
473             $this->installer_themes[$this->installer_theme_active_tab] = array();
474         }
475
476         if ( !(empty($themes)) ) {
477             $themes_for_saving = array();
478             foreach ( $themes as $k => $theme ) {
479                 if ( !($doing_query) ) {
480                     if ( isset($theme['slug']) ) {
481                         $theme_slug = $theme['slug'];
482                         if ( !(empty($theme_slug)) ) {
483                             $themes_for_saving[] = $theme_slug;
484                         }
485                     }
486                 } else {
487
488                     if ( ((isset($theme['slug'])) && (isset($theme['version'])) &&
489                             (isset($theme['theme_page_url']))) && (isset($theme['url']))
490                     ) {
491                         $theme_slug = $theme['slug'];
492                         $theme_version = $theme['version'];
493                         $theme_page_url = $theme['theme_page_url'];
494                         $theme_url = $theme['url'];
495                         if ( (!(empty($theme_slug))) && (!(empty($theme_version))) &&
496                             (!(empty($theme_page_url))) && (!(empty($theme_url)))
497                         ) {
498                             //$theme_slug is unique for every theme
499                             $themes_for_saving[$theme_slug] = array(
500                                 'version' => $theme_version,
501                                 'theme_page_url' => $theme_page_url,
502                                 'url' => $theme_url
503                             );
504
505                         }
506                     }
507                 }
508
509             }
510
511             if ( !(empty($themes_for_saving)) ) {
512                 //Has themes for saving
513                 if ( !($doing_query) ) {
514                     //Not doing query
515                     $existing_themes = get_option( $this->installer_themes_option[$this->installer_theme_active_tab] );
516                     if ( !($existing_themes) ) {
517                         //Does not yet exists
518                         delete_option( $this->installer_themes_option[$this->installer_theme_active_tab] );
519                         update_option( $this->installer_themes_option[$this->installer_theme_active_tab], $themes_for_saving );
520                     } else {
521                         //exists, check if we need to update
522                         if ( $existing_themes == $themes_for_saving ) {
523                             //Equal, no need to update here
524                         } else {
525                             //Update
526                             delete_option( $this->installer_themes_option[$this->installer_theme_active_tab] );
527                             update_option( $this->installer_themes_option[$this->installer_theme_active_tab], $themes_for_saving );
528                         }
529                     }
530                 } else {
531                     //Used for query purposes only, don't save anything
532                     return $themes_for_saving;
533                 }
534             }
535         }
536     }
537
538     /** Available themes */
539     private function installer_theme_available( $repo, $subscription_type = '' ) {
540
541         $subscription_type = intval( $subscription_type );
542         if ( $subscription_type > 0 ) {
543
544             //Here we have a case of validated subscription
545             //We need to set themes that is available to this subscription
546             $themes_associated_with_subscription = $this->installer_themes[$repo] = $this->installer_theme_get_themes_by_subscription( $subscription_type, $repo );
547             if ( !(empty($themes_associated_with_subscription)) ) {
548                 //Has themes
549                 $this->installer_themes[$repo] = $themes_associated_with_subscription;
550             }
551         } else {
552
553             //Get themes
554             $this->installer_themes[$repo] = get_option( $this->installer_themes_option[$repo] );
555         }
556     }
557
558     /** Theme upgrade check */
559     public function installer_theme_upgrade_check( $the_value ) {
560
561         //Step1: Let's looped through repos with themes and check if we have updates available for them.
562         if ( (is_array( $this->installer_repo_with_themes )) && (!(empty($this->installer_repo_with_themes))) ) {
563             foreach ( $this->installer_repo_with_themes as $k => $repo_slug ) {
564                 //Step2: Let's checked if we have update for this theme
565                 $update_available = get_option( $this->installer_themes_available_updates[$repo_slug] );
566                 if ( $update_available ) {
567                     if ( (is_array( $update_available )) && (!(empty($update_available))) ) {
568                         //Has updates available coming from this specific theme repo
569                         //Let's loop through the themes that needs update
570                         foreach ( $update_available as $theme_slug => $v ) {
571                             //Add to response API
572                             $the_value->response [$theme_slug] = array(
573                                 'theme' => $theme_slug,
574                                 'new_version' => $v['new_version'],
575                                 'url' => $v['url'],
576                                 'package' => $v['package']
577                             );
578                         }
579                     }
580                 }
581             }
582         }
583         //Return
584         return $the_value;
585     }
586
587     /** Return repositories that has themes */
588     private function installer_theme_reposities_that_has_themes( $repositories, $ret_value = true, $doing_api_query = false ) {
589
590         $repositories_with_themes = array();
591
592         if ( (is_array( $repositories )) && (!(empty($repositories))) ) {
593
594             //Let's checked if we have something before
595             $themes = get_option( 'installer_repositories_with_theme' );
596
597             if ( (!($themes)) || ($doing_api_query) ) {
598                 //Not yet defined
599                 //Loop through each repositories and check whether they have themes
600                 foreach ( $repositories as $k => $v ) {
601                     if ( isset($v['products']) ) {
602                         $products_url = $v['products'];
603                         $themes = $this->installer_theme_get_themes( $products_url, $k );
604                         if ( (is_array( $themes )) && (!(empty($themes))) ) {
605                             //Repo has themes
606                             $repositories_with_themes[] = $k;
607                         }
608                     }
609                 }
610             } else {
611                 //Already set
612                 $repositories_with_themes = $themes;
613             }
614
615             if ( (((is_array( $repositories_with_themes )) && (!(empty($repositories_with_themes)))) && (!($themes))) || ($doing_api_query) ) {
616                 //Save to db
617                 update_option( 'installer_repositories_with_theme', $repositories_with_themes );
618             }
619         }
620
621         if ( $ret_value ) {
622             return $repositories_with_themes;
623         }
624
625     }
626
627     /** When WordPress queries its own Themes API, we sync with our own */
628     public function installer_theme_sync_native_wp_api( $response, $responsetext, $class, $args, $url ) {
629
630         $api_native_string = 'api.wordpress.org/themes/';
631         if ( (strpos( $url, $api_native_string ) !== false) ) {
632             //WordPress is querying its own themes API
633             $installer_repositories = WP_Installer()->get_repositories();
634
635             //Query our own API and update repository values too
636             $this->installer_theme_reposities_that_has_themes( $installer_repositories, false, true );
637         }
638     }
639
640     /** Returns product name by theme repo slug */
641     private function installer_theme_get_repo_product_name( $theme_repo ) {
642
643         $theme_repo_name = false;
644
645         if ( isset(WP_Installer()->settings['repositories'][$theme_repo]['data']['product-name']) ) {
646             //Set
647             $prod_name = WP_Installer()->settings['repositories'][$theme_repo]['data']['product-name'];
648             if ( !(empty($prod_name)) ) {
649                 $theme_repo_name = $prod_name;
650             }
651         } else {
652             //Not yet
653             if ( $theme_repo == $this->theme_repo ) {
654                 $result = $this->installer_theme_general_api_query();
655                 if ( isset($result['product-name']) ) {
656                     $product_name = $result['product-name'];
657                     if ( !(empty($product_name)) ) {
658                         $theme_repo_name = $product_name;
659                     }
660                 }
661             }
662         }
663
664         return $theme_repo_name;
665     }
666
667     /** General query API method, returns $products */
668     private function installer_theme_general_api_query() {
669         $products = false;
670         $response = wp_remote_get( $this->repository_theme_products );
671         if ( !(is_wp_error( $response )) ) {
672             //Not WP error
673             //Evaluate response
674             if ( $response && isset($response['response']['code']) && $response['response']['code'] == 200 ) {
675                 //In this case, response is set and defined, proceed...
676                 $body = wp_remote_retrieve_body( $response );
677                 if ( $body ) {
678                     $result = json_decode( $body, true );
679                     if ( (is_array( $result )) && (!(empty($result))) ) {
680                         $products = $result;
681                     }
682                 }
683
684             }
685         }
686
687         return $products;
688     }
689
690     /** General method to check if themes are OTGS themes based on its slug*/
691     private function installer_themes_belong_to_us( $theme_slug ) {
692
693         $found = false;
694         $theme_slug = trim( $theme_slug );
695
696         foreach ( $this->installer_themes as $repo_with_theme => $themes ) {
697             foreach ( $themes as $k => $otgs_theme_slug ) {
698                 if ( $theme_slug == $otgs_theme_slug ) {
699                     //match found! Theme belongs to otgs
700                     return true;
701                 }
702             }
703         }
704         return $found;
705
706     }
707
708     /** Sets active tab on init */
709     public function installer_theme_sets_active_tab_on_init() {
710
711         if ( isset ($_SERVER ['REQUEST_URI']) ) {
712             $request_uri = $_SERVER ['REQUEST_URI'];
713             if ( isset ($_GET ['browse']) ) {
714                 $active_tab = sanitize_text_field( $_GET['browse'] );
715                 $this->installer_theme_active_tab = $active_tab;
716             } elseif ( isset ($_POST ['request'] ['browse']) ) {
717                 $active_tab = sanitize_text_field ( $_POST['request']['browse'] );
718                 $this->installer_theme_active_tab = $active_tab;
719             } elseif ( (isset ($_GET ['theme_repo'])) && (isset ($_GET ['action'])) ) {
720                 $theme_repo = sanitize_text_field( $_GET['theme_repo'] );
721                 $the_action = sanitize_text_field( $_GET['action'] );
722                 if ( ('install-theme' == $the_action) && (!(empty($theme_repo))) ) {
723                     $this->installer_theme_active_tab = $theme_repo;
724                 }
725             } elseif ( wp_get_referer() ) {
726                 $referer = wp_get_referer();
727                 $parts = parse_url( $referer );
728                 if ( isset($parts['query']) ) {
729                     parse_str( $parts['query'], $query );
730                     if ( isset($query['browse']) ) {
731                         $this->installer_theme_active_tab = $query['browse'];
732                     }
733                 }
734             }
735         }
736     }
737
738     /** WP Theme API compatibility- added num ratings */
739     /** Installer 1.7.6+ Added updated 'rating' field */
740     public function installer_theme_add_num_ratings( $themes ) {
741
742         if ( (is_array( $themes )) && (!(empty($themes))) ) {
743             foreach ( $themes as $k => $v ) {
744                 if ( !(isset($v->num_ratings)) ) {
745                     $themes[$k]->num_ratings = 100;
746                 }
747                 if ( !(isset($v->rating)) ) {
748                         $themes[$k]->rating = 100;
749                 }
750             }
751         }
752
753         return $themes;
754     }
755
756     /** When WordPress.org makes a call to its repository, let's run our own upgrade checks too */
757     public function installer_theme_sync_call_wp_theme_api( $locales ) {
758
759         $this->installer_theme_upgrade_theme_check();
760
761         return $locales;
762     }
763
764     /** Upgrade theme check */
765     private function installer_theme_upgrade_theme_check() {
766
767         // Step1-> we get all installed themes in clients local themes directory
768         $installed_themes = wp_get_themes();
769
770         // Step2: We need to loop through each repository with themes
771         foreach ( $this->installer_repo_with_themes as $k => $repo_slug ) {
772
773             // We then need to retrieved the products URL for each of this repo
774             $products_url = $this->repository_theme_products [$repo_slug];
775
776             // Step3-> we get all available themes in our repository via API based on this URL
777             $available_themes = $this->installer_theme_get_themes( $products_url, $repo_slug );
778
779             if ( !($available_themes) ) {
780
781                 // API is not available as of the moment, return..
782                 return;
783             } else {
784
785                 // We have available themes here...
786                 // Step4->let's simplify available themes data by slugs
787                 $simplified_available_themes = $this->installer_theme_savethemes_by_slug( $available_themes, true );
788
789                 // Step5->Let's loop through installed themes
790                 if ( (is_array( $installed_themes )) && (!(empty ($installed_themes))) ) {
791                     $otgs_theme_updates_available = array();
792                     foreach ( $installed_themes as $theme_slug => $theme_object ) {
793                         if ( array_key_exists( $theme_slug, $simplified_available_themes ) ) {
794
795                             // This is our theme
796                             // Step6->Let's get version of the local theme installed
797                             $local_version = $theme_object->get( 'Version' );
798
799                             // Step7->Let's get the latest version of this theme, page URL and download URL from our repository
800                             $repository_version = $simplified_available_themes [$theme_slug] ['version'];
801                             $theme_page_url = $simplified_available_themes [$theme_slug] ['theme_page_url'];
802                             $theme_download_url = $simplified_available_themes [$theme_slug] ['url'];
803
804                             // Step8->Let's compare the version
805                             if ( version_compare( $repository_version, $local_version, '>' ) ) {
806
807                                 // Update available for this theme
808                                 // Step9-> Define download URL with site key
809                                 $package_url = WP_Installer()->append_site_key_to_download_url( $theme_download_url, $this->installer_site_key [$repo_slug], $repo_slug );
810
811                                 //Step10-> Assign to updates array for later accessing.
812                                 $otgs_theme_updates_available[$theme_slug] = array(
813                                     'theme' => $theme_slug,
814                                     'new_version' => $repository_version,
815                                     'url' => $theme_page_url,
816                                     'package' => $package_url
817                                 );
818                             }
819                         }
820                     }
821                     //Exited the upgrade loop for this specific theme repository
822                     if ( !(empty($otgs_theme_updates_available)) ) {
823                         //Has updates
824                         update_option( $this->installer_themes_available_updates[$repo_slug], $otgs_theme_updates_available );
825                     } else {
826                         //No updates
827                         delete_option( $this->installer_themes_available_updates[$repo_slug] );
828                     }
829
830                 }
831             }
832         }
833     }
834
835     /** When the user is on Themes install page OTG themes repository, let's the currently selected tab */
836     public function installer_theme_add_query_arg_tab( $url, $path, $blog_id = null ) {
837
838         $wp_install_string = 'update.php?action=install-theme';
839         if ( $path == $wp_install_string ) {
840             if ( isset($this->installer_theme_active_tab) ) {
841                 if ( !(empty($this->installer_theme_active_tab)) ) {
842                     $url = add_query_arg( array(
843                         'theme_repo' => $this->installer_theme_active_tab
844                     ), $url );
845                 }
846             }
847         }
848         return $url;
849     }
850
851     /** Save frontend theme tab selected */
852     public function installer_theme_frontend_selected_tab() {
853         if ( isset($_POST["frontend_tab_selected"]) ) {
854             check_ajax_referer( 'installer_theme_frontend_selected_tab', 'installer_theme_frontend_selected_tab_nonce' );
855
856             //Client_side_active_tab
857             $frontend_tab_selected = sanitize_text_field( $_POST['frontend_tab_selected'] );
858             if ( !(empty($frontend_tab_selected)) ) {
859                 //Front end tab selected
860                 update_option( 'wp_installer_clientside_active_tab', $frontend_tab_selected, false );
861
862                 //Check for registration status
863                 if ( isset($this->theme_user_registration[$frontend_tab_selected]) ) {
864                     //Set
865                     if ( !($this->theme_user_registration[$frontend_tab_selected]) ) {
866                         //Not registered yet
867
868                         if ( is_multisite() ) {
869                             $admin_url_passed = network_admin_url();
870                         } else {
871                             $admin_url_passed = admin_url();
872                         }
873
874                         $registration_url = $admin_url_passed . 'plugin-install.php?tab=commercial#installer_repo_' . $frontend_tab_selected;
875
876                         //Message and link
877                         $theme_repo_name = $this->installer_theme_get_repo_product_name( $frontend_tab_selected );;
878                         $response['unregistered_messages'] = sprintf( __( 'To install and update %s, please %sregister%s %s for this site.', 'installer' ),
879                             $theme_repo_name, '<a href="' . $registration_url . '">', '</a>', $theme_repo_name );
880
881                     }
882                 }
883
884                 $response['output'] = $frontend_tab_selected;
885                 echo json_encode( $response );
886             }
887             die();
888         }
889         die();
890     }
891
892     /** Installer loaded aux hooks */
893     public function installer_theme_loaded_hooks() {
894
895         if ( isset($this->installer_theme_subscription_type) ) {
896             $subscription_type = intval( $this->installer_theme_subscription_type );
897             if ( $subscription_type > 0 ) {
898                 //Client is subscribed
899                 add_filter( 'installer_theme_get_themes', array($this, 'installer_theme_filter_themes_by_subscription'), 10, 2 );
900             }
901         }
902
903     }
904
905     /** Get themes by subscription type */
906     protected function installer_theme_get_themes_by_subscription( $subscription_type, $repo ) {
907
908         $themes_associated_with_subscription = array();
909         if ( isset(WP_Installer()->settings['repositories'][$repo]['data']['packages']) ) {
910             //Set
911             $packages = WP_Installer()->settings['repositories'][$repo]['data']['packages'];
912             $available_themes_subscription = array();
913             foreach ( $packages as $package_id => $package_details ) {
914                 if ( isset($package_details['products']) ) {
915                     $the_products = $package_details['products'];
916                     foreach ( $the_products as $product_slug => $product_details ) {
917                         if ( isset($product_details['subscription_type']) ) {
918                             $subscription_type_from_settings = intval( $product_details['subscription_type'] );
919                             if ( $subscription_type_from_settings == $subscription_type ) {
920                                 //We found the subscription
921                                 if ( isset($product_details['themes']) ) {
922                                     $themes_associated_with_subscription = $product_details['themes'];
923                                     return $themes_associated_with_subscription;
924                                 }
925                             }
926                         }
927
928                     }
929                 }
930             }
931         }
932         return $themes_associated_with_subscription;
933     }
934
935     /** Filter API theme response according to user subscription */
936     public function installer_theme_filter_themes_by_subscription( $themes, $active_tab ) {
937
938         //Step1, we only filter OTGS themes
939         $orig = count( $themes );
940         if ( in_array( $active_tab, $this->theme_repo ) ) {
941             //OTGS Theme
942             //Step2, we retrieved the available themes based on client subscription
943             if ( isset($this->installer_themes[$active_tab]) ) {
944                 $available_themes = $this->installer_themes[$active_tab];
945                 //Step3, we filter $themes based on this info
946                 if ( (is_array( $themes )) && (!(empty($themes))) ) {
947                     foreach ( $themes as $k => $theme ) {
948                         //Step4, get theme slug
949                         if ( isset($theme['slug']) ) {
950                             $theme_slug = $theme['slug'];
951                             if ( !(empty($theme_slug)) ) {
952                                 if ( !(in_array( $theme_slug, $available_themes )) ) {
953                                     //This theme is not in available themes
954                                     unset($themes[$k]);
955                                 }
956                             }
957                         }
958                     }
959                 }
960             }
961         }
962         $new = count( $themes );
963         if ( $orig != $new ) {
964             //It is filtered
965             $themes = array_values( $themes );
966         }
967
968         return $themes;
969     }
970
971     /** Hook to wp_loaded, fires when all Installer theme class is ready */
972     public function installer_themes_support_set_up_func() {
973         do_action( 'installer_themes_support_set_up' );
974     }
975
976 }
977
978 /** Instantiate Installer Theme Class */
979 new Installer_Theme_Class;