| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | * Base class for both admin and frontend |
| 8 | 8 | * |
| 9 | 9 | * @since 1.2 |
| 10 | 10 | */ |
| 11 | +#[AllowDynamicProperties] | |
| 11 | 12 | abstract class PLL_Base { |
| 12 | 13 | /** |
| 13 | 14 | * Stores the plugin options. |
| 14 | 15 | * |
| @@ -30,9 +31,9 @@ | ||
| 30 | 31 | |
| 31 | 32 | /** |
| 32 | 33 | * Registers hooks on insert / update post related actions and filters. |
| 33 | 34 | * |
| 34 | - * @var PLL_CRUD_Posts | |
| 35 | + * @var PLL_CRUD_Posts|null | |
| 35 | 36 | */ |
| 36 | 37 | public $posts; |
| 37 | 38 | |
| 38 | 39 | /** |
| @@ -37,9 +38,9 @@ | ||
| 37 | 38 | |
| 38 | 39 | /** |
| 39 | 40 | * Registers hooks on insert / update term related action and filters. |
| 40 | 41 | * |
| 41 | - * @var PLL_CRUD_Terms | |
| 42 | + * @var PLL_CRUD_Terms|null | |
| 42 | 43 | */ |
| 43 | 44 | public $terms; |
| 44 | 45 | |
| 45 | 46 | /** |
| @@ -61,9 +62,9 @@ | ||
| 61 | 62 | // User defined strings translations |
| 62 | 63 | add_action( 'pll_language_defined', array( $this, 'load_strings_translations' ), 5 ); |
| 63 | 64 | add_action( 'change_locale', array( $this, 'load_strings_translations' ) ); // Since WP 4.7 |
| 64 | 65 | add_action( 'personal_options_update', array( $this, 'load_strings_translations' ), 1, 0 ); // Before WP, for confirmation request when changing the user email. |
| 65 | - | |
| 66 | + add_action( 'lostpassword_post', array( $this, 'load_strings_translations' ), 10, 0 ); // Password reset email. | |
| 66 | 67 | // Switch_to_blog |
| 67 | 68 | add_action( 'switch_blog', array( $this, 'switch_blog' ), 10, 2 ); |
| 68 | 69 | } |
| 69 | 70 | |
| @@ -75,9 +76,9 @@ | ||
| 75 | 76 | * |
| 76 | 77 | * @return void |
| 77 | 78 | */ |
| 78 | 79 | public function init() { |
| 79 | - if ( $this->model->get_languages_list() ) { | |
| 80 | + if ( $this->model->has_languages() ) { | |
| 80 | 81 | $this->posts = new PLL_CRUD_Posts( $this ); |
| 81 | 82 | $this->terms = new PLL_CRUD_Terms( $this ); |
| 82 | 83 | |
| 83 | 84 | // WordPress options. |
| @@ -110,9 +111,9 @@ | ||
| 110 | 111 | * |
| 111 | 112 | * @since 1.2 |
| 112 | 113 | * @since 2.1.3 $locale parameter added. |
| 113 | 114 | * |
| 114 | - * @param string $locale Locale. Defaults to current locale. | |
| 115 | + * @param string $locale Language locale or slug. Defaults to current locale. | |
| 115 | 116 | * @return void |
| 116 | 117 | */ |
| 117 | 118 | public function load_strings_translations( $locale = '' ) { |
| 118 | 119 | if ( empty( $locale ) ) { |
| @@ -140,31 +141,78 @@ | ||
| 140 | 141 | * @param int $prev_blog_id Previous blog ID. |
| 141 | 142 | * @return void |
| 142 | 143 | */ |
| 143 | 144 | public function switch_blog( $new_blog_id, $prev_blog_id ) { |
| 144 | - if ( $this->is_active_on_new_blog( $new_blog_id, $prev_blog_id ) ) { | |
| 145 | - $this->options = get_option( 'polylang' ); // Needed for menus. | |
| 146 | - remove_action( 'pre_option_rewrite_rules', array( $this->links_model, 'prepare_rewrite_rules' ) ); | |
| 145 | + if ( (int) $new_blog_id === (int) $prev_blog_id ) { | |
| 146 | + // Do nothing if same blog. | |
| 147 | + return; | |
| 148 | + } | |
| 149 | + | |
| 150 | + $this->links_model->remove_filters(); | |
| 151 | + | |
| 152 | + if ( $this->is_active_on_current_site() ) { | |
| 147 | 153 | $this->links_model = $this->model->get_links_model(); |
| 148 | 154 | } |
| 149 | 155 | } |
| 150 | 156 | |
| 151 | 157 | /** |
| 152 | - * Checks if Polylang is active on the new blog when the blog is switched. | |
| 158 | + * Checks if Polylang is active on the current blog (useful when the blog is switched). | |
| 153 | 159 | * |
| 154 | - * @since 3.0 | |
| 160 | + * @since 3.5.2 | |
| 155 | 161 | * |
| 156 | - * @param int $new_blog_id New blog ID. | |
| 157 | - * @param int $prev_blog_id Previous blog ID. | |
| 158 | 162 | * @return bool |
| 159 | 163 | */ |
| 160 | - protected function is_active_on_new_blog( $new_blog_id, $prev_blog_id ) { | |
| 161 | - $plugins = ( $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ) ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array(); | |
| 162 | - $plugins = array_merge( $plugins, get_option( 'active_plugins', array() ) ); | |
| 164 | + protected function is_active_on_current_site(): bool { | |
| 165 | + return pll_is_plugin_active( POLYLANG_BASENAME ) && ! empty( $this->options['version'] ); | |
| 166 | + } | |
| 163 | 167 | |
| 168 | + /** | |
| 169 | + * Check if the customize menu should be removed or not. | |
| 170 | + * | |
| 171 | + * @since 3.2 | |
| 172 | + * | |
| 173 | + * @return bool True if it should be removed, false otherwise. | |
| 174 | + */ | |
| 175 | + public function should_customize_menu_be_removed() { | |
| 176 | + // Exit if a block theme isn't activated. | |
| 177 | + if ( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) { | |
| 178 | + return false; | |
| 179 | + } | |
| 180 | + | |
| 181 | + return ! $this->is_customize_register_hooked(); | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * Tells whether or not Polylang or third party callbacks are hooked to `customize_register`. | |
| 186 | + * | |
| 187 | + * @since 3.4.3 | |
| 188 | + * | |
| 189 | + * @global $wp_filter | |
| 190 | + * | |
| 191 | + * @return bool True if Polylang's callbacks are hooked, false otherwise. | |
| 192 | + */ | |
| 193 | + protected function is_customize_register_hooked() { | |
| 194 | + global $wp_filter; | |
| 195 | + | |
| 196 | + if ( empty( $wp_filter['customize_register'] ) || ! $wp_filter['customize_register'] instanceof WP_Hook ) { | |
| 197 | + return false; | |
| 198 | + } | |
| 199 | + | |
| 164 | 200 | /* |
| 165 | - * The 2nd test is needed when Polylang is not networked activated. | |
| 166 | - * The 3rd test is needed when Polylang is networked activated and a new site is created. | |
| 201 | + * 'customize_register' is hooked by: | |
| 202 | + * @see PLL_Nav_Menu::create_nav_menu_locations() | |
| 203 | + * @see PLL_Frontend_Static_Pages::filter_customizer() | |
| 167 | 204 | */ |
| 168 | - return $new_blog_id !== $prev_blog_id && in_array( POLYLANG_BASENAME, $plugins ) && get_option( 'polylang' ); | |
| 205 | + $floor = 0; | |
| 206 | + if ( ! empty( $this->nav_menu ) && (bool) $wp_filter['customize_register']->has_filter( 'customize_register', array( $this->nav_menu, 'create_nav_menu_locations' ) ) ) { | |
| 207 | + ++$floor; | |
| 208 | + } | |
| 209 | + | |
| 210 | + if ( ! empty( $this->static_pages ) && (bool) $wp_filter['customize_register']->has_filter( 'customize_register', array( $this->static_pages, 'filter_customizer' ) ) ) { | |
| 211 | + ++$floor; | |
| 212 | + } | |
| 213 | + | |
| 214 | + $count = array_sum( array_map( 'count', $wp_filter['customize_register']->callbacks ) ); | |
| 215 | + | |
| 216 | + return $count > $floor; | |
| 169 | 217 | } |
| 170 | 218 | } |