| 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 |
// get the groups |
| 119 |
foreach ($data as $key => $row) |
| 120 |
$groups[] = $row['context']; |
| 121 |
|
| 122 |
$groups = array_unique($groups); |
| 123 |
$selected = empty($_REQUEST['group']) || !in_array($_REQUEST['group'], $groups) ? -1 : $_REQUEST['group']; |
| 124 |
$s = empty($_REQUEST['s']) ? '' : wp_unslash($_REQUEST['s']); |
| 125 |
|
| 126 |
// filter for search string |
| 127 |
foreach ($data as $key => $row) { |
| 128 |
if (($selected !=-1 && $row['context'] != $selected) || (!empty($s) && stripos($row['name'], $s) === false && stripos($row['string'], $s) === false)) |
| 129 |
unset ($data[$key]); |
| 130 |
} |
| 131 |
|
| 132 |
// load translations |
| 133 |
foreach ($listlanguages as $language) { |
| 134 |
// filters by language if requested |
| 135 |
if (($lg = get_user_meta(get_current_user_id(), 'pll_filter_content', true)) && $language->slug != $lg) |
| 136 |
continue; |
| 137 |
|
| 138 |
$mo = new PLL_MO(); |
| 139 |
$mo->import_from_db($language); |
| 140 |
foreach ($data as $key=>$row) { |
| 141 |
$data[$key]['translations'][$language->slug] = $mo->translate($row['string']); |
| 142 |
$data[$key]['row'] = $key; // store the row number for convenience |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// get an array with language slugs as keys, names as values |
| 147 |
$languages = array_combine(wp_list_pluck($listlanguages, 'slug'), wp_list_pluck($listlanguages, 'name')); |
| 148 |
|
| 149 |
$string_table = new PLL_Table_String(compact('languages', 'groups', 'selected')); |
| 150 |
$string_table->prepare_items($data); |
| 151 |
break; |
| 152 |
|
| 153 |
case 'settings': |
| 154 |
$post_types = get_post_types(array('public' => true, '_builtin' => false)); |
| 155 |
$post_types = array_diff($post_types, get_post_types(array('_pll' => true))); |
| 156 |
$post_types = array_unique(apply_filters('pll_get_post_types', $post_types, true)); |
| 157 |
|
| 158 |
$taxonomies = get_taxonomies(array('public' => true, '_builtin' => false)); |
| 159 |
$taxonomies = array_diff($taxonomies, get_taxonomies(array('_pll' => true))); |
| 160 |
$taxonomies = array_unique(apply_filters('pll_get_taxonomies', $taxonomies , true)); |
| 161 |
break; |
| 162 |
|
| 163 |
default: |
| 164 |
break; |
| 165 |
} |
| 166 |
|
| 167 |
$action = isset($_REQUEST['pll_action']) ? $_REQUEST['pll_action'] : ''; |
| 168 |
|
| 169 |
switch ($action) { |
| 170 |
case 'add': |
| 171 |
check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 172 |
|
| 173 |
if ($this->model->add_language($_POST)) { |
| 174 |
// backward compatibility WP < 4.0 |
| 175 |
if (version_compare($GLOBALS['wp_version'], '4.0', '<')) { |
| 176 |
PLL_Admin::download_mo($_POST['locale']); |
| 177 |
} |
| 178 |
|
| 179 |
elseif ('en_US' != $_POST['locale']) { |
| 180 |
// attempts to install the language pack |
| 181 |
require_once(ABSPATH . 'wp-admin/includes/translation-install.php'); |
| 182 |
if (!wp_download_language_pack($_POST['locale'])) |
| 183 |
add_settings_error('general', 'pll_download_mo', __('The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang')); |
| 184 |
|
| 185 |
// force checking for themes and plugins translations updates |
| 186 |
wp_update_themes(); |
| 187 |
wp_update_plugins(); |
| 188 |
} |
| 189 |
} |
| 190 |
$this->redirect(); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 191 |
break; |
| 192 |
|
| 193 |
case 'delete': |
| 194 |
check_admin_referer('delete-lang'); |
| 195 |
|
| 196 |
if (!empty($_GET['lang'])) |
| 197 |
$this->model->delete_language((int) $_GET['lang']); |
| 198 |
|
| 199 |
$this->redirect(); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 200 |
break; |
| 201 |
|
| 202 |
case 'edit': |
| 203 |
if (!empty($_GET['lang'])) |
| 204 |
$edit_lang = $this->model->get_language((int) $_GET['lang']); |
| 205 |
break; |
| 206 |
|
| 207 |
case 'update': |
| 208 |
check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 209 |
|
| 210 |
$error = $this->model->update_language($_POST); |
| 211 |
|
| 212 |
$this->redirect(); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 213 |
break; |
| 214 |
|
| 215 |
case 'string-translation': |
| 216 |
if (!empty($_REQUEST['submit'])) { |
| 217 |
check_admin_referer( 'string-translation', '_wpnonce_string-translation' ); |
| 218 |
$strings = PLL_Admin_Strings::get_strings(); |
| 219 |
|
| 220 |
foreach ($this->model->get_languages_list() as $language) { |
| 221 |
if (empty($_POST['translation'][$language->slug])) // in case the language filter is active (thanks to John P. Bloch) |
| 222 |
continue; |
| 223 |
|
| 224 |
$mo = new PLL_MO(); |
| 225 |
$mo->import_from_db($language); |
| 226 |
|
| 227 |
foreach ($_POST['translation'][$language->slug] as $key => $translation) { |
| 228 |
$translation = apply_filters('pll_sanitize_string_translation', $translation, $strings[$key]['name'], $strings[$key]['context']); |
| 229 |
$mo->add_entry($mo->make_entry($strings[$key]['string'], $translation)); |
| 230 |
} |
| 231 |
|
| 232 |
// clean database (removes all strings which were registered some day but are no more) |
| 233 |
if (!empty($_POST['clean'])) { |
| 234 |
$new_mo = new PLL_MO(); |
| 235 |
|
| 236 |
foreach ($strings as $string) |
| 237 |
$new_mo->add_entry($mo->make_entry($string['string'], $mo->translate($string['string']))); |
| 238 |
} |
| 239 |
|
| 240 |
isset($new_mo) ? $new_mo->export_to_db($language) : $mo->export_to_db($language); |
| 241 |
} |
| 242 |
add_settings_error('general', 'pll_strings_translations_updated', __('Translations updated.', 'polylang'), 'updated'); |
| 243 |
} |
| 244 |
|
| 245 |
do_action('pll_save_strings_translations'); |
| 246 |
|
| 247 |
// unregisters strings registered through WPML API |
| 248 |
if ($string_table->current_action() == 'delete' && !empty($_REQUEST['strings']) && function_exists('icl_unregister_string')) { |
| 249 |
check_admin_referer( 'string-translation', '_wpnonce_string-translation' ); |
| 250 |
$strings = PLL_Admin_Strings::get_strings(); |
| 251 |
|
| 252 |
foreach ($_REQUEST['strings'] as $key) |
| 253 |
icl_unregister_string($strings[$key]['context'], $strings[$key]['name']); |
| 254 |
} |
| 255 |
|
| 256 |
// to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 257 |
$this->redirect(array_intersect_key($_REQUEST, array_flip(array('s', 'paged', 'group')))); |
| 258 |
break; |
| 259 |
|
| 260 |
case 'options': |
| 261 |
check_admin_referer( 'options-lang', '_wpnonce_options-lang' ); |
| 262 |
|
| 263 |
$this->options['default_lang'] = sanitize_title($_POST['default_lang']); // we have slug as value |
| 264 |
|
| 265 |
foreach(array('force_lang', 'rewrite') as $key) |
| 266 |
$this->options[$key] = isset($_POST[$key]) ? (int) $_POST[$key] : 0; |
| 267 |
|
| 268 |
if (3 == $this->options['force_lang'] && isset($_POST['domains']) && is_array($_POST['domains'])) { |
| 269 |
foreach ($_POST['domains'] as $key => $domain) { |
| 270 |
$this->options['domains'][$key] = esc_url_raw(trim($domain)); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
foreach (array('browser', 'hide_default', 'redirect_lang', 'media_support') as $key) |
| 275 |
$this->options[$key] = isset($_POST[$key]) ? 1 : 0; |
| 276 |
|
| 277 |
if (3 == $this->options['force_lang']) |
| 278 |
$this->options['browser'] = $this->options['hide_default'] = 0; |
| 279 |
|
| 280 |
foreach (array('sync', 'post_types', 'taxonomies') as $key) |
| 281 |
$this->options[$key] = empty($_POST[$key]) ? array() : array_keys($_POST[$key], 1); |
| 282 |
|
| 283 |
update_option('polylang', $this->options); |
| 284 |
|
| 285 |
// refresh rewrite rules in case rewrite, hide_default, post types or taxonomies options have been modified |
| 286 |
// it seems useless to refresh permastruct here |
| 287 |
flush_rewrite_rules(); |
| 288 |
|
| 289 |
// refresh language cache in case home urls have been modified |
| 290 |
$this->model->clean_languages_cache(); |
| 291 |
|
| 292 |
// fills existing posts & terms with default language |
| 293 |
if (isset($_POST['fill_languages']) && $nolang = $this->model->get_objects_with_no_lang()) { |
| 294 |
if (!empty($nolang['posts'])) |
| 295 |
$this->model->set_language_in_mass('post', $nolang['posts'], $this->options['default_lang']); |
| 296 |
if (!empty($nolang['terms'])) |
| 297 |
$this->model->set_language_in_mass('term', $nolang['terms'], $this->options['default_lang']); |
| 298 |
} |
| 299 |
|
| 300 |
add_settings_error('general', 'settings_updated', __('Settings saved.'), 'updated'); |
| 301 |
$this->redirect(); |
| 302 |
break; |
| 303 |
|
| 304 |
default: |
| 305 |
break; |
| 306 |
} |
| 307 |
|
| 308 |
// displays the page |
| 309 |
include(PLL_ADMIN_INC.'/view-languages.php'); |
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
* list the post metas to synchronize |
| 314 |
* |
| 315 |
* @since 1.0 |
| 316 |
* |
| 317 |
* @return array |
| 318 |
*/ |
| 319 |
static public function list_metas_to_sync() { |
| 320 |
return array( |
| 321 |
'taxonomies' => __('Taxonomies', 'polylang'), |
| 322 |
'post_meta' => __('Custom fields', 'polylang'), |
| 323 |
'comment_status' => __('Comment status', 'polylang'), |
| 324 |
'ping_status' => __('Ping status', 'polylang'), |
| 325 |
'sticky_posts' => __('Sticky posts', 'polylang'), |
| 326 |
'post_date' => __('Published date', 'polylang'), |
| 327 |
'post_format' => __('Post format', 'polylang'), |
| 328 |
'post_parent' => __('Page parent', 'polylang'), |
| 329 |
'_wp_page_template' => __('Page template', 'polylang'), |
| 330 |
'menu_order' => __('Page order', 'polylang'), |
| 331 |
'_thumbnail_id' => __('Featured image', 'polylang'), |
| 332 |
); |
| 333 |
} |
| 334 |
|
| 335 |
/* |
| 336 |
* redirects to language page (current active tab) |
| 337 |
* saves error messages in a transient for reuse in redirected page |
| 338 |
* |
| 339 |
* @since 1.5 |
| 340 |
* |
| 341 |
* @param array $args query arguments to add to the url |
| 342 |
*/ |
| 343 |
protected function redirect($args = array()) { |
| 344 |
if ($errors = get_settings_errors()) { |
| 345 |
set_transient('settings_errors', $errors, 30); |
| 346 |
$args['settings-updated'] = 1; |
| 347 |
} |
| 348 |
// remove possible 'pll_action' and 'lang' query args from the referer before redirecting |
| 349 |
wp_redirect(add_query_arg($args, remove_query_arg( array('pll_action', 'lang'), wp_get_referer() ))); |
| 350 |
exit; |
| 351 |
} |
| 352 |
} |
| 353 |
|