PluginProbe
Polylang / 3.8.8
Polylang v3.8.8
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 / src / base.php

base.php in Polylang 3.8.8, at src/base.php

238 lines 6.1 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 use WP_Syntex\Polylang\REST\Request;
7 use WP_Syntex\Polylang\Capabilities\Capabilities;
8
9 /**
10 * Base class for both admin and frontend
11 *
12 * @since 1.2
13 */
14 #[AllowDynamicProperties]
15 abstract class PLL_Base {
16 /**
17 * Capabilities.
18 *
19 * @since 3.8
20 *
21 * @var Capabilities
22 */
23 public $capabilities;
24
25 /**
26 * Stores the plugin options.
27 *
28 * @var \WP_Syntex\Polylang\Options\Options
29 */
30 public $options;
31
32 /**
33 * @var PLL_Model
34 */
35 public $model;
36
37 /**
38 * Instance of a child class of PLL_Links_Model.
39 *
40 * @var PLL_Links_Model
41 */
42 public $links_model;
43
44 /**
45 * Registers hooks on insert / update post related actions and filters.
46 *
47 * @var PLL_CRUD_Posts|null
48 */
49 public $posts;
50
51 /**
52 * Registers hooks on insert / update term related action and filters.
53 *
54 * @var PLL_CRUD_Terms|null
55 */
56 public $terms;
57
58 /**
59 * @var Request
60 */
61 public $request;
62
63 /**
64 * Constructor.
65 *
66 * @since 1.2
67 *
68 * @param PLL_Links_Model $links_model Links Model.
69 */
70 public function __construct( PLL_Links_Model &$links_model ) {
71 $this->capabilities = new Capabilities();
72 $this->links_model = &$links_model;
73 $this->model = &$links_model->model;
74 $this->options = $this->model->options;
75 $this->request = new Request( $this->model );
76
77 $GLOBALS['l10n_unloaded']['pll_string'] = true; // Short-circuit _load_textdomain_just_in_time() for 'pll_string' domain in WP 4.6+
78
79 add_action( 'widgets_init', array( $this, 'widgets_init' ) );
80
81 // User defined strings translations
82 add_action( 'pll_language_defined', array( $this, 'load_strings_translations' ), 5 );
83 add_action( 'change_locale', array( $this, 'load_strings_translations' ) ); // Since WP 4.7
84 add_action( 'personal_options_update', array( $this, 'load_strings_translations' ), 1, 0 ); // Before WP, for confirmation request when changing the user email.
85 add_action( 'lostpassword_post', array( $this, 'load_strings_translations' ), 10, 0 ); // Password reset email.
86 // Switch_to_blog
87 add_action( 'switch_blog', array( $this, 'switch_blog' ), 10, 2 );
88 }
89
90 /**
91 * Instantiates classes reacting to CRUD operations on posts and terms,
92 * only when at least one language is defined.
93 *
94 * @since 2.6
95 *
96 * @return void
97 */
98 public function init() {
99 if ( $this->model->has_languages() ) {
100 $this->posts = new PLL_CRUD_Posts( $this );
101 $this->terms = new PLL_CRUD_Terms( $this );
102
103 // WordPress options.
104 new PLL_Translate_Option( 'blogname', array(), array( 'context' => 'WordPress' ) );
105 new PLL_Translate_Option( 'blogdescription', array(), array( 'context' => 'WordPress' ) );
106 new PLL_Translate_Option( 'date_format', array(), array( 'context' => 'WordPress' ) );
107 new PLL_Translate_Option( 'time_format', array(), array( 'context' => 'WordPress' ) );
108 }
109 }
110
111 /**
112 * Registers our widgets
113 *
114 * @since 0.1
115 *
116 * @return void
117 */
118 public function widgets_init() {
119 register_widget( 'PLL_Widget_Languages' );
120
121 // Overwrites the calendar widget to filter posts by language
122 if ( ! defined( 'PLL_WIDGET_CALENDAR' ) || PLL_WIDGET_CALENDAR ) {
123 unregister_widget( 'WP_Widget_Calendar' );
124 register_widget( 'PLL_Widget_Calendar' );
125 }
126 }
127
128 /**
129 * Loads user defined strings translations
130 *
131 * @since 1.2
132 * @since 2.1.3 $locale parameter added.
133 *
134 * @param string $locale Language locale or slug. Defaults to current locale.
135 * @return void
136 */
137 public function load_strings_translations( $locale = '' ) {
138 if ( empty( $locale ) ) {
139 $locale = ( is_admin() && ! Polylang::is_ajax_on_front() ) ? get_user_locale() : get_locale();
140 }
141
142 $language = $this->model->get_language( $locale );
143
144 if ( ! empty( $language ) ) {
145 $mo = new PLL_MO();
146 $mo->import_from_db( $language );
147 $GLOBALS['l10n']['pll_string'] = &$mo;
148 } else {
149 unset( $GLOBALS['l10n']['pll_string'] );
150 }
151 }
152
153 /**
154 * Resets some variables when the blog is switched.
155 * Applied only if Polylang is active on the new blog.
156 *
157 * @since 1.5.1
158 *
159 * @param int $new_blog_id New blog ID.
160 * @param int $prev_blog_id Previous blog ID.
161 * @return void
162 */
163 public function switch_blog( $new_blog_id, $prev_blog_id ) {
164 if ( (int) $new_blog_id === (int) $prev_blog_id ) {
165 // Do nothing if same blog.
166 return;
167 }
168
169 $this->links_model->remove_filters();
170
171 if ( $this->is_active_on_current_site() ) {
172 $this->links_model = $this->model->get_links_model();
173 }
174 }
175
176 /**
177 * Checks if Polylang is active on the current blog (useful when the blog is switched).
178 *
179 * @since 3.5.2
180 *
181 * @return bool
182 */
183 protected function is_active_on_current_site(): bool {
184 return pll_is_plugin_active( POLYLANG_BASENAME ) && ! empty( $this->options['version'] );
185 }
186
187 /**
188 * Check if the customize menu should be removed or not.
189 *
190 * @since 3.2
191 *
192 * @return bool True if it should be removed, false otherwise.
193 */
194 public function should_customize_menu_be_removed() {
195 // Exit if a block theme isn't activated.
196 if ( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) {
197 return false;
198 }
199
200 return ! $this->is_customize_register_hooked();
201 }
202
203 /**
204 * Tells whether or not Polylang or third party callbacks are hooked to `customize_register`.
205 *
206 * @since 3.4.3
207 *
208 * @global $wp_filter
209 *
210 * @return bool True if Polylang's callbacks are hooked, false otherwise.
211 */
212 protected function is_customize_register_hooked() {
213 global $wp_filter;
214
215 if ( empty( $wp_filter['customize_register'] ) || ! $wp_filter['customize_register'] instanceof WP_Hook ) {
216 return false;
217 }
218
219 /*
220 * 'customize_register' is hooked by:
221 * @see PLL_Nav_Menu::create_nav_menu_locations()
222 * @see PLL_Frontend_Static_Pages::filter_customizer()
223 */
224 $floor = 0;
225 if ( ! empty( $this->nav_menu ) && (bool) $wp_filter['customize_register']->has_filter( 'customize_register', array( $this->nav_menu, 'create_nav_menu_locations' ) ) ) {
226 ++$floor;
227 }
228
229 if ( ! empty( $this->static_pages ) && (bool) $wp_filter['customize_register']->has_filter( 'customize_register', array( $this->static_pages, 'filter_customizer' ) ) ) {
230 ++$floor;
231 }
232
233 $count = array_sum( array_map( 'count', $wp_filter['customize_register']->callbacks ) );
234
235 return $count > $floor;
236 }
237 }
238