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