WordPress.org

Make WordPress Core

Ticket #9674: inline-edit-custom-taxonomies.diff

File inline-edit-custom-taxonomies.diff, 9.6 KB (added by prettyboymp, 3 years ago)

adds support for custom taxonomies to inline-edit

  • wp-admin/includes/post.php

     
    254254 
    255255        $post_IDs = array_map( 'intval', (array) $post_data['post'] ); 
    256256 
    257         $reset = array( 'post_author', 'post_status', 'post_password', 'post_parent', 'page_template', 'comment_status', 'ping_status', 'keep_private', 'tags_input', 'post_category', 'sticky' ); 
     257        $reset = array( 'post_author', 'post_status', 'post_password', 'post_parent', 'page_template', 'comment_status', 'ping_status', 'keep_private', 'tax_input', 'post_category', 'sticky' ); 
    258258        foreach ( $reset as $field ) { 
    259259                if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) ) 
    260260                        unset($post_data[$field]); 
     
    266266                else 
    267267                        unset($post_data['post_category']); 
    268268        } 
    269  
    270         if ( isset($post_data['tags_input']) ) { 
    271                 $new_tags = preg_replace( '/\s*,\s*/', ',', rtrim( trim($post_data['tags_input']), ' ,' ) ); 
    272                 $new_tags = explode(',', $new_tags); 
     269         
     270        $tax_input = array(); 
     271        if ( isset($post_data['tax_input'])) { 
     272                foreach ( $post_data['tax_input'] as $tax_name => $terms ) { 
     273                        if ( empty($terms) ) 
     274                                continue; 
     275                        $taxonomy = get_taxonomy( $tax_name ); 
     276                        if ( $taxonomy->hierarchical ) 
     277                                $tax_input[$tax_name] = array_map( 'absint', $terms );                   
     278                        else { 
     279                                $tax_input[$tax_name] = preg_replace( '/\s*,\s*/', ',', rtrim( trim($terms), ' ,' ) ); 
     280                                $tax_input[$tax_name] = explode(',', $tax_input[$tax_name]); 
     281                        } 
     282                } 
    273283        } 
    274284 
    275285        if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) { 
     
    300310                        $locked[] = $post_ID; 
    301311                        continue; 
    302312                } 
    303  
    304                 if ( isset($new_cats) ) { 
     313                 
     314                $tax_names = get_object_taxonomies( get_post($post_ID) ); 
     315                 
     316                if ( isset($new_cats) && in_array( 'category', $tax_names ) ) { 
    305317                        $cats = (array) wp_get_post_categories($post_ID); 
    306318                        $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) ); 
    307319                } 
    308  
    309                 if ( isset($new_tags) ) { 
    310                         $tags = wp_get_post_tags($post_ID, array('fields' => 'names')); 
    311                         $post_data['tags_input'] = array_unique( array_merge($tags, $new_tags) ); 
     320                 
     321                foreach ( $tax_names as $tax_name ) { 
     322                        if( isset( $tax_input[$tax_name])  ) { 
     323                                $taxonomy = get_taxonomy( $tax_name ); 
     324                                if( $taxonomy->hierarchical ) 
     325                                        $terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') ); 
     326                                else 
     327                                        $terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') ); 
     328                                $post_data['tax_input'][$tax_name] = array_merge( $terms, $tax_input[$tax_name] ); 
     329                        } 
    312330                } 
    313331 
    314332                $post_data['ID'] = $post_ID; 
  • wp-admin/includes/template.php

     
    907907        $post = get_default_post_to_edit( $screen->post_type ); 
    908908        $post_type_object = get_post_type_object( $screen->post_type ); 
    909909 
     910        $taxonomy_names = get_object_taxonomies( $screen->post_type ); 
     911        $hierarchical_taxonomies = array(); 
     912        $flat_taxonomies = array(); 
     913        foreach ( $taxonomy_names as $taxonomy_name ) { 
     914                $taxonomy = get_taxonomy( $taxonomy_name); 
     915                 
     916                if( !$taxonomy->show_ui ) continue; 
     917                 
     918                if( $taxonomy->hierarchical ) 
     919                        $hierarchical_taxonomies[] = $taxonomy; 
     920                else  
     921                        $flat_taxonomies[] = $taxonomy; 
     922        } 
     923         
    910924        $columns = wp_manage_posts_columns($screen); 
    911925        $hidden = array_intersect( array_keys( $columns ), array_filter( get_hidden_columns($screen) ) ); 
    912926        $col_count = count($columns) - count($hidden); 
     
    9981012 
    9991013        </div></fieldset> 
    10001014 
    1001 <?php if ( is_object_in_taxonomy($screen->post_type, 'category') && !$bulk ) : ?> 
     1015<?php if ( count($hierarchical_taxonomies) && !$bulk ) : ?> 
    10021016 
    10031017        <fieldset class="inline-edit-col-center inline-edit-categories"><div class="inline-edit-col"> 
    1004                 <span class="title inline-edit-categories-label"><?php _e( 'Categories' ); ?> 
     1018         
     1019<?php foreach ( $hierarchical_taxonomies as $taxonomy ) : ?> 
     1020 
     1021                <span class="title inline-edit-categories-label"><?php echo esc_html($taxonomy->label) ?> 
    10051022                        <span class="catshow"><?php _e('[more]'); ?></span> 
    10061023                        <span class="cathide" style="display:none;"><?php _e('[less]'); ?></span> 
    10071024                </span> 
    1008                 <ul class="cat-checklist"> 
    1009                         <?php wp_category_checklist(); ?> 
     1025                <ul class="cat-checklist <?php echo esc_attr($taxonomy->name)?>-checklist"> 
     1026                        <?php wp_terms_checklist(null, array('taxonomy' => $taxonomy->name)) ?> 
    10101027                </ul> 
     1028 
     1029<?php endforeach; //$hierarchical_taxonomies as $taxonomy ?> 
     1030 
    10111031        </div></fieldset> 
    10121032 
    1013 <?php endif; // is_object_in_taxonomy($screen->post_type, 'category') && !$bulk ?> 
     1033<?php endif; // count($hierarchical_taxonomies) && !$bulk ?> 
    10141034 
    10151035        <fieldset class="inline-edit-col-right"><div class="inline-edit-col"> 
    10161036 
     
    10541074 
    10551075<?php endif; // $post_type_object->hierarchical ?> 
    10561076 
    1057 <?php if ( is_object_in_taxonomy($screen->post_type, 'post_tag') && !$bulk ) : ?> 
     1077<?php if ( count($flat_taxonomies) && !$bulk ) : ?> 
    10581078 
     1079<?php foreach ( $flat_taxonomies as $taxonomy ) : ?> 
     1080 
    10591081                <label class="inline-edit-tags"> 
    1060                         <span class="title"><?php _e( 'Tags' ); ?></span> 
    1061                         <textarea cols="22" rows="1" name="tags_input" class="tags_input"></textarea> 
     1082                        <span class="title"><?php echo esc_html($taxonomy->label) ?></span> 
     1083                        <textarea cols="22" rows="1" name="tax_input[<?php echo esc_attr($taxonomy->name)?>]" class="tax_input_<?php echo esc_attr($taxonomy->name)?>"></textarea> 
    10621084                </label> 
    10631085 
    1064 <?php endif; // is_object_in_taxonomy($screen->post_type, 'post_tag') && !$bulk ?> 
     1086<?php endforeach; //$flat_taxonomies as $taxonomy ?> 
    10651087 
     1088<?php endif; // count($flat_taxonomies) && !$bulk  ?> 
     1089 
    10661090<?php if ( $bulk ) : ?> 
    10671091 
    10681092                <div class="inline-edit-group"> 
     
    12211245        if ( $post_type_object->hierarchical ) 
    12221246                echo '<div class="menu_order">' . $post->menu_order . '</div>'; 
    12231247 
    1224         if ( is_object_in_taxonomy($post->post_type, 'post_tag') ) 
    1225                 echo '<div class="tags_input">' . esc_html( str_replace( ',', ', ', get_tags_to_edit($post->ID) ) ) . '</div>'; 
     1248        $taxonomy_names = get_object_taxonomies( $post->post_type ); 
     1249        foreach ( $taxonomy_names as $taxonomy_name) { 
     1250                $taxonomy = get_taxonomy( $taxonomy_name ); 
    12261251 
    1227         if ( is_object_in_taxonomy($post->post_type, 'post_tag') ) 
    1228                 echo '<div class="post_category">' . implode( ',', wp_get_post_categories( $post->ID ) ) . '</div>'; 
     1252                if ( $taxonomy->hierarchical && $taxonomy->show_ui ) 
     1253                                echo '<div class="post_category" id="'.$taxonomy_name.'_'.$post->ID.'">' . implode( ',', wp_get_object_terms( $post->ID, $taxonomy_name, array('fields'=>'ids')) ) . '</div>'; 
     1254                elseif ( $taxonomy->show_ui ) 
     1255                        echo '<div class="tags_input" id="'.$taxonomy_name.'_'.$post->ID.'">' . esc_html( str_replace( ',', ', ', get_terms_to_edit($post->ID, $taxonomy_name) ) ) . '</div>'; 
     1256        } 
    12291257 
    1230         if ( $post->post_type == 'post' ) 
     1258        if ( !$post_type_object->hierarchical ) 
    12311259                echo '<div class="sticky">' . (is_sticky($post->ID) ? 'sticky' : '') . '</div>'; 
    12321260 
    12331261        echo '</div>'; 
  • wp-admin/js/inline-edit-post.dev.js

     
    3636                        $('#inline-edit label.inline-edit-tags').clone() 
    3737                ); 
    3838 
    39                 // categories expandable? 
     39                // hiearchical taxonomies expandable? 
    4040                $('span.catshow').click(function() { 
    41                         $('.inline-editor ul.cat-checklist').addClass("cat-hover"); 
    42                         $('.inline-editor span.cathide').show(); 
    43                         $(this).hide(); 
     41                        $(this).hide().next().show().parent().next().addClass("cat-hover"); 
    4442                }); 
    4543 
    4644                $('span.cathide').click(function() { 
    47                         $('.inline-editor ul.cat-checklist').removeClass("cat-hover"); 
    48                         $('.inline-editor span.catshow').show(); 
    49                         $(this).hide(); 
     45                        $(this).hide().prev().show().parent().next().removeClass("cat-hover"); 
    5046                }); 
    5147 
    5248                $('select[name="_status"] option[value="future"]', bulkRow).remove(); 
     
    118114 
    119115                fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password']; 
    120116                if ( t.type == 'page' ) fields.push('post_parent', 'menu_order', 'page_template'); 
    121                 if ( t.type == 'post' ) fields.push('tags_input'); 
    122117 
    123118                // add the new blank row 
    124119                editRow = $('#inline-edit').clone(true); 
     
    146141                if ( $('.sticky', rowData).text() == 'sticky' ) 
    147142                        $('input[name="sticky"]', editRow).attr("checked", "checked"); 
    148143 
    149                 // categories 
    150                 if ( cats = $('.post_category', rowData).text() ) 
    151                         $('ul.cat-checklist :checkbox', editRow).val(cats.split(',')); 
     144                // hierarchical taxonomies 
     145                $('.post_category', rowData).each(function(){ 
     146                        if( term_ids = $(this).text() ) 
     147                        { 
     148                                taxname = $(this).attr('id').replace('_'+id, ''); 
     149                                $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(',')); 
     150                        } 
     151                }); 
     152                //flat taxonomies 
     153                $('.tags_input', rowData).each(function(){ 
     154                        if( terms = $(this).text() ) 
     155                        { 
     156                                taxname = $(this).attr('id').replace('_'+id, ''); 
     157                                $('textarea.tax_input_'+taxname, editRow).val(terms); 
     158                                $('textarea.tax_input_'+taxname, editRow).suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+taxname, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } ); 
     159                        } 
     160                }); 
     161                 
    152162 
    153163                // handle the post status 
    154164                status = $('._status', rowData).text(); 
     
    180190                $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show(); 
    181191                $('.ptitle', editRow).focus(); 
    182192 
    183                 // enable autocomplete for tags 
    184                 if ( t.type == 'post' ) { 
    185                         tax = 'post_tag'; 
    186                         $('tr.inline-editor textarea[name="tags_input"]').suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+tax, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } ); 
    187                 } 
    188  
    189193                return false; 
    190194        }, 
    191195