| 1 |
<?php |
| 2 |
|
| 3 |
require_once(PLL_INC.'/list-table.php'); |
| 4 |
require_once(PLL_INC.'/admin-filters.php'); |
| 5 |
|
| 6 |
// setups the Polylang admin panel and calls for other admin related classes |
| 7 |
class Polylang_Admin extends Polylang_Base { |
| 8 |
function __construct() { |
| 9 |
new Polylang_Admin_Filters(); |
| 10 |
|
| 11 |
// adds a 'settings' link in the plugins table |
| 12 |
$plugin_file = basename(POLYLANG_DIR).'/polylang.php'; |
| 13 |
add_filter('plugin_action_links_'.$plugin_file, array(&$this, 'plugin_action_links')); |
| 14 |
|
| 15 |
// adds the link to the languages panel in the wordpress admin menu |
| 16 |
add_action('admin_menu', array(&$this, 'add_menus')); |
| 17 |
} |
| 18 |
|
| 19 |
// adds a 'settings' link in the plugins table |
| 20 |
function plugin_action_links($links) { |
| 21 |
$settings_link = '<a href="admin.php?page=mlang">' . __('Settings') . '</a>'; |
| 22 |
array_unshift( $links, $settings_link ); |
| 23 |
return $links; |
| 24 |
} |
| 25 |
|
| 26 |
// adds the link to the languages panel in the wordpress admin menu |
| 27 |
function add_menus() { |
| 28 |
add_submenu_page('options-general.php', __('Languages', 'polylang'), __('Languages', 'polylang'), 'manage_options', 'mlang', array(&$this, 'languages_page')); |
| 29 |
} |
| 30 |
|
| 31 |
// used to update the translation when a language slug has been modified |
| 32 |
function update_translations($type, $ids, $old_slug) { |
| 33 |
foreach ($ids as $id) { |
| 34 |
$tr = get_metadata($type, $id, '_translations', true); |
| 35 |
if($tr) { |
| 36 |
$tr = unserialize($tr); |
| 37 |
$tr[$_POST['slug']] = $tr[$old_slug]; |
| 38 |
unset($tr[$old_slug]); |
| 39 |
update_metadata($type, $id, '_translations', serialize($tr)); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// used to delete the translation when a language is deleted |
| 45 |
function delete_translations($type, $ids, $old_slug) { |
| 46 |
foreach ($ids as $id) { |
| 47 |
$tr = get_metadata($type, $id, '_translations', true); |
| 48 |
if($tr) { |
| 49 |
$tr = unserialize($tr); |
| 50 |
unset($tr[$old_slug]); |
| 51 |
update_metadata($type, $id, '_translations', serialize($tr)); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// the languages panel |
| 57 |
function languages_page() { |
| 58 |
global $wp_rewrite; |
| 59 |
$options = get_option('polylang'); |
| 60 |
|
| 61 |
$listlanguages = $this->get_languages_list(); |
| 62 |
$list_table = new Polylang_List_Table(); |
| 63 |
|
| 64 |
// for nav menus form |
| 65 |
$locations = get_registered_nav_menus(); |
| 66 |
$menus = wp_get_nav_menus(); |
| 67 |
$menu_lang = get_option('polylang_nav_menus'); |
| 68 |
|
| 69 |
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; |
| 70 |
|
| 71 |
switch ($action) { |
| 72 |
case 'add': |
| 73 |
check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 74 |
$error = $this->validate_lang(); |
| 75 |
|
| 76 |
if ($error == 0) { |
| 77 |
wp_insert_term($_POST['name'],'language',array('slug'=>$_POST['slug'], 'description'=>$_POST['description'])); |
| 78 |
$wp_rewrite->flush_rules(); // refresh rewrite rules |
| 79 |
|
| 80 |
if (!isset($options['default_lang'])) { // if this is the first language created, set it as default language |
| 81 |
$options['default_lang'] = $_POST['slug']; |
| 82 |
update_option('polylang', $options); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
wp_redirect('admin.php?page=mlang'. ($error ? '&error='.$error : '') ); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 87 |
exit; |
| 88 |
break; |
| 89 |
|
| 90 |
case 'delete': |
| 91 |
check_admin_referer('delete-lang'); |
| 92 |
|
| 93 |
if (isset($_GET['lang']) && $_GET['lang']) { |
| 94 |
$lang_id = (int) $_GET['lang']; |
| 95 |
$lang = $this->get_language($lang_id); |
| 96 |
$lang_slug = $lang->slug; |
| 97 |
|
| 98 |
// update the language slug in posts meta |
| 99 |
$posts = get_posts(array('numberposts'=>-1, 'fields' => 'ids', 'meta_key'=>'_translations', 'post_type'=>'any', 'post_status'=>'any')); |
| 100 |
$this->delete_translations('post', $posts, $lang_slug); |
| 101 |
|
| 102 |
// update the language slug in categories & post tags meta |
| 103 |
$terms= get_terms(get_taxonomies(array('show_ui'=>true)), array('get'=>'all', 'fields'=>'ids')); |
| 104 |
$this->delete_translations('term', $terms, $lang_slug); |
| 105 |
|
| 106 |
foreach ($terms as $id) |
| 107 |
$this->delete_term_language($id); // delete language of this term |
| 108 |
|
| 109 |
// delete menus locations |
| 110 |
foreach ($locations as $location => $description) { |
| 111 |
unset($menu_lang[$location][$lang_slug]); |
| 112 |
} |
| 113 |
update_option('polylang_nav_menus', $menu_lang); |
| 114 |
|
| 115 |
// delete the language itself |
| 116 |
wp_delete_term($lang_id, 'language'); |
| 117 |
$wp_rewrite->flush_rules(); // refresh rewrite rules |
| 118 |
|
| 119 |
// oops ! we deleted the default language... |
| 120 |
if ($options['default_lang'] == $lang_slug) { |
| 121 |
if (!empty($listlanguages)) |
| 122 |
$options['default_lang'] = reset($this->get_languages_list())->slug; // arbitrary choice... |
| 123 |
else |
| 124 |
unset($options['default_lang']); |
| 125 |
update_option('polylang', $options); |
| 126 |
} |
| 127 |
} |
| 128 |
wp_redirect('admin.php?page=mlang'); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 129 |
exit; |
| 130 |
break; |
| 131 |
|
| 132 |
case 'edit': |
| 133 |
if (isset($_GET['lang']) && $_GET['lang']) |
| 134 |
$edit_lang = $this->get_language((int) $_GET['lang']); |
| 135 |
break; |
| 136 |
|
| 137 |
case 'update': |
| 138 |
check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 139 |
$lang_id = (int) $_POST['lang_id']; |
| 140 |
$lang = $this->get_language($lang_id); |
| 141 |
$error = $this->validate_lang($lang); |
| 142 |
|
| 143 |
if ($error == 0) { |
| 144 |
// Update links to this language in posts and terms in case the slug has been modified |
| 145 |
$old_slug = $lang->slug; |
| 146 |
|
| 147 |
if ($old_slug != $_POST['slug']) { |
| 148 |
// update the language slug in posts meta |
| 149 |
$posts = get_posts(array('numberposts'=>-1, 'fields' => 'ids', 'meta_key'=>'_translations', 'post_type'=>'any', 'post_status'=>'any')); |
| 150 |
$this->update_translations('post', $posts, $old_slug); |
| 151 |
|
| 152 |
// update the language slug in categories & post tags meta |
| 153 |
$terms = get_terms(get_taxonomies(array('show_ui'=>true)), array('get'=>'all', 'fields'=>'ids')); |
| 154 |
$this->update_translations('term', $terms, $old_slug); |
| 155 |
|
| 156 |
// update menus locations |
| 157 |
foreach ($locations as $location => $description) { |
| 158 |
if (isset($menu_lang[$location][$old_slug])) { |
| 159 |
$menu_lang[$location][$_POST['slug']] = $menu_lang[$location][$old_slug]; |
| 160 |
unset($menu_lang[$location][$old_slug]); |
| 161 |
} |
| 162 |
} |
| 163 |
update_option('polylang_nav_menus', $menu_lang); |
| 164 |
|
| 165 |
// update the default language option if necessary |
| 166 |
if ($options['default_lang'] == $old_slug) { |
| 167 |
$options['default_lang'] = $_POST['slug']; |
| 168 |
update_option('polylang', $options); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// and finally update the language itself |
| 173 |
wp_update_term($lang_id, 'language', array('name'=>$_POST['name'], 'slug'=>$_POST['slug'], 'description'=>$_POST['description'])); |
| 174 |
$wp_rewrite->flush_rules(); // refresh rewrite rules |
| 175 |
} |
| 176 |
|
| 177 |
wp_redirect('admin.php?page=mlang'. ($error ? '&error='.$error : '') ); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 178 |
exit; |
| 179 |
break; |
| 180 |
|
| 181 |
case 'nav-menus': |
| 182 |
check_admin_referer( 'nav-menus-lang', '_wpnonce_nav-menus-lang' ); |
| 183 |
|
| 184 |
$menu_lang = $_POST['menu-lang']; |
| 185 |
foreach ($locations as $location => $description) |
| 186 |
foreach (array('switcher', 'show_names', 'show_flags', 'force_home') as $key) |
| 187 |
$menu_lang[$location][$key] = isset($menu_lang[$location][$key]) ? 1 : 0; |
| 188 |
|
| 189 |
update_option('polylang_nav_menus', $menu_lang); |
| 190 |
break; |
| 191 |
|
| 192 |
case 'options': |
| 193 |
check_admin_referer( 'options-lang', '_wpnonce_options-lang' ); |
| 194 |
|
| 195 |
$options['default_lang'] = $_POST['default_lang']; |
| 196 |
$options['browser'] = isset($_POST['browser']) ? 1 : 0; |
| 197 |
$options['rewrite'] = $_POST['rewrite']; |
| 198 |
$options['hide_default'] = isset($_POST['hide_default']) ? 1 : 0; |
| 199 |
update_option('polylang', $options); |
| 200 |
|
| 201 |
// refresh refresh permalink structure and rewrite rules in case rewrite or hide_default options have been modified |
| 202 |
$wp_rewrite->extra_permastructs['language'][0] = $options['rewrite'] ? '%language%' : '/language/%language%'; |
| 203 |
$wp_rewrite->flush_rules(); |
| 204 |
|
| 205 |
// fills existing posts & terms with default language |
| 206 |
if (isset($_POST['fill_languages'])) { |
| 207 |
if(isset($_POST['posts'])) { |
| 208 |
foreach(explode(',', $_POST['posts']) as $post_id) { |
| 209 |
wp_set_post_terms($post_id, $options['default_lang'], 'language' ); |
| 210 |
} |
| 211 |
} |
| 212 |
if(isset($_POST['terms'])) { |
| 213 |
foreach(explode(',', $_POST['terms']) as $term_id) { |
| 214 |
$this->update_term_language($term_id, $this->get_language($options['default_lang'])); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
break; |
| 219 |
|
| 220 |
default: |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
// prepare the list of tabs |
| 225 |
$tabs = array( |
| 226 |
'lang' => __('Languages','polylang'), |
| 227 |
'menus' => __('Menus','polylang'), |
| 228 |
'settings' => __('Settings', 'polylang') |
| 229 |
); |
| 230 |
if (!current_theme_supports( 'menus' )) |
| 231 |
unset($tabs['menus']); // don't display the menu tab if the active them does not support nav menus |
| 232 |
|
| 233 |
$active_tab = isset($_GET['tab']) && $_GET['tab'] ? $_GET['tab'] : 'lang'; |
| 234 |
|
| 235 |
switch($active_tab) { |
| 236 |
case 'lang': |
| 237 |
// prepare the list table of languages |
| 238 |
$data = array(); |
| 239 |
foreach ($listlanguages as $lang) |
| 240 |
$data[] = array_merge( (array) $lang, array('flag' => $this->get_flag($lang)) ) ; |
| 241 |
|
| 242 |
$list_table->prepare_items($data); |
| 243 |
|
| 244 |
$errors[1] = __('Enter a valid WorPress locale', 'polylang'); |
| 245 |
$errors[2] = __('The language code must be 2 characters long', 'polylang'); |
| 246 |
$errors[3] = __('The language code must be unique', 'polylang'); |
| 247 |
$errors[4] = __('The language must have a name', 'polylang'); |
| 248 |
break; |
| 249 |
|
| 250 |
case 'menus': |
| 251 |
// prepare the list of options for the language switcher |
| 252 |
// FIXME do not include the dropdown yet as I need to create a better script (only available for the widget now) |
| 253 |
$menu_options = array( |
| 254 |
'switcher' => __('Displays a language switcher at the end of the menu', 'polylang'), |
| 255 |
'show_names' => __('Displays language names', 'polylang'), |
| 256 |
'show_flags' => __('Displays flags', 'polylang'), |
| 257 |
'force_home' => __('Forces link to front page', 'polylang') |
| 258 |
); |
| 259 |
|
| 260 |
// default values |
| 261 |
foreach ($locations as $key=>$location) |
| 262 |
$menu_lang[$key] = wp_parse_args($menu_lang[$key], array('switcher'=> 0, 'show_names'=>1, 'show_flags'=>0, 'force_home'=>0)); |
| 263 |
|
| 264 |
break; |
| 265 |
|
| 266 |
case 'settings': |
| 267 |
//FIXME rework this as it would not be efficient in case of thousands posts or terms ! |
| 268 |
// detects posts & pages without language set |
| 269 |
$q = array( |
| 270 |
'numberposts'=>-1, |
| 271 |
'post_type' => 'any', |
| 272 |
'post_status'=>'any', |
| 273 |
'fields' => 'ids', |
| 274 |
'tax_query' => array(array( |
| 275 |
'taxonomy'=> 'language', |
| 276 |
'terms'=> get_terms('language', array('fields'=>'ids')), |
| 277 |
'operator'=>'NOT IN' |
| 278 |
)) |
| 279 |
); |
| 280 |
$posts = implode(',', get_posts($q)); |
| 281 |
|
| 282 |
// detects categories & post tags without language set |
| 283 |
$terms = get_terms(get_taxonomies(array('show_ui'=>true)), array('get'=>'all', 'fields'=>'ids')); |
| 284 |
|
| 285 |
foreach ($terms as $key => $term_id) { |
| 286 |
if ($this->get_term_language($term_id)) |
| 287 |
unset($terms[$key]); |
| 288 |
} |
| 289 |
$terms = implode(',', $terms); |
| 290 |
break; |
| 291 |
|
| 292 |
default: |
| 293 |
break; |
| 294 |
} |
| 295 |
// displays the page |
| 296 |
include(PLL_INC.'/languages-form.php'); |
| 297 |
} |
| 298 |
|
| 299 |
// validates data entered when creating or updating a language |
| 300 |
function validate_lang($lang = null) { |
| 301 |
// validate locale |
| 302 |
$loc = $_POST['description']; |
| 303 |
if ( !preg_match('#^[a-z]{2}$#', $loc) && !preg_match('#^[a-z]{2}_[A-Z]{2}$#', $loc) ) |
| 304 |
$error = 1; |
| 305 |
|
| 306 |
// validate slug length |
| 307 |
if (strlen($_POST['slug']) != 2) |
| 308 |
$error = 2; |
| 309 |
|
| 310 |
// validate slug is unique |
| 311 |
if ($this->get_language($_POST['slug']) != null && isset($lang) && $lang->slug != $_POST['slug']) |
| 312 |
$error = 3; |
| 313 |
|
| 314 |
// validate name |
| 315 |
if ($_POST['name'] == '') |
| 316 |
$error = 4; |
| 317 |
|
| 318 |
return isset($error) ? $error : 0; |
| 319 |
} |
| 320 |
|
| 321 |
} // class Polylang_Admin |
| 322 |
|
| 323 |
?> |
| 324 |
|