| @@ -4,8 +4,10 @@ | ||
| 4 | 4 | define('POLYLANG_URL', WP_PLUGIN_URL .'/polylang'); |
| 5 | 5 | |
| 6 | 6 | class Polylang_Admin extends Polylang_Base { |
| 7 | 7 | function __construct() { |
| 8 | + add_action('admin_init', array(&$this, 'admin_init')); | |
| 9 | + | |
| 8 | 10 | // adds a 'settings' link in the plugins table |
| 9 | 11 | $plugin_file = basename( dirname( __FILE__ ) ).'/polylang.php'; |
| 10 | 12 | add_filter('plugin_action_links_'.$plugin_file, array(&$this, 'plugin_action_links')); |
| 11 | 13 | |
| @@ -60,12 +62,26 @@ | ||
| 60 | 62 | |
| 61 | 63 | // modifies the theme location nav menu metabox |
| 62 | 64 | add_filter('admin_head-nav-menus.php', array(&$this, 'nav_menu_theme_locations')); |
| 63 | 65 | |
| 66 | + // widgets languages filter | |
| 67 | + add_action('in_widget_form', array(&$this, 'in_widget_form')); | |
| 68 | + add_filter('widget_update_callback', array(&$this, 'widget_update_callback'), 10, 4); | |
| 69 | + | |
| 64 | 70 | // refresh rewrite rules if the 'page_on_front' option is modified |
| 65 | 71 | add_action('update_option', array(&$this, 'update_option')); |
| 66 | 72 | } |
| 67 | 73 | |
| 74 | + // manage versions | |
| 75 | + // not very useful now but we don't know the future | |
| 76 | + function admin_init() { | |
| 77 | + $options = get_option('polylang'); | |
| 78 | + if ($options['version'] < POLYLANG_VERSION) { | |
| 79 | + $options['version'] = POLYLANG_VERSION; | |
| 80 | + update_option('polylang', $options); | |
| 81 | + } | |
| 82 | + } | |
| 83 | + | |
| 68 | 84 | // adds a 'settings' link in the plugins table |
| 69 | 85 | function plugin_action_links($links) { |
| 70 | 86 | $settings_link = '<a href="admin.php?page=mlang">' . __('Settings') . '</a>'; |
| 71 | 87 | array_unshift( $links, $settings_link ); |
| @@ -102,16 +118,19 @@ | ||
| 102 | 118 | switch ($action) { |
| 103 | 119 | case 'add': |
| 104 | 120 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 105 | 121 | |
| 106 | - if (isset($_POST['name']) && isset($_POST['description'])) { | |
| 122 | + // FIXME improve data validation | |
| 123 | + // FIXME add an error message if conditions are not fulfilled | |
| 124 | + if ($_POST['name'] && $_POST['description'] && $_POST['slug']) { | |
| 107 | 125 | wp_insert_term($_POST['name'],'language',array('slug'=>$_POST['slug'], 'description'=>$_POST['description'])); |
| 108 | 126 | $wp_rewrite->flush_rules(); // refresh rewrite rules |
| 127 | + | |
| 128 | + if (!isset($options['default_lang'])) { // if this is the first language created, set it as default language | |
| 129 | + $options['default_lang'] = $_POST['slug']; | |
| 130 | + update_option('polylang', $options); | |
| 131 | + } | |
| 109 | 132 | } |
| 110 | - if (!isset($options['default_lang'])) { // if this is the first language created, set it as default language | |
| 111 | - $options['default_lang'] = $_POST['slug']; | |
| 112 | - update_option('polylang', $options); | |
| 113 | - } | |
| 114 | 133 | wp_redirect('admin.php?page=mlang'); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 115 | 134 | exit; |
| 116 | 135 | break; |
| 117 | 136 | |
| @@ -117,10 +136,11 @@ | ||
| 117 | 136 | |
| 118 | 137 | case 'delete': |
| 119 | 138 | check_admin_referer( 'delete-lang'); |
| 120 | 139 | |
| 121 | - if (isset($_GET['lang'])) { | |
| 122 | - $lang = $this->get_language($_GET['lang']); | |
| 140 | + if (isset($_GET['lang']) && $_GET['lang']) { | |
| 141 | + $lang_id = (int) $_GET['lang']; | |
| 142 | + $lang = $this->get_language($lang_id); | |
| 123 | 143 | $lang_slug = $lang->slug; |
| 124 | 144 | |
| 125 | 145 | // delete all translations in posts in this language |
| 126 | 146 | $args = array('numberposts'=>-1, 'taxonomy' => 'language', 'term' => $lang_slug, 'post_type'=>'any', 'post_status'=>'any'); |
| @@ -144,15 +164,15 @@ | ||
| 144 | 164 | if ($this->get_term_language($term->term_id) == $lang) { |
| 145 | 165 | foreach ($languages as $language) { |
| 146 | 166 | delete_metadata('term', $term->term_id, '_lang-'.$language->slug); // deletes translations of this term |
| 147 | 167 | } |
| 148 | - delete_metadata('term', $term->term_id, '_language', $_GET['lang']); // delete language of this term | |
| 168 | + delete_metadata('term', $term->term_id, '_language', $lang_id); // delete language of this term | |
| 149 | 169 | } |
| 150 | 170 | delete_metadata('term', $term->term_id, '_lang-'.$lang_slug); // deletes references to this term in translated term |
| 151 | 171 | } |
| 152 | 172 | |
| 153 | 173 | // delete the language itself |
| 154 | - wp_delete_term($_GET['lang'], 'language'); | |
| 174 | + wp_delete_term($lang_id, 'language'); | |
| 155 | 175 | $wp_rewrite->flush_rules(); // refresh rewrite rules |
| 156 | 176 | |
| 157 | 177 | // oops ! we deleted the default language... |
| 158 | 178 | if ($options['default_lang'] == $lang_slug) { |
| @@ -167,18 +187,20 @@ | ||
| 167 | 187 | exit; |
| 168 | 188 | break; |
| 169 | 189 | |
| 170 | 190 | case 'edit': |
| 171 | - if (isset($_GET['lang'])) | |
| 172 | - $edit_lang = $this->get_language($_GET['lang']); | |
| 191 | + if (isset($_GET['lang']) && $_GET['lang']) | |
| 192 | + $edit_lang = $this->get_language((int) $_GET['lang']); | |
| 173 | 193 | break; |
| 174 | 194 | |
| 175 | 195 | case 'update': |
| 176 | 196 | check_admin_referer( 'add-lang', '_wpnonce_add-lang' ); |
| 177 | 197 | |
| 178 | - if (isset($_POST['lang'])) { | |
| 198 | + // FIXME add an error message if conditions are not fulfilled | |
| 199 | + if ($_POST['lang'] && $_POST['name'] && $_POST['description'] && $_POST['slug']) { | |
| 179 | 200 | // Update links to this language in posts and terms in case the slug has been modified |
| 180 | - $lang = $this->get_language($_POST['lang']); | |
| 201 | + $lang_id = (int) $_POST['lang']; | |
| 202 | + $lang = $this->get_language($lang_id); | |
| 181 | 203 | $old_slug = $lang->slug; |
| 182 | 204 | |
| 183 | 205 | if ($old_slug != $_POST['slug']) { |
| 184 | 206 | // update the language slug in posts meta |
| @@ -201,9 +223,9 @@ | ||
| 201 | 223 | } |
| 202 | 224 | } |
| 203 | 225 | |
| 204 | 226 | // and finally update the language itself |
| 205 | - wp_update_term($_POST['lang'], 'language', array('name'=>$_POST['name'], 'slug'=>$_POST['slug'], 'description'=>$_POST['description'])); | |
| 227 | + wp_update_term($lang_id, 'language', array('name'=>$_POST['name'], 'slug'=>$_POST['slug'], 'description'=>$_POST['description'])); | |
| 206 | 228 | $wp_rewrite->flush_rules(); // refresh rewrite rules |
| 207 | 229 | } |
| 208 | 230 | wp_redirect('admin.php?page=mlang'); // to refresh the page (possible thanks to the $_GET['noheader']=true) |
| 209 | 231 | exit; |
| @@ -210,8 +232,9 @@ | ||
| 210 | 232 | break; |
| 211 | 233 | |
| 212 | 234 | case 'nav-menus': |
| 213 | 235 | check_admin_referer( 'nav-menus-lang', '_wpnonce_nav-menus-lang' ); |
| 236 | + | |
| 214 | 237 | $menu_lang = $_POST['menu-lang']; |
| 215 | 238 | update_option('polylang_nav_menus', $menu_lang); |
| 216 | 239 | break; |
| 217 | 240 | |
| @@ -262,9 +285,9 @@ | ||
| 262 | 285 | // fills the language column in the posts table |
| 263 | 286 | function post_column($column, $post_id) { |
| 264 | 287 | $lang = $this->get_post_language($post_id); |
| 265 | 288 | if ($lang && $column == 'language') |
| 266 | - echo $lang->name; | |
| 289 | + echo esc_attr($lang->name); | |
| 267 | 290 | } |
| 268 | 291 | |
| 269 | 292 | // converts language term_id to slug in $query |
| 270 | 293 | // needed to filter the posts by language with wp_dropdown_categories in restrict_manage_posts |
| @@ -270,9 +293,9 @@ | ||
| 270 | 293 | // needed to filter the posts by language with wp_dropdown_categories in restrict_manage_posts |
| 271 | 294 | function parse_query($query) { |
| 272 | 295 | global $pagenow; |
| 273 | 296 | $qvars = &$query->query_vars; |
| 274 | - if ($pagenow=='edit.php' && isset($qvars['lang']) && is_numeric($qvars['lang'])) { | |
| 297 | + if ($pagenow=='edit.php' && isset($qvars['lang']) && $qvars['lang'] && is_numeric($qvars['lang'])) { | |
| 275 | 298 | $lang = $this->get_language($qvars['lang']); |
| 276 | 299 | if ($lang) |
| 277 | 300 | $qvars['lang'] = $lang->slug; |
| 278 | 301 | } |
| @@ -437,9 +460,9 @@ | ||
| 437 | 460 | // fills the language column in the 'Categories' or Post Tags table |
| 438 | 461 | function term_column($empty, $column, $term_id) { |
| 439 | 462 | $lang = $this->get_term_language($term_id); |
| 440 | 463 | if ($lang && $column == 'language') |
| 441 | - echo $lang->name; | |
| 464 | + echo esc_attr($lang->name); | |
| 442 | 465 | } |
| 443 | 466 | |
| 444 | 467 | // called when a category or post tag is created or edited |
| 445 | 468 | function save_term($term_id) { |
| @@ -528,8 +551,43 @@ | ||
| 528 | 551 | // displays a message to redirect to the languages options page |
| 529 | 552 | function nav_menu_language() { |
| 530 | 553 | echo '<p class="howto">' . sprintf (__('Please go to the %slanguages page%s to set theme locations and languages', 'polylang'), |
| 531 | 554 | '<a href="' . admin_url('options-general.php?page=mlang#menus') . '">', '</a>') . '</p>'; |
| 555 | + } | |
| 556 | + | |
| 557 | + // modifies the widgets forms to add our language dropdwown list | |
| 558 | + function in_widget_form($widget) { | |
| 559 | + $id = esc_attr($widget->id.'_lang_choice'); | |
| 560 | + $listlanguages = $this->get_languages_list(); | |
| 561 | + $widget_lang = get_option('polylang_widgets'); | |
| 562 | + | |
| 563 | + printf( | |
| 564 | + '<p><label for="%s">%s<select name="%s" id="%s" class="tags-input"><option value="0">%s</option>', | |
| 565 | + $id, | |
| 566 | + __('The widget is displayed for:', 'polylang'), | |
| 567 | + $id, | |
| 568 | + $id, | |
| 569 | + __('All languages', 'polylang') | |
| 570 | + ); | |
| 571 | + foreach ($listlanguages as $language) { | |
| 572 | + printf( | |
| 573 | + "<option value='%s'%s>%s</option>\n", | |
| 574 | + esc_attr($language->slug), | |
| 575 | + $language->slug == $widget_lang[$widget->id] ? ' selected="selected"' : '', | |
| 576 | + esc_attr($language->name) | |
| 577 | + ); | |
| 578 | + } | |
| 579 | + echo '</select></label></p>'; | |
| 580 | + } | |
| 581 | + | |
| 582 | + // called when widget options are saved | |
| 583 | + function widget_update_callback($instance, $new_instance, $old_instance, $widget) { | |
| 584 | + $id = $widget->id.'_lang_choice'; | |
| 585 | + if (isset($_POST[$id]) && $_POST[$id]) { | |
| 586 | + $widget_lang = get_option('polylang_widgets'); | |
| 587 | + $widget_lang[$widget->id] = $_POST[$id]; | |
| 588 | + update_option('polylang_widgets', $widget_lang); | |
| 589 | + } | |
| 532 | 590 | } |
| 533 | 591 | |
| 534 | 592 | // refresh rewrite rules if the 'page_on_front' option is modified |
| 535 | 593 | function update_option($option) { |