PluginProbe
Polylang / 1.4.4
Polylang v1.4.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 / admin / settings.php

settings.php in Polylang 1.4.4, at admin/settings.php

370 lines 12.9 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 *
6 * @since 1.2
7 */
8 class PLL_Settings {
9 public $links_model, $model, $options;
10 protected $strings = array(); // strings to translate
11
12 /*
13 * constructor
14 *
15 * @since 1.2
16 *
17 * @param object $model instance of PLL_Model
18 */
19 public function __construct(&$links_model) {
20 $this->links_model = &$links_model;
21 $this->model = &$links_model->model;
22 $this->options = &$this->model->options;
23
24 // adds screen options and the about box in the languages admin panel
25 add_action('load-settings_page_mlang', array(&$this, 'load_page'));
26
27 // saves per-page value in screen option
28 add_filter('set-screen-option', create_function('$s, $o, $v', 'return $v;'), 10, 3);
29 }
30
31 /*
32 * adds screen options and the about box in the languages admin panel
33 *
34 * @since 0.9.5
35 */
36 public function load_page() {
37 // test of $_GET['tab'] avoids displaying the automatically generated screen options on other tabs
38 if ((!defined('PLL_DISPLAY_ABOUT') || PLL_DISPLAY_ABOUT) && (!isset($_GET['tab']) || $_GET['tab'] == 'lang')) {
39 add_meta_box(
40 'pll_about_box',
41 __('About Polylang', 'polylang'),
42 create_function('', "include(PLL_ADMIN_INC.'/view-about.php');"),
43 'settings_page_mlang',
44 'normal'
45 );
46 }
47
48 if (!isset($_GET['tab']) || $_GET['tab'] == 'lang') {
49 add_screen_option('per_page', array(
50 'label' => __('Languages', 'polylang'),
51 'default' => 10,
52 'option' => 'pll_lang_per_page'
53 ));
54 }
55
56 if (isset($_GET['tab']) && $_GET['tab'] == 'strings') {
57 add_screen_option('per_page', array(
58 'label' => __('Strings translations', 'polylang'),
59 'default' => 10,
60 'option' => 'pll_strings_per_page'
61 ));
62 }
63 }
64
65 /*
66 * diplays the 3 tabs pages: languages, strings translations, settings
67 * also manages user input for these pages
68 *
69 * @since 0.1
70 */
71 public function languages_page() {
72 // prepare the list of tabs
73 $tabs = array('lang' => __('Languages','polylang'));
74
75 // only if at least one language has been created
76 if ($listlanguages = $this->model->get_languages_list()) {
77 $tabs['strings'] = __('Strings translation','polylang');
78 $tabs['settings'] = __('Settings', 'polylang');
79 }
80
81 $active_tab = !empty($_GET['tab']) ? $_GET['tab'] : 'lang';
82
83 switch($active_tab) {
84 case 'lang':
85 // prepare the list table of languages
86 $list_table = new PLL_Table_Languages();
87 $list_table->prepare_items($listlanguages);
88
89 // error messages for data validation
90 $errors[1] = __('Enter a valid WordPress locale', 'polylang');
91 $errors[2] = __('The language code contains invalid characters', 'polylang');
92 $errors[3] = __('The language code must be unique', 'polylang');
93 $errors[4] = __('The language must have a name', 'polylang');
94 $errors[5] = __('The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang');
95 break;
96
97 case 'strings':
98 // get the strings to translate
99 $data = $this->get_strings();
100
101 $selected = empty($_REQUEST['group']) ? -1 : $_REQUEST['group'];
102 foreach ($data as $key=>$row) {
103 $groups[] = $row['context']; // get the groups
104
105 // filter for search string
106 if (($selected !=-1 && $row['context'] != $selected) || (!empty($_REQUEST['s']) && stripos($row['name'], $_REQUEST['s']) === false && stripos($row['string'], $_REQUEST['s']) === false))
107 unset ($data[$key]);
108 }
109
110 $groups = array_unique($groups);
111
112 // load translations
113 foreach ($listlanguages as $language) {
114 // filters by language if requested
115 if (($lg = get_user_meta(get_current_user_id(), 'pll_filter_content', true)) && $language->slug != $lg)
116 continue;
117
118 $mo = new PLL_MO();
119 $mo->import_from_db($language);
120 foreach ($data as $key=>$row) {
121 $data[$key]['translations'][$language->name] = $mo->translate($row['string']);
122 $data[$key]['row'] = $key; // store the row number for convenience
123 }
124 }
125
126 $string_table = new PLL_Table_String($groups, $selected);
127 $string_table->prepare_items($data);
128 break;
129
130 case 'settings':
131 $post_types = get_post_types(array('public' => true, '_builtin' => false));
132 $post_types = array_diff($post_types, get_post_types(array('_pll' => true)));
133 $post_types = array_unique(apply_filters('pll_get_post_types', $post_types, true));
134
135 $taxonomies = get_taxonomies(array('public' => true, '_builtin' => false));
136 $taxonomies = array_diff($taxonomies, get_taxonomies(array('_pll' => true)));
137 $taxonomies = array_unique(apply_filters('pll_get_taxonomies', $taxonomies , true));
138 break;
139
140 default:
141 break;
142 }
143
144 $using_permalinks = $GLOBALS['wp_rewrite']->using_permalinks();
145
146 $action = isset($_REQUEST['pll_action']) ? $_REQUEST['pll_action'] : '';
147
148 switch ($action) {
149 case 'add':
150 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
151
152 $error = $this->model->add_language($_POST);
153
154 if (0 == $error && !PLL_Admin::download_mo($_POST['locale']))
155 $error = 5;
156
157 wp_redirect('admin.php?page=mlang'. ($error ? '&error=' . $error : '') ); // to refresh the page (possible thanks to the $_GET['noheader']=true)
158 exit;
159 break;
160
161 case 'delete':
162 check_admin_referer('delete-lang');
163
164 if (!empty($_GET['lang']))
165 $this->model->delete_language((int) $_GET['lang']);
166
167 wp_redirect('admin.php?page=mlang'); // to refresh the page (possible thanks to the $_GET['noheader']=true)
168 exit;
169 break;
170
171 case 'edit':
172 if (!empty($_GET['lang']))
173 $edit_lang = $this->model->get_language((int) $_GET['lang']);
174
175 break;
176
177 case 'update':
178 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
179
180 $error = $this->model->update_language($_POST);
181
182 wp_redirect('admin.php?page=mlang'. ($error ? '&error=' . $error : '') ); // to refresh the page (possible thanks to the $_GET['noheader']=true)
183 exit;
184 break;
185
186 case 'string-translation':
187 if (!empty($_REQUEST['submit'])) {
188 check_admin_referer( 'string-translation', '_wpnonce_string-translation' );
189 $strings = $this->get_strings();
190
191 foreach ($this->model->get_languages_list() as $language) {
192 if(empty($_POST['translation'][$language->name])) // in case the language filter is active (thanks to John P. Bloch)
193 continue;
194
195 $mo = new PLL_MO();
196 $mo->import_from_db($language);
197
198 foreach ($_POST['translation'][$language->name] as $key=>$translation)
199 $mo->add_entry($mo->make_entry($strings[$key]['string'], stripslashes($translation)));
200
201 // clean database (removes all strings which were registered some day but are no more)
202 if (!empty($_POST['clean'])) {
203 $new_mo = new PLL_MO();
204
205 foreach ($strings as $string)
206 $new_mo->add_entry($mo->make_entry($string['string'], $mo->translate($string['string'])));
207 }
208
209 isset($new_mo) ? $new_mo->export_to_db($language) : $mo->export_to_db($language);
210 }
211 }
212
213 do_action('pll_save_strings_translations');
214
215 // unregisters strings registered through WPML API
216 if ($string_table->current_action() == 'delete' && !empty($_REQUEST['strings']) && function_exists('icl_unregister_string')) {
217 check_admin_referer( 'string-translation', '_wpnonce_string-translation' );
218 $strings = $this->get_strings();
219
220 foreach ($_REQUEST['strings'] as $key)
221 icl_unregister_string($strings[$key]['context'], $strings[$key]['name']);
222 }
223
224 // to refresh the page (possible thanks to the $_GET['noheader']=true)
225 $url = 'admin.php?page=mlang&tab=strings';
226 foreach(array('s', 'paged', 'group') as $qv)
227 $url = empty($_REQUEST[$qv]) ? $url : $url . '&' . $qv . '=' . $_REQUEST[$qv];
228 wp_redirect($url);
229 exit;
230 break;
231
232 case 'options':
233 check_admin_referer( 'options-lang', '_wpnonce_options-lang' );
234
235 $this->options['default_lang'] = sanitize_title($_POST['default_lang']); // we have slug as value
236
237 foreach(array('force_lang', 'rewrite') as $key)
238 $this->options[$key] = isset($_POST[$key]) ? (int) $_POST[$key] : 0;
239
240 // FIXME : TODO error message if not a valid url
241 if (isset($_POST['domains']) && is_array($_POST['domains'])) {
242 foreach ($_POST['domains'] as $key => $domain) {
243 $this->options['domains'][$key] = esc_url_raw(trim($domain));
244 }
245 }
246
247 foreach (array('browser', 'hide_default', 'redirect_lang', 'media_support') as $key)
248 $this->options[$key] = isset($_POST[$key]) ? 1 : 0;
249
250 if (3 == $this->options['force_lang'])
251 $this->options['browser'] = $this->options['hide_default'] = 0;
252
253 foreach (array('sync', 'post_types', 'taxonomies') as $key)
254 $this->options[$key] = empty($_POST[$key]) ? array() : array_keys($_POST[$key], 1);
255
256 update_option('polylang', $this->options);
257
258 // refresh rewrite rules in case rewrite, hide_default, post types or taxonomies options have been modified
259 // it seems useless to refresh permastruct here
260 flush_rewrite_rules();
261
262 // refresh language cache in case home urls have been modified
263 $this->model->clean_languages_cache();
264
265 // fills existing posts & terms with default language
266 if (isset($_POST['fill_languages']) && $nolang = $this->model->get_objects_with_no_lang()) {
267 if (!empty($nolang['posts']))
268 $this->model->set_language_in_mass('post', $nolang['posts'], $this->options['default_lang']);
269 if (!empty($nolang['terms']))
270 $this->model->set_language_in_mass('term', $nolang['terms'], $this->options['default_lang']);
271 }
272
273 wp_redirect('admin.php?page=mlang&tab=settings&updated=true'); // updated=true interpreted by WP
274 exit;
275 break;
276
277 default:
278 break;
279 }
280
281 // displays the page
282 include(PLL_ADMIN_INC.'/view-languages.php');
283 }
284
285 /*
286 * register strings for translation making sure it is not duplicate or empty
287 *
288 * @since 0.6
289 *
290 * @param string $name a unique name for the string
291 * @param string $string the string to register
292 * @param string $context optional the group in which the string is registered, defaults to 'polylang'
293 * @param bool $multiline optional wether the string table should display a multiline textarea or a single line input, defaults to single line
294 */
295 public function register_string($name, $string, $context = 'polylang', $multiline = false) {
296 // backward compatibility with Polylang older than 1.1
297 if (is_bool($context)) {
298 $multiline = $context;
299 $context = 'polylang';
300 }
301
302 $to_register = compact('name', 'string', 'context', 'multiline');
303 if (!in_array($to_register, $this->strings) && $to_register['string'])
304 $this->strings[] = $to_register;
305 }
306
307 /*
308 * get registered strings
309 *
310 * @since 0.6.1
311 *
312 * @return array list of all registered strings
313 */
314 public function &get_strings() {
315 // WP strings
316 $this->register_string(__('Site Title'), get_option('blogname'), 'WordPress');
317 $this->register_string(__('Tagline'), get_option('blogdescription'), 'WordPress');
318 $this->register_string(__('Date Format'), get_option('date_format'), 'WordPress');
319 $this->register_string(__('Time Format'), get_option('time_format'), 'WordPress');
320
321 // widgets titles
322 global $wp_registered_widgets;
323 $sidebars = wp_get_sidebars_widgets();
324 foreach ($sidebars as $sidebar => $widgets) {
325 if ($sidebar == 'wp_inactive_widgets' || empty($widgets))
326 continue;
327
328 foreach ($widgets as $widget) {
329 // nothing can be done if the widget is created using pre WP2.8 API :(
330 // there is no object, so we can't access it to get the widget options
331 if (!isset($wp_registered_widgets[$widget]['callback'][0]) || !is_object($wp_registered_widgets[$widget]['callback'][0]) || !method_exists($wp_registered_widgets[$widget]['callback'][0], 'get_settings'))
332 continue;
333
334 $widget_settings = $wp_registered_widgets[$widget]['callback'][0]->get_settings();
335 $number = $wp_registered_widgets[$widget]['params'][0]['number'];
336 // don't enable widget title translation if the widget is visible in only one language or if there is no title
337 if (empty($widget_settings[$number]['pll_lang']) && isset($widget_settings[$number]['title']) && $title = $widget_settings[$number]['title'])
338 $this->register_string(__('Widget title', 'polylang'), $title, 'Widget');
339 }
340 }
341
342 // allow plugins to modify our list of strings, mainly for use by our PLL_WPML_Compat class
343 $this->strings = apply_filters('pll_get_strings', $this->strings);
344 return $this->strings;
345 }
346
347 /*
348 * list the post metas to synchronize
349 *
350 * @since 1.0
351 *
352 * @return array
353 */
354 static public function list_metas_to_sync() {
355 return array(
356 'taxonomies' => __('Taxonomies', 'polylang'),
357 'post_meta' => __('Custom fields', 'polylang'),
358 'comment_status' => __('Comment status', 'polylang'),
359 'ping_status' => __('Ping status', 'polylang'),
360 'sticky_posts' => __('Sticky posts', 'polylang'),
361 'post_date' => __('Published date', 'polylang'),
362 'post_format' => __('Post format', 'polylang'),
363 'post_parent' => __('Page parent', 'polylang'),
364 '_wp_page_template' => __('Page template', 'polylang'),
365 'menu_order' => __('Page order', 'polylang'),
366 '_thumbnail_id' => __('Featured image', 'polylang'),
367 );
368 }
369 }
370