PluginProbe
Polylang / 2.4
Polylang v2.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 / settings / settings.php

settings.php in Polylang 2.4, at settings/settings.php

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