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

198 lines 5.4 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 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 ) {
162 $plugins = ( $sitewide_plugins = get_site_option( 'active_sitewide_plugins' ) ) && is_array( $sitewide_plugins ) ? array_keys( $sitewide_plugins ) : array();
163 $plugins = array_merge( $plugins, get_option( 'active_plugins', array() ) );
164
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' );
170 }
171
172 /**
173 * Check if the customize menu should be removed or not.
174 *
175 * @since 3.2
176 *
177 * @return bool True if it should be removed, false otherwise.
178 */
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;
183 }
184
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;
196 }
197 }
198