PLL_Choose_Lang_Url

Choose the language when the language code is added to all urls The language is set in plugins_loaded with priority 1 as done by WPML Some actions have to be delayed to wait for $wp_rewrite availibility.

Defined (1)

The class is defined in the following location(s).

/frontend/choose-lang-url.php  
  1. class PLL_Choose_Lang_Url extends PLL_Choose_lang { 
  2. protected $index = 'index.php'; // need this before $wp_rewrite is created, also harcoded in wp-includes/rewrite.php 
  3.  
  4. /** 
  5. * sets the language 
  6. * @since 1.8 
  7. */ 
  8. public function init() { 
  9. parent::init(); 
  10.  
  11. if ( ! did_action( 'pll_language_defined' ) ) { 
  12. $this->set_language_from_url(); 
  13.  
  14. add_action( 'request', array( $this, 'request' ) ); 
  15.  
  16. /** 
  17. * finds the language according to information found in the url 
  18. * @since 1.2 
  19. */ 
  20. public function set_language_from_url() { 
  21. $host = str_replace( 'www.', '', parse_url( $this->links_model->home, PHP_URL_HOST ) ); 
  22. $home_path = parse_url( $this->links_model->home, PHP_URL_PATH ); 
  23.  
  24. $requested_host = str_replace( 'www.', '', $_SERVER['HTTP_HOST'] ); 
  25. $requested_uri = rtrim( str_replace( $this->index, '', $_SERVER['REQUEST_URI'] ), '/' ); // some PHP setups turn requests for / into /index.php in REQUEST_URI 
  26.  
  27. // home is resquested 
  28. if ( $requested_host == $host && $requested_uri == $home_path && empty( $_SERVER['QUERY_STRING'] ) ) { 
  29. $this->home_language(); 
  30. add_action( 'setup_theme', array( $this, 'home_requested' ) ); 
  31.  
  32. // take care to post & page preview http://wordpress.org/support/topic/static-frontpage-url-parameter-url-language-information 
  33. elseif ( isset( $_GET['preview'] ) && ( ( isset( $_GET['p'] ) && $id = (int) $_GET['p'] ) || ( isset( $_GET['page_id'] ) && $id = (int) $_GET['page_id'] ) ) ) { 
  34. $curlang = ( $lg = $this->model->post->get_language( $id ) ) ? $lg : $this->model->get_language( $this->options['default_lang'] ); 
  35.  
  36. // take care to ( unattached ) attachments 
  37. elseif ( isset( $_GET['attachment_id'] ) && $id = (int) $_GET['attachment_id'] ) { 
  38. $curlang = ( $lg = $this->model->post->get_language( $id ) ) ? $lg : $this->get_preferred_language(); 
  39.  
  40. elseif ( $slug = $this->links_model->get_language_from_url() ) { 
  41. $curlang = $this->model->get_language( $slug ); 
  42.  
  43. elseif ( $this->options['hide_default'] ) { 
  44. $curlang = $this->model->get_language( $this->options['default_lang'] ); 
  45.  
  46. // if no language found, check_language_code_in_url will attempt to find one and redirect to the correct url 
  47. // otherwise 404 will be fired in the preferred language 
  48. $this->set_language( empty( $curlang ) ? $this->get_preferred_language() : $curlang ); 
  49.  
  50.  
  51. /** 
  52. * adds the current language in query vars 
  53. * useful for subdomains and multiple domains 
  54. * @since 1.8 
  55. * @param array $qv main request query vars 
  56. * @return array modified query vars 
  57. */ 
  58. public function request( $qv ) { 
  59. // FIXME take care not to break untranslated content 
  60. // FIXME media ? 
  61.  
  62. // untranslated post types 
  63. if ( isset( $qv['post_type'] ) && ! $this->model->is_translated_post_type( $qv['post_type'] ) ) { 
  64. return $qv; 
  65.  
  66. // untranslated taxonomies 
  67. $tax_qv = array_filter( wp_list_pluck( get_taxonomies( array(), 'objects' ), 'query_var' ) ); // get all taxonomies query vars 
  68. $tax_qv = array_intersect( $tax_qv, array_keys( $qv ) ); // get all queried taxonomies query vars 
  69.  
  70. if ( ! $this->model->is_translated_taxonomy( array_keys( $tax_qv ) ) ) { 
  71. return $qv; 
  72.  
  73. if ( isset( $this->curlang ) && empty( $qv['lang'] ) ) { 
  74. $qv['lang'] = $this->curlang->slug; 
  75.  
  76. return $qv;