| 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 |
* choose_lang => reference to PLL_Choose_Lang object |
| 14 |
* curlang => current language |
| 15 |
* filters => reference to PLL_Frontend_Filters object |
| 16 |
* filters_links => reference to PLL_Frontend_Filters_Links object |
| 17 |
* filters_search => reference to PLL_Frontend_Filters_Search object |
| 18 |
* posts => reference to PLL_CRUD_Posts object |
| 19 |
* terms => reference to PLL_CRUD_Terms object |
| 20 |
* nav_menu => reference to PLL_Frontend_Nav_Menu object |
| 21 |
* sync => reference to PLL_Sync object |
| 22 |
* auto_translate => optional, reference to PLL_Auto_Translate object |
| 23 |
* |
| 24 |
* @since 1.2 |
| 25 |
*/ |
| 26 |
class PLL_Frontend extends PLL_Base { |
| 27 |
public $curlang; |
| 28 |
public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
* |
| 33 |
* @since 1.2 |
| 34 |
* |
| 35 |
* @param object $links_model |
| 36 |
*/ |
| 37 |
public function __construct( &$links_model ) { |
| 38 |
parent::__construct( $links_model ); |
| 39 |
|
| 40 |
add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 ); |
| 41 |
|
| 42 |
// Avoids the language being the queried object when querying multiple taxonomies |
| 43 |
add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 ); |
| 44 |
|
| 45 |
// Filters posts by language |
| 46 |
add_action( 'parse_query', array( $this, 'parse_query' ), 6 ); |
| 47 |
|
| 48 |
// Not before 'check_canonical_url' |
| 49 |
if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) { |
| 50 |
add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Setups the language chooser based on options |
| 56 |
* |
| 57 |
* @since 1.2 |
| 58 |
*/ |
| 59 |
public function init() { |
| 60 |
parent::init(); |
| 61 |
|
| 62 |
$this->links = new PLL_Frontend_Links( $this ); |
| 63 |
|
| 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 |
// Cross domain |
| 79 |
if ( PLL_COOKIE ) { |
| 80 |
$class = array( 2 => 'PLL_Xdata_Subdomain', 3 => 'PLL_Xdata_Domain' ); |
| 81 |
if ( isset( $class[ $this->options['force_lang'] ] ) && class_exists( $class[ $this->options['force_lang'] ] ) ) { |
| 82 |
$this->xdata = new $class[ $this->options['force_lang'] ]( $this ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ( get_option( 'permalink_structure' ) ) { |
| 87 |
// Translate slugs |
| 88 |
if ( class_exists( 'PLL_Frontend_Translate_Slugs' ) ) { |
| 89 |
$slugs_model = new PLL_Translate_Slugs_Model( $this ); |
| 90 |
$this->translate_slugs = new PLL_Frontend_Translate_Slugs( $slugs_model, $this->curlang ); |
| 91 |
} |
| 92 |
|
| 93 |
// Share term slugs |
| 94 |
if ( $this->options['force_lang'] && class_exists( 'PLL_Share_Term_Slug' ) ) { |
| 95 |
$this->share_term_slug = new PLL_Share_Term_Slug( $this ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if ( class_exists( 'PLL_Sync_Post_Model' ) ) { |
| 100 |
$this->sync_post_model = new PLL_Sync_Post_Model( $this ); |
| 101 |
} |
| 102 |
|
| 103 |
if ( class_exists( 'PLL_Sync_Post' ) ) { |
| 104 |
$this->sync_post = new PLL_Sync_Post( $this ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Setups filters and nav menus once the language has been defined |
| 110 |
* |
| 111 |
* @since 1.2 |
| 112 |
*/ |
| 113 |
public function pll_language_defined() { |
| 114 |
// Filters |
| 115 |
$this->filters_links = new PLL_Frontend_Filters_Links( $this ); |
| 116 |
$this->filters = new PLL_Frontend_Filters( $this ); |
| 117 |
$this->filters_search = new PLL_Frontend_Filters_Search( $this ); |
| 118 |
$this->posts = new PLL_CRUD_Posts( $this ); |
| 119 |
$this->terms = new PLL_CRUD_Terms( $this ); |
| 120 |
|
| 121 |
$this->sync = new PLL_Sync( $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 object $query WP_Query object |
| 135 |
*/ |
| 136 |
public function parse_tax_query( $query ) { |
| 137 |
$pll_query = new PLL_Query( $query, $this->model ); |
| 138 |
$queried_taxonomies = $pll_query->get_queried_taxonomies(); |
| 139 |
|
| 140 |
if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) { |
| 141 |
$query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts |
| 147 |
* |
| 148 |
* @since 1.2 |
| 149 |
* |
| 150 |
* @param object $query WP_Query object |
| 151 |
*/ |
| 152 |
public function parse_query( $query ) { |
| 153 |
$qv = $query->query_vars; |
| 154 |
$pll_query = new PLL_Query( $query, $this->model ); |
| 155 |
$taxonomies = $pll_query->get_queried_taxonomies(); |
| 156 |
|
| 157 |
// Allow filtering recent posts and secondary queries by the current language |
| 158 |
if ( ! empty( $this->curlang ) ) { |
| 159 |
$pll_query->filter_query( $this->curlang ); |
| 160 |
} |
| 161 |
|
| 162 |
// Modifies query vars when the language is queried |
| 163 |
if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) { |
| 164 |
// Do we query a custom taxonomy? |
| 165 |
$taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) ); |
| 166 |
|
| 167 |
// Remove pages query when the language is set unless we do a search |
| 168 |
// Take care not to break the single page, attachment and taxonomies queries! |
| 169 |
if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) && ! $query->is_category && ! $query->is_tag ) { |
| 170 |
$query->set( 'post_type', 'post' ); |
| 171 |
} |
| 172 |
|
| 173 |
// Unset the is_archive flag for language pages to prevent loading the archive template |
| 174 |
// Keep archive flag for comment feed otherwise the language filter does not work |
| 175 |
if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) { |
| 176 |
$query->is_archive = false; |
| 177 |
} |
| 178 |
|
| 179 |
// Unset the is_tax flag except if another custom tax is queried |
| 180 |
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 ) ) { |
| 181 |
$query->is_tax = false; |
| 182 |
unset( $query->queried_object ); // FIXME useless? |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Auto translate posts and terms ids |
| 189 |
* |
| 190 |
* @since 1.2 |
| 191 |
*/ |
| 192 |
public function auto_translate() { |
| 193 |
$this->auto_translate = new PLL_Frontend_Auto_Translate( $this ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Resets some variables when switching blog |
| 198 |
* Overrides parent method |
| 199 |
* |
| 200 |
* @since 1.5.1 |
| 201 |
* |
| 202 |
* @param int $new_blog |
| 203 |
* @param int $old_blog |
| 204 |
*/ |
| 205 |
public function switch_blog( $new_blog, $old_blog ) { |
| 206 |
// Need to check that some languages are defined when user is logged in, has several blogs, some without any languages |
| 207 |
if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) { |
| 208 |
static $restore_curlang; |
| 209 |
if ( empty( $restore_curlang ) ) { |
| 210 |
$restore_curlang = $this->curlang->slug; // To always remember the current language through blogs |
| 211 |
} |
| 212 |
|
| 213 |
$lang = $this->model->get_language( $restore_curlang ); |
| 214 |
$this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] ); |
| 215 |
|
| 216 |
if ( isset( $this->static_pages ) ) { |
| 217 |
$this->static_pages->init(); |
| 218 |
} |
| 219 |
|
| 220 |
$this->load_strings_translations(); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|