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