| 1 |
<?php |
| 2 |
|
| 3 |
require_once(INC_DIR.'/list-table.php'); |
| 4 |
|
| 5 |
class Polylang_Admin extends Polylang_Base { |
| 6 |
function __construct() { |
| 7 |
add_action('admin_init', array(&$this, 'admin_init')); |
| 8 |
|
| 9 |
// adds a 'settings' link in the plugins table |
| 10 |
$plugin_file = basename(POLYLANG_DIR).'/polylang.php'; |
| 11 |
add_filter('plugin_action_links_'.$plugin_file, array(&$this, 'plugin_action_links')); |
| 12 |
|
| 13 |
// adds the link to the languages panel in the wordpress admin menu |
| 14 |
add_action('admin_menu', array(&$this, 'add_menus')); |
| 15 |
|
| 16 |
// setup js scripts andd css styles |
| 17 |
add_action('admin_print_scripts', array($this,'admin_js')); |
| 18 |
add_action('admin_print_styles', array($this,'admin_css')); |
| 19 |
|
| 20 |
// add the language column (as well as a filter by language) in 'All Posts' an 'All Pages' panels |
| 21 |
add_filter('manage_posts_columns', array(&$this, 'add_post_column'), 10, 2); |
| 22 |
add_filter('manage_pages_columns', array(&$this, 'add_post_column')); |
| 23 |
add_action('manage_posts_custom_column', array(&$this, 'post_column'), 10, 2); |
| 24 |
add_action('manage_pages_custom_column', array(&$this, 'post_column'), 10, 2); |
| 25 |
add_filter('parse_query',array(&$this,'parse_query')); |
| 26 |
add_action('restrict_manage_posts', array(&$this, 'restrict_manage_posts')); |
| 27 |
|
| 28 |
// adds the Languages box in the 'Edit Post' and 'Edit Page' panels |
| 29 |
add_action('add_meta_boxes', array(&$this, 'add_meta_boxes')); |
| 30 |
|
| 31 |
// ajax response for post metabox |
| 32 |
add_action('wp_ajax_post_lang_choice', array($this,'post_lang_choice')); |
| 33 |
|
| 34 |
// adds actions related to languages when saving or deleting posts and pages |
| 35 |
add_action('save_post', array(&$this, 'save_post')); |
| 36 |
add_action('before_delete_post', array(&$this, 'delete_post')); |
| 37 |
|
| 38 |
// ajax response for edit term form |
| 39 |
add_action('wp_ajax_term_lang_choice', array($this,'term_lang_choice')); |
| 40 |
|
| 41 |
// modifies the theme location nav menu metabox |
| 42 |
add_filter('admin_head-nav-menus.php', array(&$this, 'nav_menu_theme_locations')); |
| 43 |
|
| 44 |
// widgets languages filter |
| 45 |
add_action('in_widget_form', array(&$this, 'in_widget_form')); |
| 46 |
add_filter('widget_update_callback', array(&$this, 'widget_update_callback'), 10, 4); |
| 47 |
|
| 48 |
// language management for users |
| 49 |
add_filter('locale', array(&$this, 'get_locale')); |
| 50 |
add_action('personal_options_update', array(&$this, 'personal_options_update')); |
| 51 |
add_action('personal_options', array(&$this, 'personal_options')); |
| 52 |
|
| 53 |
// refresh rewrite rules if the 'page_on_front' option is modified |
| 54 |
add_action('update_option', array(&$this, 'update_option')); |
| 55 |
} |
| 56 |
|
| 57 |
function admin_init() { |
| 58 |
// add these actions and filters here and not in the constructor to be sure all taxonomies are registered |
| 59 |
foreach (get_taxonomies(array('show_ui'=>true)) as $tax) { |
| 60 |
// adds the language field in the 'Categories' and 'Post Tags' panels |
| 61 |
add_action($tax.'_add_form_fields', array(&$this, 'add_term_form')); |
| 62 |
|
| 63 |
// adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels |
| 64 |
add_action($tax.'_edit_form_fields', array(&$this, 'edit_term_form')); |
| 65 |
|
| 66 |
// adds the language column in the 'Categories' and 'Post Tags' tables |
| 67 |
add_filter('manage_edit-'.$tax.'_columns', array(&$this, 'add_term_column')); |
| 68 |
add_action('manage_'.$tax.'_custom_column', array(&$this, 'term_column'), 10, 3); |
| 69 |
|
| 70 |
// adds actions related to languages when saving or deleting categories and post tags |
| 71 |
add_action('created_'.$tax, array(&$this, 'save_term')); |
| 72 |
add_action('edited_'.$tax, array(&$this, 'save_term')); |
| 73 |
add_action('delete_'.$tax, array(&$this, 'delete_term')); |
| 74 |
} |
| 75 |
|
| 76 |
// manage versions |
| 77 |
$options = get_option('polylang'); |
| 78 |
if ($options['version'] < POLYLANG_VERSION) { |
| 79 |
|
| 80 |
if($options['version'] < '0.4') |
| 81 |
$options['hide_default'] = 0; // option introduced in 0.4 |
| 82 |
|
| 83 |
$options['version'] = POLYLANG_VERSION; |
| 84 |
update_option('polylang', $options); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// adds a 'settings' link in the plugins table |
| 89 |
function plugin_action_links($links) { |
| 90 |
$settings_link = '<a href="admin.php?page=mlang">' . __('Settings') . '</a>'; |
| 91 |
array_unshift( $links, $settings_link ); |
| 92 |
return $links; |
| 93 |
} |
| 94 |
|
| 95 |
// adds the link to the languages panel in the wordpress admin menu |
| 96 |
function add_menus() { |
| 97 |
add_submenu_page('options-general.php', __('Languages', 'polylang'), __('Languages', 'polylang'), 'manage_options', 'mlang', array($this, 'languages_page')); |
| 98 |
} |
| 99 |
|
| 100 |
// setup js scripts |
| 101 |
function admin_js() { |
| 102 |
wp_enqueue_script('polylang_admin', WP_PLUGIN_URL .'/polylang/js/admin.js'); |
| 103 |
} |
| 104 |
|
| 105 |
// setup css styles |
| 106 |
function admin_css() { |
| 107 |
wp_enqueue_style('polylang_admin', WP_PLUGIN_URL .'/polylang/css/admin.css'); |
| 108 |
} |
| 109 |
|
| 110 |
// the languages panel |
| 111 |
function languages_page() { |
| 112 |
global $wp_rewrite; |
| 113 |
$options = get_option('polylang'); |
| 114 |
|
| 115 |
$listlanguages = $this->get_languages_list(); |
| 116 |
$list_table = new Polylang_List_Table(); |
| 117 |
|
| 118 |
// for nav menus form |
| 119 |
$locations = get_registered_nav_menus(); |
| 120 |
$menus = wp_get_nav_menus(); |
| 121 |
$menu_lang = get_option('polylang_nav_menus'); |
| 122 |
|
| 123 |
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; |
| 124 |
|
| 125 |
switch ($action) { |
| 126 |
case 'add': |
| 127 |
check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 128 |
$error = $this->validate_lang(); |
| 129 |
|
| 130 |
if ($error == 0) { |
| 131 |
wp_insert_term($_POST['name'],'language',array('slug'=>$_POST['slug'], 'description'=>$_POST['description'])); |
| 132 |
$wp_rewrite->flush_rules(); // refresh rewrite rules |
| 133 |
|
| 134 |
if (!isset($options['default_lang'])) { // if this is the first language created, set it as default language |
| 135 |
$options['default_lang'] = $_POST['slug']; |
| 136 |
update_option('polylang', $options); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
wp_redirect('admin.php?page=mlang'. ($error ? '&error='.$error : '') ); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 141 |
exit; |
| 142 |
break; |
| 143 |
|
| 144 |
case 'delete': |
| 145 |
check_admin_referer( 'delete-lang'); |
| 146 |
|
| 147 |
if (isset($_GET['lang']) && $_GET['lang']) { |
| 148 |
$lang_id = (int) $_GET['lang']; |
| 149 |
$lang = $this->get_language($lang_id); |
| 150 |
$lang_slug = $lang->slug; |
| 151 |
|
| 152 |
// delete all translations in posts in this language |
| 153 |
$args = array('numberposts'=>-1, 'taxonomy' => 'language', 'term' => $lang_slug, 'post_type'=>'any', 'post_status'=>'any'); |
| 154 |
$posts = get_posts($args); |
| 155 |
foreach ($posts as $post) { |
| 156 |
foreach ($listlanguages as $language) { |
| 157 |
delete_post_meta($post->ID, '_lang-'.$language->slug); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// delete references to this language in all posts |
| 162 |
$args = array('numberposts'=>-1, 'meta_key'=>'_lang-'.$lang_slug, 'post_type'=>'any', 'post_status'=>'any'); |
| 163 |
$posts = get_posts($args); |
| 164 |
foreach ($posts as $post) { |
| 165 |
delete_post_meta($post->ID, '_lang-'.$lang_slug); |
| 166 |
} |
| 167 |
|
| 168 |
// delete references to this language in categories & post tags |
| 169 |
$terms = get_terms(get_taxonomies(array('show_ui'=>true)), 'get=all'); |
| 170 |
foreach ($terms as $term) { |
| 171 |
if ($this->get_term_language($term->term_id) == $lang) { |
| 172 |
foreach ($listlanguages as $language) { |
| 173 |
delete_metadata('term', $term->term_id, '_lang-'.$language->slug); // deletes translations of this term |
| 174 |
} |
| 175 |
delete_metadata('term', $term->term_id, '_language', $lang_id); // delete language of this term |
| 176 |
} |
| 177 |
delete_metadata('term', $term->term_id, '_lang-'.$lang_slug); // deletes references to this term in translated term |
| 178 |
} |
| 179 |
|
| 180 |
// delete the language itself |
| 181 |
wp_delete_term($lang_id, 'language'); |
| 182 |
$wp_rewrite->flush_rules(); // refresh rewrite rules |
| 183 |
|
| 184 |
// oops ! we deleted the default language... |
| 185 |
if ($options['default_lang'] == $lang_slug) { |
| 186 |
if (!empty($listlanguages)) |
| 187 |
$options['default_lang'] = reset($this->get_languages_list())->slug; // arbitrary choice... |
| 188 |
else |
| 189 |
unset($options['default_lang']); |
| 190 |
update_option('polylang', $options); |
| 191 |
} |
| 192 |
} |
| 193 |
wp_redirect('admin.php?page=mlang'); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 194 |
exit; |
| 195 |
break; |
| 196 |
|
| 197 |
case 'edit': |
| 198 |
if (isset($_GET['lang']) && $_GET['lang']) |
| 199 |
$edit_lang = $this->get_language((int) $_GET['lang']); |
| 200 |
break; |
| 201 |
|
| 202 |
case 'update': |
| 203 |
check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 204 |
$lang_id = (int) $_POST['lang']; |
| 205 |
$lang = $this->get_language($lang_id); |
| 206 |
$error = $this->validate_lang($lang); |
| 207 |
|
| 208 |
if ($error == 0) { |
| 209 |
// Update links to this language in posts and terms in case the slug has been modified |
| 210 |
$old_slug = $lang->slug; |
| 211 |
|
| 212 |
if ($old_slug != $_POST['slug']) { |
| 213 |
// update the language slug in posts meta |
| 214 |
$args = array('numberposts'=>-1, 'meta_key'=>'_lang-'.$old_slug, 'post_type'=>'any', 'post_status'=>'any'); |
| 215 |
$posts = get_posts($args); |
| 216 |
foreach ($posts as $post) { |
| 217 |
$post_id = get_post_meta($post->ID, '_lang-'.$old_slug, true); |
| 218 |
delete_post_meta($post->ID, '_lang-'.$old_slug); |
| 219 |
update_post_meta($post->ID, '_lang-'.$_POST['slug'], $post_id); |
| 220 |
} |
| 221 |
|
| 222 |
// update the language slug in categories & post tags meta |
| 223 |
$terms = get_terms(get_taxonomies(array('show_ui'=>true)), 'get=all'); |
| 224 |
foreach ($terms as $term) { |
| 225 |
if ($term_id = get_metadata('term', $term->term_id, '_lang-'.$old_slug, true)) { |
| 226 |
delete_metadata('term', $term->term_id, '_lang-'.$old_slug); |
| 227 |
update_metadata('term', $term->term_id, '_lang-'.$_POST['slug'], $term_id); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
// and finally update the language itself |
| 233 |
wp_update_term($lang_id, 'language', array('name'=>$_POST['name'], 'slug'=>$_POST['slug'], 'description'=>$_POST['description'])); |
| 234 |
$wp_rewrite->flush_rules(); // refresh rewrite rules |
| 235 |
} |
| 236 |
|
| 237 |
wp_redirect('admin.php?page=mlang'. ($error ? '&error='.$error : '') ); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 238 |
exit; |
| 239 |
break; |
| 240 |
|
| 241 |
case 'nav-menus': |
| 242 |
check_admin_referer( 'nav-menus-lang', '_wpnonce_nav-menus-lang' ); |
| 243 |
|
| 244 |
$menu_lang = $_POST['menu-lang']; |
| 245 |
update_option('polylang_nav_menus', $menu_lang); |
| 246 |
break; |
| 247 |
|
| 248 |
case 'options': |
| 249 |
check_admin_referer( 'options-lang', '_wpnonce_options-lang' ); |
| 250 |
|
| 251 |
$options['default_lang'] = $_POST['default_lang']; |
| 252 |
$options['browser'] = isset($_POST['browser']) ? 1 : 0; |
| 253 |
$options['rewrite'] = $_POST['rewrite']; |
| 254 |
$options['hide_default'] = isset($_POST['hide_default']) ? 1 : 0; |
| 255 |
update_option('polylang', $options); |
| 256 |
|
| 257 |
// refresh refresh permalink structure and rewrite rules in case rewrite or hide_default options have been modified |
| 258 |
$wp_rewrite->extra_permastructs['language'][0] = $options['rewrite'] ? '%language%' : '/language/%language%'; |
| 259 |
$wp_rewrite->flush_rules(); |
| 260 |
|
| 261 |
// fills existing posts & terms with default language |
| 262 |
if (isset($_POST['fill_languages'])) { |
| 263 |
if(isset($_POST['posts'])) { |
| 264 |
foreach(explode(',', $_POST['posts']) as $post_id) { |
| 265 |
wp_set_post_terms($post_id, $options['default_lang'], 'language' ); |
| 266 |
} |
| 267 |
} |
| 268 |
if(isset($_POST['terms'])) { |
| 269 |
foreach(explode(',', $_POST['terms']) as $term_id) { |
| 270 |
update_metadata('term', $term_id, '_language', $options['default_lang'] ); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
break; |
| 275 |
|
| 276 |
default: |
| 277 |
} |
| 278 |
|
| 279 |
// prepare the list table of languages |
| 280 |
$data = array(); |
| 281 |
foreach ($listlanguages as $lang) |
| 282 |
$data[] = (array) $lang; |
| 283 |
|
| 284 |
$list_table->prepare_items($data); |
| 285 |
|
| 286 |
|
| 287 |
// detects posts & pages without language set |
| 288 |
$q = array( |
| 289 |
'numberposts'=>-1, |
| 290 |
'post_type' => 'any', |
| 291 |
'post_status'=>'any', |
| 292 |
'fields' => 'ids', |
| 293 |
'tax_query' => array(array( |
| 294 |
'taxonomy'=> 'language', |
| 295 |
'terms'=> get_terms('language', array('fields'=>'ids')), |
| 296 |
'operator'=>'NOT IN' |
| 297 |
)) |
| 298 |
); |
| 299 |
$posts = implode(',', get_posts($q)); |
| 300 |
|
| 301 |
// detects categories & post tags without language set |
| 302 |
$terms = get_terms(get_taxonomies(array('show_ui'=>true)), array('get'=>'all', 'fields'=>'ids')); |
| 303 |
foreach ($terms as $key => $term_id) { |
| 304 |
if (get_metadata('term', $term_id, '_language')) |
| 305 |
unset($terms[$key]); |
| 306 |
} |
| 307 |
$terms = implode(',', $terms); |
| 308 |
|
| 309 |
$errors[1] = __('Enter a valid WorPress locale', 'polylang'); |
| 310 |
$errors[2] = __('The language code must be 2 characters long', 'polylang'); |
| 311 |
$errors[3] = __('The language code must be unique', 'polylang'); |
| 312 |
$errors[4] = __('The language must have a name', 'polylang'); |
| 313 |
|
| 314 |
// displays the page |
| 315 |
include(INC_DIR.'/languages-form.php'); |
| 316 |
} |
| 317 |
|
| 318 |
// validates data entered when creating or updating a language |
| 319 |
function validate_lang($lang = null) { |
| 320 |
// validate locale |
| 321 |
$loc = $_POST['description']; |
| 322 |
if ( !preg_match('#^[a-z]{2}$#', $loc) && !preg_match('#^[a-z]{2}_[A-Z]{2}$#', $loc) ) |
| 323 |
$error = 1; |
| 324 |
|
| 325 |
// validate slug length |
| 326 |
if (strlen($_POST['slug']) != 2) |
| 327 |
$error = 2; |
| 328 |
|
| 329 |
// validate slug is unique |
| 330 |
if ($this->get_language($_POST['slug']) != null && isset($lang) && $lang->slug != $_POST['slug']) |
| 331 |
$error = 3; |
| 332 |
|
| 333 |
// validate name |
| 334 |
if ($_POST['name'] == '') |
| 335 |
$error = 4; |
| 336 |
|
| 337 |
return isset($error) ? $error : 0; |
| 338 |
} |
| 339 |
|
| 340 |
// adds the language column (before the date column) in the posts and pages list table |
| 341 |
function add_post_column($columns, $post_type ='') { |
| 342 |
if ($post_type == '' || get_post_type_object($post_type)->show_ui) { |
| 343 |
foreach (array( 'date', 'comments' ) as $k) { |
| 344 |
if (array_key_exists($k, $columns)) |
| 345 |
$end[$k] = array_pop($columns); |
| 346 |
} |
| 347 |
|
| 348 |
$columns['language'] = __('Language', 'polylang'); |
| 349 |
|
| 350 |
if (isset($end)) |
| 351 |
$columns = array_merge($columns, $end); |
| 352 |
} |
| 353 |
return $columns; |
| 354 |
} |
| 355 |
|
| 356 |
// fills the language column in the posts table |
| 357 |
function post_column($column, $post_id) { |
| 358 |
if ($column == 'language' && $lang = $this->get_post_language($post_id)) |
| 359 |
echo esc_attr($lang->name); |
| 360 |
} |
| 361 |
|
| 362 |
// converts language term_id to slug in $query |
| 363 |
// needed to filter the posts by language with wp_dropdown_categories in restrict_manage_posts |
| 364 |
function parse_query($query) { |
| 365 |
global $pagenow; |
| 366 |
$qvars = &$query->query_vars; |
| 367 |
if ($pagenow=='edit.php' && isset($qvars['lang']) && $qvars['lang'] && is_numeric($qvars['lang']) && $lang = $this->get_language($qvars['lang'])) |
| 368 |
$qvars['lang'] = $lang->slug; |
| 369 |
} |
| 370 |
|
| 371 |
// adds a filter for languages in the Posts and Pages panels |
| 372 |
function restrict_manage_posts() { |
| 373 |
global $wp_query; |
| 374 |
$screen = get_current_screen(); // since WP 3.1 |
| 375 |
$languages = $this->get_languages_list(); |
| 376 |
|
| 377 |
if (!empty($languages) && $screen->base == 'edit') { |
| 378 |
$qvars = $wp_query->query; |
| 379 |
wp_dropdown_categories(array( |
| 380 |
'show_option_all' => __('Show all languages', 'polylang'), |
| 381 |
'name' => 'lang', |
| 382 |
'selected' => isset($qvars['lang']) ? $qvars['lang'] : 0, |
| 383 |
'taxonomy' => 'language', |
| 384 |
'hide_empty' => 0 |
| 385 |
)); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
// adds the Languages box in the 'Edit Post' and 'Edit Page' panels (as well as in custom post types panels |
| 390 |
function add_meta_boxes() { |
| 391 |
foreach(get_post_types( array( 'show_ui' => true ) ) as $ptype) |
| 392 |
add_meta_box('ml_box', __('Languages','polylang'), array(&$this,'post_language'), $ptype, 'side','high'); |
| 393 |
} |
| 394 |
|
| 395 |
// the Languages metabox in the 'Edit Post' and 'Edit Page' panels |
| 396 |
function post_language() { |
| 397 |
global $post_ID; |
| 398 |
$post_type = get_post_type($post_ID); |
| 399 |
|
| 400 |
$lang = $this->get_post_language($post_ID); |
| 401 |
if (isset($_GET['new_lang'])) |
| 402 |
$lang = $this->get_language($_GET['new_lang']); |
| 403 |
|
| 404 |
$listlanguages = $this->get_languages_list(); |
| 405 |
|
| 406 |
include(INC_DIR.'/post-metabox.php'); |
| 407 |
} |
| 408 |
|
| 409 |
// ajax response for post metabox |
| 410 |
function post_lang_choice() { |
| 411 |
$listlanguages = $this->get_languages_list(); |
| 412 |
|
| 413 |
$lang = $this->get_language($_POST['lang']); |
| 414 |
$post_ID = $_POST['post_id']; |
| 415 |
$post_type = get_post_type($post_ID); |
| 416 |
|
| 417 |
if ($lang && !is_wp_error($lang)) |
| 418 |
include(INC_DIR.'/post-translations.php'); |
| 419 |
|
| 420 |
die(); |
| 421 |
} |
| 422 |
|
| 423 |
// called when a post (or page) is saved, published or updated |
| 424 |
// the underscore in '_lang' hides the post meta in the Custom Fields metabox in the Edit Post screen |
| 425 |
function save_post($post_id) { |
| 426 |
if ($id = wp_is_post_revision($post_id)) |
| 427 |
$post_id = $id; |
| 428 |
|
| 429 |
if (isset($_POST['post_lang_choice'])) |
| 430 |
wp_set_post_terms($post_id, $_POST['post_lang_choice'], 'language' ); |
| 431 |
|
| 432 |
$lang = $this->get_post_language($post_id); |
| 433 |
$listlanguages = $this->get_languages_list(); |
| 434 |
|
| 435 |
foreach ($listlanguages as $language) { |
| 436 |
|
| 437 |
if (isset($_POST[$language->slug]) && $_POST[$language->slug]) { |
| 438 |
// FIXME I don't check that the translated post is in the right language ! |
| 439 |
update_post_meta($post_id, '_lang-'.$language->slug, $_POST[$language->slug]); // saves the links to translated posts |
| 440 |
if ($lang) |
| 441 |
update_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lang->slug, $post_id); // tells the translated to link to this post |
| 442 |
} |
| 443 |
else { |
| 444 |
// deletes the translation in case there was previously one |
| 445 |
if ($lang) |
| 446 |
delete_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lang->slug); // in the translated post |
| 447 |
|
| 448 |
delete_post_meta($post_id, '_lang-'.$language->slug); // in this post |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
// propagates translations between them |
| 453 |
foreach ($listlanguages as $language) { |
| 454 |
foreach ($listlanguages as $lg) { |
| 455 |
if ($lg != $lang && $lg != $language && $id = $this->get_translated_post($post_id, $lg)) |
| 456 |
update_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lg->slug, $id); |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
// called when a post (or page) is deleted |
| 462 |
function delete_post($post_id) { |
| 463 |
if ($id = wp_is_post_revision($post_id)) |
| 464 |
$post_id = $id; |
| 465 |
|
| 466 |
if($lang = $this->get_post_language($post_id)) { |
| 467 |
foreach ($this->get_languages_list() as $language) { |
| 468 |
delete_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lang->slug); // delete links to this post |
| 469 |
// WP deletes post metas linked to this post so it is useless to do it before |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
// adds the language field in the 'Categories' and 'Post Tags' panels |
| 475 |
function add_term_form() { |
| 476 |
$taxonomy = $_GET['taxonomy']; |
| 477 |
$lang = null; |
| 478 |
if (isset($_GET['new_lang'])) |
| 479 |
$lang = $this->get_language($_GET['new_lang']); |
| 480 |
|
| 481 |
$listlanguages = $this->get_languages_list(); |
| 482 |
|
| 483 |
// displays the language field |
| 484 |
include(INC_DIR.'/add-term-form.php'); |
| 485 |
} |
| 486 |
|
| 487 |
// adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels |
| 488 |
function edit_term_form($tag) { |
| 489 |
$term_id = $tag->term_id; |
| 490 |
$lang = $this->get_term_language($term_id); |
| 491 |
$taxonomy = $tag->taxonomy; |
| 492 |
$listlanguages = $this->get_languages_list(); |
| 493 |
|
| 494 |
include(INC_DIR.'/edit-term-form.php'); |
| 495 |
} |
| 496 |
|
| 497 |
// adds the language column (before the posts column) in the 'Categories' or Post Tags table |
| 498 |
function add_term_column($columns) { |
| 499 |
if (array_key_exists('posts', $columns)) |
| 500 |
$end = array_pop($columns); |
| 501 |
|
| 502 |
$columns['language'] = __('Language', 'polylang'); |
| 503 |
|
| 504 |
if (isset($end)) |
| 505 |
$columns['posts'] = $end; |
| 506 |
|
| 507 |
return $columns; |
| 508 |
} |
| 509 |
|
| 510 |
// fills the language column in the 'Categories' or Post Tags table |
| 511 |
function term_column($empty, $column, $term_id) { |
| 512 |
if ($column == 'language' && $lang = $this->get_term_language($term_id)) |
| 513 |
echo esc_attr($lang->name); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
// called when a category or post tag is created or edited |
| 518 |
function save_term($term_id) { |
| 519 |
|
| 520 |
if (isset($_POST['term_lang_choice']) && $_POST['term_lang_choice']) |
| 521 |
update_metadata('term', $term_id, '_language', $_POST['term_lang_choice'] ); |
| 522 |
else |
| 523 |
delete_metadata('term', $term_id, '_language'); |
| 524 |
|
| 525 |
|
| 526 |
$lang = $this->get_term_language($term_id); |
| 527 |
$listlanguages = $this->get_languages_list(); |
| 528 |
|
| 529 |
foreach ($listlanguages as $language) { |
| 530 |
$slug = '_lang-'.$language->slug; |
| 531 |
if (isset($_POST[$slug]) && $_POST[$slug]) { |
| 532 |
// FIXME I don't check that the translated term is in the right language ! |
| 533 |
update_metadata('term', $term_id, $slug, $_POST[$slug] ); |
| 534 |
if ($lang) |
| 535 |
update_metadata('term', $_POST[$slug], '_lang-'.$lang->slug, $term_id ); |
| 536 |
} |
| 537 |
else { |
| 538 |
// deletes the translation in case there was previously one |
| 539 |
if ($lang) |
| 540 |
delete_metadata('term', $this->get_translated_term($term_id, $language), '_lang-'.$lang->slug); // in the translated term |
| 541 |
delete_metadata('term', $term_id, '_lang-'.$language->slug); // in this term |
| 542 |
} |
| 543 |
|
| 544 |
} |
| 545 |
|
| 546 |
// propagates translations between them |
| 547 |
if (isset($lang)) { |
| 548 |
foreach ($listlanguages as $language) { |
| 549 |
foreach ($listlanguages as $lg) { |
| 550 |
if ($lg != $lang && $lg != $language && $id = $this->get_translated_term($term_id, $lg)) |
| 551 |
update_metadata('term',$this->get_translated_term($term_id, $language), '_lang-'.$lg->slug, $id); |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
} |
| 557 |
|
| 558 |
// called when a category or post tag is deleted |
| 559 |
function delete_term($term_id) { |
| 560 |
if($lang = $this->get_term_language($term_id)) { |
| 561 |
delete_metadata('term', $term_id, '_language' ); |
| 562 |
$listlanguages = $this->get_languages_list(); |
| 563 |
foreach ($listlanguages as $language) { |
| 564 |
delete_metadata('term', $this->get_translated_term($term_id, $language), '_lang-'.$lang->slug); // delete links to this term |
| 565 |
} |
| 566 |
foreach ($listlanguages as $language) { |
| 567 |
delete_metadata('term', $term_id, '_lang-'.$language->slug); |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
// returns all terms in the $taxonomy in the $term_language which have no translation in the $translation_language |
| 573 |
function get_terms_not_translated($taxonomy, $term_language, $translation_language) { |
| 574 |
$new_terms = array(); |
| 575 |
foreach (get_terms($taxonomy, 'hide_empty=0') as $term) { |
| 576 |
$lang = $this->get_term_language($term->term_id); |
| 577 |
if ($lang && $lang->name == $term_language->name && !$this->get_translated_term($term->term_id, $translation_language)) |
| 578 |
$new_terms[] = $term; |
| 579 |
} |
| 580 |
return $new_terms; |
| 581 |
} |
| 582 |
|
| 583 |
// ajax response for edit term form |
| 584 |
function term_lang_choice() |
| 585 |
{ |
| 586 |
if ($_POST['lang']) { |
| 587 |
$lang = $this->get_language($_POST['lang']); |
| 588 |
$term_id = isset($_POST['term_id']) ? $_POST['term_id'] : null; |
| 589 |
$taxonomy = $_POST['taxonomy']; |
| 590 |
|
| 591 |
$listlanguages = $this->get_languages_list(); |
| 592 |
|
| 593 |
include(INC_DIR.'/term-translations.php'); |
| 594 |
} |
| 595 |
die(); |
| 596 |
} |
| 597 |
|
| 598 |
// modifies the theme location nav menu metabox |
| 599 |
function nav_menu_theme_locations() { |
| 600 |
// only if the theme supports nav menus and a nav menu exists |
| 601 |
if ( ! current_theme_supports( 'menus' ) || !isset($_REQUEST['menu']) ) |
| 602 |
return; |
| 603 |
|
| 604 |
// thanks to: http://wordpress.stackexchange.com/questions/2770/how-to-add-a-custom-metabox-to-the-menu-management-admin-screen |
| 605 |
$metabox = &$GLOBALS['wp_meta_boxes']['nav-menus']['side']['default']['nav-menu-theme-locations']; |
| 606 |
$metabox['callback'] = array(&$this,'nav_menu_language'); |
| 607 |
$metabox['title'] = __('Theme locations and languages', 'polylang'); |
| 608 |
} |
| 609 |
|
| 610 |
// displays a message to redirect to the languages options page |
| 611 |
function nav_menu_language() { |
| 612 |
echo '<p class="howto">' . sprintf (__('Please go to the %slanguages page%s to set theme locations and languages', 'polylang'), |
| 613 |
'<a href="' . admin_url('options-general.php?page=mlang#menus') . '">', '</a>') . '</p>'; |
| 614 |
} |
| 615 |
|
| 616 |
// modifies the widgets forms to add our language dropdwown list |
| 617 |
function in_widget_form($widget) { |
| 618 |
$id = esc_attr($widget->id.'_lang_choice'); |
| 619 |
$widget_lang = get_option('polylang_widgets'); |
| 620 |
|
| 621 |
printf( |
| 622 |
'<p><label for="%s">%s<select name="%s" id="%s" class="tags-input"><option value="0">%s</option>', |
| 623 |
$id, |
| 624 |
__('The widget is displayed for:', 'polylang'), |
| 625 |
$id, |
| 626 |
$id, |
| 627 |
__('All languages', 'polylang') |
| 628 |
); |
| 629 |
foreach ($this->get_languages_list() as $language) { |
| 630 |
printf( |
| 631 |
"<option value='%s'%s>%s</option>\n", |
| 632 |
esc_attr($language->slug), |
| 633 |
isset($widget_lang[$widget->id]) && $language->slug == $widget_lang[$widget->id] ? ' selected="selected"' : '', |
| 634 |
esc_attr($language->name) |
| 635 |
); |
| 636 |
} |
| 637 |
echo '</select></label></p>'; |
| 638 |
} |
| 639 |
|
| 640 |
// called when widget options are saved |
| 641 |
function widget_update_callback($instance, $new_instance, $old_instance, $widget) { |
| 642 |
$widget_lang = get_option('polylang_widgets'); |
| 643 |
$widget_lang[$widget->id] = $_POST[$widget->id.'_lang_choice']; |
| 644 |
update_option('polylang_widgets', $widget_lang); |
| 645 |
return $instance; |
| 646 |
} |
| 647 |
|
| 648 |
// returns the locale based on user preference |
| 649 |
function get_locale($locale) { |
| 650 |
return get_user_meta(get_current_user_id(), 'user_lang', 'true'); |
| 651 |
} |
| 652 |
|
| 653 |
// updates language user preference |
| 654 |
function personal_options_update($user_id) { |
| 655 |
update_user_meta($user_id, 'user_lang', $_POST['user_lang']); |
| 656 |
} |
| 657 |
|
| 658 |
// form for language user preference |
| 659 |
function personal_options($profileuser) { |
| 660 |
include(INC_DIR.'/personal-options.php'); |
| 661 |
} |
| 662 |
|
| 663 |
// refresh rewrite rules if the 'page_on_front' option is modified |
| 664 |
function update_option($option) { |
| 665 |
global $wp_rewrite; |
| 666 |
if ($option == 'page_on_front') |
| 667 |
$wp_rewrite->flush_rules(); |
| 668 |
} |
| 669 |
} // class Polylang_Admin |
| 670 |
|
| 671 |
?> |
| 672 |
|