PluginProbe
Polylang / 1.6
Polylang v1.6
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.6, at admin/settings.php

347 lines 11.2 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 $active_tab;
11
12 /*
13 * constructor
14 *
15 * @since 1.2
16 *
17 * @param object $polylang
18 */
19 public function __construct(&$polylang) {
20 $this->links_model = &$polylang->links_model;
21 $this->model = &$polylang->model;
22 $this->options = &$polylang->options;
23
24 $this->active_tab = !empty($_GET['tab']) ? $_GET['tab'] : 'lang';
25
26 PLL_Admin_Strings::init();
27
28 // adds screen options and the about box in the languages admin panel
29 add_action('load-settings_page_mlang', array(&$this, 'load_page'));
30
31 // saves per-page value in screen option
32 add_filter('set-screen-option', create_function('$s, $o, $v', 'return $v;'), 10, 3);
33 }
34
35 /*
36 * adds screen options and the about box in the languages admin panel
37 *
38 * @since 0.9.5
39 */
40 public function load_page() {
41 // test of $this->active_tab avoids displaying the automatically generated screen options on other tabs
42 switch ($this->active_tab) {
43 case 'lang':
44 ob_start();
45 include(PLL_ADMIN_INC.'/view-recommended.php');
46 $content = trim(ob_get_contents());
47 ob_end_clean();
48
49 if (strlen($content) > 0) {
50 add_meta_box(
51 'pll_recommended',
52 __('Recommended plugins', 'polylang'),
53 create_function('', "echo '$content';"),
54 'settings_page_mlang',
55 'normal'
56 );
57 }
58
59 if (!defined('PLL_DISPLAY_ABOUT') || PLL_DISPLAY_ABOUT) {
60 add_meta_box(
61 'pll_about_box',
62 __('About Polylang', 'polylang'),
63 create_function('', "include(PLL_ADMIN_INC.'/view-about.php');"),
64 'settings_page_mlang',
65 'normal'
66 );
67 }
68
69 add_screen_option('per_page', array(
70 'label' => __('Languages', 'polylang'),
71 'default' => 10,
72 'option' => 'pll_lang_per_page'
73 ));
74 break;
75
76 case 'strings':
77 add_screen_option('per_page', array(
78 'label' => __('Strings translations', 'polylang'),
79 'default' => 10,
80 'option' => 'pll_strings_per_page'
81 ));
82 break;
83
84 default:
85 break;
86 }
87 }
88
89 /*
90 * diplays the 3 tabs pages: languages, strings translations, settings
91 * also manages user input for these pages
92 *
93 * @since 0.1
94 */
95 public function languages_page() {
96 // prepare the list of tabs
97 $tabs = array('lang' => __('Languages','polylang'));
98
99 // only if at least one language has been created
100 if ($listlanguages = $this->model->get_languages_list()) {
101 $tabs['strings'] = __('Strings translation','polylang');
102 $tabs['settings'] = __('Settings', 'polylang');
103 }
104
105 $tabs = apply_filters('pll_settings_tabs', $tabs);
106
107 switch($this->active_tab) {
108 case 'lang':
109 // prepare the list table of languages
110 $list_table = new PLL_Table_Languages();
111 $list_table->prepare_items($listlanguages);
112 break;
113
114 case 'strings':
115 // get the strings to translate
116 $data = PLL_Admin_Strings::get_strings();
117
118 $selected = empty($_REQUEST['group']) ? -1 : $_REQUEST['group'];
119 foreach ($data as $key=>$row) {
120 $groups[] = $row['context']; // get the groups
121
122 // filter for search string
123 if (($selected !=-1 && $row['context'] != $selected) || (!empty($_REQUEST['s']) && stripos($row['name'], $_REQUEST['s']) === false && stripos($row['string'], $_REQUEST['s']) === false))
124 unset ($data[$key]);
125 }
126
127 $groups = array_unique($groups);
128
129 // load translations
130 foreach ($listlanguages as $language) {
131 // filters by language if requested
132 if (($lg = get_user_meta(get_current_user_id(), 'pll_filter_content', true)) && $language->slug != $lg)
133 continue;
134
135 $mo = new PLL_MO();
136 $mo->import_from_db($language);
137 foreach ($data as $key=>$row) {
138 $data[$key]['translations'][$language->name] = $mo->translate($row['string']);
139 $data[$key]['row'] = $key; // store the row number for convenience
140 }
141 }
142
143 $string_table = new PLL_Table_String($groups, $selected);
144 $string_table->prepare_items($data);
145 break;
146
147 case 'settings':
148 $post_types = get_post_types(array('public' => true, '_builtin' => false));
149 $post_types = array_diff($post_types, get_post_types(array('_pll' => true)));
150 $post_types = array_unique(apply_filters('pll_get_post_types', $post_types, true));
151
152 $taxonomies = get_taxonomies(array('public' => true, '_builtin' => false));
153 $taxonomies = array_diff($taxonomies, get_taxonomies(array('_pll' => true)));
154 $taxonomies = array_unique(apply_filters('pll_get_taxonomies', $taxonomies , true));
155 break;
156
157 default:
158 break;
159 }
160
161 $action = isset($_REQUEST['pll_action']) ? $_REQUEST['pll_action'] : '';
162
163 switch ($action) {
164 case 'add':
165 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
166
167 if ($this->model->add_language($_POST)) {
168 // backward compatibility WP < 4.0
169 if (version_compare($GLOBALS['wp_version'], '4.0', '<')) {
170 PLL_Admin::download_mo($_POST['locale']);
171 }
172
173 else {
174 // attempts to install the language pack
175 require_once(ABSPATH . 'wp-admin/includes/translation-install.php');
176 if (!wp_download_language_pack($_POST['locale']))
177 add_settings_error('general', 'pll_download_mo', __('The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang'));
178
179 // force checking for themes and plugins translations updates
180 wp_update_themes();
181 wp_update_plugins();
182 }
183 }
184 $this->redirect(); // to refresh the page (possible thanks to the $_GET['noheader']=true)
185 break;
186
187 case 'delete':
188 check_admin_referer('delete-lang');
189
190 if (!empty($_GET['lang']))
191 $this->model->delete_language((int) $_GET['lang']);
192
193 $this->redirect(); // to refresh the page (possible thanks to the $_GET['noheader']=true)
194 break;
195
196 case 'edit':
197 if (!empty($_GET['lang']))
198 $edit_lang = $this->model->get_language((int) $_GET['lang']);
199 break;
200
201 case 'update':
202 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
203
204 $error = $this->model->update_language($_POST);
205
206 $this->redirect(); // to refresh the page (possible thanks to the $_GET['noheader']=true)
207 break;
208
209 case 'string-translation':
210 if (!empty($_REQUEST['submit'])) {
211 check_admin_referer( 'string-translation', '_wpnonce_string-translation' );
212 $strings = PLL_Admin_Strings::get_strings();
213
214 foreach ($this->model->get_languages_list() as $language) {
215 if(empty($_POST['translation'][$language->name])) // in case the language filter is active (thanks to John P. Bloch)
216 continue;
217
218 $mo = new PLL_MO();
219 $mo->import_from_db($language);
220
221 foreach ($_POST['translation'][$language->name] as $key => $translation) {
222 $translation = apply_filters('pll_sanitize_string_translation', $translation, $strings[$key]['name'], $strings[$key]['context']);
223 $mo->add_entry($mo->make_entry($strings[$key]['string'], $translation));
224 }
225
226 // clean database (removes all strings which were registered some day but are no more)
227 if (!empty($_POST['clean'])) {
228 $new_mo = new PLL_MO();
229
230 foreach ($strings as $string)
231 $new_mo->add_entry($mo->make_entry($string['string'], $mo->translate($string['string'])));
232 }
233
234 isset($new_mo) ? $new_mo->export_to_db($language) : $mo->export_to_db($language);
235 }
236 add_settings_error('general', 'pll_strings_translations_updated', __('Translations updated.', 'polylang'), 'updated');
237 }
238
239 do_action('pll_save_strings_translations');
240
241 // unregisters strings registered through WPML API
242 if ($string_table->current_action() == 'delete' && !empty($_REQUEST['strings']) && function_exists('icl_unregister_string')) {
243 check_admin_referer( 'string-translation', '_wpnonce_string-translation' );
244 $strings = PLL_Admin_Strings::get_strings();
245
246 foreach ($_REQUEST['strings'] as $key)
247 icl_unregister_string($strings[$key]['context'], $strings[$key]['name']);
248 }
249
250 // to refresh the page (possible thanks to the $_GET['noheader']=true)
251 $this->redirect(array_intersect_key($_REQUEST, array_flip(array('s', 'paged', 'group'))));
252 break;
253
254 case 'options':
255 check_admin_referer( 'options-lang', '_wpnonce_options-lang' );
256
257 $this->options['default_lang'] = sanitize_title($_POST['default_lang']); // we have slug as value
258
259 foreach(array('force_lang', 'rewrite') as $key)
260 $this->options[$key] = isset($_POST[$key]) ? (int) $_POST[$key] : 0;
261
262 if (3 == $this->options['force_lang'] && isset($_POST['domains']) && is_array($_POST['domains'])) {
263 foreach ($_POST['domains'] as $key => $domain) {
264 $this->options['domains'][$key] = esc_url_raw(trim($domain));
265 }
266 $this->options['domains'][$this->options['default_lang']] = get_option('home');
267 }
268
269 foreach (array('browser', 'hide_default', 'redirect_lang', 'media_support') as $key)
270 $this->options[$key] = isset($_POST[$key]) ? 1 : 0;
271
272 if (3 == $this->options['force_lang'])
273 $this->options['browser'] = $this->options['hide_default'] = 0;
274
275 foreach (array('sync', 'post_types', 'taxonomies') as $key)
276 $this->options[$key] = empty($_POST[$key]) ? array() : array_keys($_POST[$key], 1);
277
278 update_option('polylang', $this->options);
279
280 // refresh rewrite rules in case rewrite, hide_default, post types or taxonomies options have been modified
281 // it seems useless to refresh permastruct here
282 flush_rewrite_rules();
283
284 // refresh language cache in case home urls have been modified
285 $this->model->clean_languages_cache();
286
287 // fills existing posts & terms with default language
288 if (isset($_POST['fill_languages']) && $nolang = $this->model->get_objects_with_no_lang()) {
289 if (!empty($nolang['posts']))
290 $this->model->set_language_in_mass('post', $nolang['posts'], $this->options['default_lang']);
291 if (!empty($nolang['terms']))
292 $this->model->set_language_in_mass('term', $nolang['terms'], $this->options['default_lang']);
293 }
294
295 add_settings_error('general', 'settings_updated', __('Settings saved.'), 'updated');
296 $this->redirect();
297 break;
298
299 default:
300 break;
301 }
302
303 // displays the page
304 include(PLL_ADMIN_INC.'/view-languages.php');
305 }
306
307 /*
308 * list the post metas to synchronize
309 *
310 * @since 1.0
311 *
312 * @return array
313 */
314 static public function list_metas_to_sync() {
315 return array(
316 'taxonomies' => __('Taxonomies', 'polylang'),
317 'post_meta' => __('Custom fields', 'polylang'),
318 'comment_status' => __('Comment status', 'polylang'),
319 'ping_status' => __('Ping status', 'polylang'),
320 'sticky_posts' => __('Sticky posts', 'polylang'),
321 'post_date' => __('Published date', 'polylang'),
322 'post_format' => __('Post format', 'polylang'),
323 'post_parent' => __('Page parent', 'polylang'),
324 '_wp_page_template' => __('Page template', 'polylang'),
325 'menu_order' => __('Page order', 'polylang'),
326 '_thumbnail_id' => __('Featured image', 'polylang'),
327 );
328 }
329
330 /*
331 * redirects to language page (current active tab)
332 * saves error messages in a transient for reuse in redirected page
333 *
334 * @since 1.5
335 *
336 * @param array $args query arguments to add to the url
337 */
338 protected function redirect($args = array()) {
339 if ($errors = get_settings_errors()) {
340 set_transient('settings_errors', $errors, 30);
341 $args['settings-updated'] = 1;
342 }
343 wp_redirect(add_query_arg($args, wp_get_referer() ));
344 exit;
345 }
346 }
347