3 * Installer Class for Theme Support
5 * Supports automatic updates and installation of Toolset/WPML Themes
7 * @class Installer_Theme_Class
10 * @author OnTheGoSystems
13 if ( !defined( 'ABSPATH' ) ) {
18 * Installer_Theme_Class
20 class Installer_Theme_Class {
22 /** Theme Repository */
26 private $repository_api;
28 /** Repository Theme Products */
29 private $repository_theme_products;
32 private $installer_site_url;
35 private $installer_site_key;
37 /** The Themes Option */
38 protected $installer_themes_option;
40 /** Update settings */
41 protected $installer_themes_available_updates;
44 protected $installer_themes = array();
46 /** Repository with themes */
47 protected $installer_repo_with_themes;
50 protected $installer_theme_active_tab;
52 /** Theme user registration */
53 protected $theme_user_registration;
55 /** Client active subscription */
56 protected $installer_theme_subscription_type;
58 public function __construct() {
62 //Get installer repositories
63 $installer_repositories = WP_Installer()->get_repositories();
65 //Get repos with themes
66 $repos_with_themes = $this->installer_theme_reposities_that_has_themes( $installer_repositories );
68 if ( is_array( $repos_with_themes ) ) {
70 $this->installer_repo_with_themes = $repos_with_themes;
72 //Let's looped through repos with themes
73 foreach ( $repos_with_themes as $k => $repo ) {
75 //$repo could be 'toolset' or 'wpml'
76 //Assign each repo with theme to property
77 $this->theme_repo[] = $repo;
79 if ( (isset($installer_repositories[$repo]['api-url'])) && (isset($installer_repositories[$repo]['products'])) ) {
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;
88 if ( WP_Installer()->repository_has_valid_subscription( $repo ) ) {
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 = '';
95 //We only set themes available to this validated subscription
96 $this->installer_theme_available( $repo, $this->installer_theme_subscription_type );
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;
102 /** We are ready.. let's initialize .... */
106 add_action( 'installer_themes_support_set_up', array($this, 'installer_theme_loaded_hooks') );
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') );
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;
132 if ( is_array( $repo_with_themes ) ) {
133 foreach ( $repo_with_themes as $k => $v ) {
136 $theme_repo_name = $this->installer_theme_get_repo_product_name( $v );
137 $the_hyperlink_text = esc_js( $theme_repo_name );
139 if ( is_multisite() ) {
140 $admin_url_passed = network_admin_url();
142 $admin_url_passed = admin_url();
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
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' );
161 $installer_ajax_url = str_replace( 'http://', 'https://', $installer_ajax_url );
163 $installer_ajax_url = str_replace( 'https://', 'http://', $installer_ajax_url );
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 );
169 wp_localize_script( 'installer-theme-install', 'installer_theme_install_localize',
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' )
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 ) {
184 $any_subscription_has_theme = array();
185 $number_of_registrations = array();
187 //Step1, we looped through JS array
188 foreach ( $js_array as $repo_slug => $js_details ) {
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 ) {
196 $number_of_registrations[] = $repo_slug;
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;
208 if ( $themes_available ) {
209 $any_subscription_has_theme[] = $repo_slug;
216 //Step4, we are done looping, check if there are any repos that have themes
217 if ( empty($registration_status) ) {
219 //No registration on any repos
222 } elseif ( !(empty($registration_status)) ) {
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) ) {
230 //Has subscription found
236 /** Check if its the commercial plugin screen */
237 private function installer_theme_is_commercial_plugin_screen( $current_screen ) {
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 ) {
250 /** Current screen */
251 private function installer_theme_current_screen() {
253 $current_screen_loaded = false;
255 if ( function_exists( 'get_current_screen' ) ) {
257 $screen_output = get_current_screen();
258 $current_screen_loaded = $screen_output->id;
262 return $current_screen_loaded;
266 /** Override WordPress Themes API */
267 public function installer_theme_api_override( $api_boolean, $action, $args ) {
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
279 } elseif ( isset($args->slug) ) {
280 //We are installing our themes
281 $theme_to_install = $args->slug;
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 ) {
288 if ( !(empty($theme_to_install)) ) {
297 /** Override WordPress Themes API response with our own themes API*/
298 public function installer_theme_api_override_response( $res, $action, $args ) {
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]) ) {
310 if ( !($this->theme_user_registration[$browse]) ) {
312 $res = new stdClass();
313 $res->info = array();
314 $res->themes = array();
318 $themes = $this->installer_theme_get_themes( '', $browse );
319 $res = $this->installer_theme_format_response( $themes, $action );
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;
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 ) {
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 );
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 )) ) {
355 private function installer_theme_get_themes( $product_url = '', $repo_source = '' ) {
358 if ( empty($product_url) ) {
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];
365 $query_remote_url = $product_url;
368 //Let's retrieved current installer settings so we won't be querying all the time
369 $current_installer_settings = WP_Installer()->get_settings();
371 //Set $themes to FALSE by default
374 if ( (is_array( $current_installer_settings )) && (!(empty($current_installer_settings))) ) {
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'];
387 $response = wp_remote_get( $query_remote_url );
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 );
395 if ( !(is_wp_error( $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 );
402 $products = json_decode( $body, true );
403 if ( isset($products['downloads']['themes']) ) {
404 $themes = $products['downloads']['themes'];
412 //Return themes, can be filtered by user subscription type
413 return apply_filters( 'installer_theme_get_themes', $themes, $this->installer_theme_active_tab );
416 /** Format response in compatibility with WordPress Theme API response */
417 private function installer_theme_format_response( $themes, $action, $slug = '' ) {
419 //Let's append download link only when retrieving theme information for installation
420 if ( ('theme_information' == $action) && (!(empty($slug))) ) {
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 );
433 $res = new stdClass();
434 $res->info = array();
435 $res->themes = array();
438 $res->info['page'] = 1;
439 $res->info['pages'] = 10;
441 //Let's count available themes ;
442 $res->info['results'] = count( $themes );
444 //Let's saved themes for easy access later on
445 $this->installer_theme_savethemes_by_slug( $themes );
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 );
452 $this->installer_theme_available( $this->installer_theme_active_tab );
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);
463 $res->themes = $theme_compatible_array;
464 $res->themes = apply_filters( 'installer_theme_hook_response_theme', $res->themes );
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 ) {
472 if ( !($doing_query) ) {
473 $this->installer_themes[$this->installer_theme_active_tab] = array();
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;
488 if ( ((isset($theme['slug'])) && (isset($theme['version'])) &&
489 (isset($theme['theme_page_url']))) && (isset($theme['url']))
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)))
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,
511 if ( !(empty($themes_for_saving)) ) {
512 //Has themes for saving
513 if ( !($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 );
521 //exists, check if we need to update
522 if ( $existing_themes == $themes_for_saving ) {
523 //Equal, no need to update here
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 );
531 //Used for query purposes only, don't save anything
532 return $themes_for_saving;
538 /** Available themes */
539 private function installer_theme_available( $repo, $subscription_type = '' ) {
541 $subscription_type = intval( $subscription_type );
542 if ( $subscription_type > 0 ) {
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)) ) {
549 $this->installer_themes[$repo] = $themes_associated_with_subscription;
554 $this->installer_themes[$repo] = get_option( $this->installer_themes_option[$repo] );
558 /** Theme upgrade check */
559 public function installer_theme_upgrade_check( $the_value ) {
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'],
576 'package' => $v['package']
587 /** Return repositories that has themes */
588 private function installer_theme_reposities_that_has_themes( $repositories, $ret_value = true, $doing_api_query = false ) {
590 $repositories_with_themes = array();
592 if ( (is_array( $repositories )) && (!(empty($repositories))) ) {
594 //Let's checked if we have something before
595 $themes = get_option( 'installer_repositories_with_theme' );
597 if ( (!($themes)) || ($doing_api_query) ) {
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))) ) {
606 $repositories_with_themes[] = $k;
612 $repositories_with_themes = $themes;
615 if ( (((is_array( $repositories_with_themes )) && (!(empty($repositories_with_themes)))) && (!($themes))) || ($doing_api_query) ) {
617 update_option( 'installer_repositories_with_theme', $repositories_with_themes );
622 return $repositories_with_themes;
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 ) {
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();
635 //Query our own API and update repository values too
636 $this->installer_theme_reposities_that_has_themes( $installer_repositories, false, true );
640 /** Returns product name by theme repo slug */
641 private function installer_theme_get_repo_product_name( $theme_repo ) {
643 $theme_repo_name = false;
645 if ( isset(WP_Installer()->settings['repositories'][$theme_repo]['data']['product-name']) ) {
647 $prod_name = WP_Installer()->settings['repositories'][$theme_repo]['data']['product-name'];
648 if ( !(empty($prod_name)) ) {
649 $theme_repo_name = $prod_name;
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;
664 return $theme_repo_name;
667 /** General query API method, returns $products */
668 private function installer_theme_general_api_query() {
670 $response = wp_remote_get( $this->repository_theme_products );
671 if ( !(is_wp_error( $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 );
678 $result = json_decode( $body, true );
679 if ( (is_array( $result )) && (!(empty($result))) ) {
690 /** General method to check if themes are OTGS themes based on its slug*/
691 private function installer_themes_belong_to_us( $theme_slug ) {
694 $theme_slug = trim( $theme_slug );
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
708 /** Sets active tab on init */
709 public function installer_theme_sets_active_tab_on_init() {
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;
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'];
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 ) {
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;
747 if ( !(isset($v->rating)) ) {
748 $themes[$k]->rating = 100;
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 ) {
759 $this->installer_theme_upgrade_theme_check();
764 /** Upgrade theme check */
765 private function installer_theme_upgrade_theme_check() {
767 // Step1-> we get all installed themes in clients local themes directory
768 $installed_themes = wp_get_themes();
770 // Step2: We need to loop through each repository with themes
771 foreach ( $this->installer_repo_with_themes as $k => $repo_slug ) {
773 // We then need to retrieved the products URL for each of this repo
774 $products_url = $this->repository_theme_products [$repo_slug];
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 );
779 if ( !($available_themes) ) {
781 // API is not available as of the moment, return..
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 );
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 ) ) {
796 // Step6->Let's get version of the local theme installed
797 $local_version = $theme_object->get( 'Version' );
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'];
804 // Step8->Let's compare the version
805 if ( version_compare( $repository_version, $local_version, '>' ) ) {
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 );
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
821 //Exited the upgrade loop for this specific theme repository
822 if ( !(empty($otgs_theme_updates_available)) ) {
824 update_option( $this->installer_themes_available_updates[$repo_slug], $otgs_theme_updates_available );
827 delete_option( $this->installer_themes_available_updates[$repo_slug] );
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 ) {
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
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' );
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 );
862 //Check for registration status
863 if ( isset($this->theme_user_registration[$frontend_tab_selected]) ) {
865 if ( !($this->theme_user_registration[$frontend_tab_selected]) ) {
868 if ( is_multisite() ) {
869 $admin_url_passed = network_admin_url();
871 $admin_url_passed = admin_url();
874 $registration_url = $admin_url_passed . 'plugin-install.php?tab=commercial#installer_repo_' . $frontend_tab_selected;
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 );
884 $response['output'] = $frontend_tab_selected;
885 echo json_encode( $response );
892 /** Installer loaded aux hooks */
893 public function installer_theme_loaded_hooks() {
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 );
905 /** Get themes by subscription type */
906 protected function installer_theme_get_themes_by_subscription( $subscription_type, $repo ) {
908 $themes_associated_with_subscription = array();
909 if ( isset(WP_Installer()->settings['repositories'][$repo]['data']['packages']) ) {
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;
932 return $themes_associated_with_subscription;
935 /** Filter API theme response according to user subscription */
936 public function installer_theme_filter_themes_by_subscription( $themes, $active_tab ) {
938 //Step1, we only filter OTGS themes
939 $orig = count( $themes );
940 if ( in_array( $active_tab, $this->theme_repo ) ) {
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
962 $new = count( $themes );
963 if ( $orig != $new ) {
965 $themes = array_values( $themes );
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' );
978 /** Instantiate Installer Theme Class */
979 new Installer_Theme_Class;