Advertisement
dgwatkins

sunrise.php

Jan 18th, 2018
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.52 KB | None | 0 0
  1. <?php
  2. /**
  3.  * WPML Sunrise Script - START
  4.  * @author OnTheGoSystems
  5.  * @version 3.7.0
  6.  *
  7.  * Place this script in the wp-content folder and add "define('SUNRISE', 'on');" in wp-config.php
  8.  * in order to enable using different domains for different languages in multisite mode
  9.  *
  10.  * Experimental feature
  11.  */
  12.  
  13. /**
  14.  * Class WPML_Sunrise_Lang_In_Domains
  15.  * @author OnTheGoSystems
  16.  */
  17. class WPML_Sunrise_Lang_In_Domains {
  18.  
  19.     /** @var  wpdb $wpdb */
  20.     private $wpdb;
  21.  
  22.     /** @var  string $table_prefix */
  23.     private $table_prefix;
  24.  
  25.     /** @var  string $current_blog */
  26.     private $current_blog;
  27.  
  28.     /** @var  bool $no_recursion */
  29.     private $no_recursion;
  30.  
  31.     /**
  32.      * Method init
  33.      */
  34.     public function init() {
  35.         if ( ! defined( 'WPML_SUNRISE_MULTISITE_DOMAINS' ) ) {
  36.             define( 'WPML_SUNRISE_MULTISITE_DOMAINS', true );
  37.         }
  38.  
  39.         add_filter( 'query', array( $this, 'query_filter' ) );
  40.     }
  41.  
  42.     /**
  43.      * @param string $q
  44.      *
  45.      * @return string
  46.      */
  47.     public function query_filter( $q ) {
  48.         $this->set_private_properties();
  49.  
  50.         if ( ! $this->current_blog && ! $this->no_recursion ) {
  51.  
  52.             $this->no_recursion = true;
  53.  
  54.             $domains = $this->extract_variables_from_query( $q, 'domain' );
  55.  
  56.             if ( $domains && $this->query_has_no_result( $q ) ) {
  57.                 $q = $this->transpose_query_if_one_domain_is_matching( $q, $domains );
  58.             }
  59.  
  60.             $this->no_recursion = false;
  61.         }
  62.  
  63.         return $q;
  64.     }
  65.  
  66.     /**
  67.      * method set_private_properties
  68.      */
  69.     private function set_private_properties() {
  70.         global $wpdb, $table_prefix, $current_blog;
  71.  
  72.         $this->wpdb = $wpdb;
  73.         $this->table_prefix = $table_prefix;
  74.         $this->current_blog = $current_blog;
  75.  
  76.     }
  77.  
  78.     /**
  79.      * @param string $query
  80.      *
  81.      * @return array
  82.      */
  83.     public function extract_variables_from_query( $query, $field ) {
  84.         $variables  = array();
  85.         $patterns = array(
  86.             '#WHERE\s+' . $field . '\s+IN\s*\(([^\)]+)\)#',
  87.             '#WHERE\s+' . $field . '\s*=\s*([^\s]+)#',
  88.             '#AND\s+' . $field . '\s+IN\s*\(([^\)]+)\)#',
  89.             '#AND\s+' . $field . '\s*=\s*([^\s]+)#',
  90.         );
  91.  
  92.         foreach ( $patterns as $pattern ) {
  93.             $found = preg_match( $pattern, $query, $matches );
  94.             if ( $found && array_key_exists( 1, $matches ) ) {
  95.                 $variables = $matches[1];
  96.                 $variables = preg_replace( '/\s+/', '', $variables );
  97.                 $variables = preg_replace( '/[\'"]/', '', $variables );
  98.                 $variables = explode( ',', $variables );
  99.                 break;
  100.             }
  101.         }
  102.  
  103.         return $variables;
  104.     }
  105.  
  106.     /**
  107.      * @param string $q
  108.      *
  109.      * @return bool
  110.      */
  111.     private function query_has_no_result( $q ) {
  112.         return ! (bool) $this->wpdb->get_row( $q );
  113.     }
  114.  
  115.     /**
  116.      * @param string $q
  117.      * @param array  $domains
  118.      *
  119.      * @return string
  120.      */
  121.     private function transpose_query_if_one_domain_is_matching( $q, $domains ) {
  122.         $paths         = $this->extract_variables_from_query( $q, 'path' );
  123.         $placeholders  = implode( ',', array_fill( 0, sizeof( $paths ), '%s' ) );
  124.         $paths[]       = BLOG_ID_CURRENT_SITE;
  125.  
  126.         // The order is to get the default site last in the results.
  127.         $blogs         = $this->wpdb->get_col( $this->wpdb->prepare(
  128.             "SELECT blog_id FROM {$this->wpdb->blogs} WHERE path IN ($placeholders) ORDER BY blog_id = %d",
  129.             $paths
  130.         ) );
  131.  
  132.         $found_blog_id = null;
  133.         foreach ( (array) $blogs as $blog_id ) {
  134.             $prefix = $this->table_prefix;
  135.  
  136.             if ( $blog_id > 1 ) {
  137.                 $prefix .= $blog_id . '_';
  138.             }
  139.  
  140.             $icl_settings = $this->wpdb->get_var( "SELECT option_value FROM {$prefix}options WHERE option_name = 'icl_sitepress_settings'" );
  141.  
  142.             if ( $icl_settings ) {
  143.                 $icl_settings = unserialize( $icl_settings );
  144.  
  145.                 if ( $icl_settings && 2 === (int) $icl_settings['language_negotiation_type'] ) {
  146.                     $found_blog_id = $this->get_blog_id_from_domain( $domains, $icl_settings, $blog_id );
  147.                     if ( $found_blog_id ) {
  148.                         $q = $this->wpdb->prepare( "SELECT blog_id FROM {$this->wpdb->blogs} WHERE blog_id = %d", $found_blog_id );
  149.                         break;
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.  
  155.         return $q;
  156.     }
  157.  
  158.     /**
  159.      * @param array $domains
  160.      * @param array $wpml_settings
  161.      * @param       $blog_id
  162.      *
  163.      * @return mixed
  164.      */
  165.     private function get_blog_id_from_domain( array $domains, array $wpml_settings, $blog_id ) {
  166.         foreach ( $domains as $domain ) {
  167.             if ( in_array( 'http://' . $domain, $wpml_settings['language_domains'], true ) ) {
  168.                 return $blog_id;
  169.             } elseif ( in_array( $domain, $wpml_settings['language_domains'], true ) ) {
  170.                 return $blog_id;
  171.             }
  172.         }
  173.  
  174.         return null;
  175.     }
  176. }
  177.  
  178. $wpml_sunrise_lang_in_domains = new WPML_Sunrise_Lang_In_Domains();
  179. $wpml_sunrise_lang_in_domains->init();
  180.  
  181. /**
  182.  * WPML Sunrise Script - END
  183.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement