PluginProbe
Polylang / trunk
Polylang vtrunk
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 trunk, at src/base.php

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