| @@ -11,14 +11,14 @@ | ||
| 11 | 11 | class PLL_Frontend extends PLL_Base { |
| 12 | 12 | /** |
| 13 | 13 | * Current language. |
| 14 | 14 | * |
| 15 | - * @var PLL_Language | |
| 15 | + * @var PLL_Language|null | |
| 16 | 16 | */ |
| 17 | 17 | public $curlang; |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * @var PLL_Frontend_Auto_Translate | |
| 20 | + * @var PLL_Frontend_Auto_Translate|null | |
| 21 | 21 | */ |
| 22 | 22 | public $auto_translate; |
| 23 | 23 | |
| 24 | 24 | /** |
| @@ -23,43 +23,58 @@ | ||
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * The class selecting the current language. |
| 26 | 26 | * |
| 27 | - * @var PLL_Choose_Lang | |
| 27 | + * @var PLL_Choose_Lang|null | |
| 28 | 28 | */ |
| 29 | 29 | public $choose_lang; |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | - * @var PLL_Frontend_Filters | |
| 32 | + * @var PLL_Frontend_Filters|null | |
| 33 | 33 | */ |
| 34 | 34 | public $filters; |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | - * @var PLL_Frontend_Filters_Links | |
| 37 | + * @var PLL_Frontend_Filters_Links|null | |
| 38 | 38 | */ |
| 39 | 39 | public $filters_links; |
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | - * @var PLL_Frontend_Filters_Search | |
| 42 | + * @var PLL_Frontend_Filters_Search|null | |
| 43 | 43 | */ |
| 44 | 44 | public $filters_search; |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | - * @var PLL_Frontend_Links | |
| 47 | + * @var PLL_Frontend_Links|null | |
| 48 | 48 | */ |
| 49 | 49 | public $links; |
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | - * @var PLL_Frontend_Nav_Menu | |
| 52 | + * @var PLL_Default_Term|null | |
| 53 | 53 | */ |
| 54 | + public $default_term; | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * @var PLL_Frontend_Nav_Menu|null | |
| 58 | + */ | |
| 54 | 59 | public $nav_menu; |
| 55 | 60 | |
| 56 | 61 | /** |
| 57 | - * @var PLL_Frontend_Static_Pages | |
| 62 | + * @var PLL_Frontend_Static_Pages|null | |
| 58 | 63 | */ |
| 59 | 64 | public $static_pages; |
| 60 | 65 | |
| 61 | 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 | + /** | |
| 62 | 77 | * Constructor. |
| 63 | 78 | * |
| 64 | 79 | * @since 1.2 |
| 65 | 80 | * |
| @@ -79,8 +94,22 @@ | ||
| 79 | 94 | // Not before 'check_canonical_url' |
| 80 | 95 | if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) { |
| 81 | 96 | add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 ); |
| 82 | 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(); | |
| 83 | 112 | } |
| 84 | 113 | |
| 85 | 114 | /** |
| 86 | 115 | * Setups the language chooser based on options |
| @@ -91,12 +120,10 @@ | ||
| 91 | 120 | parent::init(); |
| 92 | 121 | |
| 93 | 122 | $this->links = new PLL_Frontend_Links( $this ); |
| 94 | 123 | |
| 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 | - } | |
| 124 | + $this->default_term = new PLL_Default_Term( $this ); | |
| 125 | + $this->default_term->add_hooks(); | |
| 99 | 126 | |
| 100 | 127 | // Setup the language chooser |
| 101 | 128 | $c = array( 'Content', 'Url', 'Url', 'Domain' ); |
| 102 | 129 | $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ]; |
| @@ -118,9 +145,18 @@ | ||
| 118 | 145 | // Filters |
| 119 | 146 | $this->filters_links = new PLL_Frontend_Filters_Links( $this ); |
| 120 | 147 | $this->filters = new PLL_Frontend_Filters( $this ); |
| 121 | 148 | $this->filters_search = new PLL_Frontend_Filters_Search( $this ); |
| 149 | + $this->filters_widgets = new PLL_Frontend_Filters_Widgets( $this ); | |
| 122 | 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 | + | |
| 123 | 159 | // Auto translate for Ajax |
| 124 | 160 | if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) { |
| 125 | 161 | $this->auto_translate(); |
| 126 | 162 | } |
| @@ -207,24 +243,58 @@ | ||
| 207 | 243 | * @param int $prev_blog_id Previous blog ID. |
| 208 | 244 | * @return void |
| 209 | 245 | */ |
| 210 | 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 | + } | |
| 251 | + | |
| 211 | 252 | parent::switch_blog( $new_blog_id, $prev_blog_id ); |
| 212 | 253 | |
| 213 | 254 | // 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 | - } | |
| 255 | + if ( ! $this->is_active_on_current_site() || ! $this->model->has_languages() || ! did_action( 'pll_language_defined' ) ) { | |
| 256 | + return; | |
| 257 | + } | |
| 219 | 258 | |
| 220 | - $lang = $this->model->get_language( $restore_curlang ); | |
| 221 | - $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] ); | |
| 259 | + static $restore_curlang; | |
| 222 | 260 | |
| 223 | - if ( isset( $this->static_pages ) ) { | |
| 224 | - $this->static_pages->init(); | |
| 225 | - } | |
| 261 | + if ( empty( $restore_curlang ) ) { | |
| 262 | + $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs. | |
| 263 | + } | |
| 226 | 264 | |
| 227 | - $this->load_strings_translations(); | |
| 265 | + $lang = $this->model->get_language( $restore_curlang ); | |
| 266 | + $this->curlang = $lang ?: $this->model->get_default_language(); | |
| 267 | + if ( empty( $this->curlang ) ) { | |
| 268 | + return; | |
| 228 | 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' ); | |
| 229 | 299 | } |
| 230 | 300 | } |