PluginProbe
Polylang / 3.5
Polylang v3.5
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.5, at include/base.php

221 lines 6.0 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 Locale. 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 $this->links_model->remove_filters();
146
147 if ( $this->is_active_on_new_blog( $new_blog_id, $prev_blog_id ) ) {
148 $this->options = get_option( 'polylang' ); // Needed for menus.
149 $this->links_model = $this->model->get_links_model();
150 }
151 }
152
153 /**
154 * Checks if Polylang is active on the new blog when the blog is switched.
155 *
156 * @since 3.0
157 *
158 * @param int $new_blog_id New blog ID.
159 * @param int $prev_blog_id Previous blog ID.
160 * @return bool
161 */
162 protected function is_active_on_new_blog( $new_blog_id, $prev_blog_id ) {
163 /*
164 * The 2nd test is needed when Polylang is not networked activated.
165 * The 3rd test is needed when Polylang is networked activated and a new site is created.
166 */
167 return $new_blog_id !== $prev_blog_id && pll_is_plugin_active( POLYLANG_BASENAME ) && get_option( 'polylang' );
168 }
169
170 /**
171 * Check if the customize menu should be removed or not.
172 *
173 * @since 3.2
174 *
175 * @return bool True if it should be removed, false otherwise.
176 */
177 public function should_customize_menu_be_removed() {
178 // Exit if a block theme isn't activated.
179 if ( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) {
180 return false;
181 }
182
183 return ! $this->is_customize_register_hooked();
184 }
185
186 /**
187 * Tells whether or not Polylang or third party callbacks are hooked to `customize_register`.
188 *
189 * @since 3.4.3
190 *
191 * @global $wp_filter
192 *
193 * @return bool True if Polylang's callbacks are hooked, false otherwise.
194 */
195 protected function is_customize_register_hooked() {
196 global $wp_filter;
197
198 if ( empty( $wp_filter['customize_register'] ) || ! $wp_filter['customize_register'] instanceof WP_Hook ) {
199 return false;
200 }
201
202 /*
203 * 'customize_register' is hooked by:
204 * @see PLL_Nav_Menu::create_nav_menu_locations()
205 * @see PLL_Frontend_Static_Pages::filter_customizer()
206 */
207 $floor = 0;
208 if ( ! empty( $this->nav_menu ) && (bool) $wp_filter['customize_register']->has_filter( 'customize_register', array( $this->nav_menu, 'create_nav_menu_locations' ) ) ) {
209 $floor++;
210 }
211
212 if ( ! empty( $this->static_pages ) && (bool) $wp_filter['customize_register']->has_filter( 'customize_register', array( $this->static_pages, 'filter_customizer' ) ) ) {
213 $floor++;
214 }
215
216 $count = array_sum( array_map( 'count', $wp_filter['customize_register']->callbacks ) );
217
218 return $count > $floor;
219 }
220 }
221