diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index 5e27b81488..1fe24f7695 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -150,6 +150,9 @@ case 'add-category' : // On the Fly $parent = 0; $post_category = isset($_POST['post_category'])? (array) $_POST['post_category'] : array(); $checked_categories = array_map( 'absint', (array) $post_category ); + $popular_ids = isset( $_POST['popular_ids'] ) ? + array_map( 'absint', explode( ',', $_POST['popular_ids'] ) ) : + false; $x = new WP_Ajax_Response(); foreach ( $names as $cat_name ) { @@ -163,7 +166,7 @@ case 'add-category' : // On the Fly continue; $category = get_category( $cat_id ); ob_start(); - wp_category_checklist( 0, $cat_id, $checked_categories ); + wp_category_checklist( 0, $cat_id, $checked_categories, $popular_ids ); $data = ob_get_contents(); ob_end_clean(); $x->add( array( diff --git a/wp-admin/edit-form-advanced.php b/wp-admin/edit-form-advanced.php index ca48323d4d..b44e365fda 100644 --- a/wp-admin/edit-form-advanced.php +++ b/wp-admin/edit-form-advanced.php @@ -260,7 +260,7 @@ function post_categories_meta_box($post) {
'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) ); + + if ( is_array( $popular_cats ) ) + $args['popular_cats'] = $popular_cats; + else + $args['popular_cats'] = get_terms( 'category', array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) ); + if ( $descendants_and_self ) { $categories = get_categories( "child_of=$descendants_and_self&hierarchical=0&hide_empty=0" ); $self = get_category( $descendants_and_self ); @@ -169,13 +174,22 @@ function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $select $categories = get_categories('get=all'); } - $args = array($categories, 0, $args); - $output = call_user_func_array(array(&$walker, 'walk'), $args); + // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache) + $checked_categories = array(); + for ( $i = 0; isset($categories[$i]); $i++ ) { + if ( in_array($categories[$i]->term_id, $args['selected_cats']) ) { + $checked_categories[] = $categories[$i]; + unset($categories[$i]); + } + } - echo $output; + // Put checked cats on top + echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args)); + // Then the rest of them + echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args)); } -function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10 ) { +function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) { global $post_ID; if ( $post_ID ) $checked_categories = wp_get_post_categories($post_ID); @@ -186,6 +200,8 @@ function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10 ) { $popular_ids = array(); foreach ( (array) $categories as $category ) { $popular_ids[] = $category->term_id; + if ( !$echo ) // hack for AJAX use + continue; $id = "popular-category-$category->term_id"; ?> diff --git a/wp-admin/js/post.js b/wp-admin/js/post.js index 99cb5c88c3..a81fc81386 100644 --- a/wp-admin/js/post.js +++ b/wp-admin/js/post.js @@ -120,8 +120,9 @@ jQuery(document).ready( function() { jQuery('#in-category-' + id + ', #in-popular-category-' + id).attr( 'checked', c ); noSyncChecks = false; }; + var popularCats = jQuery('#categorychecklist-pop :checkbox').map( function() { return parseInt(jQuery(this).val(), 10); } ).get().join(','); var catAddBefore = function( s ) { - s.data += '&' + jQuery( '#categorychecklist :checked' ).serialize(); + s.data += '&popular_ids=' + popularCats + '&' + jQuery( '#categorychecklist :checked' ).serialize(); return s; }; var catAddAfter = function( r, s ) { @@ -151,7 +152,8 @@ jQuery(document).ready( function() { } ); jQuery('#category-add-toggle').click( function() { jQuery(this).parents('div:first').toggleClass( 'wp-hidden-children' ); - categoryTabs.tabsClick( 1 ); + // categoryTabs.tabs( 'select', '#categories-all' ); // this is broken (in the UI beta?) + categoryTabs.find( 'a[href="#categories-all"]' ).click(); jQuery('#newcat').focus(); return false; } ); diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 5317c5478a..5ade668bbf 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -145,7 +145,7 @@ class WP_Scripts { 'save' => __('Save'), 'cancel' => __('Cancel'), ) ); - $this->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists', 'postbox', 'slug'), '20080422' ); + $this->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists', 'postbox', 'slug'), '20080519' ); $this->localize( 'post', 'postL10n', array( 'tagsUsed' => __('Tags used on this post:'), 'add' => attribute_escape(__('Add')),