+/**
+ * Sends back current comment total and new page links if they need to be updated.
+ *
+ * Contrary to normal success AJAX response ("1"), die with time() on success.
+ *
+ * @since 2.7
+ *
+ * @param int $comment_id
+ * @return die
+ */
+function _wp_ajax_delete_comment_response( $comment_id ) {
+ $total = (int) @$_POST['_total'];
+ $per_page = (int) @$_POST['_per_page'];
+ $page = (int) @$_POST['_page'];
+ $url = esc_url_raw( @$_POST['_url'] );
+ // JS didn't send us everything we need to know. Just die with success message
+ if ( !$total || !$per_page || !$page || !$url )
+ die( (string) time() );
+
+ if ( --$total < 0 ) // Take the total from POST and decrement it (since we just deleted one)
+ $total = 0;
+
+ if ( 0 != $total % $per_page && 1 != mt_rand( 1, $per_page ) ) // Only do the expensive stuff on a page-break, and about 1 other time per page
+ die( (string) time() );
+
+ $post_id = 0;
+ $status = 'total_comments'; // What type of comment count are we looking for?
+ $parsed = parse_url( $url );
+ if ( isset( $parsed['query'] ) ) {
+ parse_str( $parsed['query'], $query_vars );
+ if ( !empty( $query_vars['comment_status'] ) )
+ $status = $query_vars['comment_status'];
+ if ( !empty( $query_vars['p'] ) )
+ $post_id = (int) $query_vars['p'];
+ }
+
+ $comment_count = wp_count_comments($post_id);
+ $time = time(); // The time since the last comment count
+
+ if ( isset( $comment_count->$status ) ) // We're looking for a known type of comment count
+ $total = $comment_count->$status;
+ // else use the decremented value from above
+
+ $page_links = paginate_links( array(
+ 'base' => add_query_arg( 'apage', '%#%', $url ),
+ 'format' => '',
+ 'prev_text' => __('«'),
+ 'next_text' => __('»'),
+ 'total' => ceil($total / $per_page),
+ 'current' => $page
+ ) );
+ $x = new WP_Ajax_Response( array(
+ 'what' => 'comment',
+ 'id' => $comment_id, // here for completeness - not used
+ 'supplemental' => array(
+ 'pageLinks' => $page_links,
+ 'total' => $total,
+ 'time' => $time
+ )
+ ) );
+ $x->send();
+}
+
+function _wp_ajax_add_hierarchical_term() {
+ $action = $_POST['action'];
+ $taxonomy = get_taxonomy(substr($action, 4));
+ check_ajax_referer( $action, '_ajax_nonce-add-' . $taxonomy->name );
+ if ( !current_user_can( $taxonomy->cap->edit_terms ) )
+ die('-1');
+ $names = explode(',', $_POST['new'.$taxonomy->name]);
+ $parent = isset($_POST['new'.$taxonomy->name.'_parent']) ? (int) $_POST['new'.$taxonomy->name.'_parent'] : 0;
+ if ( 0 > $parent )
+ $parent = 0;
+ if ( $taxonomy->name == 'category' )
+ $post_category = isset($_POST['post_category']) ? (array) $_POST['post_category'] : array();
+ else
+ $post_category = ( isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name]) ) ? (array) $_POST['tax_input'][$taxonomy->name] : array();
+ $checked_categories = array_map( 'absint', (array) $post_category );
+ $popular_ids = wp_popular_terms_checklist($taxonomy->name, 0, 10, false);
+
+ foreach ( $names as $cat_name ) {
+ $cat_name = trim($cat_name);
+ $category_nicename = sanitize_title($cat_name);
+ if ( '' === $category_nicename )
+ continue;
+ if ( !($cat_id = term_exists($cat_name, $taxonomy->name, $parent)) ) {
+ $new_term = wp_insert_term($cat_name, $taxonomy->name, array('parent' => $parent));
+ $cat_id = $new_term['term_id'];
+ }
+ $checked_categories[] = $cat_id;
+ if ( $parent ) // Do these all at once in a second
+ continue;
+ $category = get_term( $cat_id, $taxonomy->name );
+ ob_start();
+ wp_terms_checklist( 0, array( 'taxonomy' => $taxonomy->name, 'descendants_and_self' => $cat_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids ));
+ $data = ob_get_contents();
+ ob_end_clean();
+ $add = array(
+ 'what' => $taxonomy->name,
+ 'id' => $cat_id,
+ 'data' => str_replace( array("\n", "\t"), '', $data),
+ 'position' => -1
+ );
+ }
+
+ if ( $parent ) { // Foncy - replace the parent and all its children
+ $parent = get_term( $parent, $taxonomy->name );
+ $term_id = $parent->term_id;
+
+ while ( $parent->parent ) { // get the top parent
+ $parent = &get_term( $parent->parent, $taxonomy->name );
+ if ( is_wp_error( $parent ) )
+ break;
+ $term_id = $parent->term_id;
+ }
+
+ ob_start();
+ wp_terms_checklist( 0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $term_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids));
+ $data = ob_get_contents();
+ ob_end_clean();
+ $add = array(
+ 'what' => $taxonomy->name,
+ 'id' => $term_id,
+ 'data' => str_replace( array("\n", "\t"), '', $data),
+ 'position' => -1
+ );
+ }
+
+ ob_start();
+ wp_dropdown_categories( array(
+ 'taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new'.$taxonomy->name.'_parent', 'orderby' => 'name',
+ 'hierarchical' => 1, 'show_option_none' => '— '.$taxonomy->labels->parent_item.' —'
+ ) );
+ $sup = ob_get_contents();
+ ob_end_clean();
+ $add['supplemental'] = array( 'newcat_parent' => $sup );
+
+ $x = new WP_Ajax_Response( $add );
+ $x->send();
+}
+