| @@ -3,39 +3,83 @@ | ||
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | - * Frontend controller | |
| 8 | - * accessible as $polylang global object | |
| 7 | + * Main Polylang class when on frontend, accessible from @see PLL(). | |
| 9 | 8 | * |
| 10 | - * Properties: | |
| 11 | - * options => inherited, reference to Polylang options array | |
| 12 | - * model => inherited, reference to PLL_Model object | |
| 13 | - * links_model => inherited, reference to PLL_Links_Model object | |
| 14 | - * links => reference to PLL_Links object | |
| 15 | - * static_pages => reference to PLL_Frontend_Static_Pages object | |
| 16 | - * choose_lang => reference to PLL_Choose_Lang object | |
| 17 | - * curlang => current language | |
| 18 | - * filters => reference to PLL_Frontend_Filters object | |
| 19 | - * filters_links => reference to PLL_Frontend_Filters_Links object | |
| 20 | - * filters_search => reference to PLL_Frontend_Filters_Search object | |
| 21 | - * posts => reference to PLL_CRUD_Posts object | |
| 22 | - * terms => reference to PLL_CRUD_Terms object | |
| 23 | - * nav_menu => reference to PLL_Frontend_Nav_Menu object | |
| 24 | - * auto_translate => optional, reference to PLL_Auto_Translate object | |
| 25 | - * | |
| 26 | 9 | * @since 1.2 |
| 27 | 10 | */ |
| 28 | 11 | class PLL_Frontend extends PLL_Base { |
| 12 | + /** | |
| 13 | + * Current language. | |
| 14 | + * | |
| 15 | + * @var PLL_Language|null | |
| 16 | + */ | |
| 29 | 17 | public $curlang; |
| 30 | - public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate; | |
| 31 | 18 | |
| 32 | 19 | /** |
| 33 | - * Constructor | |
| 20 | + * @var PLL_Frontend_Auto_Translate|null | |
| 21 | + */ | |
| 22 | + public $auto_translate; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * The class selecting the current language. | |
| 34 | 26 | * |
| 27 | + * @var PLL_Choose_Lang|null | |
| 28 | + */ | |
| 29 | + public $choose_lang; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * @var PLL_Frontend_Filters|null | |
| 33 | + */ | |
| 34 | + public $filters; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * @var PLL_Frontend_Filters_Links|null | |
| 38 | + */ | |
| 39 | + public $filters_links; | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * @var PLL_Frontend_Filters_Search|null | |
| 43 | + */ | |
| 44 | + public $filters_search; | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * @var PLL_Frontend_Links|null | |
| 48 | + */ | |
| 49 | + public $links; | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * @var PLL_Default_Term|null | |
| 53 | + */ | |
| 54 | + public $default_term; | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * @var PLL_Frontend_Nav_Menu|null | |
| 58 | + */ | |
| 59 | + public $nav_menu; | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * @var PLL_Frontend_Static_Pages|null | |
| 63 | + */ | |
| 64 | + public $static_pages; | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * @var PLL_Frontend_Filters_Widgets|null | |
| 68 | + */ | |
| 69 | + public $filters_widgets; | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * @var PLL_Canonical | |
| 73 | + */ | |
| 74 | + public $canonical; | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Constructor. | |
| 78 | + * | |
| 35 | 79 | * @since 1.2 |
| 36 | 80 | * |
| 37 | - * @param object $links_model | |
| 81 | + * @param PLL_Links_Model $links_model Reference to the links model. | |
| 38 | 82 | */ |
| 39 | 83 | public function __construct( &$links_model ) { |
| 40 | 84 | parent::__construct( $links_model ); |
| 41 | 85 | |
| @@ -50,8 +94,22 @@ | ||
| 50 | 94 | // Not before 'check_canonical_url' |
| 51 | 95 | if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) { |
| 52 | 96 | add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 ); |
| 53 | 97 | } |
| 98 | + | |
| 99 | + add_action( 'admin_bar_menu', array( $this, 'remove_customize_admin_bar' ), 41 ); // After WP_Admin_Bar::add_menus | |
| 100 | + | |
| 101 | + /* | |
| 102 | + * Static front page and page for posts. | |
| 103 | + * | |
| 104 | + * Early instantiated to be able to correctly initialize language properties. | |
| 105 | + * Also loaded in customizer preview, directly reading the request as we act before WP. | |
| 106 | + */ | |
| 107 | + if ( 'page' === get_option( 'show_on_front' ) || ( isset( $_REQUEST['wp_customize'] ) && 'on' === $_REQUEST['wp_customize'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 108 | + $this->static_pages = new PLL_Frontend_Static_Pages( $this ); | |
| 109 | + } | |
| 110 | + | |
| 111 | + $this->model->set_languages_ready(); | |
| 54 | 112 | } |
| 55 | 113 | |
| 56 | 114 | /** |
| 57 | 115 | * Setups the language chooser based on options |
| @@ -62,12 +120,10 @@ | ||
| 62 | 120 | parent::init(); |
| 63 | 121 | |
| 64 | 122 | $this->links = new PLL_Frontend_Links( $this ); |
| 65 | 123 | |
| 66 | - // Static front page and page for posts | |
| 67 | - if ( 'page' === get_option( 'show_on_front' ) ) { | |
| 68 | - $this->static_pages = new PLL_Frontend_Static_Pages( $this ); | |
| 69 | - } | |
| 124 | + $this->default_term = new PLL_Default_Term( $this ); | |
| 125 | + $this->default_term->add_hooks(); | |
| 70 | 126 | |
| 71 | 127 | // Setup the language chooser |
| 72 | 128 | $c = array( 'Content', 'Url', 'Url', 'Domain' ); |
| 73 | 129 | $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ]; |
| @@ -81,8 +137,10 @@ | ||
| 81 | 137 | /** |
| 82 | 138 | * Setups filters and nav menus once the language has been defined |
| 83 | 139 | * |
| 84 | 140 | * @since 1.2 |
| 141 | + * | |
| 142 | + * @return void | |
| 85 | 143 | */ |
| 86 | 144 | public function pll_language_defined() { |
| 87 | 145 | // Filters |
| 88 | 146 | $this->filters_links = new PLL_Frontend_Filters_Links( $this ); |
| @@ -87,9 +145,18 @@ | ||
| 87 | 145 | // Filters |
| 88 | 146 | $this->filters_links = new PLL_Frontend_Filters_Links( $this ); |
| 89 | 147 | $this->filters = new PLL_Frontend_Filters( $this ); |
| 90 | 148 | $this->filters_search = new PLL_Frontend_Filters_Search( $this ); |
| 149 | + $this->filters_widgets = new PLL_Frontend_Filters_Widgets( $this ); | |
| 91 | 150 | |
| 151 | + /* | |
| 152 | + * Redirects to canonical url before WordPress redirect_canonical | |
| 153 | + * but after Nextgen Gallery which hacks $_SERVER['REQUEST_URI'] !!! | |
| 154 | + * and restores it in 'template_redirect' with priority 1. | |
| 155 | + */ | |
| 156 | + $this->canonical = new PLL_Canonical( $this ); | |
| 157 | + add_action( 'template_redirect', array( $this->canonical, 'check_canonical_url' ), 4 ); | |
| 158 | + | |
| 92 | 159 | // Auto translate for Ajax |
| 93 | 160 | if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) { |
| 94 | 161 | $this->auto_translate(); |
| 95 | 162 | } |
| @@ -95,13 +162,14 @@ | ||
| 95 | 162 | } |
| 96 | 163 | } |
| 97 | 164 | |
| 98 | 165 | /** |
| 99 | - * When querying multiple taxonomies, makes sure that the language is not the queried object | |
| 166 | + * When querying multiple taxonomies, makes sure that the language is not the queried object. | |
| 100 | 167 | * |
| 101 | 168 | * @since 1.8 |
| 102 | 169 | * |
| 103 | - * @param object $query WP_Query object | |
| 170 | + * @param WP_Query $query WP_Query object. | |
| 171 | + * @return void | |
| 104 | 172 | */ |
| 105 | 173 | public function parse_tax_query( $query ) { |
| 106 | 174 | $pll_query = new PLL_Query( $query, $this->model ); |
| 107 | 175 | $queried_taxonomies = $pll_query->get_queried_taxonomies(); |
| @@ -111,13 +179,14 @@ | ||
| 111 | 179 | } |
| 112 | 180 | } |
| 113 | 181 | |
| 114 | 182 | /** |
| 115 | - * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts | |
| 183 | + * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts. | |
| 116 | 184 | * |
| 117 | 185 | * @since 1.2 |
| 118 | 186 | * |
| 119 | - * @param object $query WP_Query object | |
| 187 | + * @param WP_Query $query WP_Query object. | |
| 188 | + * @return void | |
| 120 | 189 | */ |
| 121 | 190 | public function parse_query( $query ) { |
| 122 | 191 | $qv = $query->query_vars; |
| 123 | 192 | $pll_query = new PLL_Query( $query, $this->model ); |
| @@ -156,8 +225,10 @@ | ||
| 156 | 225 | /** |
| 157 | 226 | * Auto translate posts and terms ids |
| 158 | 227 | * |
| 159 | 228 | * @since 1.2 |
| 229 | + * | |
| 230 | + * @return void | |
| 160 | 231 | */ |
| 161 | 232 | public function auto_translate() { |
| 162 | 233 | $this->auto_translate = new PLL_Frontend_Auto_Translate( $this ); |
| 163 | 234 | } |
| @@ -162,31 +233,68 @@ | ||
| 162 | 233 | $this->auto_translate = new PLL_Frontend_Auto_Translate( $this ); |
| 163 | 234 | } |
| 164 | 235 | |
| 165 | 236 | /** |
| 166 | - * Resets some variables when switching blog | |
| 167 | - * Overrides parent method | |
| 237 | + * Resets some variables when the blog is switched. | |
| 238 | + * Overrides the parent method. | |
| 168 | 239 | * |
| 169 | 240 | * @since 1.5.1 |
| 170 | 241 | * |
| 171 | - * @param int $new_blog | |
| 172 | - * @param int $old_blog | |
| 242 | + * @param int $new_blog_id New blog ID. | |
| 243 | + * @param int $prev_blog_id Previous blog ID. | |
| 244 | + * @return void | |
| 173 | 245 | */ |
| 174 | - public function switch_blog( $new_blog, $old_blog ) { | |
| 175 | - // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages | |
| 176 | - if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) { | |
| 177 | - static $restore_curlang; | |
| 178 | - if ( empty( $restore_curlang ) ) { | |
| 179 | - $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs | |
| 180 | - } | |
| 246 | + public function switch_blog( $new_blog_id, $prev_blog_id ) { | |
| 247 | + if ( (int) $new_blog_id === (int) $prev_blog_id ) { | |
| 248 | + // Do nothing if same blog. | |
| 249 | + return; | |
| 250 | + } | |
| 181 | 251 | |
| 182 | - $lang = $this->model->get_language( $restore_curlang ); | |
| 183 | - $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] ); | |
| 252 | + parent::switch_blog( $new_blog_id, $prev_blog_id ); | |
| 184 | 253 | |
| 185 | - if ( isset( $this->static_pages ) ) { | |
| 186 | - $this->static_pages->init(); | |
| 187 | - } | |
| 254 | + // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages. | |
| 255 | + if ( ! $this->is_active_on_current_site() || ! $this->model->has_languages() || ! did_action( 'pll_language_defined' ) ) { | |
| 256 | + return; | |
| 257 | + } | |
| 188 | 258 | |
| 189 | - $this->load_strings_translations(); | |
| 259 | + static $restore_curlang; | |
| 260 | + | |
| 261 | + if ( empty( $restore_curlang ) ) { | |
| 262 | + $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs. | |
| 190 | 263 | } |
| 264 | + | |
| 265 | + $lang = $this->model->get_language( $restore_curlang ); | |
| 266 | + $this->curlang = $lang ?: $this->model->get_default_language(); | |
| 267 | + if ( empty( $this->curlang ) ) { | |
| 268 | + return; | |
| 269 | + } | |
| 270 | + | |
| 271 | + if ( isset( $this->static_pages ) ) { | |
| 272 | + $this->static_pages->init(); | |
| 273 | + } | |
| 274 | + | |
| 275 | + // Send the slug instead of the locale here to avoid conflicts with same locales. | |
| 276 | + $this->load_strings_translations( $this->curlang->slug ); | |
| 277 | + } | |
| 278 | + | |
| 279 | + /** | |
| 280 | + * Remove the customize admin bar on front-end when using a block theme. | |
| 281 | + * | |
| 282 | + * WordPress removes the Customizer menu if a block theme is activated and no other plugins interact with it. | |
| 283 | + * As Polylang interacts with the Customizer, we have to delete this menu ourselves in the case of a block theme, | |
| 284 | + * unless another plugin than Polylang interacts with the Customizer. | |
| 285 | + * | |
| 286 | + * @since 3.2 | |
| 287 | + * | |
| 288 | + * @return void | |
| 289 | + */ | |
| 290 | + public function remove_customize_admin_bar() { | |
| 291 | + if ( ! $this->should_customize_menu_be_removed() ) { | |
| 292 | + return; | |
| 293 | + } | |
| 294 | + | |
| 295 | + global $wp_admin_bar; | |
| 296 | + | |
| 297 | + remove_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' ); // To avoid the script launch. | |
| 298 | + $wp_admin_bar->remove_menu( 'customize' ); | |
| 191 | 299 | } |
| 192 | 300 | } |