'; } } add_action('admin_head', 'bc_admin_head'); function bc_admin_screen() { global $wpdb, $bc_urls; // Maybe make this an option some time $per_page = 15; $paged = empty($_GET['paged']) ? 1 : intval($_GET['paged']); $orderbys = array( 'Author' => 'post_author', 'Date Posted' => 'post_date', 'Date Modified' => 'post_modified', 'Title' => 'post_title', 'Status' => 'post_status' ); $orderbys_flip = array_flip($orderbys); // Sorting $orderby = ( in_array($_GET['orderby'], $orderbys) ) ? $_GET['orderby'] : 'post_date'; $order = ( strtolower($_GET['order']) == 'asc' ) ? 'asc' : 'desc'; $_POST = stripslashes_deep($_POST); $_GET = stripslashes_deep($_GET); $posts = array(); $num = intval($_GET['num']); $what = htmlentities($_GET['what']); $message = ''; // Deal with any update messages we might have: switch ( $_GET['done'] ) { case 'add': $message = "Added $num posts to the category “$what”."; break; case 'remove': $message = "Removed $num posts from the category “$what”."; break; case 'tag': $message = "Tagged $num posts with “$what”."; break; case 'untag': $message = "Untagged $num posts with “$what”."; break; } if ( !empty($message) ) { echo "

$message

"; } // Create the hidden input which passes our current filter to the action script. if ( !empty($_GET['cat']) ) $filter = ''; if ( !empty($_GET['s']) ) $filter = ''; if ( !empty($_GET['t']) ) $filter = ''; echo '

Batch Categories

'; // Filtering echo '
'; // Sorting echo '
'; // Category drop-down echo '
' . wp_dropdown_categories('hide_empty=0&hierarchical=1&echo=0&selected=' . ( isset($_GET['cat']) ? intval($_GET['cat']) : -1 )) . '
'; // ...then the keyword search. echo '
'; // ...then the tag filter. echo '
'; echo '
'; // Fetch our posts. if ( !empty($_GET['cat']) || !empty($_GET['s']) || !empty($_GET['t']) ) { // A cat has been given; fetch posts that are in that category. $q = "paged=$paged&posts_per_page=$per_page&orderby=$orderby&order=$order&post_type=post"; if ( !empty($_GET['cat']) ) { $cat = get_cat_name(intval($_GET['cat'])); $q .= "&category_name=$cat"; } // A keyword has been given; get posts whose content contains that keyword. if ( !empty($_GET['s']) ) { $q .= "&s=" . urlencode($_GET['s']); } // A tag has been given; get posts tagged with that tag. if ( !empty($_GET['t']) ) { $t = preg_replace('#[^a-z0-9\-\,\+]*#i', '', $_GET['t']); $q .= "&tag=$t"; } $query = new WP_Query; $posts = $query->query($q); // Pagination $pagination = ''; if ( $query->max_num_pages > 1 ) { $current = preg_replace('/&?paged=[0-9]+/i', '', strip_tags($_SERVER['REQUEST_URI'])); // I'll happily take suggestions on a better way to do this, but it's 3am so $pagination .= "
"; if ( $paged > 1 ) { $prev = $paged - 1; $pagination .= ""; } for ( $i = 1; $i <= $query->max_num_pages; $i++ ) { if ( $i == $paged ) { $pagination .= "$i"; } else { $pagination .= "$i"; } } if ( $paged < $query->max_num_pages ) { $next = $paged + 1; $pagination .= ""; } $pagination .= "
"; } echo $pagination; } echo "
"; // tablenav // No posts have been fetched, let's tell the user: if ( empty($_GET['cat']) && empty($_GET['s']) && empty($_GET['t']) ) { echo '

To get started, select some criteria above.

'; } else { // Criteria were given, but no posts were matched. if ( empty($posts) ) { echo '

No posts matched that criteria, sorry! Try again with something different.

'; } // Criteria were given, posts were matched... let's go! else { echo '
'; foreach ( (array) $posts as $post ) { $categories = wp_get_post_categories($post->ID); $cats = ''; foreach ( (array) $categories as $cat ) { $cats .= '' . get_cat_name($cat). ' '; } $_tags = wp_get_post_tags($post->ID); $tags = ''; foreach ( $_tags as $tag ) { $tags .= "$tag->name, "; } $tags = substr($tags, 0, strlen($tags) - 2); if ( empty ($tags) ) { $tags = 'No Tags'; } echo ' '; } echo '
' . sprintf( 'Looked for posts in %s, tagged with %s, %s ordered by %s %s. Found %s posts; displaying %s per page.', !empty($_GET['cat']) ? get_cat_name($cat) : 'any category', !empty($_GET['t']) ? htmlentities($_GET['t']) : 'any tag', !empty($_GET['s']) ? 'containing the string ' . htmlentities($_GET['s']) . ', ' : '', strtolower($orderbys_flip[$orderby]), $order == 'asc' ? 'ascending' : 'descending', $query->found_posts, $per_page ) . '
ID Date Title Current Categories Current Tags
' . $post->ID . ' '; if ( '0000-00-00 00:00:00' == $post->post_date ) { _e('Unpublished'); } else { if ( ( time() - get_post_time() ) < 86400 ) { echo sprintf( __('%s ago'), human_time_diff( get_post_time() ) ); } else { echo date(__('Y/m/d'), strtotime($post->post_date)); } } echo ' ' . $post->post_title . ' ' . $cats . ' ' . $tags . ' View Edit
'; // Now, our actions. echo '
' . $filter . '
' . wp_dropdown_categories('hide_empty=0&hierarchical=1&echo=0') . '
' . $pagination . '
'; wp_nonce_field('batch_categories-edit'); echo '
'; } } } ?>