WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/22/2010 07:30:05 PM (12 years ago)
Author:
ryan
Message:

Create metaboxes for hierarchical taxonomies. Props prettyboymp. fixes #10122

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-ajax.php

    r12797 r12798  
    201201}
    202202
     203function _wp_ajax_add_hierarchical_term() {
     204    $action = $_POST['action'];
     205    $taxonomy = get_taxonomy(substr($action, 4));
     206    check_ajax_referer( $action );
     207    if ( !current_user_can( 'manage_categories' ) )
     208        die('-1');
     209    $names = explode(',', $_POST['new'.$taxonomy->name]);
     210    $parent = isset($_POST['new'.$taxonomy->name.'_parent']) ? (int) $_POST['new'.$taxonomy->name.'_parent'] : 0;
     211    if ( 0 > $parent )
     212        $parent = 0;
     213    if ( $taxonomy->name == 'category' )
     214        $post_category = isset($_POST['post_category']) ? (array) $_POST['post_category'] : array();
     215    else
     216        $post_category = ( isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name]) ) ? (array) $_POST['tax_input'][$taxonomy->name] : array();
     217    $checked_categories = array_map( 'absint', (array) $post_category );
     218    $popular_ids = wp_popular_terms_checklist($taxonomy->name, 0, 10, false);
     219
     220    foreach ( $names as $cat_name ) {
     221        $cat_name = trim($cat_name);
     222        $category_nicename = sanitize_title($cat_name);
     223        if ( '' === $category_nicename )
     224            continue;
     225        if ( !($cat_id = is_term($cat_name, $taxonomy->name, $parent)) ) {
     226            $new_term = wp_insert_term($cat_name, $taxonomy->name, array('parent' => $parent));
     227            $cat_id = $new_term['term_id'];
     228        }
     229        $checked_categories[] = $cat_id;
     230        if ( $parent ) // Do these all at once in a second
     231            continue;
     232        $category = get_term( $cat_id, $taxonomy->name );
     233        ob_start();
     234            wp_terms_checklist( 0, array( 'taxonomy' => $taxonomy->name, 'descendants_and_self' => $cat_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids ));
     235        $data = ob_get_contents();
     236        ob_end_clean();
     237        $add = array(
     238            'what' => $taxonomy->name,
     239            'id' => $cat_id,
     240            'data' => str_replace( array("\n", "\t"), '', $data),
     241            'position' => -1
     242        );
     243    }
     244
     245    if ( $parent ) { // Foncy - replace the parent and all its children
     246        $parent = get_term( $parent, $taxonomy->name );
     247        $term_id = $parent->term_id;
     248
     249        while ( $parent->parent ) { // get the top parent
     250            $parent = &get_term( $parent->parent, $taxonomy->name );
     251            if ( is_wp_error( $parent ) )
     252                break;
     253            $term_id = $parent->term_id;
     254        }
     255
     256        ob_start();
     257            wp_terms_checklist( 0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $term_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids));
     258        $data = ob_get_contents();
     259        ob_end_clean();
     260        $add = array(
     261            'what' => $taxonomy->name,
     262            'id' => $term_id,
     263            'data' => str_replace( array("\n", "\t"), '', $data),
     264            'position' => -1
     265        );
     266    }
     267
     268    ob_start();
     269        wp_dropdown_categories( array( 'taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new'.$taxonomy->name.'_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __('Parent category') ) );
     270    $sup = ob_get_contents();
     271    ob_end_clean();
     272    $add['supplemental'] = array( 'newcat_parent' => $sup );
     273
     274    $x = new WP_Ajax_Response( $add );
     275    $x->send();
     276}
     277
    203278$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
    204279switch ( $action = $_POST['action'] ) :
     
    410485    die( '0' );
    411486    break;
    412 case 'add-category' : // On the Fly
    413     check_ajax_referer( $action );
    414     if ( !current_user_can( 'manage_categories' ) )
    415         die('-1');
    416     $names = explode(',', $_POST['newcat']);
    417     if ( 0 > $parent = (int) $_POST['newcat_parent'] )
    418         $parent = 0;
    419     $post_category = isset($_POST['post_category'])? (array) $_POST['post_category'] : array();
    420     $checked_categories = array_map( 'absint', (array) $post_category );
    421     $popular_ids = wp_popular_terms_checklist('category', 0, 10, false);
    422 
    423     foreach ( $names as $cat_name ) {
    424         $cat_name = trim($cat_name);
    425         $category_nicename = sanitize_title($cat_name);
    426         if ( '' === $category_nicename )
    427             continue;
    428         $cat_id = wp_create_category( $cat_name, $parent );
    429         $checked_categories[] = $cat_id;
    430         if ( $parent ) // Do these all at once in a second
    431             continue;
    432         $category = get_category( $cat_id );
    433         ob_start();
    434             wp_category_checklist( 0, $cat_id, $checked_categories, $popular_ids );
    435         $data = ob_get_contents();
    436         ob_end_clean();
    437         $add = array(
    438             'what' => 'category',
    439             'id' => $cat_id,
    440             'data' => str_replace( array("\n", "\t"), '', $data),
    441             'position' => -1
    442         );
    443     }
    444     if ( $parent ) { // Foncy - replace the parent and all its children
    445         $parent = get_category( $parent );
    446         $term_id = $parent->term_id;
    447 
    448         while ( $parent->parent ) { // get the top parent
    449             $parent = &get_category( $parent->parent );
    450             if ( is_wp_error( $parent ) )
    451                 break;
    452             $term_id = $parent->term_id;
    453         }
    454 
    455         ob_start();
    456             wp_category_checklist( 0, $term_id, $checked_categories, $popular_ids, null, false );
    457         $data = ob_get_contents();
    458         ob_end_clean();
    459         $add = array(
    460             'what' => 'category',
    461             'id' => $term_id,
    462             'data' => str_replace( array("\n", "\t"), '', $data),
    463             'position' => -1
    464         );
    465     }
    466 
    467     ob_start();
    468         wp_dropdown_categories( array( 'hide_empty' => 0, 'name' => 'newcat_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __('Parent category') ) );
    469     $sup = ob_get_contents();
    470     ob_end_clean();
    471     $add['supplemental'] = array( 'newcat_parent' => $sup );
    472 
    473     $x = new WP_Ajax_Response( $add );
    474     $x->send();
    475     break;
    476487case 'add-link-category' : // On the Fly
    477488    check_ajax_referer( $action );
     
    512523    }
    513524
    514     if ( category_exists( trim( $_POST['cat_name'] ), $_POST['category_parent'] ) ) {
     525    if ( is_term( trim( $_POST['cat_name'] ), $_POST['taxonomy'], $_POST['category_parent'] ) ) {
    515526        $x = new WP_Ajax_Response( array(
    516527            'what' => 'cat',
     
    530541    }
    531542
    532     if ( !$cat || (!$cat = get_category( $cat )) )
     543    if ( !$cat || (!$cat = get_term( $cat, $_POST['taxonomy'] ) ) )
    533544        die('0');
    534545
Note: See TracChangeset for help on using the changeset viewer.