PluginProbe
Polylang / 3.7.6
Polylang v3.7.6
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / base.php

base.php in Polylang 3.7.6, at include/base.php

219 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Base class for both admin and frontend
8 *
9 * @since 1.2
10 */
11 #[AllowDynamicProperties]
12 abstract class PLL_Base {
13 /**
14 * Stores the plugin options.
15 *
16 * @var array
17 */
18 public $options;
19
20 /**
21 * @var PLL_Model
22 */
23 public $model;
24
25 /**
26 * Instance of a child class of PLL_Links_Model.
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 *
49 * @since 1.2
50 *
51 * @param PLL_Links_Model $links_model Links Model.
52 */
53 public function __construct( &$links_model ) {
54 $this->links_model = &$links_model;
55 $this->model = &$links_model->model;
56 $this->options = &$this->model->options;
57
58 $GLOBALS['l10n_unloaded']['pll_string'] = true; // Short-circuit _load_textdomain_just_in_time() for 'pll_string' domain in WP 4.6+
59
60 add_action( 'widgets_init', array( $this, 'widgets_init' ) );
61
62 // User defined strings translations
63 add_action( 'pll_language_defined', array( $this, 'load_strings_translations' ), 5 );
64 add_action( 'change_locale', array( $this, 'load_strings_translations' ) ); // Since WP 4.7
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 // Switch_to_blog
68 add_action( 'switch_blog', array( $this, 'switch_blog' ), 10, 2 );
69 }
70
71 /**
72 * Instantiates classes reacting to CRUD operations on posts and terms,
73 * only when at least one language is defined.
74 *
75 * @since 2.6
76 *
77 * @return void
78 */
79 public function init() {
80 if ( $this->model->has_languages() ) {
81 $this->posts = new PLL_CRUD_Posts( $this );
82 $this->terms = new PLL_CRUD_Terms( $this );
83
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' ) );
89 }
90 }
91
92 /**
93 * Registers our widgets
94 *
95 * @since 0.1
96 *
97 * @return void
98 */
99 public function widgets_init() {
100 register_widget( 'PLL_Widget_Languages' );
101
102 // Overwrites the calendar widget to filter posts by language
103 if ( ! defined( 'PLL_WIDGET_CALENDAR' ) || PLL_WIDGET_CALENDAR ) {
104 unregister_widget( 'WP_Widget_Calendar' );
105 register_widget( 'PLL_Widget_Calendar' );
106 }
107 }
108
109 /**
110 * Loads user defined strings translations
111 *
112 * @since 1.2
113 * @since 2.1.3 $locale parameter added.
114 *
115 * @param string $locale Language locale or slug. Defaults to current locale.
116 * @return void
117 */
118 public function load_strings_translations( $locale = '' ) {
119 if ( empty( $locale ) ) {
120 $locale = ( is_admin() && ! Polylang::is_ajax_on_front() ) ? get_user_locale() : get_locale();
121 }
122
123 $language = $this->model->get_language( $locale );
124
125 if ( ! empty( $language ) ) {
126 $mo = new PLL_MO();
127 $mo->import_from_db( $language );
128 $GLOBALS['l10n']['pll_string'] = &$mo;
129 } else {
130 unset( $GLOBALS['l10n']['pll_string'] );
131 }
132 }
133
134 /**
135 * Resets some variables when the blog is switched.
136 * Applied only if Polylang is active on the new blog.
137 *
138 * @since 1.5.1
139 *
140 * @param int $new_blog_id New blog ID.
141 * @param int $prev_blog_id Previous blog ID.
142 * @return void
143 */
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 }
149
150 $this->links_model->remove_filters();
151
152 if ( $this->is_active_on_current_site() ) {
153 $this->links_model = $this->model->get_links_model();
154 }
155 }
156
157 /**
158 * Checks if Polylang is active on the current blog (useful when the blog is switched).
159 *
160 * @since 3.5.2
161 *
162 * @return bool
163 */
164 protected function is_active_on_current_site(): bool {
165 return pll_is_plugin_active( POLYLANG_BASENAME ) && ! empty( $this->options['version'] );
166 }
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
200 /*
201 * 'customize_register' is hooked by:
202 * @see PLL_Nav_Menu::create_nav_menu_locations()
203 * @see PLL_Frontend_Static_Pages::filter_customizer()
204 */
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;
217 }
218 }
219