| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | 7 | * Base class for both admin and frontend |
| 5 | 8 | * |
| @@ -4,17 +7,49 @@ | ||
| 4 | 7 | * Base class for both admin and frontend |
| 5 | 8 | * |
| 6 | 9 | * @since 1.2 |
| 7 | 10 | */ |
| 11 | +#[AllowDynamicProperties] | |
| 8 | 12 | abstract class PLL_Base { |
| 9 | - public $links_model, $model, $options; | |
| 13 | + /** | |
| 14 | + * Stores the plugin options. | |
| 15 | + * | |
| 16 | + * @var array | |
| 17 | + */ | |
| 18 | + public $options; | |
| 10 | 19 | |
| 11 | 20 | /** |
| 12 | - * Constructor | |
| 21 | + * @var PLL_Model | |
| 22 | + */ | |
| 23 | + public $model; | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * Instance of a child class of PLL_Links_Model. | |
| 13 | 27 | * |
| 28 | + * @var PLL_Links_Model | |
| 29 | + */ | |
| 30 | + public $links_model; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Registers hooks on insert / update post related actions and filters. | |
| 34 | + * | |
| 35 | + * @var PLL_CRUD_Posts|null | |
| 36 | + */ | |
| 37 | + public $posts; | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * Registers hooks on insert / update term related action and filters. | |
| 41 | + * | |
| 42 | + * @var PLL_CRUD_Terms|null | |
| 43 | + */ | |
| 44 | + public $terms; | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Constructor. | |
| 48 | + * | |
| 14 | 49 | * @since 1.2 |
| 15 | 50 | * |
| 16 | - * @param object $links_model | |
| 51 | + * @param PLL_Links_Model $links_model Links Model. | |
| 17 | 52 | */ |
| 18 | 53 | public function __construct( &$links_model ) { |
| 19 | 54 | $this->links_model = &$links_model; |
| 20 | 55 | $this->model = &$links_model->model; |
| @@ -26,39 +61,32 @@ | ||
| 26 | 61 | |
| 27 | 62 | // User defined strings translations |
| 28 | 63 | add_action( 'pll_language_defined', array( $this, 'load_strings_translations' ), 5 ); |
| 29 | 64 | add_action( 'change_locale', array( $this, 'load_strings_translations' ) ); // Since WP 4.7 |
| 30 | - | |
| 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. | |
| 31 | 67 | // Switch_to_blog |
| 32 | 68 | add_action( 'switch_blog', array( $this, 'switch_blog' ), 10, 2 ); |
| 33 | 69 | } |
| 34 | 70 | |
| 35 | 71 | /** |
| 36 | - * Instantiate classes always needed | |
| 72 | + * Instantiates classes reacting to CRUD operations on posts and terms, | |
| 73 | + * only when at least one language is defined. | |
| 37 | 74 | * |
| 38 | 75 | * @since 2.6 |
| 76 | + * | |
| 77 | + * @return void | |
| 39 | 78 | */ |
| 40 | 79 | public function init() { |
| 41 | - // REST API | |
| 42 | - if ( class_exists( 'PLL_REST_API' ) ) { | |
| 43 | - $this->rest_api = new PLL_REST_API( $this ); | |
| 44 | - } | |
| 45 | - | |
| 46 | 80 | if ( $this->model->get_languages_list() ) { |
| 47 | - // Used by content duplicate and post synchronization | |
| 48 | - if ( class_exists( 'PLL_Sync_Content' ) ) { | |
| 49 | - $this->sync_content = new PLL_Sync_Content( $this ); | |
| 50 | - } | |
| 81 | + $this->posts = new PLL_CRUD_Posts( $this ); | |
| 82 | + $this->terms = new PLL_CRUD_Terms( $this ); | |
| 51 | 83 | |
| 52 | - // Active languages | |
| 53 | - if ( class_exists( 'PLL_Active_Languages' ) ) { | |
| 54 | - $this->active_languages = new PLL_Active_Languages( $this ); | |
| 55 | - } | |
| 56 | - | |
| 57 | - // Share post slugs | |
| 58 | - if ( get_option( 'permalink_structure' ) && $this->options['force_lang'] && class_exists( 'PLL_Share_Post_Slug' ) ) { | |
| 59 | - $this->share_post_slug = new PLL_Share_Post_Slug( $this ); | |
| 60 | - } | |
| 84 | + // WordPress options. | |
| 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' ) ); | |
| 61 | 89 | } |
| 62 | 90 | } |
| 63 | 91 | |
| 64 | 92 | /** |
| @@ -64,8 +92,10 @@ | ||
| 64 | 92 | /** |
| 65 | 93 | * Registers our widgets |
| 66 | 94 | * |
| 67 | 95 | * @since 0.1 |
| 96 | + * | |
| 97 | + * @return void | |
| 68 | 98 | */ |
| 69 | 99 | public function widgets_init() { |
| 70 | 100 | register_widget( 'PLL_Widget_Languages' ); |
| 71 | 101 | |
| @@ -82,12 +112,13 @@ | ||
| 82 | 112 | * @since 1.2 |
| 83 | 113 | * @since 2.1.3 $locale parameter added. |
| 84 | 114 | * |
| 85 | 115 | * @param string $locale Locale. Defaults to current locale. |
| 116 | + * @return void | |
| 86 | 117 | */ |
| 87 | 118 | public function load_strings_translations( $locale = '' ) { |
| 88 | 119 | if ( empty( $locale ) ) { |
| 89 | - $locale = get_locale(); | |
| 120 | + $locale = ( is_admin() && ! Polylang::is_ajax_on_front() ) ? get_user_locale() : get_locale(); | |
| 90 | 121 | } |
| 91 | 122 | |
| 92 | 123 | $language = $this->model->get_language( $locale ); |
| 93 | 124 | |
| @@ -100,70 +131,67 @@ | ||
| 100 | 131 | } |
| 101 | 132 | } |
| 102 | 133 | |
| 103 | 134 | /** |
| 104 | - * Resets some variables when switching blog | |
| 105 | - * 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. | |
| 106 | 137 | * |
| 107 | 138 | * @since 1.5.1 |
| 108 | 139 | * |
| 109 | - * @param int $new_blog | |
| 110 | - * @param int $old_blog | |
| 111 | - * @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 | |
| 112 | 143 | */ |
| 113 | - public function switch_blog( $new_blog, $old_blog ) { | |
| 144 | + public function switch_blog( $new_blog_id, $prev_blog_id ) { | |
| 145 | + if ( $this->is_active_on_new_blog( $new_blog_id, $prev_blog_id ) ) { | |
| 146 | + $this->options = get_option( 'polylang' ); // Needed for menus. | |
| 147 | + remove_action( 'pre_option_rewrite_rules', array( $this->links_model, 'prepare_rewrite_rules' ) ); | |
| 148 | + $this->links_model = $this->model->get_links_model(); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * Checks if Polylang is active on the new blog when the blog is switched. | |
| 154 | + * | |
| 155 | + * @since 3.0 | |
| 156 | + * | |
| 157 | + * @param int $new_blog_id New blog ID. | |
| 158 | + * @param int $prev_blog_id Previous blog ID. | |
| 159 | + * @return bool | |
| 160 | + */ | |
| 161 | + protected function is_active_on_new_blog( $new_blog_id, $prev_blog_id ) { | |
| 114 | 162 | $plugins = ( $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ) ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array(); |
| 115 | 163 | $plugins = array_merge( $plugins, get_option( 'active_plugins', array() ) ); |
| 116 | 164 | |
| 117 | - // 2nd test needed when Polylang is not networked activated | |
| 118 | - // 3rd test needed when Polylang is networked activated and a new site is created | |
| 119 | - if ( $new_blog != $old_blog && in_array( POLYLANG_BASENAME, $plugins ) && get_option( 'polylang' ) ) { | |
| 120 | - $this->options = get_option( 'polylang' ); // Needed for menus | |
| 121 | - remove_action( 'pre_option_rewrite_rules', array( $this->links_model, 'prepare_rewrite_rules' ) ); | |
| 122 | - $this->links_model = $this->model->get_links_model(); | |
| 123 | - return true; | |
| 124 | - } | |
| 125 | - return false; | |
| 165 | + /* | |
| 166 | + * The 2nd test is needed when Polylang is not networked activated. | |
| 167 | + * The 3rd test is needed when Polylang is networked activated and a new site is created. | |
| 168 | + */ | |
| 169 | + return $new_blog_id !== $prev_blog_id && in_array( POLYLANG_BASENAME, $plugins ) && get_option( 'polylang' ); | |
| 126 | 170 | } |
| 127 | 171 | |
| 128 | 172 | /** |
| 129 | - * Some backward compatibility with Polylang < 1.2 | |
| 130 | - * Allows for example to call $polylang->get_languages_list() instead of $polylang->model->get_languages_list() | |
| 131 | - * This works but should be slower than the direct call, thus an error is triggered in debug mode | |
| 173 | + * Check if the customize menu should be removed or not. | |
| 132 | 174 | * |
| 133 | - * @since 1.2 | |
| 175 | + * @since 3.2 | |
| 134 | 176 | * |
| 135 | - * @param string $func function name | |
| 136 | - * @param array $args function arguments | |
| 177 | + * @return bool True if it should be removed, false otherwise. | |
| 137 | 178 | */ |
| 138 | - public function __call( $func, $args ) { | |
| 139 | - foreach ( $this as $prop => &$obj ) { | |
| 140 | - if ( is_object( $obj ) && method_exists( $obj, $func ) ) { | |
| 141 | - if ( WP_DEBUG ) { | |
| 142 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 143 | - $i = 1 + empty( $debug[1]['line'] ); // The file and line are in $debug[2] if the function was called using call_user_func | |
| 144 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 145 | - sprintf( | |
| 146 | - '%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", | |
| 147 | - esc_html( $func ), | |
| 148 | - esc_html( $prop ), | |
| 149 | - esc_html( $debug[ $i ]['file'] ), | |
| 150 | - absint( $debug[ $i ]['line'] ) | |
| 151 | - ) | |
| 152 | - ); | |
| 153 | - } | |
| 154 | - return call_user_func_array( array( $obj, $func ), $args ); | |
| 155 | - } | |
| 179 | + public function should_customize_menu_be_removed() { | |
| 180 | + // Exit if a block theme isn't activated. | |
| 181 | + if ( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) { | |
| 182 | + return false; | |
| 156 | 183 | } |
| 157 | 184 | |
| 158 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 159 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 160 | - sprintf( | |
| 161 | - 'Call to undefined function PLL()->%1$s() in %2$s on line %3$s' . "\nError handler", | |
| 162 | - esc_html( $func ), | |
| 163 | - esc_html( $debug[0]['file'] ), | |
| 164 | - absint( $debug[0]['line'] ) | |
| 165 | - ), | |
| 166 | - E_USER_ERROR | |
| 167 | - ); | |
| 185 | + global $wp_filter; | |
| 186 | + if ( empty( $wp_filter['customize_register'] ) ) { | |
| 187 | + return false; | |
| 188 | + } | |
| 189 | + | |
| 190 | + $customize_register_hooks = count( array_merge( ...array_values( $wp_filter['customize_register']->callbacks ) ) ); | |
| 191 | + if ( $customize_register_hooks > 1 ) { | |
| 192 | + return false; | |
| 193 | + } | |
| 194 | + | |
| 195 | + return true; | |
| 168 | 196 | } |
| 169 | 197 | } |