| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Frontend controller |
| 5 |
* accessible as $polylang global object |
| 6 |
* |
| 7 |
* Properties: |
| 8 |
* options => inherited, reference to Polylang options array |
| 9 |
* model => inherited, reference to PLL_Model object |
| 10 |
* links_model => inherited, reference to PLL_Links_Model object |
| 11 |
* links => reference to PLL_Links object |
| 12 |
* static_pages => reference to PLL_Frontend_Static_Pages object |
| 13 |
* filters_links => inherited, reference to PLL_Frontend_Filters_Links object |
| 14 |
* choose_lang => reference to PLL_Choose_lang object |
| 15 |
* curlang => current language |
| 16 |
* filters => reference to PLL_Filters object |
| 17 |
* filters_search => reference to PLL_Frontend_Filters_Search object |
| 18 |
* nav_menu => reference to PLL_Frontend_Nav_Menu object |
| 19 |
* auto_translate => optional, reference to PLL_Auto_Translate object |
| 20 |
* |
| 21 |
* @since 1.2 |
| 22 |
*/ |
| 23 |
class PLL_Frontend extends PLL_Base { |
| 24 |
public $curlang; |
| 25 |
public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate; |
| 26 |
|
| 27 |
/** |
| 28 |
* Constructor |
| 29 |
* |
| 30 |
* @since 1.2 |
| 31 |
* |
| 32 |
* @param object $links_model |
| 33 |
*/ |
| 34 |
public function __construct( &$links_model ) { |
| 35 |
parent::__construct( $links_model ); |
| 36 |
|
| 37 |
add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 ); |
| 38 |
|
| 39 |
// Avoids the language being the queried object when querying multiple taxonomies |
| 40 |
add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 ); |
| 41 |
|
| 42 |
// Filters posts by language |
| 43 |
add_action( 'parse_query', array( $this, 'parse_query' ), 6 ); |
| 44 |
|
| 45 |
// Not before 'check_canonical_url' |
| 46 |
if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) { |
| 47 |
add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Setups the language chooser based on options |
| 53 |
* |
| 54 |
* @since 1.2 |
| 55 |
*/ |
| 56 |
public function init() { |
| 57 |
$this->links = new PLL_Frontend_Links( $this ); |
| 58 |
|
| 59 |
// Don't set any language for REST requests |
| 60 |
if ( 0 === strpos( str_replace( 'index.php', '', $_SERVER['REQUEST_URI'] ), '/' . rest_get_url_prefix() . '/' ) ) { |
| 61 |
/** This action is documented in include/class-polylang.php */ |
| 62 |
do_action( 'pll_no_language_defined' ); |
| 63 |
} else { |
| 64 |
// Static front page and page for posts |
| 65 |
if ( 'page' === get_option( 'show_on_front' ) ) { |
| 66 |
$this->static_pages = new PLL_Frontend_Static_Pages( $this ); |
| 67 |
} |
| 68 |
|
| 69 |
// Setup the language chooser |
| 70 |
$c = array( 'Content', 'Url', 'Url', 'Domain' ); |
| 71 |
$class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ]; |
| 72 |
$this->choose_lang = new $class( $this ); |
| 73 |
$this->choose_lang->init(); |
| 74 |
|
| 75 |
// Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content |
| 76 |
$this->nav_menu = new PLL_Frontend_Nav_Menu( $this ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Setups filters and nav menus once the language has been defined |
| 82 |
* |
| 83 |
* @since 1.2 |
| 84 |
*/ |
| 85 |
public function pll_language_defined() { |
| 86 |
// Filters |
| 87 |
$this->filters_links = new PLL_Frontend_Filters_Links( $this ); |
| 88 |
$this->filters = new PLL_Frontend_Filters( $this ); |
| 89 |
$this->filters_search = new PLL_Frontend_Filters_Search( $this ); |
| 90 |
|
| 91 |
// Auto translate for Ajax |
| 92 |
if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) { |
| 93 |
$this->auto_translate(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* When querying multiple taxonomies, makes sure that the language is not the queried object |
| 99 |
* |
| 100 |
* @since 1.8 |
| 101 |
* |
| 102 |
* @param object $query WP_Query object |
| 103 |
*/ |
| 104 |
public function parse_tax_query( $query ) { |
| 105 |
$pll_query = new PLL_Query( $query, $this->model ); |
| 106 |
$queried_taxonomies = $pll_query->get_queried_taxonomies(); |
| 107 |
|
| 108 |
if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) { |
| 109 |
$query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* mMdifies some query vars to "hide" that the language is a taxonomy and avoid conflicts |
| 115 |
* |
| 116 |
* @since 1.2 |
| 117 |
* |
| 118 |
* @param object $query WP_Query object |
| 119 |
*/ |
| 120 |
public function parse_query( $query ) { |
| 121 |
$qv = $query->query_vars; |
| 122 |
$pll_query = new PLL_Query( $query, $this->model ); |
| 123 |
$taxonomies = $pll_query->get_queried_taxonomies(); |
| 124 |
|
| 125 |
// Allow filtering recent posts and secondary queries by the current language |
| 126 |
if ( ! empty( $this->curlang ) ) { |
| 127 |
$pll_query->filter_query( $this->curlang ); |
| 128 |
} |
| 129 |
|
| 130 |
// Modifies query vars when the language is queried |
| 131 |
if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) { |
| 132 |
// Do we query a custom taxonomy? |
| 133 |
$taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) ); |
| 134 |
|
| 135 |
// Remove pages query when the language is set unless we do a search |
| 136 |
// Take care not to break the single page, attachment and taxonomies queries! |
| 137 |
if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_page && ! $query->is_attachment && empty( $taxonomies ) ) { |
| 138 |
$query->set( 'post_type', 'post' ); |
| 139 |
} |
| 140 |
|
| 141 |
// Unset the is_archive flag for language pages to prevent loading the archive template |
| 142 |
// Keep archive flag for comment feed otherwise the language filter does not work |
| 143 |
if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) { |
| 144 |
$query->is_archive = false; |
| 145 |
} |
| 146 |
|
| 147 |
// Unset the is_tax flag except if another custom tax is queried |
| 148 |
if ( empty( $taxonomies ) && ( $query->is_category || $query->is_tag || $query->is_author || $query->is_post_type_archive || $query->is_date || $query->is_search || $query->is_feed ) ) { |
| 149 |
$query->is_tax = false; |
| 150 |
unset( $query->queried_object ); // FIXME useless? |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Auto translate posts and terms ids |
| 157 |
* |
| 158 |
* @since 1.2 |
| 159 |
*/ |
| 160 |
public function auto_translate() { |
| 161 |
$this->auto_translate = new PLL_Frontend_Auto_Translate( $this ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Resets some variables when switching blog |
| 166 |
* Overrides parent method |
| 167 |
* |
| 168 |
* @since 1.5.1 |
| 169 |
* |
| 170 |
* @param int $new_blog |
| 171 |
* @param int $old_blog |
| 172 |
*/ |
| 173 |
public function switch_blog( $new_blog, $old_blog ) { |
| 174 |
// Need to check that some languages are defined when user is logged in, has several blogs, some without any languages |
| 175 |
if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) { |
| 176 |
static $restore_curlang; |
| 177 |
if ( empty( $restore_curlang ) ) { |
| 178 |
$restore_curlang = $this->curlang->slug; // To always remember the current language through blogs |
| 179 |
} |
| 180 |
|
| 181 |
$lang = $this->model->get_language( $restore_curlang ); |
| 182 |
$this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] ); |
| 183 |
|
| 184 |
if ( isset( $this->static_pages ) ) { |
| 185 |
$this->static_pages->init(); |
| 186 |
} |
| 187 |
|
| 188 |
$this->load_strings_translations(); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|