ldc-sbv/pole-sbv-content/plugins/polylang/frontend/frontend.php

192 lines
6.7 KiB
PHP
Raw Normal View History

2017-11-17 16:22:31 +01:00
<?php
/**
2018-04-16 15:29:38 +02:00
* Frontend controller
2017-11-17 16:22:31 +01:00
* accessible as $polylang global object
*
2018-04-16 15:29:38 +02:00
* Properties:
2017-11-17 16:22:31 +01:00
* options => inherited, reference to Polylang options array
* model => inherited, reference to PLL_Model object
* links_model => inherited, reference to PLL_Links_Model object
* links => reference to PLL_Links object
* static_pages => reference to PLL_Frontend_Static_Pages object
* filters_links => inherited, reference to PLL_Frontend_Filters_Links object
* choose_lang => reference to PLL_Choose_lang object
* curlang => current language
* filters => reference to PLL_Filters object
* filters_search => reference to PLL_Frontend_Filters_Search object
* nav_menu => reference to PLL_Frontend_Nav_Menu object
* auto_translate => optional, reference to PLL_Auto_Translate object
*
* @since 1.2
*/
class PLL_Frontend extends PLL_Base {
public $curlang;
public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate;
/**
2018-04-16 15:29:38 +02:00
* Constructor
2017-11-17 16:22:31 +01:00
*
* @since 1.2
*
* @param object $links_model
*/
public function __construct( &$links_model ) {
parent::__construct( $links_model );
add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 );
2018-04-16 15:29:38 +02:00
// Avoids the language being the queried object when querying multiple taxonomies
2017-11-17 16:22:31 +01:00
add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 );
2018-04-16 15:29:38 +02:00
// Filters posts by language
2017-11-17 16:22:31 +01:00
add_action( 'parse_query', array( $this, 'parse_query' ), 6 );
2018-04-16 15:29:38 +02:00
// Not before 'check_canonical_url'
2017-11-17 16:22:31 +01:00
if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) {
add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 );
}
}
/**
2018-04-16 15:29:38 +02:00
* Setups the language chooser based on options
2017-11-17 16:22:31 +01:00
*
* @since 1.2
*/
public function init() {
$this->links = new PLL_Frontend_Links( $this );
2018-04-16 15:29:38 +02:00
// Don't set any language for REST requests when Polylang Pro is not active
if ( ! class_exists( 'PLL_REST_Translated_Object' ) && 0 === strpos( str_replace( 'index.php', '', $_SERVER['REQUEST_URI'] ), '/' . rest_get_url_prefix() . '/' ) ) {
/** This action is documented in include/class-polylang.php */
do_action( 'pll_no_language_defined' );
} else {
// Static front page and page for posts
if ( 'page' === get_option( 'show_on_front' ) ) {
$this->static_pages = new PLL_Frontend_Static_Pages( $this );
}
2017-11-17 16:22:31 +01:00
2018-04-16 15:29:38 +02:00
// Setup the language chooser
$c = array( 'Content', 'Url', 'Url', 'Domain' );
$class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ];
$this->choose_lang = new $class( $this );
$this->choose_lang->init();
2017-11-17 16:22:31 +01:00
2018-04-16 15:29:38 +02:00
// Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content
$this->nav_menu = new PLL_Frontend_Nav_Menu( $this );
}
2017-11-17 16:22:31 +01:00
}
/**
2018-04-16 15:29:38 +02:00
* Setups filters and nav menus once the language has been defined
2017-11-17 16:22:31 +01:00
*
* @since 1.2
*/
public function pll_language_defined() {
2018-04-16 15:29:38 +02:00
// Filters
2017-11-17 16:22:31 +01:00
$this->filters_links = new PLL_Frontend_Filters_Links( $this );
$this->filters = new PLL_Frontend_Filters( $this );
$this->filters_search = new PLL_Frontend_Filters_Search( $this );
// Auto translate for Ajax
if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) {
$this->auto_translate();
}
}
/**
2018-04-16 15:29:38 +02:00
* When querying multiple taxonomies, makes sure that the language is not the queried object
2017-11-17 16:22:31 +01:00
*
* @since 1.8
*
* @param object $query WP_Query object
*/
public function parse_tax_query( $query ) {
$pll_query = new PLL_Query( $query, $this->model );
$queried_taxonomies = $pll_query->get_queried_taxonomies();
if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) {
$query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms );
}
}
/**
2018-04-16 15:29:38 +02:00
* mMdifies some query vars to "hide" that the language is a taxonomy and avoid conflicts
2017-11-17 16:22:31 +01:00
*
* @since 1.2
*
* @param object $query WP_Query object
*/
public function parse_query( $query ) {
$qv = $query->query_vars;
$pll_query = new PLL_Query( $query, $this->model );
$taxonomies = $pll_query->get_queried_taxonomies();
// Allow filtering recent posts and secondary queries by the current language
if ( ! empty( $this->curlang ) ) {
$pll_query->filter_query( $this->curlang );
}
2018-04-16 15:29:38 +02:00
// Modifies query vars when the language is queried
2017-11-17 16:22:31 +01:00
if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) {
2018-04-16 15:29:38 +02:00
// Do we query a custom taxonomy?
2017-12-11 16:34:19 +01:00
$taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) );
2017-11-17 16:22:31 +01:00
2018-04-16 15:29:38 +02:00
// Remove pages query when the language is set unless we do a search
// Take care not to break the single page, attachment and taxonomies queries!
2017-11-17 16:22:31 +01:00
if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_page && ! $query->is_attachment && empty( $taxonomies ) ) {
$query->set( 'post_type', 'post' );
}
2018-04-16 15:29:38 +02:00
// Unset the is_archive flag for language pages to prevent loading the archive template
// Keep archive flag for comment feed otherwise the language filter does not work
2017-11-17 16:22:31 +01:00
if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) {
$query->is_archive = false;
}
2018-04-16 15:29:38 +02:00
// Unset the is_tax flag except if another custom tax is queried
2017-12-11 16:34:19 +01:00
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 ) ) {
2017-11-17 16:22:31 +01:00
$query->is_tax = false;
unset( $query->queried_object ); // FIXME useless?
}
}
}
/**
2018-04-16 15:29:38 +02:00
* Auto translate posts and terms ids
2017-11-17 16:22:31 +01:00
*
* @since 1.2
*/
public function auto_translate() {
$this->auto_translate = new PLL_Frontend_Auto_Translate( $this );
}
/**
2018-04-16 15:29:38 +02:00
* Resets some variables when switching blog
* Overrides parent method
2017-11-17 16:22:31 +01:00
*
* @since 1.5.1
*
* @param int $new_blog
* @param int $old_blog
*/
public function switch_blog( $new_blog, $old_blog ) {
2018-04-16 15:29:38 +02:00
// Need to check that some languages are defined when user is logged in, has several blogs, some without any languages
2017-11-17 16:22:31 +01:00
if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
static $restore_curlang;
if ( empty( $restore_curlang ) ) {
2018-04-16 15:29:38 +02:00
$restore_curlang = $this->curlang->slug; // To always remember the current language through blogs
2017-11-17 16:22:31 +01:00
}
$lang = $this->model->get_language( $restore_curlang );
$this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );
if ( isset( $this->static_pages ) ) {
$this->static_pages->init();
}
$this->load_strings_translations();
}
}
}