| @@ -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 | * |
| @@ -16,10 +17,8 @@ | ||
| 16 | 17 | */ |
| 17 | 18 | public $options; |
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | - * Instance of PLL_Model. | |
| 21 | - * | |
| 22 | 21 | * @var PLL_Model |
| 23 | 22 | */ |
| 24 | 23 | public $model; |
| 25 | 24 | |
| @@ -32,9 +31,9 @@ | ||
| 32 | 31 | |
| 33 | 32 | /** |
| 34 | 33 | * Registers hooks on insert / update post related actions and filters. |
| 35 | 34 | * |
| 36 | - * @var PLL_CRUD_Posts | |
| 35 | + * @var PLL_CRUD_Posts|null | |
| 37 | 36 | */ |
| 38 | 37 | public $posts; |
| 39 | 38 | |
| 40 | 39 | /** |
| @@ -39,18 +38,18 @@ | ||
| 39 | 38 | |
| 40 | 39 | /** |
| 41 | 40 | * Registers hooks on insert / update term related action and filters. |
| 42 | 41 | * |
| 43 | - * @var PLL_CRUD_Terms | |
| 42 | + * @var PLL_CRUD_Terms|null | |
| 44 | 43 | */ |
| 45 | 44 | public $terms; |
| 46 | 45 | |
| 47 | 46 | /** |
| 48 | - * Constructor | |
| 47 | + * Constructor. | |
| 49 | 48 | * |
| 50 | 49 | * @since 1.2 |
| 51 | 50 | * |
| 52 | - * @param object $links_model | |
| 51 | + * @param PLL_Links_Model $links_model Links Model. | |
| 53 | 52 | */ |
| 54 | 53 | public function __construct( &$links_model ) { |
| 55 | 54 | $this->links_model = &$links_model; |
| 56 | 55 | $this->model = &$links_model->model; |
| @@ -62,9 +61,10 @@ | ||
| 62 | 61 | |
| 63 | 62 | // User defined strings translations |
| 64 | 63 | add_action( 'pll_language_defined', array( $this, 'load_strings_translations' ), 5 ); |
| 65 | 64 | add_action( 'change_locale', array( $this, 'load_strings_translations' ) ); // Since WP 4.7 |
| 66 | - | |
| 65 | + add_action( 'personal_options_update', array( $this, 'load_strings_translations' ), 1, 0 ); // Before WP, for confirmation request when changing the user email. | |
| 66 | + add_action( 'lostpassword_post', array( $this, 'load_strings_translations' ), 10, 0 ); // Password reset email. | |
| 67 | 67 | // Switch_to_blog |
| 68 | 68 | add_action( 'switch_blog', array( $this, 'switch_blog' ), 10, 2 ); |
| 69 | 69 | } |
| 70 | 70 | |
| @@ -72,19 +72,21 @@ | ||
| 72 | 72 | * Instantiates classes reacting to CRUD operations on posts and terms, |
| 73 | 73 | * only when at least one language is defined. |
| 74 | 74 | * |
| 75 | 75 | * @since 2.6 |
| 76 | + * | |
| 77 | + * @return void | |
| 76 | 78 | */ |
| 77 | 79 | public function init() { |
| 78 | - if ( $this->model->get_languages_list() ) { | |
| 80 | + if ( $this->model->has_languages() ) { | |
| 79 | 81 | $this->posts = new PLL_CRUD_Posts( $this ); |
| 80 | 82 | $this->terms = new PLL_CRUD_Terms( $this ); |
| 81 | 83 | |
| 82 | 84 | // WordPress options. |
| 83 | - new PLL_Translate_Option( 'blogname', array(), array( 'context' => 'WorPress' ) ); | |
| 84 | - new PLL_Translate_Option( 'blogdescription', array(), array( 'context' => 'WorPress' ) ); | |
| 85 | - new PLL_Translate_Option( 'date_format', array(), array( 'context' => 'WorPress' ) ); | |
| 86 | - new PLL_Translate_Option( 'time_format', array(), array( 'context' => 'WorPress' ) ); | |
| 85 | + new PLL_Translate_Option( 'blogname', array(), array( 'context' => 'WordPress' ) ); | |
| 86 | + new PLL_Translate_Option( 'blogdescription', array(), array( 'context' => 'WordPress' ) ); | |
| 87 | + new PLL_Translate_Option( 'date_format', array(), array( 'context' => 'WordPress' ) ); | |
| 88 | + new PLL_Translate_Option( 'time_format', array(), array( 'context' => 'WordPress' ) ); | |
| 87 | 89 | } |
| 88 | 90 | } |
| 89 | 91 | |
| 90 | 92 | /** |
| @@ -90,8 +92,10 @@ | ||
| 90 | 92 | /** |
| 91 | 93 | * Registers our widgets |
| 92 | 94 | * |
| 93 | 95 | * @since 0.1 |
| 96 | + * | |
| 97 | + * @return void | |
| 94 | 98 | */ |
| 95 | 99 | public function widgets_init() { |
| 96 | 100 | register_widget( 'PLL_Widget_Languages' ); |
| 97 | 101 | |
| @@ -107,13 +111,14 @@ | ||
| 107 | 111 | * |
| 108 | 112 | * @since 1.2 |
| 109 | 113 | * @since 2.1.3 $locale parameter added. |
| 110 | 114 | * |
| 111 | - * @param string $locale Locale. Defaults to current locale. | |
| 115 | + * @param string $locale Language locale or slug. Defaults to current locale. | |
| 116 | + * @return void | |
| 112 | 117 | */ |
| 113 | 118 | public function load_strings_translations( $locale = '' ) { |
| 114 | 119 | if ( empty( $locale ) ) { |
| 115 | - $locale = get_locale(); | |
| 120 | + $locale = ( is_admin() && ! Polylang::is_ajax_on_front() ) ? get_user_locale() : get_locale(); | |
| 116 | 121 | } |
| 117 | 122 | |
| 118 | 123 | $language = $this->model->get_language( $locale ); |
| 119 | 124 | |
| @@ -126,70 +131,89 @@ | ||
| 126 | 131 | } |
| 127 | 132 | } |
| 128 | 133 | |
| 129 | 134 | /** |
| 130 | - * Resets some variables when switching blog | |
| 131 | - * Applies only if Polylang is active on the new blog | |
| 135 | + * Resets some variables when the blog is switched. | |
| 136 | + * Applied only if Polylang is active on the new blog. | |
| 132 | 137 | * |
| 133 | 138 | * @since 1.5.1 |
| 134 | 139 | * |
| 135 | - * @param int $new_blog | |
| 136 | - * @param int $old_blog | |
| 137 | - * @return bool not used by WP but by child class | |
| 140 | + * @param int $new_blog_id New blog ID. | |
| 141 | + * @param int $prev_blog_id Previous blog ID. | |
| 142 | + * @return void | |
| 138 | 143 | */ |
| 139 | - public function switch_blog( $new_blog, $old_blog ) { | |
| 140 | - $plugins = ( $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ) ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array(); | |
| 141 | - $plugins = array_merge( $plugins, get_option( 'active_plugins', array() ) ); | |
| 144 | + public function switch_blog( $new_blog_id, $prev_blog_id ) { | |
| 145 | + if ( (int) $new_blog_id === (int) $prev_blog_id ) { | |
| 146 | + // Do nothing if same blog. | |
| 147 | + return; | |
| 148 | + } | |
| 142 | 149 | |
| 143 | - // 2nd test needed when Polylang is not networked activated | |
| 144 | - // 3rd test needed when Polylang is networked activated and a new site is created | |
| 145 | - if ( $new_blog != $old_blog && in_array( POLYLANG_BASENAME, $plugins ) && get_option( 'polylang' ) ) { | |
| 146 | - $this->options = get_option( 'polylang' ); // Needed for menus | |
| 147 | - remove_action( 'pre_option_rewrite_rules', array( $this->links_model, 'prepare_rewrite_rules' ) ); | |
| 150 | + $this->links_model->remove_filters(); | |
| 151 | + | |
| 152 | + if ( $this->is_active_on_current_site() ) { | |
| 153 | + $this->options = get_option( 'polylang' ); // Needed for menus. | |
| 148 | 154 | $this->links_model = $this->model->get_links_model(); |
| 149 | - return true; | |
| 150 | 155 | } |
| 151 | - return false; | |
| 152 | 156 | } |
| 153 | 157 | |
| 154 | 158 | /** |
| 155 | - * Some backward compatibility with Polylang < 1.2 | |
| 156 | - * Allows for example to call $polylang->get_languages_list() instead of $polylang->model->get_languages_list() | |
| 157 | - * This works but should be slower than the direct call, thus an error is triggered in debug mode | |
| 159 | + * Checks if Polylang is active on the current blog (useful when the blog is switched). | |
| 158 | 160 | * |
| 159 | - * @since 1.2 | |
| 161 | + * @since 3.5.2 | |
| 160 | 162 | * |
| 161 | - * @param string $func function name | |
| 162 | - * @param array $args function arguments | |
| 163 | + * @return bool | |
| 163 | 164 | */ |
| 164 | - public function __call( $func, $args ) { | |
| 165 | - foreach ( $this as $prop => &$obj ) { | |
| 166 | - if ( is_object( $obj ) && method_exists( $obj, $func ) ) { | |
| 167 | - if ( WP_DEBUG ) { | |
| 168 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 169 | - $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func | |
| 170 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 171 | - sprintf( | |
| 172 | - '%1$s was called incorrectly in %3$s on line %4$s: the call to $polylang->%1$s() has been deprecated in Polylang 1.2, use PLL()->%2$s->%1$s() instead.' . "\nError handler", | |
| 173 | - esc_html( $func ), | |
| 174 | - esc_html( $prop ), | |
| 175 | - esc_html( $debug[ $i ]['file'] ), | |
| 176 | - absint( $debug[ $i ]['line'] ) | |
| 177 | - ) | |
| 178 | - ); | |
| 179 | - } | |
| 180 | - return call_user_func_array( array( $obj, $func ), $args ); | |
| 181 | - } | |
| 165 | + protected function is_active_on_current_site(): bool { | |
| 166 | + return pll_is_plugin_active( POLYLANG_BASENAME ) && get_option( 'polylang' ); | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Check if the customize menu should be removed or not. | |
| 171 | + * | |
| 172 | + * @since 3.2 | |
| 173 | + * | |
| 174 | + * @return bool True if it should be removed, false otherwise. | |
| 175 | + */ | |
| 176 | + public function should_customize_menu_be_removed() { | |
| 177 | + // Exit if a block theme isn't activated. | |
| 178 | + if ( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) { | |
| 179 | + return false; | |
| 182 | 180 | } |
| 183 | 181 | |
| 184 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 185 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 186 | - sprintf( | |
| 187 | - 'Call to undefined function PLL()->%1$s() in %2$s on line %3$s' . "\nError handler", | |
| 188 | - esc_html( $func ), | |
| 189 | - esc_html( $debug[0]['file'] ), | |
| 190 | - absint( $debug[0]['line'] ) | |
| 191 | - ), | |
| 192 | - E_USER_ERROR | |
| 193 | - ); | |
| 182 | + return ! $this->is_customize_register_hooked(); | |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * Tells whether or not Polylang or third party callbacks are hooked to `customize_register`. | |
| 187 | + * | |
| 188 | + * @since 3.4.3 | |
| 189 | + * | |
| 190 | + * @global $wp_filter | |
| 191 | + * | |
| 192 | + * @return bool True if Polylang's callbacks are hooked, false otherwise. | |
| 193 | + */ | |
| 194 | + protected function is_customize_register_hooked() { | |
| 195 | + global $wp_filter; | |
| 196 | + | |
| 197 | + if ( empty( $wp_filter['customize_register'] ) || ! $wp_filter['customize_register'] instanceof WP_Hook ) { | |
| 198 | + return false; | |
| 199 | + } | |
| 200 | + | |
| 201 | + /* | |
| 202 | + * 'customize_register' is hooked by: | |
| 203 | + * @see PLL_Nav_Menu::create_nav_menu_locations() | |
| 204 | + * @see PLL_Frontend_Static_Pages::filter_customizer() | |
| 205 | + */ | |
| 206 | + $floor = 0; | |
| 207 | + if ( ! empty( $this->nav_menu ) && (bool) $wp_filter['customize_register']->has_filter( 'customize_register', array( $this->nav_menu, 'create_nav_menu_locations' ) ) ) { | |
| 208 | + ++$floor; | |
| 209 | + } | |
| 210 | + | |
| 211 | + if ( ! empty( $this->static_pages ) && (bool) $wp_filter['customize_register']->has_filter( 'customize_register', array( $this->static_pages, 'filter_customizer' ) ) ) { | |
| 212 | + ++$floor; | |
| 213 | + } | |
| 214 | + | |
| 215 | + $count = array_sum( array_map( 'count', $wp_filter['customize_register']->callbacks ) ); | |
| 216 | + | |
| 217 | + return $count > $floor; | |
| 194 | 218 | } |
| 195 | 219 | } |