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

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

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