PluginProbe
Polylang / 2.0.3
Polylang v2.0.3
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
← All changes | settings/settings.php +121 -182 2.92.0.3 View file →
@@ -1,14 +1,11 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * A class for the Polylang settings pages
8 5 * accessible in $polylang global object
9 6 *
10 - * Properties:
7 + * properties:
11 8 * options => inherited, reference to Polylang options array
12 9 * model => inherited, reference to PLL_Model object
13 10 * links_model => inherited, reference to PLL_Links_Model object
14 11 * links => inherited, reference to PLL_Admin_Links object
@@ -19,24 +16,11 @@
19 16 *
20 17 * @since 1.2
21 18 */
22 19 class PLL_Settings extends PLL_Admin_Base {
20 + protected $active_tab, $modules;
23 21
24 22 /**
25 - * Name of the active module
26 - *
27 - * @var string $active_tab
28 - */
29 - protected $active_tab;
30 -
31 - /**
32 - * Array of modules classes
33 - *
34 - * @var array $modules
35 - */
36 - protected $modules;
37 -
38 - /**
39 23 * Constructor
40 24 *
41 25 * @since 1.2
42 26 *
@@ -44,21 +28,19 @@
44 28 */
45 29 public function __construct( &$links_model ) {
46 30 parent::__construct( $links_model );
47 31
48 - if ( isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
49 - $this->active_tab = 'mlang' === $_GET['page'] ? 'lang' : substr( sanitize_key( $_GET['page'] ), 6 ); // phpcs:ignore WordPress.Security.NonceVerification
50 - }
32 + $this->active_tab = ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'lang';
51 33
52 34 PLL_Admin_Strings::init();
53 35
36 + // FIXME put this as late as possible
54 37 add_action( 'admin_init', array( $this, 'register_settings_modules' ) );
55 38
56 - // Adds screen options and the about box in the languages admin panel
57 - add_action( 'load-toplevel_page_mlang', array( $this, 'load_page' ) );
58 - add_action( 'load-languages_page_mlang_strings', array( $this, 'load_page_strings' ) );
39 + // adds screen options and the about box in the languages admin panel
40 + add_action( 'load-settings_page_mlang', array( $this, 'load_page' ) );
59 41
60 - // Saves per-page value in screen option
42 + // saves per-page value in screen option
61 43 add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 );
62 44 }
63 45
64 46 /**
@@ -66,21 +48,26 @@
66 48 *
67 49 * @since 1.8
68 50 */
69 51 public function register_settings_modules() {
70 - $modules = array();
52 + $modules = array(
53 + 'PLL_Settings_Tools',
54 + 'PLL_Settings_Licenses',
55 + );
71 56
72 57 if ( $this->model->get_languages_list() ) {
73 - $modules = array(
58 + $modules = array_merge( array(
74 59 'PLL_Settings_Url',
75 60 'PLL_Settings_Browser',
76 61 'PLL_Settings_Media',
77 62 'PLL_Settings_CPT',
78 - );
63 + 'PLL_Settings_Sync',
64 + 'PLL_Settings_WPML',
65 + 'PLL_Settings_Share_Slug',
66 + 'PLL_Settings_Translate_Slugs',
67 + ), $modules );
79 68 }
80 69
81 - $modules[] = 'PLL_Settings_Licenses';
82 -
83 70 /**
84 71 * Filter the list of setting modules
85 72 *
86 73 * @since 1.8
@@ -95,14 +82,23 @@
95 82 }
96 83 }
97 84
98 85 /**
86 + * Adds the link to the languages panel in the WordPress admin menu
87 + *
88 + * @since 0.1
89 + */
90 + public function add_menus() {
91 + add_submenu_page( 'options-general.php', $title = __( 'Languages', 'polylang' ), $title, 'manage_options', 'mlang', array( $this, 'languages_page' ) );
92 + }
93 +
94 + /**
99 95 * Loads the about metabox
100 96 *
101 97 * @since 0.8
102 98 */
103 99 public function metabox_about() {
104 - include __DIR__ . '/view-about.php';
100 + include( PLL_SETTINGS_INC.'/view-about.php' );
105 101 }
106 102
107 103 /**
108 104 * Adds screen options and the about box in the languages admin panel
@@ -109,44 +105,38 @@
109 105 *
110 106 * @since 0.9.5
111 107 */
112 108 public function load_page() {
113 - if ( ! defined( 'PLL_DISPLAY_ABOUT' ) || PLL_DISPLAY_ABOUT ) {
114 - add_meta_box(
115 - 'pll-about-box',
116 - __( 'About Polylang', 'polylang' ),
117 - array( $this, 'metabox_about' ),
118 - 'toplevel_page_mlang',
119 - 'normal'
120 - );
121 - }
109 + // test of $this->active_tab avoids displaying the automatically generated screen options on other tabs
110 + switch ( $this->active_tab ) {
111 + case 'lang':
112 + if ( ! defined( 'PLL_DISPLAY_ABOUT' ) || PLL_DISPLAY_ABOUT ) {
113 + add_meta_box(
114 + 'pll-about-box',
115 + __( 'About Polylang', 'polylang' ),
116 + array( $this, 'metabox_about' ),
117 + 'settings_page_mlang',
118 + 'normal'
119 + );
120 + }
122 121
123 - add_screen_option(
124 - 'per_page',
125 - array(
126 - 'label' => __( 'Languages', 'polylang' ),
127 - 'default' => 10,
128 - 'option' => 'pll_lang_per_page',
129 - )
130 - );
122 + add_screen_option( 'per_page', array(
123 + 'label' => __( 'Languages', 'polylang' ),
124 + 'default' => 10,
125 + 'option' => 'pll_lang_per_page',
126 + ) );
131 127
132 - add_action( 'admin_notices', array( $this, 'notice_objects_with_no_lang' ) );
133 - }
128 + add_action( 'admin_notices', array( $this, 'notice_objects_with_no_lang' ) );
129 + break;
134 130
135 - /**
136 - * Adds screen options in the strings translations admin panel
137 - *
138 - * @since 2.1
139 - */
140 - public function load_page_strings() {
141 - add_screen_option(
142 - 'per_page',
143 - array(
144 - 'label' => __( 'Strings translations', 'polylang' ),
145 - 'default' => 10,
146 - 'option' => 'pll_strings_per_page',
147 - )
148 - );
131 + case 'strings':
132 + add_screen_option( 'per_page', array(
133 + 'label' => __( 'Strings translations', 'polylang' ),
134 + 'default' => 10,
135 + 'option' => 'pll_strings_per_page',
136 + ) );
137 + break;
138 + }
149 139 }
150 140
151 141 /**
152 142 * Save the "Views/Uploads per page" option set by this user
@@ -152,11 +142,11 @@
152 142 * Save the "Views/Uploads per page" option set by this user
153 143 *
154 144 * @since 0.9.5
155 145 *
156 - * @param mixed $status false or value returned by previous filter
157 - * @param string $option Name of the option being changed
158 - * @param string $value Value of the option
146 + * @param mixed $status false or value returned by previous filter
147 + * @param string $option Name of the option being changed
148 + * @param string $value Value of the option
159 149 *
160 150 * @return string New value if this is our option, otherwise nothing
161 151 */
162 152 public function set_screen_option( $status, $option, $value ) {
@@ -173,58 +163,39 @@
173 163 public function handle_actions( $action ) {
174 164 switch ( $action ) {
175 165 case 'add':
176 166 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
177 - $errors = $this->model->add_language( $_POST );
178 167
179 - if ( is_wp_error( $errors ) ) {
180 - foreach ( $errors->get_error_messages() as $message ) {
181 - add_settings_error( 'general', 'pll_add_language', $message );
168 + if ( $this->model->add_language( $_POST ) && 'en_US' !== $_POST['locale'] ) {
169 + // attempts to install the language pack
170 + require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
171 + if ( ! wp_download_language_pack( $_POST['locale'] ) ) {
172 + add_settings_error( 'general', 'pll_download_mo', __( 'The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang' ) );
182 173 }
183 - } else {
184 - add_settings_error( 'general', 'pll_languages_created', __( 'Language added.', 'polylang' ), 'updated' );
185 - $locale = sanitize_text_field( wp_unslash( $_POST['locale'] ) ); // phpcs:ignore WordPress.Security
186 174
187 - if ( 'en_US' !== $locale && current_user_can( 'install_languages' ) ) {
188 - // Attempts to install the language pack
189 - require_once ABSPATH . 'wp-admin/includes/translation-install.php';
190 - if ( ! wp_download_language_pack( $locale ) ) {
191 - add_settings_error( 'general', 'pll_download_mo', __( 'The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang' ) );
192 - }
193 -
194 - // Force checking for themes and plugins translations updates
195 - wp_clean_themes_cache();
196 - wp_clean_plugins_cache();
197 - }
175 + // force checking for themes and plugins translations updates
176 + wp_clean_themes_cache();
177 + wp_clean_plugins_cache();
198 178 }
199 - self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true )
200 - break;
179 + self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
180 + break;
201 181
202 182 case 'delete':
203 183 check_admin_referer( 'delete-lang' );
204 184
205 - if ( ! empty( $_GET['lang'] ) && $this->model->delete_language( (int) $_GET['lang'] ) ) {
206 - add_settings_error( 'general', 'pll_languages_deleted', __( 'Language deleted.', 'polylang' ), 'updated' );
185 + if ( ! empty( $_GET['lang'] ) ) {
186 + $this->model->delete_language( (int) $_GET['lang'] );
207 187 }
208 188
209 - self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true )
210 - break;
189 + self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
190 + break;
211 191
212 192 case 'update':
213 193 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
214 - $errors = $this->model->update_language( $_POST );
194 + $error = $this->model->update_language( $_POST );
195 + self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
196 + break;
215 197
216 - if ( is_wp_error( $errors ) ) {
217 - foreach ( $errors->get_error_messages() as $message ) {
218 - add_settings_error( 'general', 'pll_update_language', $message );
219 - }
220 - } else {
221 - add_settings_error( 'general', 'pll_languages_updated', __( 'Language updated.', 'polylang' ), 'updated' );
222 - }
223 -
224 - self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true )
225 - break;
226 -
227 198 case 'default-lang':
228 199 check_admin_referer( 'default-lang' );
229 200
230 201 if ( $lang = $this->model->get_language( (int) $_GET['lang'] ) ) {
@@ -230,10 +201,10 @@
230 201 if ( $lang = $this->model->get_language( (int) $_GET['lang'] ) ) {
231 202 $this->model->update_default_lang( $lang->slug );
232 203 }
233 204
234 - self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true )
235 - break;
205 + self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
206 + break;
236 207
237 208 case 'content-default-lang':
238 209 check_admin_referer( 'content-default-lang' );
239 210
@@ -245,32 +216,22 @@
245 216 $this->model->set_language_in_mass( 'term', $nolang['terms'], $this->options['default_lang'] );
246 217 }
247 218 }
248 219
249 - self::redirect(); // To refresh the page ( possible thanks to the $_GET['noheader']=true )
250 - break;
220 + self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
221 + break;
251 222
252 223 case 'activate':
253 224 check_admin_referer( 'pll_activate' );
254 - if ( isset( $_GET['module'] ) ) {
255 - $module = sanitize_key( $_GET['module'] );
256 - if ( isset( $this->modules[ $module ] ) ) {
257 - $this->modules[ $module ]->activate();
258 - }
259 - }
225 + $this->modules[ $_GET['module'] ]->activate();
260 226 self::redirect();
261 - break;
227 + break;
262 228
263 229 case 'deactivate':
264 230 check_admin_referer( 'pll_deactivate' );
265 - if ( isset( $_GET['module'] ) ) {
266 - $module = sanitize_key( $_GET['module'] );
267 - if ( isset( $this->modules[ $module ] ) ) {
268 - $this->modules[ $module ]->deactivate();
269 - }
270 - }
231 + $this->modules[ $_GET['module'] ]->deactivate();
271 232 self::redirect();
272 - break;
233 + break;
273 234
274 235 default:
275 236 /**
276 237 * Fires when a non default action has been sent to Polylang settings
@@ -277,43 +238,61 @@
277 238 *
278 239 * @since 1.8
279 240 */
280 241 do_action( "mlang_action_$action" );
281 - break;
242 + break;
282 243 }
283 244 }
284 245
285 246 /**
286 247 * Displays the 3 tabs pages: languages, strings translations, settings
287 - * Also manages user input for these pages
248 + * also manages user input for these pages
288 249 *
289 250 * @since 0.1
290 251 */
291 252 public function languages_page() {
253 + // prepare the list of tabs
254 + $tabs = array( 'lang' => __( 'Languages','polylang' ) );
255 +
256 + // only if at least one language has been created
257 + if ( $listlanguages = $this->model->get_languages_list() ) {
258 + $tabs['strings'] = __( 'Strings translations', 'polylang' );
259 + }
260 +
261 + $tabs['settings'] = __( 'Settings', 'polylang' );
262 +
263 + /**
264 + * Filter the list of tabs in Polylang settings
265 + *
266 + * @since 1.5.1
267 + *
268 + * @param array $tabs list of tab names
269 + */
270 + $tabs = apply_filters( 'pll_settings_tabs', $tabs );
271 +
292 272 switch ( $this->active_tab ) {
293 273 case 'lang':
294 - // Prepare the list table of languages
274 + // prepare the list table of languages
295 275 $list_table = new PLL_Table_Languages();
296 - $list_table->prepare_items( $this->model->get_languages_list() );
297 - break;
276 + $list_table->prepare_items( $listlanguages );
277 + break;
298 278
299 279 case 'strings':
300 - $string_table = new PLL_Table_String( $this->model->get_languages_list() );
280 + $string_table = new PLL_Table_String( $listlanguages );
301 281 $string_table->prepare_items();
302 - break;
282 + break;
303 283 }
304 284
305 - // Handle user input
306 - $action = isset( $_REQUEST['pll_action'] ) ? sanitize_key( $_REQUEST['pll_action'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
307 - if ( 'edit' === $action && ! empty( $_GET['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
308 - // phpcs:ignore WordPress.Security.NonceVerification, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
285 + // handle user input
286 + $action = isset( $_REQUEST['pll_action'] ) ? $_REQUEST['pll_action'] : '';
287 + if ( 'edit' === $action && ! empty( $_GET['lang'] ) ) {
309 288 $edit_lang = $this->model->get_language( (int) $_GET['lang'] );
310 289 } else {
311 290 $this->handle_actions( $action );
312 291 }
313 292
314 - // Displays the page
315 - include __DIR__ . '/view-languages.php';
293 + // displays the page
294 + include( PLL_SETTINGS_INC.'/view-languages.php' );
316 295 }
317 296
318 297 /**
319 298 * Enqueues scripts and styles
@@ -322,13 +301,12 @@
322 301 parent::admin_enqueue_scripts();
323 302
324 303 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
325 304
326 - wp_enqueue_script( 'pll_admin', plugins_url( '/js/admin' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery', 'wp-ajax-response', 'postbox', 'jquery-ui-selectmenu' ), POLYLANG_VERSION );
327 - wp_localize_script( 'pll_admin', 'pll_flag_base_url', plugins_url( '/flags/', POLYLANG_FILE ) );
328 - wp_localize_script( 'pll_admin', 'pll_dismiss_notice', esc_html__( 'Dismiss this notice.', 'polylang' ) );
305 + wp_enqueue_script( 'pll_admin', POLYLANG_URL .'/js/admin'.$suffix.'.js', array( 'jquery', 'wp-ajax-response', 'postbox', 'jquery-ui-selectmenu' ), POLYLANG_VERSION );
306 + wp_localize_script( 'pll_admin', 'pll_flag_base_url', POLYLANG_URL . '/flags/' );
329 307
330 - wp_enqueue_style( 'pll_selectmenu', plugins_url( '/css/selectmenu' . $suffix . '.css', POLYLANG_FILE ), array(), POLYLANG_VERSION );
308 + wp_enqueue_style( 'pll_selectmenu', POLYLANG_URL .'/css/selectmenu'.$suffix.'.css', array(), POLYLANG_VERSION );
331 309 }
332 310
333 311 /**
334 312 * Displays a notice when there are objects with no language assigned
@@ -335,13 +313,13 @@
335 313 *
336 314 * @since 1.8
337 315 */
338 316 public function notice_objects_with_no_lang() {
339 - if ( ! empty( $this->options['default_lang'] ) && $this->model->get_objects_with_no_lang( 1 ) ) {
317 + if ( ! empty( $this->options['default_lang'] ) && $this->model->get_objects_with_no_lang() ) {
340 318 printf(
341 319 '<div class="error"><p>%s <a href="%s">%s</a></p></div>',
342 320 esc_html__( 'There are posts, pages, categories or tags without language.', 'polylang' ),
343 - esc_url( wp_nonce_url( '?page=mlang&pll_action=content-default-lang&noheader=true', 'content-default-lang' ) ),
321 + wp_nonce_url( '?page=mlang&amp;pll_action=content-default-lang&amp;noheader=true', 'content-default-lang' ),
344 322 esc_html__( 'You can set them all to the default language.', 'polylang' )
345 323 );
346 324 }
347 325 }
@@ -353,53 +331,14 @@
353 331 * @since 1.5
354 332 *
355 333 * @param array $args query arguments to add to the url
356 334 */
357 - public static function redirect( $args = array() ) {
335 + static public function redirect( $args = array() ) {
358 336 if ( $errors = get_settings_errors() ) {
359 337 set_transient( 'settings_errors', $errors, 30 );
360 338 $args['settings-updated'] = 1;
361 339 }
362 - // Remove possible 'pll_action' and 'lang' query args from the referer before redirecting
363 - wp_safe_redirect( add_query_arg( $args, remove_query_arg( array( 'pll_action', 'lang' ), wp_get_referer() ) ) );
340 + // remove possible 'pll_action' and 'lang' query args from the referer before redirecting
341 + wp_safe_redirect( add_query_arg( $args, remove_query_arg( array( 'pll_action', 'lang' ), wp_get_referer() ) ) );
364 342 exit;
365 - }
366 -
367 - /**
368 - * Get the list of predefined languages
369 - *
370 - * @since 2.3
371 - */
372 - public static function get_predefined_languages() {
373 - require_once ABSPATH . 'wp-admin/includes/translation-install.php';
374 -
375 - $languages = include __DIR__ . '/languages.php';
376 - $translations = wp_get_available_translations();
377 -
378 - // Keep only languages with existing WP language pack
379 - // Unless the transient has expired and we don't have an internet connection to refresh it
380 - if ( ! empty( $translations ) ) {
381 - $translations['en_US'] = ''; // Languages packs don't include en_US
382 - $languages = array_intersect_key( $languages, $translations );
383 - }
384 -
385 - /**
386 - * Filter the list of predefined languages
387 - *
388 - * @since 1.7.10
389 - * @since 2.3 The languages arrays use associative keys instead of numerical keys
390 - * @see https://github.com/polylang/polylang/blob/2.8.2/settings/languages.php the list of predefined languages
391 - *
392 - * @param array $languages
393 - */
394 - $languages = apply_filters( 'pll_predefined_languages', $languages );
395 -
396 - // Keep only languages with all necessary informations
397 - foreach ( $languages as $k => $lang ) {
398 - if ( ! isset( $lang['code'], $lang['locale'], $lang['name'], $lang['dir'], $lang['flag'] ) ) {
399 - unset( $languages[ $k ] );
400 - }
401 - }
402 -
403 - return $languages;
404 343 }
405 344 }