Advertisement
mbis

Language field in Bulk Tools

Jun 20th, 2020
1,595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1. /**
  2.  * Filter languages in bulk tools
  3.  */
  4. function pm_ignore_lang_posts($default_uri, $native_slug, $object, $slug, $native_uri) {
  5.     global $permalink_manager_uris;
  6.  
  7.     // 1A. Do not use for native permalinks
  8.     if(!empty($native_uri)) { return $default_uri; }
  9.  
  10.     // 1B. Use only for bulk tools!
  11.     if(empty($_REQUEST['mode']) || $_REQUEST['mode'] !== 'custom_uris') { return $default_uri; }
  12.  
  13.     // 1C. Regenerate all items if no language is selected
  14.     if(!empty($_REQUEST['languages']) && is_array($_REQUEST['languages'])) {
  15.         $selected_languages = $_REQUEST['languages'];
  16.     } else {
  17.         return $default_uri;
  18.     }
  19.  
  20.     // 2. Get language of post/term
  21.     $language_code = Permalink_Manager_Language_Plugins::get_language_code($object);
  22.  
  23.     // 3. Get object ID
  24.     if(!empty($object->ID)) {
  25.         $object_id = $object->ID;
  26.     } else if(!empty($object->term_taxonomy_id)) {
  27.         $object_id = "tax-{$object->term_taxonomy_id}";
  28.     } else {
  29.         return $default_uri;
  30.     }
  31.  
  32.     // 4. Restore old custom permalink if post/term has no language set or language is different than selected
  33.     if((empty($language_code) || !in_array($language_code, $selected_languages))) {
  34.         $default_uri = (!empty($permalink_manager_uris[$object_id])) ? $permalink_manager_uris[$object_id] : $default_uri;
  35.     }
  36.  
  37.     return $default_uri;
  38. }
  39. add_filter('permalink_manager_filter_default_post_uri', 'pm_ignore_lang_posts', 99, 5);
  40. add_filter('permalink_manager_filter_default_term_uri', 'pm_ignore_lang_posts', 99, 5);
  41.  
  42. /**
  43.  * Add "Languages field"
  44.  */
  45. function pm_tools_language_field($fields) {
  46.     $wpml_languages = apply_filters('wpml_active_languages', NULL, 'orderby=id&order=desc');
  47.  
  48.     if(!empty($wpml_languages)) {
  49.         $all_languages = array();
  50.  
  51.         foreach($wpml_languages as $language) {
  52.             $all_languages[$language['language_code']] = $language['translated_name'];
  53.         }
  54.  
  55.         if(!empty($all_languages)) {
  56.             $fields['languages'] = array(
  57.                 'label' => __( 'Select languages', 'permalink-manager' ),
  58.                 'type' => 'checkbox',
  59.                 'container' => 'row',
  60.                 'choices' => $all_languages,
  61.                 'select_all' => '',
  62.                 'unselect_all' => '',
  63.             );
  64.         }
  65.     }
  66.  
  67.     return $fields;
  68. }
  69. add_filter('permalink_manager_tools_fields', 'pm_tools_language_field', 99);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement