sunrise.php 2.13 KB
Newer Older
John Punzalan's avatar
John Punzalan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
<?php
  
  // WPML Sunrise Script - START
  // Version 1.0beta
  // Place this script in the wp-content folder and add "define('SUNRISE', 'on');" in wp-config.php n order to enable using different domains for different languages in multisite mode
  // 
  // Experimental feature
  define('WPML_SUNRISE_MULTISITE_DOMAINS', true);
  add_filter('query', 'sunrise_wpml_filter_queries');
  function sunrise_wpml_filter_queries($q){
      global $wpdb, $table_prefix, $current_blog;
        
      static $no_recursion;  
      
      if(empty($current_blog) && empty($no_recursion)){
          
          $no_recursion = true;      
          
          if(preg_match("#SELECT \\* FROM {$wpdb->blogs} WHERE domain = '(.*)'#", $q, $matches)){
              
              if(!$wpdb->get_row($q)){
                  $icl_blogs = $wpdb->get_col("SELECT blog_id FROM {$wpdb->blogs}");
                  foreach($icl_blogs as $blog_id){
                      $prefix = $blog_id > 1 ? $table_prefix . $blog_id . '_' : $table_prefix;                
                      $icl_settings = $wpdb->get_var("SELECT option_value FROM {$prefix}options WHERE option_name='icl_sitepress_settings'");
                      if($icl_settings){
                          $icl_settings = unserialize($icl_settings);                                                                        
                          if($icl_settings && $icl_settings['language_negotiation_type'] == 2){
                              if(in_array('http://' . $matches[1], $icl_settings['language_domains'])){
                                  $found_blog_id = $blog_id;
                                  break;
                              }
                          }
                      }
                  }
                
                  if ( isset( $found_blog_id ) && $found_blog_id ) {
                      $q = $wpdb->prepare("SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d ", $found_blog_id );
                  }   
              }
                          
          }
          
          $no_recursion = false;      
          
      }
      
      
      return $q;
  }
  // WPML Sunrise Script - END
  
?>