| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class Sublanguage_admin_ui extends Sublanguage_admin { |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | /** |
|---|
| 7 | * @var array |
|---|
| 8 | */ |
|---|
| 9 | var $extra_post_types = array(); |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * @var array |
|---|
| 13 | */ |
|---|
| 14 | var $menu_languages; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | /** deprecated?? |
|---|
| 18 | * @from 1.0 |
|---|
| 19 | */ |
|---|
| 20 | var $page_name = 'sublanguage'; |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * @var string |
|---|
| 24 | */ |
|---|
| 25 | var $option_page_name = 'translate_options'; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * @override Sublanguage_admin::load |
|---|
| 30 | * |
|---|
| 31 | * @from 2.0 |
|---|
| 32 | */ |
|---|
| 33 | public function load() { |
|---|
| 34 | |
|---|
| 35 | parent::load(); |
|---|
| 36 | |
|---|
| 37 | add_action('admin_menu', array($this, 'admin_menu')); |
|---|
| 38 | |
|---|
| 39 | // save post and taxnomy options |
|---|
| 40 | add_action('init', array($this, 'save_post_option'), 99); |
|---|
| 41 | |
|---|
| 42 | // register post types without UI |
|---|
| 43 | add_action('init', array($this, 'register_extra_post'), 20); |
|---|
| 44 | |
|---|
| 45 | // init languages @from 2.5 |
|---|
| 46 | add_action('init', array($this, 'init_languages'), 20); |
|---|
| 47 | |
|---|
| 48 | // init gutenberg |
|---|
| 49 | add_action('init', array($this, 'init_gutenberg')); |
|---|
| 50 | |
|---|
| 51 | // redirect post to requested translation |
|---|
| 52 | add_filter('redirect_post_location', array($this, 'language_redirect')); |
|---|
| 53 | |
|---|
| 54 | // on load post.php |
|---|
| 55 | add_action('load-post.php', array($this, 'admin_post_page')); |
|---|
| 56 | add_action('load-post-new.php', array($this, 'admin_post_page')); |
|---|
| 57 | |
|---|
| 58 | // on load edit.php |
|---|
| 59 | add_action('load-edit.php', array($this, 'admin_edit_page')); |
|---|
| 60 | |
|---|
| 61 | // save term translations |
|---|
| 62 | add_action('edit_term', array($this, 'save_term_translation'), 10, 3); |
|---|
| 63 | |
|---|
| 64 | // tags table |
|---|
| 65 | add_action('load-edit-tags.php', array($this, 'admin_edit_tags')); |
|---|
| 66 | |
|---|
| 67 | // add language meta box in appearance > menu |
|---|
| 68 | add_action('admin_init', array($this, 'add_language_meta_box')); |
|---|
| 69 | |
|---|
| 70 | // disabled in 2.10 |
|---|
| 71 | // register CSS and JS for option translation page |
|---|
| 72 | // add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); |
|---|
| 73 | |
|---|
| 74 | // enqueue ajax script |
|---|
| 75 | add_action('admin_enqueue_scripts', array($this, 'ajax_enqueue_scripts')); |
|---|
| 76 | |
|---|
| 77 | // add option translation page to tools menu |
|---|
| 78 | add_action('admin_menu', array($this, 'admin_menu')); |
|---|
| 79 | |
|---|
| 80 | // feature disabled in 2.10 |
|---|
| 81 | // Editor button |
|---|
| 82 | // add_action('admin_head', array($this, 'load_editor_button')); |
|---|
| 83 | // add_action('load-post.php', array($this, 'load_editor_page')); |
|---|
| 84 | |
|---|
| 85 | // feature disabled in 2.10 |
|---|
| 86 | // Register for Tinymce Advanced Plugin |
|---|
| 87 | // add_filter('tadv_allowed_buttons', array($this, 'tadv_register_button')); |
|---|
| 88 | // add_action('admin_head', array($this, 'tadv_set_icon')); |
|---|
| 89 | |
|---|
| 90 | // Flush rewrite rules if needed |
|---|
| 91 | add_action('wp_loaded', array($this, 'flush_rewrite_rules'), 12); |
|---|
| 92 | add_action('generate_rewrite_rules', array($this, 'generate_rewrite_rules')); |
|---|
| 93 | add_action('save_post_page', array($this, 'save_page'), 10, 3); |
|---|
| 94 | add_action('post_updated', array($this, 'update_page'), 10, 3); |
|---|
| 95 | |
|---|
| 96 | // set nav menu item translation defaults |
|---|
| 97 | add_filter('sublanguage_default-nav_menu_item', array($this, 'nav_menu_item_default_options')); |
|---|
| 98 | add_filter('sublanguage_post_type_metakeys', array($this, 'nav_menu_item_metakeys'), 10, 2); |
|---|
| 99 | |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Register placeholder mce plugin |
|---|
| 105 | * |
|---|
| 106 | * @filter 'mce_external_plugins' |
|---|
| 107 | * @from 2.7 |
|---|
| 108 | * @removed from 2.10 |
|---|
| 109 | */ |
|---|
| 110 | public function register_placeholder_plugin( $plugins ) { |
|---|
| 111 | |
|---|
| 112 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 113 | |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * prepare placeholder |
|---|
| 118 | * |
|---|
| 119 | * @filter 'wp_editor_settings' |
|---|
| 120 | * @from 2.7 |
|---|
| 121 | * @removed from 2.10 |
|---|
| 122 | */ |
|---|
| 123 | public function prepare_placeholder($settings, $editor_id) { |
|---|
| 124 | |
|---|
| 125 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 126 | |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * Add placeholder in editor |
|---|
| 131 | * |
|---|
| 132 | * @filter 'the_editor' |
|---|
| 133 | * @from 2.7 |
|---|
| 134 | * @removed from 2.10 |
|---|
| 135 | */ |
|---|
| 136 | public function add_editor_placeholder($textarea_html) { |
|---|
| 137 | |
|---|
| 138 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 139 | |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | /** |
|---|
| 146 | * Admin init |
|---|
| 147 | * |
|---|
| 148 | * @hook 'admin_init' |
|---|
| 149 | * @from 1.5 |
|---|
| 150 | */ |
|---|
| 151 | public function admin_init() { |
|---|
| 152 | |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | /** |
|---|
| 157 | * Add sublanguage subpage for options translation |
|---|
| 158 | * |
|---|
| 159 | * @hook 'admin_menu' |
|---|
| 160 | * @from 1.5 |
|---|
| 161 | */ |
|---|
| 162 | public function admin_menu() { |
|---|
| 163 | |
|---|
| 164 | add_submenu_page( |
|---|
| 165 | 'options-general.php', |
|---|
| 166 | __('Sublanguage Settings', 'sublanguage'), |
|---|
| 167 | __('Sublanguage', 'sublanguage'), |
|---|
| 168 | 'manage_options', |
|---|
| 169 | 'sublanguage-settings', |
|---|
| 170 | array($this, 'print_setting_page') |
|---|
| 171 | ); |
|---|
| 172 | |
|---|
| 173 | $post_types = get_post_types(array( |
|---|
| 174 | 'show_ui' => true |
|---|
| 175 | )); |
|---|
| 176 | |
|---|
| 177 | foreach ($post_types as $post_type) { |
|---|
| 178 | |
|---|
| 179 | if ($this->is_post_type_translatable($post_type)) { |
|---|
| 180 | |
|---|
| 181 | if (in_array($post_type, $this->extra_post_types)) { // -> extra (without default interface) custom posts |
|---|
| 182 | |
|---|
| 183 | add_submenu_page ( |
|---|
| 184 | 'tools.php', |
|---|
| 185 | $post_type . ' Post Language Options', |
|---|
| 186 | 'Language Options', |
|---|
| 187 | 'manage_options', |
|---|
| 188 | $post_type . '_language_option', |
|---|
| 189 | array($this, 'print_extra_post_language_option_page') |
|---|
| 190 | ); |
|---|
| 191 | |
|---|
| 192 | } else if ($post_type === 'nav_menu_item') { |
|---|
| 193 | |
|---|
| 194 | add_submenu_page ( // -> nav menu items |
|---|
| 195 | 'tools.php', |
|---|
| 196 | 'Nav Menu Items Language Options', |
|---|
| 197 | 'Nav Menu Items Language Options', |
|---|
| 198 | 'manage_options', |
|---|
| 199 | $post_type . '_language_option', |
|---|
| 200 | array($this, 'print_extra_post_language_option_page') |
|---|
| 201 | ); |
|---|
| 202 | |
|---|
| 203 | } else if ($post_type === 'post') { // -> posts |
|---|
| 204 | |
|---|
| 205 | add_submenu_page ( |
|---|
| 206 | 'edit.php', |
|---|
| 207 | 'Post Language Options', |
|---|
| 208 | 'Language Options', |
|---|
| 209 | 'manage_options', |
|---|
| 210 | $post_type . '_language_option', |
|---|
| 211 | array($this, 'print_post_language_option_page') |
|---|
| 212 | ); |
|---|
| 213 | |
|---|
| 214 | } else { // -> public custom posts |
|---|
| 215 | |
|---|
| 216 | add_submenu_page ( |
|---|
| 217 | 'edit.php?post_type=' . $post_type, |
|---|
| 218 | 'Post Language Options', |
|---|
| 219 | 'Language Options', |
|---|
| 220 | 'manage_options', |
|---|
| 221 | $post_type . '_language_option', |
|---|
| 222 | array($this, 'print_post_language_option_page') |
|---|
| 223 | ); |
|---|
| 224 | |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | if ($this->is_post_type_translatable('attachment')) { |
|---|
| 232 | |
|---|
| 233 | add_submenu_page ( |
|---|
| 234 | 'upload.php', |
|---|
| 235 | 'Media Language Options', |
|---|
| 236 | 'Language Options', |
|---|
| 237 | 'manage_options', |
|---|
| 238 | 'attachment_language_option', |
|---|
| 239 | array($this, 'print_attachment_language_option_page') |
|---|
| 240 | ); |
|---|
| 241 | |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | $taxonomies = get_taxonomies(array( |
|---|
| 245 | // 'public' => true |
|---|
| 246 | )); |
|---|
| 247 | |
|---|
| 248 | foreach ($taxonomies as $taxonomy) { |
|---|
| 249 | |
|---|
| 250 | if ($this->is_taxonomy_translatable($taxonomy)) { |
|---|
| 251 | |
|---|
| 252 | add_submenu_page ( |
|---|
| 253 | '', // no parent |
|---|
| 254 | 'Taxonomy Language Options', |
|---|
| 255 | 'Taxonomy Language Options', |
|---|
| 256 | 'manage_options', |
|---|
| 257 | $taxonomy . '_language_option', |
|---|
| 258 | array($this, 'print_taxonomy_language_option_page') |
|---|
| 259 | ); |
|---|
| 260 | |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | // disabled in 2.10 |
|---|
| 266 | // options translate page in tools |
|---|
| 267 | // add_submenu_page ( |
|---|
| 268 | // 'tools.php', |
|---|
| 269 | // 'Translate Options', |
|---|
| 270 | // 'Translate Options', |
|---|
| 271 | // 'manage_options', |
|---|
| 272 | // $this->option_page_name, |
|---|
| 273 | // array($this, 'print_page') |
|---|
| 274 | // ); |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | /* Settings |
|---|
| 281 | ----------------------------------------------- */ |
|---|
| 282 | |
|---|
| 283 | /** |
|---|
| 284 | * Print general setting page |
|---|
| 285 | * |
|---|
| 286 | * @from 1.5 |
|---|
| 287 | */ |
|---|
| 288 | public function print_setting_page() { |
|---|
| 289 | |
|---|
| 290 | include plugin_dir_path( __FILE__ ) . 'include/settings-page.php'; |
|---|
| 291 | |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | /** |
|---|
| 295 | * Print post-type option page |
|---|
| 296 | * |
|---|
| 297 | * @callback for add_submenu_page() |
|---|
| 298 | * |
|---|
| 299 | * @from 2.0 |
|---|
| 300 | */ |
|---|
| 301 | public function print_post_language_option_page() { |
|---|
| 302 | |
|---|
| 303 | $screen = get_current_screen(); |
|---|
| 304 | $post_type = empty($screen->post_type) ? 'post' : $screen->post_type; |
|---|
| 305 | $post_type_obj = get_post_type_object($post_type); |
|---|
| 306 | $meta_keys = $this->query_post_type_metakeys($post_type); |
|---|
| 307 | $registered_meta_keys = get_registered_meta_keys( 'posts' ); |
|---|
| 308 | |
|---|
| 309 | if ($this->is_post_type_translatable($post_type)) { |
|---|
| 310 | |
|---|
| 311 | include plugin_dir_path( __FILE__ ) . 'include/settings-post-option-page.php'; |
|---|
| 312 | |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | /** |
|---|
| 318 | * Print extra post-type option page |
|---|
| 319 | * |
|---|
| 320 | * @callback for add_submenu_page() |
|---|
| 321 | * |
|---|
| 322 | * @from 2.0 |
|---|
| 323 | */ |
|---|
| 324 | public function print_extra_post_language_option_page() { |
|---|
| 325 | |
|---|
| 326 | $page = esc_attr($_GET['page']); |
|---|
| 327 | $post_type = substr($page, 0, strrpos($page, '_language_option')); |
|---|
| 328 | $post_type_obj = get_post_type_object($post_type); |
|---|
| 329 | $meta_keys = $this->query_post_type_metakeys($post_type); |
|---|
| 330 | $registered_meta_keys = get_registered_meta_keys( 'posts' ); |
|---|
| 331 | |
|---|
| 332 | if ($this->is_post_type_translatable($post_type)) { |
|---|
| 333 | |
|---|
| 334 | include plugin_dir_path( __FILE__ ) . 'include/settings-post-option-page.php'; |
|---|
| 335 | |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | /** |
|---|
| 341 | * Print attachment option page |
|---|
| 342 | * |
|---|
| 343 | * @from 2.0 |
|---|
| 344 | */ |
|---|
| 345 | public function print_attachment_language_option_page() { |
|---|
| 346 | |
|---|
| 347 | $post_type = 'attachment'; |
|---|
| 348 | $post_type_obj = get_post_type_object($post_type); |
|---|
| 349 | $meta_keys = $this->query_post_type_metakeys($post_type); |
|---|
| 350 | $registered_meta_keys = get_registered_meta_keys( 'posts' ); |
|---|
| 351 | |
|---|
| 352 | if ($this->is_post_type_translatable($post_type)) { |
|---|
| 353 | |
|---|
| 354 | include plugin_dir_path( __FILE__ ) . 'include/settings-attachment-option-page.php'; |
|---|
| 355 | |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | /** |
|---|
| 361 | * Print taxonomy option page |
|---|
| 362 | * |
|---|
| 363 | * @from 1.5 |
|---|
| 364 | */ |
|---|
| 365 | public function print_taxonomy_language_option_page() { |
|---|
| 366 | |
|---|
| 367 | if (isset($_GET['taxonomy'])) { |
|---|
| 368 | |
|---|
| 369 | $taxonomy = esc_attr($_GET['taxonomy']); |
|---|
| 370 | $tax_obj = get_taxonomy($taxonomy); |
|---|
| 371 | $meta_keys = $this->query_taxonomy_metakeys($taxonomy); |
|---|
| 372 | $registered_meta_keys = get_registered_meta_keys( 'term' ); |
|---|
| 373 | |
|---|
| 374 | if ($this->is_taxonomy_translatable($taxonomy)) { |
|---|
| 375 | |
|---|
| 376 | include plugin_dir_path( __FILE__ ) . 'include/settings-taxonomy-option-page.php'; |
|---|
| 377 | |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | /** |
|---|
| 385 | * Save post-type/taxonomy option page |
|---|
| 386 | * |
|---|
| 387 | * @from 2.0 |
|---|
| 388 | */ |
|---|
| 389 | public function save_post_option() { |
|---|
| 390 | |
|---|
| 391 | if (current_user_can('manage_options')) { |
|---|
| 392 | |
|---|
| 393 | if (isset($_POST['sublanguage_post_option'], $_POST['post_type']) && wp_verify_nonce($_POST['sublanguage_post_option'], 'sublanguage_action')) { |
|---|
| 394 | |
|---|
| 395 | $post_type = esc_attr($_POST['post_type']); |
|---|
| 396 | |
|---|
| 397 | if ($this->is_post_type_translatable($post_type)) { |
|---|
| 398 | |
|---|
| 399 | $translations = $this->get_option('translations', array()); |
|---|
| 400 | |
|---|
| 401 | // permalinks |
|---|
| 402 | if (isset($_POST['cpt'])) { |
|---|
| 403 | |
|---|
| 404 | $cpt = isset($_POST['cpt']) ? array_map(array($this, 'sanitize_slug'), $_POST['cpt']) : array(); |
|---|
| 405 | |
|---|
| 406 | if (!isset($translations['post_type'][$post_type]) || $translations['post_type'][$post_type] !== $cpt) { |
|---|
| 407 | $translations['post_type'][$post_type] = $cpt; |
|---|
| 408 | $this->update_option('translations', $translations); |
|---|
| 409 | $this->update_option('need_flush', 1); |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | // custom post type archive |
|---|
| 415 | if (isset($_POST['cpt_archive'])) { |
|---|
| 416 | |
|---|
| 417 | $cpt_archive = isset($_POST['cpt_archive']) ? array_map(array($this, 'sanitize_slug'), $_POST['cpt_archive']) : array(); |
|---|
| 418 | |
|---|
| 419 | if (!isset($translations['cpt_archive'][$post_type]) || $translations['cpt_archive'][$post_type] !== $cpt_archive) { |
|---|
| 420 | $translations['cpt_archive'][$post_type] = $cpt_archive; |
|---|
| 421 | $this->update_option('translations', $translations); |
|---|
| 422 | $this->update_option('need_flush', 1); |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | $post_types_options = $this->get_post_types_options(); |
|---|
| 428 | |
|---|
| 429 | // fields |
|---|
| 430 | $fields = isset($_POST['fields']) ? array_map('esc_attr', $_POST['fields']) : array(); |
|---|
| 431 | $post_types_options[$post_type]['fields'] = $fields; |
|---|
| 432 | |
|---|
| 433 | // meta |
|---|
| 434 | $meta_keys = isset($_POST['meta_keys']) ? array_map('esc_attr', $_POST['meta_keys']) : array(); |
|---|
| 435 | $post_types_options[$post_type]['meta_keys'] = $meta_keys; |
|---|
| 436 | |
|---|
| 437 | // advanced options |
|---|
| 438 | $exclude_untranslated = isset($_POST['exclude_untranslated']) && $_POST['exclude_untranslated']; |
|---|
| 439 | $post_types_options[$post_type]['exclude_untranslated'] = $exclude_untranslated; |
|---|
| 440 | |
|---|
| 441 | // @from 2.5 gutenberg metabox_compat option |
|---|
| 442 | if (isset($_POST['gutenberg_async_switch']) && $_POST['gutenberg_async_switch']) { |
|---|
| 443 | unset($post_types_options[$post_type]['gutenberg_metabox_compat']); |
|---|
| 444 | } else { |
|---|
| 445 | $post_types_options[$post_type]['gutenberg_metabox_compat'] = 1; |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | // @from 2.6 Revision |
|---|
| 449 | if (isset($_POST['enable_revisions']) && $_POST['enable_revisions']) { |
|---|
| 450 | $post_types_options[$post_type]['enable_revisions'] = 1; |
|---|
| 451 | } else { |
|---|
| 452 | unset($post_types_options[$post_type]['enable_revisions']); |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | $this->update_option('post_type', $post_types_options); |
|---|
| 456 | |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | wp_redirect($_POST['_wp_http_referer']); |
|---|
| 460 | exit; |
|---|
| 461 | |
|---|
| 462 | } else if (isset($_POST['sublanguage_taxonomy_option'], $_POST['taxonomy']) && wp_verify_nonce($_POST['sublanguage_taxonomy_option'], 'sublanguage_action')) { |
|---|
| 463 | |
|---|
| 464 | $taxonomy = esc_attr($_POST['taxonomy']); |
|---|
| 465 | |
|---|
| 466 | if ($this->is_taxonomy_translatable($taxonomy)) { |
|---|
| 467 | |
|---|
| 468 | // permalinks |
|---|
| 469 | if (isset($_POST['tax'])) { |
|---|
| 470 | |
|---|
| 471 | $translations = $this->get_option('translations', array()); |
|---|
| 472 | $tax = isset($_POST['tax']) ? array_map(array($this, 'sanitize_slug'), $_POST['tax']) : array(); |
|---|
| 473 | |
|---|
| 474 | if (!isset($translations['taxonomy'][$taxonomy]) || $translations['taxonomy'][$taxonomy] !== $tax) { |
|---|
| 475 | $translations['taxonomy'][$taxonomy] = $tax; |
|---|
| 476 | $this->update_option('translations', $translations); |
|---|
| 477 | $this->update_option('need_flush', 1); |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | $this->update_option('translations', $translations); |
|---|
| 481 | |
|---|
| 482 | } |
|---|
| 483 | |
|---|
| 484 | $taxonomies_options = $this->get_taxonomies_options(); |
|---|
| 485 | |
|---|
| 486 | // fields |
|---|
| 487 | $fields = isset($_POST['fields']) ? array_map('esc_attr', $_POST['fields']) : array(); |
|---|
| 488 | $taxonomies_options[$taxonomy]['fields'] = $fields; |
|---|
| 489 | |
|---|
| 490 | // meta |
|---|
| 491 | $meta_keys = isset($_POST['meta_keys']) ? array_map('esc_attr', $_POST['meta_keys']) : array(); |
|---|
| 492 | $taxonomies_options[$taxonomy]['meta_keys'] = $meta_keys; |
|---|
| 493 | |
|---|
| 494 | $this->update_option('taxonomy', $taxonomies_options); |
|---|
| 495 | |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | wp_redirect($_POST['_wp_http_referer']); |
|---|
| 499 | exit; |
|---|
| 500 | |
|---|
| 501 | } else if (isset($_POST['sublanguage_settings_option']) && wp_verify_nonce($_POST['sublanguage_settings_option'], 'sublanguage_action')) { |
|---|
| 502 | |
|---|
| 503 | $options = get_option($this->option_name); |
|---|
| 504 | |
|---|
| 505 | $post_type_options = $this->get_post_types_options(); |
|---|
| 506 | $post_type_objs = get_post_types(array(), 'objects'); |
|---|
| 507 | |
|---|
| 508 | foreach ($post_type_objs as $post_type_obj) { |
|---|
| 509 | |
|---|
| 510 | if (isset($_POST['post_type']) && in_array($post_type_obj->name, $_POST['post_type'])) { |
|---|
| 511 | |
|---|
| 512 | $post_type_options[$post_type_obj->name] = $this->get_post_type_options($post_type_obj->name); // use filter to set default options |
|---|
| 513 | $post_type_options[$post_type_obj->name]['translatable'] = true; |
|---|
| 514 | |
|---|
| 515 | } else if ($this->is_post_type_translatable($post_type_obj->name)) { |
|---|
| 516 | |
|---|
| 517 | $post_type_options[$post_type_obj->name]['translatable'] = false; |
|---|
| 518 | |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | $taxonomies_options = $this->get_taxonomies_options(); |
|---|
| 524 | $taxonomy_objs = get_taxonomies(array('show_ui' => true), 'objects'); |
|---|
| 525 | |
|---|
| 526 | foreach ($taxonomy_objs as $taxonomy_obj) { |
|---|
| 527 | |
|---|
| 528 | if (isset($_POST['taxonomy']) && in_array($taxonomy_obj->name, $_POST['taxonomy'])) { |
|---|
| 529 | |
|---|
| 530 | $taxonomies_options[$taxonomy_obj->name] = $this->get_taxonomy_options($taxonomy_obj->name); // use filter to set default options |
|---|
| 531 | $taxonomies_options[$taxonomy_obj->name]['translatable'] = true; |
|---|
| 532 | |
|---|
| 533 | } else if ($this->is_taxonomy_translatable($taxonomy_obj->name)) { |
|---|
| 534 | |
|---|
| 535 | $taxonomies_options[$taxonomy_obj->name]['translatable'] = false; |
|---|
| 536 | |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | $options['post_type'] = $post_type_options; |
|---|
| 542 | $options['taxonomy'] = $taxonomies_options; |
|---|
| 543 | $options['show_slug'] = (isset($_POST['show_slug']) && $_POST['show_slug']) ? true : false; |
|---|
| 544 | $options['autodetect'] = (isset($_POST['autodetect']) && $_POST['autodetect']) ? true : false; |
|---|
| 545 | $options['current_first'] = (isset($_POST['current_first']) && $_POST['current_first']) ? true : false; |
|---|
| 546 | $options['main'] = isset($_POST['main']) ? intval($_POST['main']) : 0; |
|---|
| 547 | $options['default'] = isset($_POST['default']) ? intval($_POST['default']) : 0; |
|---|
| 548 | $options['frontend_ajax'] = (isset($_POST['frontend_ajax']) && $_POST['frontend_ajax']) ? true : false; |
|---|
| 549 | $options['need_flush'] = 1; |
|---|
| 550 | |
|---|
| 551 | update_option($this->option_name, $options); |
|---|
| 552 | |
|---|
| 553 | wp_redirect($_POST['_wp_http_referer']); |
|---|
| 554 | exit; |
|---|
| 555 | |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | /** |
|---|
| 563 | * Sanitize slug |
|---|
| 564 | * |
|---|
| 565 | * @from 2.3 |
|---|
| 566 | * @from 2.8 use 'sanitize_text_field' instead of 'sanitize_title' |
|---|
| 567 | */ |
|---|
| 568 | public function sanitize_slug($slug) { |
|---|
| 569 | |
|---|
| 570 | return sanitize_text_field($slug); |
|---|
| 571 | |
|---|
| 572 | } |
|---|
| 573 | |
|---|
| 574 | /** |
|---|
| 575 | * Get post_type meta keys |
|---|
| 576 | * |
|---|
| 577 | * @from 2.0 |
|---|
| 578 | */ |
|---|
| 579 | private function query_post_type_metakeys($post_type) { |
|---|
| 580 | global $wpdb; |
|---|
| 581 | |
|---|
| 582 | $prefixes = array(); |
|---|
| 583 | |
|---|
| 584 | foreach ($this->get_languages() as $language) { |
|---|
| 585 | |
|---|
| 586 | $prefixes[] = $this->get_prefix($language); |
|---|
| 587 | |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | $sql_prefixes = implode("%' AND meta.meta_key NOT LIKE '", array_map('esc_sql', $prefixes)); |
|---|
| 591 | $sql_blacklist = implode("', '", array_map('esc_sql', $this->get_post_meta_keys_blacklist())); |
|---|
| 592 | $sql_post_type = esc_sql($post_type); |
|---|
| 593 | |
|---|
| 594 | // find all existing meta data for this post type |
|---|
| 595 | $results = $wpdb->get_results(" |
|---|
| 596 | SELECT meta.meta_key, meta.meta_value |
|---|
| 597 | FROM $wpdb->postmeta AS meta |
|---|
| 598 | LEFT JOIN $wpdb->posts AS post ON (post.ID = meta.post_id) |
|---|
| 599 | WHERE post.post_type = '$sql_post_type' AND meta.meta_key NOT LIKE '$sql_prefixes%' AND meta.meta_key NOT IN ('$sql_blacklist') |
|---|
| 600 | GROUP BY meta.meta_key" |
|---|
| 601 | ); |
|---|
| 602 | |
|---|
| 603 | // empty array of meta data sorted by meta keys |
|---|
| 604 | $meta_keys = array(); |
|---|
| 605 | |
|---|
| 606 | // registered meta_keys |
|---|
| 607 | $registered_meta_keys = $this->get_post_type_metakeys($post_type); |
|---|
| 608 | |
|---|
| 609 | foreach ($registered_meta_keys as $meta_key) { |
|---|
| 610 | |
|---|
| 611 | $meta_keys[$meta_key] = array(); |
|---|
| 612 | |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | // add database results |
|---|
| 616 | foreach ($results as $row) { |
|---|
| 617 | |
|---|
| 618 | $meta_keys[$row->meta_key][] = substr(wp_strip_all_tags($row->meta_value, true), 0, 120); |
|---|
| 619 | |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | /** |
|---|
| 623 | * Filter default post type option |
|---|
| 624 | * |
|---|
| 625 | * @from 2.3 |
|---|
| 626 | * |
|---|
| 627 | * @param mixed. Default option |
|---|
| 628 | */ |
|---|
| 629 | $meta_keys = apply_filters("sublanguage_post_type_metakeys", $meta_keys, $post_type); |
|---|
| 630 | |
|---|
| 631 | ksort($meta_keys); |
|---|
| 632 | |
|---|
| 633 | return $meta_keys; |
|---|
| 634 | |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | /* |
|---|
| 638 | * List of post meta keys that should never be translated |
|---|
| 639 | * |
|---|
| 640 | * @from 1.5 |
|---|
| 641 | */ |
|---|
| 642 | private function get_post_meta_keys_blacklist() { |
|---|
| 643 | |
|---|
| 644 | return apply_filters('sublanguage_meta_keys_blacklist', array( |
|---|
| 645 | '_wp_attached_file', |
|---|
| 646 | '_wp_attachment_metadata', |
|---|
| 647 | '_edit_lock', |
|---|
| 648 | '_edit_last', |
|---|
| 649 | '_wp_page_template' |
|---|
| 650 | )); |
|---|
| 651 | |
|---|
| 652 | } |
|---|
| 653 | |
|---|
| 654 | /** |
|---|
| 655 | * Get taxonomy meta keys |
|---|
| 656 | * |
|---|
| 657 | * @from 2.0 |
|---|
| 658 | */ |
|---|
| 659 | private function query_taxonomy_metakeys($taxonomy) { |
|---|
| 660 | global $wpdb; |
|---|
| 661 | |
|---|
| 662 | $prefixes = array(); |
|---|
| 663 | |
|---|
| 664 | foreach ($this->get_languages() as $language) { |
|---|
| 665 | |
|---|
| 666 | $prefixes[] = $this->get_prefix($language); |
|---|
| 667 | |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | $sql_prefixes = implode("%' AND meta.meta_key NOT LIKE '", array_map('esc_sql', $prefixes)); |
|---|
| 671 | $sql_blacklist = implode("', '", array_map('esc_sql', $this->get_taxonomy_meta_keys_blacklist())); |
|---|
| 672 | $sql_taxonomy = esc_sql($taxonomy); |
|---|
| 673 | |
|---|
| 674 | // find all existing meta data for this post type |
|---|
| 675 | $results = $wpdb->get_results(" |
|---|
| 676 | SELECT meta.meta_key, meta.meta_value |
|---|
| 677 | FROM $wpdb->termmeta AS meta |
|---|
| 678 | LEFT JOIN $wpdb->term_taxonomy AS tt ON (tt.term_id = meta.term_id) |
|---|
| 679 | WHERE tt.taxonomy = '$sql_taxonomy' AND meta.meta_key NOT LIKE '$sql_prefixes%' AND meta.meta_key NOT IN ('$sql_blacklist') |
|---|
| 680 | GROUP BY meta.meta_key" |
|---|
| 681 | ); |
|---|
| 682 | |
|---|
| 683 | // empty array of meta data sorted by meta keys |
|---|
| 684 | $meta_keys = array(); |
|---|
| 685 | |
|---|
| 686 | // registered meta_keys |
|---|
| 687 | $registered_meta_keys = $this->get_taxonomy_metakeys($taxonomy); |
|---|
| 688 | |
|---|
| 689 | foreach ($registered_meta_keys as $meta_key) { |
|---|
| 690 | |
|---|
| 691 | $meta_keys[$meta_key] = array(); |
|---|
| 692 | |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | // add database results |
|---|
| 696 | foreach ($results as $row) { |
|---|
| 697 | |
|---|
| 698 | $meta_keys[$row->meta_key][] = substr(wp_strip_all_tags($row->meta_value, true), 0, 120); |
|---|
| 699 | |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | ksort($meta_keys); |
|---|
| 703 | |
|---|
| 704 | return $meta_keys; |
|---|
| 705 | |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| 708 | /* |
|---|
| 709 | * List of taxonomy meta keys that should never be translated |
|---|
| 710 | * |
|---|
| 711 | * @from 1.5 |
|---|
| 712 | */ |
|---|
| 713 | private function get_taxonomy_meta_keys_blacklist() { |
|---|
| 714 | |
|---|
| 715 | return apply_filters('sublanguage_meta_keys_blacklist', array()); |
|---|
| 716 | |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | /** |
|---|
| 720 | * Flush rewrite rules |
|---|
| 721 | * |
|---|
| 722 | * @hook 'wp_loaded' |
|---|
| 723 | * @from 2.0 |
|---|
| 724 | */ |
|---|
| 725 | public function flush_rewrite_rules() { |
|---|
| 726 | |
|---|
| 727 | if ($this->get_option('need_flush')) { |
|---|
| 728 | |
|---|
| 729 | $this->disable_translate_home_url = true; |
|---|
| 730 | |
|---|
| 731 | flush_rewrite_rules(); |
|---|
| 732 | |
|---|
| 733 | $this->disable_translate_home_url = false; |
|---|
| 734 | |
|---|
| 735 | } |
|---|
| 736 | |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | /** |
|---|
| 740 | * Remove 'need_flush' flag after flush |
|---|
| 741 | * |
|---|
| 742 | * @hook for 'generate_rewrite_rules' |
|---|
| 743 | * @from 2.0 |
|---|
| 744 | */ |
|---|
| 745 | public function generate_rewrite_rules($wp_rewrite) { |
|---|
| 746 | |
|---|
| 747 | $this->update_option('need_flush', 0); |
|---|
| 748 | |
|---|
| 749 | } |
|---|
| 750 | |
|---|
| 751 | /** |
|---|
| 752 | * Flush after creating a root page |
|---|
| 753 | * |
|---|
| 754 | * @hook for 'generate_rewrite_rules' |
|---|
| 755 | * @from 2.0 |
|---|
| 756 | */ |
|---|
| 757 | public function save_page($post_ID, $post, $update) { |
|---|
| 758 | |
|---|
| 759 | if (!$update && $post->post_parent === 0) { |
|---|
| 760 | |
|---|
| 761 | $this->update_option('need_flush', 1); |
|---|
| 762 | |
|---|
| 763 | } |
|---|
| 764 | |
|---|
| 765 | } |
|---|
| 766 | |
|---|
| 767 | /** |
|---|
| 768 | * Flush after editing a root page |
|---|
| 769 | * |
|---|
| 770 | * @hook for 'post_updated' |
|---|
| 771 | * @from 2.0 |
|---|
| 772 | * @from 2.10 Remove post_name unchanged condition |
|---|
| 773 | */ |
|---|
| 774 | public function update_page($post_ID, $post_after, $post_before) { |
|---|
| 775 | |
|---|
| 776 | if (($post_after->post_type === 'page' || $post_before->post_type === 'page') && ($post_after->post_parent === 0 || $post_before->post_parent === 0) && ($post_after->post_status === 'publish' || $post_before->post_status === 'publish')) { |
|---|
| 777 | |
|---|
| 778 | $this->update_option('need_flush', 1); |
|---|
| 779 | |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | } |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | |
|---|
| 786 | /* Languages UI |
|---|
| 787 | ----------------------------------------------- */ |
|---|
| 788 | |
|---|
| 789 | /** |
|---|
| 790 | * Init languages ui |
|---|
| 791 | * |
|---|
| 792 | * @hook 'init' |
|---|
| 793 | * |
|---|
| 794 | * @from 2.5 |
|---|
| 795 | */ |
|---|
| 796 | public function init_languages() { |
|---|
| 797 | |
|---|
| 798 | // preset slug and title |
|---|
| 799 | add_filter('wp_insert_post_data', array($this, 'set_default_slug_and_name'), 10, 2); |
|---|
| 800 | |
|---|
| 801 | // Update post on save |
|---|
| 802 | add_action('save_post_' . $this->language_post_type, array($this, 'save_language_meta_box'), 10, 3); |
|---|
| 803 | |
|---|
| 804 | // sort languages by menu_order in language list (no default order because it is not hierarchical) |
|---|
| 805 | add_filter( 'parse_query', array($this, 'sort_languages_table' )); |
|---|
| 806 | |
|---|
| 807 | // never hide the slug meta box |
|---|
| 808 | add_filter('default_hidden_meta_boxes', array($this, 'hide_language_settings'), null, 2); |
|---|
| 809 | |
|---|
| 810 | // Remove title, slug and attributes on new post |
|---|
| 811 | add_action('load-post-new.php', array($this, 'new_language_post')); |
|---|
| 812 | |
|---|
| 813 | // add meta-box for locale and rtl |
|---|
| 814 | add_action('load-post.php', array($this, 'edit_language_post')); |
|---|
| 815 | |
|---|
| 816 | add_filter( 'post_updated_messages', array($this, 'post_updated_messages')); |
|---|
| 817 | |
|---|
| 818 | } |
|---|
| 819 | |
|---|
| 820 | /** |
|---|
| 821 | * Customize notice messages for language posts |
|---|
| 822 | * |
|---|
| 823 | * @filter for 'post_updated_messages' |
|---|
| 824 | * |
|---|
| 825 | * @from 2.3 |
|---|
| 826 | */ |
|---|
| 827 | public function post_updated_messages($messages) { |
|---|
| 828 | |
|---|
| 829 | $post = get_post(); |
|---|
| 830 | |
|---|
| 831 | if (isset($post->post_type) && $post->post_type === $this->language_post_type) { |
|---|
| 832 | |
|---|
| 833 | $messages[$this->language_post_type][1] = __( 'Language updated.', 'sublanguage' ); |
|---|
| 834 | $messages[$this->language_post_type][4] = __( 'Language updated.', 'sublanguage' ); |
|---|
| 835 | $messages[$this->language_post_type][6] = __( 'Language added.', 'sublanguage' ); |
|---|
| 836 | $messages[$this->language_post_type][7] = __( 'Language saved.', 'sublanguage' ); |
|---|
| 837 | $messages[$this->language_post_type][10] = __( 'Language draft updated.', 'sublanguage' ); |
|---|
| 838 | |
|---|
| 839 | } |
|---|
| 840 | |
|---|
| 841 | return $messages; |
|---|
| 842 | } |
|---|
| 843 | |
|---|
| 844 | /** |
|---|
| 845 | * Remove title on post-new.php |
|---|
| 846 | * |
|---|
| 847 | * Hook for 'load-post-new.php' |
|---|
| 848 | * |
|---|
| 849 | * @from 1.2 |
|---|
| 850 | */ |
|---|
| 851 | public function new_language_post() { |
|---|
| 852 | |
|---|
| 853 | $current_screen = get_current_screen(); |
|---|
| 854 | |
|---|
| 855 | if (isset($current_screen->post_type) && $current_screen->post_type === $this->language_post_type) { |
|---|
| 856 | |
|---|
| 857 | remove_post_type_support($current_screen->post_type, 'title'); |
|---|
| 858 | remove_meta_box('slugdiv', $current_screen->post_type, 'normal'); |
|---|
| 859 | |
|---|
| 860 | add_meta_box( |
|---|
| 861 | 'languages_local', |
|---|
| 862 | __( 'Locale', 'sublanguage' ), |
|---|
| 863 | array($this, 'locale_dropdown_meta_box_callback'), |
|---|
| 864 | 'language', |
|---|
| 865 | 'normal', |
|---|
| 866 | 'default' |
|---|
| 867 | ); |
|---|
| 868 | |
|---|
| 869 | } |
|---|
| 870 | |
|---|
| 871 | } |
|---|
| 872 | |
|---|
| 873 | /** |
|---|
| 874 | * Add meta-boxes on post.php |
|---|
| 875 | * |
|---|
| 876 | * Hook for 'load-post-new.php' |
|---|
| 877 | * |
|---|
| 878 | * @from 1.2 |
|---|
| 879 | */ |
|---|
| 880 | public function edit_language_post() { |
|---|
| 881 | |
|---|
| 882 | $current_screen = get_current_screen(); |
|---|
| 883 | |
|---|
| 884 | if (isset($current_screen->post_type) && $current_screen->post_type === $this->language_post_type) { |
|---|
| 885 | |
|---|
| 886 | add_meta_box( |
|---|
| 887 | 'languages_local', |
|---|
| 888 | __( 'Locale', 'sublanguage' ), |
|---|
| 889 | array($this, 'locale_meta_box_callback'), |
|---|
| 890 | $this->language_post_type, |
|---|
| 891 | 'normal', |
|---|
| 892 | 'default' |
|---|
| 893 | ); |
|---|
| 894 | |
|---|
| 895 | add_meta_box( |
|---|
| 896 | 'languages_settings', |
|---|
| 897 | __( 'Settings', 'sublanguage' ), |
|---|
| 898 | array($this, 'settings_meta_box_callback'), |
|---|
| 899 | $this->language_post_type, |
|---|
| 900 | 'normal', |
|---|
| 901 | 'default' |
|---|
| 902 | ); |
|---|
| 903 | |
|---|
| 904 | } |
|---|
| 905 | |
|---|
| 906 | } |
|---|
| 907 | |
|---|
| 908 | /** |
|---|
| 909 | * Print language locale input meta-box |
|---|
| 910 | * |
|---|
| 911 | * @from 1.0 |
|---|
| 912 | */ |
|---|
| 913 | public function locale_meta_box_callback( $post ) { |
|---|
| 914 | |
|---|
| 915 | include plugin_dir_path( __FILE__ ) . 'include/language-locale-metabox.php'; |
|---|
| 916 | |
|---|
| 917 | } |
|---|
| 918 | |
|---|
| 919 | /** |
|---|
| 920 | * Print language locale dropdown meta-box |
|---|
| 921 | * |
|---|
| 922 | * @from 1.0 |
|---|
| 923 | */ |
|---|
| 924 | public function locale_dropdown_meta_box_callback( $post ) { |
|---|
| 925 | |
|---|
| 926 | include plugin_dir_path( __FILE__ ) . 'include/language-dropdown-metabox.php'; |
|---|
| 927 | |
|---|
| 928 | } |
|---|
| 929 | |
|---|
| 930 | /** |
|---|
| 931 | * Print meta-box |
|---|
| 932 | * |
|---|
| 933 | * @from 1.0 |
|---|
| 934 | */ |
|---|
| 935 | public function settings_meta_box_callback( $post ) { |
|---|
| 936 | |
|---|
| 937 | include plugin_dir_path( __FILE__ ) . 'include/language-settings-metabox.php'; |
|---|
| 938 | |
|---|
| 939 | } |
|---|
| 940 | |
|---|
| 941 | /** |
|---|
| 942 | * Set default language title and slug |
|---|
| 943 | * Filter for 'wp_insert_post_data' |
|---|
| 944 | * |
|---|
| 945 | * @from 1.0 |
|---|
| 946 | * @from 2.5 use post_excerpt for language tag (used for language detection + lang href) |
|---|
| 947 | */ |
|---|
| 948 | public function set_default_slug_and_name($data, $postarr) { |
|---|
| 949 | |
|---|
| 950 | if ($data['post_type'] === $this->language_post_type) { |
|---|
| 951 | |
|---|
| 952 | if (isset($_POST['language_locale_dropdown'], $_POST['language_locale_dropdown_nonce']) && wp_verify_nonce( $_POST['language_locale_dropdown_nonce'], 'language_locale_dropdown_action' )) { |
|---|
| 953 | |
|---|
| 954 | require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); |
|---|
| 955 | |
|---|
| 956 | $translations = wp_get_available_translations(); |
|---|
| 957 | $locale = $_POST['language_locale_dropdown']; |
|---|
| 958 | |
|---|
| 959 | $data['post_content'] = esc_attr($_POST['language_locale_dropdown']); |
|---|
| 960 | |
|---|
| 961 | if (isset($translations[$locale]['native_name'])) { |
|---|
| 962 | |
|---|
| 963 | $data['post_title'] = $translations[$locale]['native_name']; |
|---|
| 964 | |
|---|
| 965 | } else { // -> added in 1.4.7 |
|---|
| 966 | |
|---|
| 967 | $data['post_title'] = 'English'; |
|---|
| 968 | |
|---|
| 969 | } |
|---|
| 970 | |
|---|
| 971 | if (isset($translations[$locale]['iso'])) { |
|---|
| 972 | |
|---|
| 973 | while (is_array($translations[$locale]['iso'])) { |
|---|
| 974 | |
|---|
| 975 | $translations[$locale]['iso'] = array_shift($translations[$locale]['iso']); |
|---|
| 976 | |
|---|
| 977 | } |
|---|
| 978 | |
|---|
| 979 | $data['post_name'] = $translations[$locale]['iso']; |
|---|
| 980 | |
|---|
| 981 | } else { // -> added in 1.4.7 |
|---|
| 982 | |
|---|
| 983 | $data['post_name'] = 'en'; |
|---|
| 984 | |
|---|
| 985 | } |
|---|
| 986 | |
|---|
| 987 | // -> @since 2.5 |
|---|
| 988 | $data['post_excerpt'] = $data['post_name']; |
|---|
| 989 | |
|---|
| 990 | } else { |
|---|
| 991 | |
|---|
| 992 | if (isset($postarr['language_locale_nonce'], $postarr['language_locale']) && wp_verify_nonce($postarr['language_locale_nonce'], 'language_locale_action' )) { |
|---|
| 993 | |
|---|
| 994 | $data['post_content'] = $_POST['language_locale']; |
|---|
| 995 | |
|---|
| 996 | } |
|---|
| 997 | |
|---|
| 998 | if (isset($postarr['language_settings_nonce']) && wp_verify_nonce($postarr['language_settings_nonce'], 'language_settings_action')) { |
|---|
| 999 | |
|---|
| 1000 | if (isset($postarr['language_tag'])) { |
|---|
| 1001 | |
|---|
| 1002 | $data['post_excerpt'] = $postarr['language_tag']; |
|---|
| 1003 | |
|---|
| 1004 | } |
|---|
| 1005 | |
|---|
| 1006 | } |
|---|
| 1007 | |
|---|
| 1008 | } |
|---|
| 1009 | |
|---|
| 1010 | $this->update_option('need_flush', 1); |
|---|
| 1011 | |
|---|
| 1012 | } |
|---|
| 1013 | |
|---|
| 1014 | return $data; |
|---|
| 1015 | } |
|---|
| 1016 | |
|---|
| 1017 | /** |
|---|
| 1018 | * Save meta-box. |
|---|
| 1019 | * Hook for "save_post_{$post->post_type}" |
|---|
| 1020 | * |
|---|
| 1021 | * @from 1.4.7 update main option if post is trashed or if not exist. |
|---|
| 1022 | * @from 1.0 |
|---|
| 1023 | */ |
|---|
| 1024 | public function save_language_meta_box( $post_id, $post, $update ) { |
|---|
| 1025 | |
|---|
| 1026 | if ($post->post_type === $this->language_post_type) { |
|---|
| 1027 | |
|---|
| 1028 | if ((!defined('DOING_AUTOSAVE') || !DOING_AUTOSAVE) && current_user_can('edit_language', $post_id)) { |
|---|
| 1029 | |
|---|
| 1030 | // save locale (into post_content) |
|---|
| 1031 | |
|---|
| 1032 | if (isset($_POST['language_locale_nonce'], $_POST['language_locale']) |
|---|
| 1033 | && wp_verify_nonce($_POST['language_locale_nonce'], 'language_locale_action' )) { |
|---|
| 1034 | |
|---|
| 1035 | $locale = esc_attr($_POST['language_locale']); |
|---|
| 1036 | |
|---|
| 1037 | // download language pack |
|---|
| 1038 | |
|---|
| 1039 | if ($post->post_content != $locale) { |
|---|
| 1040 | |
|---|
| 1041 | require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); |
|---|
| 1042 | |
|---|
| 1043 | wp_download_language_pack($locale); |
|---|
| 1044 | |
|---|
| 1045 | wp_update_post(array( |
|---|
| 1046 | 'ID' => $post->ID, |
|---|
| 1047 | 'post_content' => $locale |
|---|
| 1048 | )); |
|---|
| 1049 | |
|---|
| 1050 | } |
|---|
| 1051 | |
|---|
| 1052 | $this->update_option('need_flush', 1); |
|---|
| 1053 | |
|---|
| 1054 | } |
|---|
| 1055 | |
|---|
| 1056 | // save language settings |
|---|
| 1057 | if (isset($_POST['language_settings_nonce']) |
|---|
| 1058 | && wp_verify_nonce($_POST['language_settings_nonce'], 'language_settings_action')) { |
|---|
| 1059 | |
|---|
| 1060 | if (isset($_POST['language_rtl']) && $_POST['language_rtl']) { |
|---|
| 1061 | |
|---|
| 1062 | update_post_meta($post->ID, 'rtl', 1); |
|---|
| 1063 | |
|---|
| 1064 | } |
|---|
| 1065 | |
|---|
| 1066 | } |
|---|
| 1067 | |
|---|
| 1068 | // verify settings -> added in 1.4.7 |
|---|
| 1069 | if ($post->post_status == "trash" && $this->is_main($post)) { |
|---|
| 1070 | |
|---|
| 1071 | $next_language = $this->get_valid_language($post); |
|---|
| 1072 | |
|---|
| 1073 | if ($next_language) { |
|---|
| 1074 | |
|---|
| 1075 | $this->update_option('main', $next_language->ID); |
|---|
| 1076 | |
|---|
| 1077 | } |
|---|
| 1078 | |
|---|
| 1079 | } else if (!$this->get_option('main')) { |
|---|
| 1080 | |
|---|
| 1081 | $this->update_option('main', $post_id); |
|---|
| 1082 | |
|---|
| 1083 | } |
|---|
| 1084 | |
|---|
| 1085 | } |
|---|
| 1086 | |
|---|
| 1087 | } |
|---|
| 1088 | |
|---|
| 1089 | } |
|---|
| 1090 | |
|---|
| 1091 | /** |
|---|
| 1092 | * Order posts by menu_order |
|---|
| 1093 | * @hook 'parse_query' |
|---|
| 1094 | * |
|---|
| 1095 | * @from 1.0 |
|---|
| 1096 | */ |
|---|
| 1097 | public function sort_languages_table($query) { |
|---|
| 1098 | |
|---|
| 1099 | if (function_exists('get_current_screen')) { |
|---|
| 1100 | |
|---|
| 1101 | $screen = get_current_screen(); |
|---|
| 1102 | |
|---|
| 1103 | if (isset($screen->id) && $screen->id == 'edit-language') { |
|---|
| 1104 | |
|---|
| 1105 | $query->query_vars['orderby'] = 'menu_order'; |
|---|
| 1106 | $query->query_vars['order'] = 'ASC'; |
|---|
| 1107 | |
|---|
| 1108 | } |
|---|
| 1109 | |
|---|
| 1110 | } |
|---|
| 1111 | |
|---|
| 1112 | } |
|---|
| 1113 | |
|---|
| 1114 | /** |
|---|
| 1115 | * By default hide settings |
|---|
| 1116 | * @hook 'default_hidden_meta_boxes' |
|---|
| 1117 | * |
|---|
| 1118 | * @from 1.0 |
|---|
| 1119 | */ |
|---|
| 1120 | public function hide_language_settings($hidden, $screen) { |
|---|
| 1121 | |
|---|
| 1122 | if (isset($screen) && $screen->id == 'language') { |
|---|
| 1123 | |
|---|
| 1124 | $hidden[] = 'languages_settings'; |
|---|
| 1125 | |
|---|
| 1126 | $key = array_search('slugdiv', $hidden); |
|---|
| 1127 | |
|---|
| 1128 | if ($key !== false) { |
|---|
| 1129 | |
|---|
| 1130 | unset($hidden[$key]); |
|---|
| 1131 | |
|---|
| 1132 | } |
|---|
| 1133 | |
|---|
| 1134 | } |
|---|
| 1135 | |
|---|
| 1136 | return $hidden; |
|---|
| 1137 | } |
|---|
| 1138 | |
|---|
| 1139 | /** |
|---|
| 1140 | * find a language not in trash |
|---|
| 1141 | * |
|---|
| 1142 | * @from 1.4.7 |
|---|
| 1143 | */ |
|---|
| 1144 | public function get_valid_language($except_post) { |
|---|
| 1145 | |
|---|
| 1146 | foreach ($this->get_languages() as $language) { |
|---|
| 1147 | |
|---|
| 1148 | if ($language->post_status != 'trash' && $language->ID !== $except_post->ID) return $language; |
|---|
| 1149 | |
|---|
| 1150 | } |
|---|
| 1151 | |
|---|
| 1152 | return false; |
|---|
| 1153 | } |
|---|
| 1154 | |
|---|
| 1155 | |
|---|
| 1156 | |
|---|
| 1157 | |
|---|
| 1158 | /* Posts UI |
|---|
| 1159 | ----------------------------------------------- */ |
|---|
| 1160 | |
|---|
| 1161 | /** |
|---|
| 1162 | * Fire filters on post.php |
|---|
| 1163 | * |
|---|
| 1164 | * Hook for 'load-post.php' |
|---|
| 1165 | * |
|---|
| 1166 | * @from 1.1 |
|---|
| 1167 | */ |
|---|
| 1168 | public function admin_post_page() { |
|---|
| 1169 | |
|---|
| 1170 | $current_screen = get_current_screen(); |
|---|
| 1171 | |
|---|
| 1172 | if ($this->get_language() && isset($current_screen->post_type) && $this->is_post_type_translatable($current_screen->post_type)) { |
|---|
| 1173 | |
|---|
| 1174 | // translate $post global |
|---|
| 1175 | add_action('edit_form_top', array($this, 'edit_form')); |
|---|
| 1176 | |
|---|
| 1177 | // print language tab |
|---|
| 1178 | add_action('edit_form_top', array($this, 'print_post_language_tabs')); |
|---|
| 1179 | |
|---|
| 1180 | // post title placeholder |
|---|
| 1181 | add_filter('enter_title_here', array($this, 'set_post_title_placeholder'), 10, 2); |
|---|
| 1182 | |
|---|
| 1183 | // allow translate home url |
|---|
| 1184 | add_filter('home_url', array($this,'translate_home_url'), 10, 4); |
|---|
| 1185 | |
|---|
| 1186 | } |
|---|
| 1187 | |
|---|
| 1188 | } |
|---|
| 1189 | |
|---|
| 1190 | /** |
|---|
| 1191 | * Change the values of $post at the begin of form in post.php |
|---|
| 1192 | * |
|---|
| 1193 | * @hook 'edit_form_top' |
|---|
| 1194 | * @from 1.0 |
|---|
| 1195 | */ |
|---|
| 1196 | public function edit_form($post) { |
|---|
| 1197 | |
|---|
| 1198 | if ($this->is_sub()) { |
|---|
| 1199 | |
|---|
| 1200 | foreach ($this->fields as $field) { |
|---|
| 1201 | |
|---|
| 1202 | $post->$field = $this->translate_post_field($post, $field, null, ''); |
|---|
| 1203 | |
|---|
| 1204 | } |
|---|
| 1205 | |
|---|
| 1206 | } |
|---|
| 1207 | |
|---|
| 1208 | } |
|---|
| 1209 | |
|---|
| 1210 | /** |
|---|
| 1211 | * Customize title placeholder |
|---|
| 1212 | * |
|---|
| 1213 | * @filter 'enter_title_here' |
|---|
| 1214 | * @from 1.2 |
|---|
| 1215 | */ |
|---|
| 1216 | public function set_post_title_placeholder($title, $post) { |
|---|
| 1217 | |
|---|
| 1218 | if ($this->is_sub()) { |
|---|
| 1219 | |
|---|
| 1220 | return get_post($post->post_parent)->post_title; |
|---|
| 1221 | |
|---|
| 1222 | } |
|---|
| 1223 | |
|---|
| 1224 | return $title; |
|---|
| 1225 | } |
|---|
| 1226 | |
|---|
| 1227 | /** |
|---|
| 1228 | * Renders language switch tab |
|---|
| 1229 | * |
|---|
| 1230 | * @hook 'edit_form_top' |
|---|
| 1231 | * @from 1.5 |
|---|
| 1232 | */ |
|---|
| 1233 | public function print_post_language_tabs($post) { |
|---|
| 1234 | |
|---|
| 1235 | include plugin_dir_path( __FILE__ ) . 'include/posts-form-language-tabs.php'; |
|---|
| 1236 | |
|---|
| 1237 | } |
|---|
| 1238 | |
|---|
| 1239 | |
|---|
| 1240 | /** |
|---|
| 1241 | * fire filters on edit.php |
|---|
| 1242 | * |
|---|
| 1243 | * @hook 'load-edit.php' |
|---|
| 1244 | * @from 1.2 |
|---|
| 1245 | */ |
|---|
| 1246 | public function admin_edit_page() { |
|---|
| 1247 | |
|---|
| 1248 | $current_screen = get_current_screen(); |
|---|
| 1249 | |
|---|
| 1250 | if ($this->get_language() && isset($current_screen->post_type) && $this->is_post_type_translatable($current_screen->post_type)) { |
|---|
| 1251 | |
|---|
| 1252 | add_filter('views_'.$current_screen->id, array($this, 'table_views')); |
|---|
| 1253 | add_action('restrict_manage_posts', array($this, 'print_table_filtering')); |
|---|
| 1254 | |
|---|
| 1255 | } |
|---|
| 1256 | |
|---|
| 1257 | } |
|---|
| 1258 | |
|---|
| 1259 | /* |
|---|
| 1260 | * Set requested language |
|---|
| 1261 | * Filter for 'wp_redirect' |
|---|
| 1262 | * |
|---|
| 1263 | * @from 1.0 |
|---|
| 1264 | */ |
|---|
| 1265 | public function language_redirect( $location ) { |
|---|
| 1266 | |
|---|
| 1267 | if (isset($_POST['post_language_switch'], $_POST['sublanguage_switch_language_nonce']) && wp_verify_nonce($_POST['sublanguage_switch_language_nonce'], 'sublanguage_switch_language')) { |
|---|
| 1268 | |
|---|
| 1269 | if ($this->get_language_by($_POST['post_language_switch'], 'post_name')) { |
|---|
| 1270 | |
|---|
| 1271 | $location = add_query_arg(array($this->language_query_var => esc_attr($_POST['post_language_switch'])), $location); |
|---|
| 1272 | |
|---|
| 1273 | } |
|---|
| 1274 | |
|---|
| 1275 | } |
|---|
| 1276 | |
|---|
| 1277 | return $location; |
|---|
| 1278 | } |
|---|
| 1279 | |
|---|
| 1280 | /** |
|---|
| 1281 | * Print language switch for posts table |
|---|
| 1282 | * Add language param in view links |
|---|
| 1283 | * |
|---|
| 1284 | * @filter "views_{$this->screen->id}" ('views_edit-post') |
|---|
| 1285 | * @from 1.2 |
|---|
| 1286 | */ |
|---|
| 1287 | public function table_views($views) { |
|---|
| 1288 | |
|---|
| 1289 | $new_views = array(); |
|---|
| 1290 | |
|---|
| 1291 | $base_url = admin_url('edit.php'); |
|---|
| 1292 | |
|---|
| 1293 | if (isset($_GET['post_status'])) { |
|---|
| 1294 | |
|---|
| 1295 | $base_url = add_query_arg(array('post_status' => esc_attr($_GET['post_status'])), $base_url); |
|---|
| 1296 | |
|---|
| 1297 | } |
|---|
| 1298 | |
|---|
| 1299 | if (isset($_GET['post_type'])) { |
|---|
| 1300 | |
|---|
| 1301 | $base_url = add_query_arg(array('post_type' => esc_attr($_GET['post_type'])), $base_url); |
|---|
| 1302 | |
|---|
| 1303 | } |
|---|
| 1304 | |
|---|
| 1305 | ob_start(); |
|---|
| 1306 | |
|---|
| 1307 | include plugin_dir_path( __FILE__ ) . 'include/posts-table-language-switch.php'; |
|---|
| 1308 | |
|---|
| 1309 | $new_views[] = ob_get_contents(); |
|---|
| 1310 | ob_end_clean(); |
|---|
| 1311 | |
|---|
| 1312 | $language = $this->get_language(); |
|---|
| 1313 | |
|---|
| 1314 | if ($language) { |
|---|
| 1315 | |
|---|
| 1316 | foreach ($views as $view) { |
|---|
| 1317 | |
|---|
| 1318 | if (preg_match('/href=[\'"]([^\'"]*)/', $view, $matches)) { |
|---|
| 1319 | |
|---|
| 1320 | $match_decoded = html_entity_decode($matches[1]); //handle HTML encoding in links with existing parameters (IE in WooCommerce "Sort Products" link) // thx to @delacqua |
|---|
| 1321 | $new_views[] = str_replace($matches[1], add_query_arg(array($this->language_query_var => $language->post_name), $match_decoded), $view); |
|---|
| 1322 | |
|---|
| 1323 | } else { |
|---|
| 1324 | |
|---|
| 1325 | $new_views[] = $view; |
|---|
| 1326 | |
|---|
| 1327 | } |
|---|
| 1328 | |
|---|
| 1329 | } |
|---|
| 1330 | |
|---|
| 1331 | } |
|---|
| 1332 | |
|---|
| 1333 | return $new_views; |
|---|
| 1334 | } |
|---|
| 1335 | |
|---|
| 1336 | /** |
|---|
| 1337 | * Add language switch on posts table |
|---|
| 1338 | * |
|---|
| 1339 | * Hook for 'restrict_manage_posts' |
|---|
| 1340 | * |
|---|
| 1341 | * @from 1.1 |
|---|
| 1342 | */ |
|---|
| 1343 | public function print_table_filtering() { |
|---|
| 1344 | |
|---|
| 1345 | $language = $this->get_language(); |
|---|
| 1346 | |
|---|
| 1347 | if ($language) { |
|---|
| 1348 | |
|---|
| 1349 | echo '<input type=hidden name="'.$this->language_query_var.'" value="'.$language->post_name.'"/>'; |
|---|
| 1350 | |
|---|
| 1351 | } |
|---|
| 1352 | |
|---|
| 1353 | } |
|---|
| 1354 | |
|---|
| 1355 | |
|---|
| 1356 | |
|---|
| 1357 | |
|---|
| 1358 | |
|---|
| 1359 | |
|---|
| 1360 | /* Terms UI |
|---|
| 1361 | ----------------------------------------------- */ |
|---|
| 1362 | |
|---|
| 1363 | /** |
|---|
| 1364 | * fire filters on edit.php |
|---|
| 1365 | * |
|---|
| 1366 | * @hook 'load-edit-tags.php' |
|---|
| 1367 | * @from 1.2 |
|---|
| 1368 | */ |
|---|
| 1369 | public function admin_edit_tags() { |
|---|
| 1370 | |
|---|
| 1371 | $current_screen = get_current_screen(); |
|---|
| 1372 | |
|---|
| 1373 | if ($this->get_language() && isset($current_screen->taxonomy) && $this->is_taxonomy_translatable($current_screen->taxonomy)) { |
|---|
| 1374 | |
|---|
| 1375 | add_action($current_screen->taxonomy.'_edit_form_fields', array($this, 'add_term_edit_form'), 12, 2); |
|---|
| 1376 | |
|---|
| 1377 | add_action('after-'.$current_screen->taxonomy.'-table', array($this, 'add_terms_language_switch')); |
|---|
| 1378 | |
|---|
| 1379 | } |
|---|
| 1380 | |
|---|
| 1381 | } |
|---|
| 1382 | |
|---|
| 1383 | /** |
|---|
| 1384 | * Add translation box on terms edit form. |
|---|
| 1385 | * |
|---|
| 1386 | * @from 1.0 |
|---|
| 1387 | */ |
|---|
| 1388 | public function add_term_edit_form($tag, $taxonomy) { |
|---|
| 1389 | |
|---|
| 1390 | include plugin_dir_path( __FILE__ ) . 'include/terms-edit-form.php'; |
|---|
| 1391 | |
|---|
| 1392 | } |
|---|
| 1393 | |
|---|
| 1394 | /** |
|---|
| 1395 | * |
|---|
| 1396 | * @from 1.2 |
|---|
| 1397 | */ |
|---|
| 1398 | public function add_terms_language_switch($taxonomy) { |
|---|
| 1399 | |
|---|
| 1400 | include plugin_dir_path( __FILE__ ) . 'include/terms-table-language-switch.php'; |
|---|
| 1401 | |
|---|
| 1402 | } |
|---|
| 1403 | |
|---|
| 1404 | /** |
|---|
| 1405 | * Intercept update term and save term translation |
|---|
| 1406 | * |
|---|
| 1407 | * @hook "edit_term" |
|---|
| 1408 | * @from 1.0 |
|---|
| 1409 | */ |
|---|
| 1410 | public function save_term_translation($term_id, $tt_id, $taxonomy) { |
|---|
| 1411 | |
|---|
| 1412 | if ($this->is_taxonomy_translatable($taxonomy) |
|---|
| 1413 | && isset($_POST['sublanguage_term_nonce'], $_POST['sublanguage_term'][$taxonomy]) |
|---|
| 1414 | && wp_verify_nonce($_POST['sublanguage_term_nonce'], 'sublanguage')) { |
|---|
| 1415 | |
|---|
| 1416 | foreach ($_POST['sublanguage_term'][$taxonomy] as $lng_id => $data) { |
|---|
| 1417 | |
|---|
| 1418 | $language = $this->get_language_by($lng_id, 'ID'); |
|---|
| 1419 | |
|---|
| 1420 | if ($this->is_sub($language)) { |
|---|
| 1421 | |
|---|
| 1422 | $this->update_term_translation($term_id, $data, $language); |
|---|
| 1423 | |
|---|
| 1424 | } |
|---|
| 1425 | |
|---|
| 1426 | } |
|---|
| 1427 | |
|---|
| 1428 | } |
|---|
| 1429 | |
|---|
| 1430 | } |
|---|
| 1431 | |
|---|
| 1432 | |
|---|
| 1433 | |
|---|
| 1434 | |
|---|
| 1435 | /* Menu and Extra Post Types |
|---|
| 1436 | ----------------------------------------------- */ |
|---|
| 1437 | |
|---|
| 1438 | /** |
|---|
| 1439 | * Add metabox on admin menu page |
|---|
| 1440 | * @from 1.2 |
|---|
| 1441 | */ |
|---|
| 1442 | public function add_language_meta_box() { |
|---|
| 1443 | |
|---|
| 1444 | add_meta_box( |
|---|
| 1445 | 'language_menu', |
|---|
| 1446 | __('Language', 'sublanguage'), |
|---|
| 1447 | array( $this, 'render_menu_language_metabox' ), |
|---|
| 1448 | 'nav-menus', |
|---|
| 1449 | 'side', |
|---|
| 1450 | 'high' |
|---|
| 1451 | ); |
|---|
| 1452 | |
|---|
| 1453 | } |
|---|
| 1454 | |
|---|
| 1455 | /** |
|---|
| 1456 | * Render Meta Box content |
|---|
| 1457 | * @from 1.2 |
|---|
| 1458 | */ |
|---|
| 1459 | public function render_menu_language_metabox() { |
|---|
| 1460 | |
|---|
| 1461 | include plugin_dir_path( __FILE__ ) . 'include/nav-menu-language-metabox.php'; |
|---|
| 1462 | |
|---|
| 1463 | } |
|---|
| 1464 | |
|---|
| 1465 | /** |
|---|
| 1466 | * set default nav_menu_item options |
|---|
| 1467 | * |
|---|
| 1468 | * @filter "sublanguage_default-$post_type" |
|---|
| 1469 | * @from 2.3 |
|---|
| 1470 | */ |
|---|
| 1471 | public function nav_menu_item_default_options($defaults) { |
|---|
| 1472 | |
|---|
| 1473 | $defaults['meta_keys'] = array('sublanguage_hide', '_menu_item_url'); |
|---|
| 1474 | |
|---|
| 1475 | return $defaults; |
|---|
| 1476 | } |
|---|
| 1477 | |
|---|
| 1478 | /** |
|---|
| 1479 | * Force 'sublanguage_hide' to appear in meta_keys list |
|---|
| 1480 | * |
|---|
| 1481 | * @filter "sublanguage_post_type_metakeys" |
|---|
| 1482 | * @from 2.3 |
|---|
| 1483 | */ |
|---|
| 1484 | public function nav_menu_item_metakeys($value, $post_type) { |
|---|
| 1485 | |
|---|
| 1486 | if ($post_type === 'nav_menu_item' && empty($value['sublanguage_hide'])) { |
|---|
| 1487 | |
|---|
| 1488 | $value['sublanguage_hide'] = true; |
|---|
| 1489 | |
|---|
| 1490 | } |
|---|
| 1491 | |
|---|
| 1492 | return $value; |
|---|
| 1493 | } |
|---|
| 1494 | |
|---|
| 1495 | /** |
|---|
| 1496 | * Re-register translatable custom post without admin UI |
|---|
| 1497 | * |
|---|
| 1498 | * @hook 'init' |
|---|
| 1499 | * @from 1.5 |
|---|
| 1500 | */ |
|---|
| 1501 | public function register_extra_post() { |
|---|
| 1502 | |
|---|
| 1503 | $cpts = get_post_types(array( |
|---|
| 1504 | 'show_ui' => false |
|---|
| 1505 | ), 'objects' ); |
|---|
| 1506 | |
|---|
| 1507 | foreach ($cpts as $cpt) { |
|---|
| 1508 | |
|---|
| 1509 | if ($this->is_post_type_translatable($cpt->name)) { |
|---|
| 1510 | |
|---|
| 1511 | register_post_type($cpt->name, array( |
|---|
| 1512 | 'labels' => array( |
|---|
| 1513 | 'name' => isset($cpt->labels->name) ? $cpt->labels->name : $cpt->name, |
|---|
| 1514 | 'singular_name' => isset($cpt->labels->singular_name) ? $cpt->labels->singular_name : $cpt->name |
|---|
| 1515 | ), |
|---|
| 1516 | 'show_ui' => true, |
|---|
| 1517 | 'menu_position' => 50, |
|---|
| 1518 | 'show_in_menu' => 'tools.php', |
|---|
| 1519 | )); |
|---|
| 1520 | |
|---|
| 1521 | remove_post_type_support($cpt->name, 'editor'); |
|---|
| 1522 | |
|---|
| 1523 | if ($cpt->name === 'nav_menu_item') { |
|---|
| 1524 | |
|---|
| 1525 | add_filter('the_posts', array($this, 'fix_nav_menu_item_parent'), 15); |
|---|
| 1526 | add_filter('the_posts', array($this, 'nav_menu_replace_language_keyword'), 14); |
|---|
| 1527 | add_filter('the_title', array($this, 'translate_nav_menu_item_title'), 10, 2); |
|---|
| 1528 | add_filter('enter_title_here', array($this, 'nav_menu_item_title_placeholder'), 11, 2); |
|---|
| 1529 | |
|---|
| 1530 | add_action('admin_init', array($this, 'register_nav_menu_item_metabox')); |
|---|
| 1531 | |
|---|
| 1532 | } else { |
|---|
| 1533 | |
|---|
| 1534 | $this->extra_post_types[] = $cpt->name; |
|---|
| 1535 | |
|---|
| 1536 | } |
|---|
| 1537 | |
|---|
| 1538 | add_action('save_post_' . $cpt->name, array($this, 'save_extra_custom_post'), 10, 2); |
|---|
| 1539 | |
|---|
| 1540 | } |
|---|
| 1541 | |
|---|
| 1542 | } |
|---|
| 1543 | |
|---|
| 1544 | if ($this->extra_post_types) { |
|---|
| 1545 | |
|---|
| 1546 | add_action('admin_init', array($this, 'register_extra_post_metabox')); |
|---|
| 1547 | |
|---|
| 1548 | } |
|---|
| 1549 | |
|---|
| 1550 | } |
|---|
| 1551 | |
|---|
| 1552 | /** |
|---|
| 1553 | * Register metabox for extra post-types (without UI) |
|---|
| 1554 | * |
|---|
| 1555 | * @hook 'admin_init' |
|---|
| 1556 | * @from 1.5 |
|---|
| 1557 | */ |
|---|
| 1558 | public function register_extra_post_metabox() { |
|---|
| 1559 | |
|---|
| 1560 | add_meta_box( |
|---|
| 1561 | 'sublanguage-cpt-translation', |
|---|
| 1562 | __('Parameters', 'sublanguage'), |
|---|
| 1563 | array( $this, 'print_extra_post_metabox' ), |
|---|
| 1564 | $this->extra_post_types, |
|---|
| 1565 | 'normal' |
|---|
| 1566 | ); |
|---|
| 1567 | |
|---|
| 1568 | } |
|---|
| 1569 | |
|---|
| 1570 | /** |
|---|
| 1571 | * Register metabox specifically for Nav Menu Items |
|---|
| 1572 | * |
|---|
| 1573 | * @hook 'admin_init' |
|---|
| 1574 | * @from 2.0 |
|---|
| 1575 | */ |
|---|
| 1576 | public function register_nav_menu_item_metabox() { |
|---|
| 1577 | |
|---|
| 1578 | add_meta_box( |
|---|
| 1579 | 'sublanguage-cpt-translation', |
|---|
| 1580 | __('Parameters', 'sublanguage'), |
|---|
| 1581 | array( $this, 'print_nav_menu_items_metabox' ), |
|---|
| 1582 | 'nav_menu_item', |
|---|
| 1583 | 'normal' |
|---|
| 1584 | ); |
|---|
| 1585 | |
|---|
| 1586 | } |
|---|
| 1587 | |
|---|
| 1588 | /** |
|---|
| 1589 | * Render metabox for extra post types |
|---|
| 1590 | * |
|---|
| 1591 | * @callback for 'add_meta_box()' |
|---|
| 1592 | * @from 1.5 |
|---|
| 1593 | */ |
|---|
| 1594 | public function print_extra_post_metabox($post) { |
|---|
| 1595 | |
|---|
| 1596 | include plugin_dir_path( __FILE__ ) . 'include/extra-post-metabox.php'; |
|---|
| 1597 | |
|---|
| 1598 | } |
|---|
| 1599 | |
|---|
| 1600 | /** |
|---|
| 1601 | * Render metabox for extra post types |
|---|
| 1602 | * |
|---|
| 1603 | * @callback for 'add_meta_box()' |
|---|
| 1604 | * @from 1.5 |
|---|
| 1605 | */ |
|---|
| 1606 | public function print_nav_menu_items_metabox($post) { |
|---|
| 1607 | |
|---|
| 1608 | include plugin_dir_path( __FILE__ ) . 'include/nav-menu-item-metabox.php'; |
|---|
| 1609 | |
|---|
| 1610 | } |
|---|
| 1611 | |
|---|
| 1612 | /** |
|---|
| 1613 | * Correct menu items parents |
|---|
| 1614 | * |
|---|
| 1615 | * @filter 'the_posts' |
|---|
| 1616 | * @from 1.5 |
|---|
| 1617 | */ |
|---|
| 1618 | public function fix_nav_menu_item_parent($posts) { |
|---|
| 1619 | |
|---|
| 1620 | foreach ($posts as $post) { |
|---|
| 1621 | |
|---|
| 1622 | if ($post->post_type === 'nav_menu_item') { |
|---|
| 1623 | |
|---|
| 1624 | $_menu_item_menu_item_parent = get_post_meta($post->ID, '_menu_item_menu_item_parent', true); |
|---|
| 1625 | |
|---|
| 1626 | $post->post_parent = $_menu_item_menu_item_parent; |
|---|
| 1627 | |
|---|
| 1628 | } |
|---|
| 1629 | |
|---|
| 1630 | } |
|---|
| 1631 | |
|---|
| 1632 | return $posts; |
|---|
| 1633 | } |
|---|
| 1634 | |
|---|
| 1635 | |
|---|
| 1636 | /** |
|---|
| 1637 | * Replace 'language' keyword by language name |
|---|
| 1638 | * |
|---|
| 1639 | * @filter 'the_posts' |
|---|
| 1640 | * @from 1.5 |
|---|
| 1641 | */ |
|---|
| 1642 | public function nav_menu_replace_language_keyword($posts) { |
|---|
| 1643 | |
|---|
| 1644 | $menu_language_index = 0; |
|---|
| 1645 | $languages = $this->get_languages(); |
|---|
| 1646 | |
|---|
| 1647 | foreach ($posts as $post) { |
|---|
| 1648 | |
|---|
| 1649 | if ($post->post_type === 'nav_menu_item' && $post->post_title === 'language' && get_post_meta($post->ID, '_menu_item_type', true) === 'custom' && isset($languages[$menu_language_index])) { |
|---|
| 1650 | |
|---|
| 1651 | if (empty($this->menu_languages)) { |
|---|
| 1652 | |
|---|
| 1653 | $this->menu_languages = array(); |
|---|
| 1654 | |
|---|
| 1655 | } |
|---|
| 1656 | |
|---|
| 1657 | $this->menu_languages[$post->ID] = $languages[$menu_language_index]; |
|---|
| 1658 | |
|---|
| 1659 | $menu_language_index++; |
|---|
| 1660 | |
|---|
| 1661 | } |
|---|
| 1662 | |
|---|
| 1663 | } |
|---|
| 1664 | |
|---|
| 1665 | return $posts; |
|---|
| 1666 | } |
|---|
| 1667 | |
|---|
| 1668 | /** |
|---|
| 1669 | * Translate nav menu items title |
|---|
| 1670 | * |
|---|
| 1671 | * @filter 'the_title' |
|---|
| 1672 | * @from 1.5 |
|---|
| 1673 | */ |
|---|
| 1674 | public function translate_nav_menu_item_title($title, $post_id) { |
|---|
| 1675 | |
|---|
| 1676 | $post = get_post($post_id); |
|---|
| 1677 | |
|---|
| 1678 | if (isset($post->post_type) && $post->post_type === 'nav_menu_item') { |
|---|
| 1679 | |
|---|
| 1680 | $title = apply_filters( 'sublanguage_translate_post_field', $post->post_title, $post, 'post_title'); |
|---|
| 1681 | |
|---|
| 1682 | $_menu_item_type = get_post_meta($post_id, '_menu_item_type', true); |
|---|
| 1683 | |
|---|
| 1684 | if ($_menu_item_type === 'post_type') { |
|---|
| 1685 | |
|---|
| 1686 | if (!$title) { |
|---|
| 1687 | |
|---|
| 1688 | $_menu_item_object_id = get_post_meta($post_id, '_menu_item_object_id', true); |
|---|
| 1689 | |
|---|
| 1690 | $object_post = get_post($_menu_item_object_id); |
|---|
| 1691 | |
|---|
| 1692 | $title = apply_filters( 'sublanguage_translate_post_field', $object_post->post_title, $object_post, 'post_title'); |
|---|
| 1693 | |
|---|
| 1694 | } |
|---|
| 1695 | |
|---|
| 1696 | } else if ($_menu_item_type === 'taxonomy') { |
|---|
| 1697 | |
|---|
| 1698 | if (!$title) { |
|---|
| 1699 | |
|---|
| 1700 | $_menu_item_object_id = get_post_meta($post_id, '_menu_item_object_id', true); |
|---|
| 1701 | $_menu_item_object = get_post_meta($post_id, '_menu_item_object', true); |
|---|
| 1702 | |
|---|
| 1703 | $object_term = get_term_by('id', $_menu_item_object_id, $_menu_item_object); |
|---|
| 1704 | |
|---|
| 1705 | $title = $object_term->name; |
|---|
| 1706 | |
|---|
| 1707 | } |
|---|
| 1708 | |
|---|
| 1709 | |
|---|
| 1710 | } else if ($_menu_item_type === 'custom') { |
|---|
| 1711 | |
|---|
| 1712 | if ($title === 'language' && isset($this->menu_languages[$post->ID])) { |
|---|
| 1713 | |
|---|
| 1714 | $title = $this->menu_languages[$post->ID]->post_title; |
|---|
| 1715 | |
|---|
| 1716 | } |
|---|
| 1717 | |
|---|
| 1718 | } |
|---|
| 1719 | |
|---|
| 1720 | } |
|---|
| 1721 | |
|---|
| 1722 | return $title; |
|---|
| 1723 | } |
|---|
| 1724 | |
|---|
| 1725 | /** |
|---|
| 1726 | * Customize title placeholder for nav menu items |
|---|
| 1727 | * |
|---|
| 1728 | * @filter 'enter_title_here' |
|---|
| 1729 | * @from 1.5 |
|---|
| 1730 | */ |
|---|
| 1731 | public function nav_menu_item_title_placeholder($title, $post) { |
|---|
| 1732 | |
|---|
| 1733 | if ($post->post_type === 'nav_menu_item') { |
|---|
| 1734 | |
|---|
| 1735 | $_menu_item_type = get_post_meta($post->ID, '_menu_item_type', true); |
|---|
| 1736 | |
|---|
| 1737 | if ($_menu_item_type === 'post_type') { |
|---|
| 1738 | |
|---|
| 1739 | $_menu_item_object_id = get_post_meta($post->ID, '_menu_item_object_id', true); |
|---|
| 1740 | |
|---|
| 1741 | $object_post = get_post($_menu_item_object_id); |
|---|
| 1742 | |
|---|
| 1743 | $title = apply_filters( 'sublanguage_translate_post_field', $object_post->post_title, $object_post, 'post_title'); |
|---|
| 1744 | |
|---|
| 1745 | |
|---|
| 1746 | } else if ($_menu_item_type === 'taxonomy') { |
|---|
| 1747 | |
|---|
| 1748 | $_menu_item_object_id = get_post_meta($post->ID, '_menu_item_object_id', true); |
|---|
| 1749 | $_menu_item_object = get_post_meta($post->ID, '_menu_item_object', true); |
|---|
| 1750 | |
|---|
| 1751 | $object_term = get_term_by('id', $_menu_item_object_id, $_menu_item_object); |
|---|
| 1752 | |
|---|
| 1753 | $title = $object_term->name; |
|---|
| 1754 | |
|---|
| 1755 | } else if ($_menu_item_type === 'custom') { |
|---|
| 1756 | |
|---|
| 1757 | $title = $post->post_title; |
|---|
| 1758 | |
|---|
| 1759 | } |
|---|
| 1760 | |
|---|
| 1761 | } |
|---|
| 1762 | |
|---|
| 1763 | return $title; |
|---|
| 1764 | } |
|---|
| 1765 | |
|---|
| 1766 | /** |
|---|
| 1767 | * Save translatable custom post type without admin UI |
|---|
| 1768 | * |
|---|
| 1769 | * @hook for "save_post_{$post->post_type}" |
|---|
| 1770 | * @from 1.5 |
|---|
| 1771 | */ |
|---|
| 1772 | public function save_extra_custom_post($post_id, $post) { |
|---|
| 1773 | |
|---|
| 1774 | if ((!defined('DOING_AUTOSAVE') || !DOING_AUTOSAVE) && current_user_can('edit_post', $post_id)) { |
|---|
| 1775 | |
|---|
| 1776 | if (isset($_POST['sublanguage_extra_cpt_nonce'], $_POST['sublanguage_extra_cpt']) && wp_verify_nonce($_POST['sublanguage_extra_cpt_nonce'], 'sublanguage' )) { |
|---|
| 1777 | |
|---|
| 1778 | foreach ($_POST['sublanguage_extra_cpt'] as $key => $value) { |
|---|
| 1779 | |
|---|
| 1780 | if (in_array($key, $this->get_post_type_metakeys($post->post_type))) { |
|---|
| 1781 | |
|---|
| 1782 | update_post_meta($post->ID, $key, $value); |
|---|
| 1783 | |
|---|
| 1784 | } |
|---|
| 1785 | |
|---|
| 1786 | } |
|---|
| 1787 | |
|---|
| 1788 | if (isset($_POST['_menu_item_classes'])) { |
|---|
| 1789 | |
|---|
| 1790 | update_post_meta($post->ID, '_menu_item_classes', explode(' ', $_POST['_menu_item_classes'])); |
|---|
| 1791 | |
|---|
| 1792 | } |
|---|
| 1793 | |
|---|
| 1794 | } |
|---|
| 1795 | |
|---|
| 1796 | } |
|---|
| 1797 | |
|---|
| 1798 | } |
|---|
| 1799 | |
|---|
| 1800 | |
|---|
| 1801 | |
|---|
| 1802 | |
|---|
| 1803 | |
|---|
| 1804 | /* Option translation |
|---|
| 1805 | ----------------------------------------------- */ |
|---|
| 1806 | |
|---|
| 1807 | /* |
|---|
| 1808 | * Renders Translate Options page |
|---|
| 1809 | * |
|---|
| 1810 | * @from 1.5 |
|---|
| 1811 | * @removed from 2.10 |
|---|
| 1812 | */ |
|---|
| 1813 | public function print_page() { |
|---|
| 1814 | |
|---|
| 1815 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 1816 | |
|---|
| 1817 | } |
|---|
| 1818 | |
|---|
| 1819 | /* |
|---|
| 1820 | * List of options that should not be translated |
|---|
| 1821 | * |
|---|
| 1822 | * @from 1.5 |
|---|
| 1823 | * @removed from 2.10 |
|---|
| 1824 | */ |
|---|
| 1825 | private function get_options_blacklist() { |
|---|
| 1826 | |
|---|
| 1827 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 1828 | |
|---|
| 1829 | } |
|---|
| 1830 | |
|---|
| 1831 | /** |
|---|
| 1832 | * Enqueue javascript and styles on option translation page |
|---|
| 1833 | * |
|---|
| 1834 | * @from 1.5 |
|---|
| 1835 | * @removed from 2.10 |
|---|
| 1836 | */ |
|---|
| 1837 | public function admin_enqueue_scripts($hook) { |
|---|
| 1838 | |
|---|
| 1839 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 1840 | |
|---|
| 1841 | } |
|---|
| 1842 | |
|---|
| 1843 | |
|---|
| 1844 | |
|---|
| 1845 | |
|---|
| 1846 | |
|---|
| 1847 | /* Add Editor Button |
|---|
| 1848 | ----------------------------------------------- */ |
|---|
| 1849 | |
|---|
| 1850 | /** |
|---|
| 1851 | * Fire filters on post.php |
|---|
| 1852 | * |
|---|
| 1853 | * @hook 'load-post.php' |
|---|
| 1854 | * |
|---|
| 1855 | * @from 1.1 |
|---|
| 1856 | * @removed from 2.10 |
|---|
| 1857 | */ |
|---|
| 1858 | public function load_editor_page() { |
|---|
| 1859 | |
|---|
| 1860 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 1861 | |
|---|
| 1862 | } |
|---|
| 1863 | |
|---|
| 1864 | /** |
|---|
| 1865 | * @from 1.3 |
|---|
| 1866 | * @removed from 2.10 |
|---|
| 1867 | */ |
|---|
| 1868 | public function load_editor_button() { |
|---|
| 1869 | |
|---|
| 1870 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 1871 | |
|---|
| 1872 | } |
|---|
| 1873 | |
|---|
| 1874 | /** |
|---|
| 1875 | * @from 1.3 |
|---|
| 1876 | * @removed from 2.10 |
|---|
| 1877 | */ |
|---|
| 1878 | public function register_tinymce_button( $buttons ) { |
|---|
| 1879 | |
|---|
| 1880 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 1881 | |
|---|
| 1882 | } |
|---|
| 1883 | |
|---|
| 1884 | /** |
|---|
| 1885 | * @from 1.3 |
|---|
| 1886 | * @removed from 2.10 |
|---|
| 1887 | */ |
|---|
| 1888 | public function add_tinymce_button( $plugin_array ) { |
|---|
| 1889 | |
|---|
| 1890 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 1891 | |
|---|
| 1892 | } |
|---|
| 1893 | |
|---|
| 1894 | /** |
|---|
| 1895 | * Print post translations data for javascript |
|---|
| 1896 | * |
|---|
| 1897 | * @hook 'admin_footer-{...}' |
|---|
| 1898 | * |
|---|
| 1899 | * @from 1.3 |
|---|
| 1900 | * @removed from 2.10 |
|---|
| 1901 | */ |
|---|
| 1902 | public function print_javascript_post_translations() { |
|---|
| 1903 | |
|---|
| 1904 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 1905 | |
|---|
| 1906 | } |
|---|
| 1907 | |
|---|
| 1908 | /** |
|---|
| 1909 | * Use button with Tinymce Advanced plugin |
|---|
| 1910 | * |
|---|
| 1911 | * Hook for 'tadv_allowed_buttons' |
|---|
| 1912 | * |
|---|
| 1913 | * @from 1.3 |
|---|
| 1914 | * @removed from 2.10 |
|---|
| 1915 | */ |
|---|
| 1916 | public function tadv_register_button($buttons) { |
|---|
| 1917 | |
|---|
| 1918 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 1919 | |
|---|
| 1920 | } |
|---|
| 1921 | |
|---|
| 1922 | /** |
|---|
| 1923 | * Use button with Tinymce Advanced plugin |
|---|
| 1924 | * |
|---|
| 1925 | * Hook for 'admin_head' |
|---|
| 1926 | * |
|---|
| 1927 | * @from 1.3 |
|---|
| 1928 | * @removed from 2.10 |
|---|
| 1929 | */ |
|---|
| 1930 | public function tadv_set_icon() { |
|---|
| 1931 | |
|---|
| 1932 | trigger_error('feature is removed and function has been disabled, Sublanguage v2.10'); |
|---|
| 1933 | |
|---|
| 1934 | |
|---|
| 1935 | } |
|---|
| 1936 | |
|---|
| 1937 | |
|---|
| 1938 | |
|---|
| 1939 | |
|---|
| 1940 | /* gutenberg |
|---|
| 1941 | ------------------- */ |
|---|
| 1942 | |
|---|
| 1943 | /** |
|---|
| 1944 | * Init Gutenberg |
|---|
| 1945 | * |
|---|
| 1946 | * @from 2.5 |
|---|
| 1947 | */ |
|---|
| 1948 | public function init_gutenberg() { |
|---|
| 1949 | |
|---|
| 1950 | wp_register_script( |
|---|
| 1951 | 'sublanguage-gutenberg', |
|---|
| 1952 | plugins_url( 'js/gutenberg.js', __FILE__ ), |
|---|
| 1953 | array( 'wp-blocks', 'wp-element' ), |
|---|
| 1954 | $this->version |
|---|
| 1955 | ); |
|---|
| 1956 | |
|---|
| 1957 | wp_register_style( |
|---|
| 1958 | 'sublanguage-gutenberg-styles', |
|---|
| 1959 | plugins_url('css/gutenberg.css', __FILE__), |
|---|
| 1960 | array('wp-edit-blocks'), |
|---|
| 1961 | $this->version |
|---|
| 1962 | ); |
|---|
| 1963 | |
|---|
| 1964 | if (function_exists('register_block_type')) { |
|---|
| 1965 | |
|---|
| 1966 | register_block_type( 'sublanguage/language-manager', array( |
|---|
| 1967 | 'editor_script' => 'sublanguage-gutenberg', |
|---|
| 1968 | 'editor_style' => 'sublanguage-gutenberg-styles' |
|---|
| 1969 | )); |
|---|
| 1970 | |
|---|
| 1971 | } |
|---|
| 1972 | |
|---|
| 1973 | add_action('block_editor_meta_box_hidden_fields', array($this, 'gutenberg_hidden_fields')); |
|---|
| 1974 | |
|---|
| 1975 | } |
|---|
| 1976 | |
|---|
| 1977 | /** |
|---|
| 1978 | * Add language hidden fields on gutenberg hidden section |
|---|
| 1979 | * |
|---|
| 1980 | * @from 2.5 |
|---|
| 1981 | */ |
|---|
| 1982 | public function gutenberg_hidden_fields($post) { |
|---|
| 1983 | |
|---|
| 1984 | if ( $this->get_post_type_option($post->post_type, 'gutenberg_metabox_compat')) { |
|---|
| 1985 | |
|---|
| 1986 | include plugin_dir_path( __FILE__ ) . 'include/gutenberg-metabox.php'; |
|---|
| 1987 | |
|---|
| 1988 | } |
|---|
| 1989 | |
|---|
| 1990 | } |
|---|
| 1991 | |
|---|
| 1992 | |
|---|
| 1993 | |
|---|
| 1994 | } |
|---|