mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-23 01:27:36 +01:00
Set and get post cats to taxonomy. see #4189
git-svn-id: http://svn.automattic.com/wordpress/trunk@5529 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
675cc0d029
commit
0e05576999
@ -678,25 +678,10 @@ function get_tags_to_edit( $post_id ) {
|
||||
}
|
||||
|
||||
function get_nested_categories( $default = 0, $parent = 0 ) {
|
||||
global $post_ID, $link_id, $mode, $wpdb;
|
||||
global $post_ID, $mode, $wpdb;
|
||||
|
||||
if ( $post_ID ) {
|
||||
$checked_categories = $wpdb->get_col( "
|
||||
SELECT category_id
|
||||
FROM $wpdb->categories, $wpdb->post2cat
|
||||
WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID' AND rel_type = 'category'
|
||||
" );
|
||||
|
||||
if ( count( $checked_categories ) == 0 ) {
|
||||
// No selected categories, strange
|
||||
$checked_categories[] = $default;
|
||||
}
|
||||
} else if ( $link_id ) {
|
||||
$checked_categories = $wpdb->get_col( "
|
||||
SELECT category_id
|
||||
FROM $wpdb->categories, $wpdb->link2cat
|
||||
WHERE $wpdb->link2cat.category_id = cat_ID AND $wpdb->link2cat.link_id = '$link_id'
|
||||
" );
|
||||
$checked_categories = wp_get_post_categories($post_ID);
|
||||
|
||||
if ( count( $checked_categories ) == 0 ) {
|
||||
// No selected categories, strange
|
||||
@ -706,12 +691,12 @@ function get_nested_categories( $default = 0, $parent = 0 ) {
|
||||
$checked_categories[] = $default;
|
||||
}
|
||||
|
||||
$cats = return_categories_list( $parent);
|
||||
$cats = get_categories("child_of=$parent&hide_empty=0&get=ids");
|
||||
$result = array ();
|
||||
|
||||
if ( is_array( $cats ) ) {
|
||||
foreach ( $cats as $cat) {
|
||||
$result[$cat]['children'] = get_nested_categories( $default, $cat);
|
||||
//$result[$cat]['children'] = get_nested_categories( $default, $cat);
|
||||
$result[$cat]['cat_ID'] = $cat;
|
||||
$result[$cat]['checked'] = in_array( $cat, $checked_categories );
|
||||
$result[$cat]['cat_name'] = get_the_category_by_ID( $cat);
|
||||
@ -754,7 +739,7 @@ function dropdown_link_categories( $default = 0 ) {
|
||||
$checked_categories[] = $default;
|
||||
}
|
||||
|
||||
$categories = get_terms('link_category', 'orderby=count');
|
||||
$categories = get_terms('link_category', 'orderby=count&hide_empty=0');
|
||||
|
||||
if ( empty($categories) )
|
||||
return;
|
||||
@ -772,7 +757,7 @@ function cat_rows( $parent = 0, $level = 0, $categories = 0 ) {
|
||||
if ( !$categories )
|
||||
$categories = get_categories( 'hide_empty=0' );
|
||||
|
||||
$children = _get_category_hierarchy();
|
||||
$children = _get_term_hierarchy('category');
|
||||
|
||||
if ( $categories ) {
|
||||
ob_start();
|
||||
@ -959,14 +944,14 @@ function wp_dropdown_cats( $currentcat = 0, $currentparent = 0, $parent = 0, $le
|
||||
|
||||
if ( $categories ) {
|
||||
foreach ( $categories as $category ) {
|
||||
if ( $currentcat != $category->cat_ID && $parent == $category->category_parent) {
|
||||
if ( $currentcat != $category->term_id && $parent == $category->parent) {
|
||||
$pad = str_repeat( '– ', $level );
|
||||
$category->cat_name = wp_specialchars( $category->cat_name );
|
||||
echo "\n\t<option value='$category->cat_ID'";
|
||||
if ( $currentparent == $category->cat_ID )
|
||||
$category->name = wp_specialchars( $category->name );
|
||||
echo "\n\t<option value='$category->term_id'";
|
||||
if ( $currentparent == $category->term_id )
|
||||
echo " selected='selected'";
|
||||
echo ">$pad$category->cat_name</option>";
|
||||
wp_dropdown_cats( $currentcat, $currentparent, $category->cat_ID, $level +1, $categories );
|
||||
echo ">$pad$category->name</option>";
|
||||
wp_dropdown_cats( $currentcat, $currentparent, $category->term_id, $level +1, $categories );
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -21,26 +21,26 @@ if ( ! empty($cat_ID) ) {
|
||||
<div id="ajax-response"></div>
|
||||
<?php echo $form ?>
|
||||
<input type="hidden" name="action" value="<?php echo $action ?>" />
|
||||
<input type="hidden" name="cat_ID" value="<?php echo $category->cat_ID ?>" />
|
||||
<input type="hidden" name="cat_ID" value="<?php echo $category->term_id ?>" />
|
||||
<?php wp_nonce_field($nonce_action); ?>
|
||||
<table class="editform" width="100%" cellspacing="2" cellpadding="5">
|
||||
<tr>
|
||||
<th width="33%" scope="row" valign="top"><label for="cat_name"><?php _e('Category name:') ?></label></th>
|
||||
<td width="67%"><input name="cat_name" id="cat_name" type="text" value="<?php echo attribute_escape($category->cat_name); ?>" size="40" /></td>
|
||||
<td width="67%"><input name="cat_name" id="cat_name" type="text" value="<?php echo attribute_escape($category->name); ?>" size="40" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" valign="top"><label for="category_nicename"><?php _e('Category slug:') ?></label></th>
|
||||
<td><input name="category_nicename" id="category_nicename" type="text" value="<?php echo attribute_escape($category->category_nicename); ?>" size="40" /></td>
|
||||
<td><input name="category_nicename" id="category_nicename" type="text" value="<?php echo attribute_escape($category->slug); ?>" size="40" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" valign="top"><label for="category_parent"><?php _e('Category parent:') ?></label></th>
|
||||
<td>
|
||||
<?php wp_dropdown_categories('hide_empty=0&name=category_parent&orderby=name&selected=' . $category->category_parent . '&hierarchical=1&show_option_none=' . __('None')); ?>
|
||||
<?php wp_dropdown_categories('hide_empty=0&name=category_parent&orderby=name&selected=' . $category->parent . '&hierarchical=1&show_option_none=' . __('None')); ?>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" valign="top"><label for="category_description"><?php _e('Description: (optional)') ?></label></th>
|
||||
<td><textarea name="category_description" id="category_description" rows="5" cols="50" style="width: 97%;"><?php echo wp_specialchars($category->category_description); ?></textarea></td>
|
||||
<td><textarea name="category_description" id="category_description" rows="5" cols="50" style="width: 97%;"><?php echo wp_specialchars($category->description); ?></textarea></td>
|
||||
</tr>
|
||||
</table>
|
||||
<p class="submit"><input type="submit" name="submit" value="<?php echo $submit_text ?>" /></p>
|
||||
|
@ -12,9 +12,9 @@ function get_category_children($id, $before = '/', $after = '') {
|
||||
continue;
|
||||
|
||||
$category = get_category($cat_id);
|
||||
if ( $category->category_parent == $id ) {
|
||||
$chain .= $before.$category->cat_ID.$after;
|
||||
$chain .= get_category_children($category->cat_ID, $before, $after);
|
||||
if ( $category->parent == $id ) {
|
||||
$chain .= $before.$category->term_id.$after;
|
||||
$chain .= get_category_children($category->term_id, $before, $after);
|
||||
}
|
||||
}
|
||||
return $chain;
|
||||
@ -29,9 +29,9 @@ function get_category_link($category_id) {
|
||||
$catlink = $file . '?cat=' . $category_id;
|
||||
} else {
|
||||
$category = &get_category($category_id);
|
||||
$category_nicename = $category->category_nicename;
|
||||
$category_nicename = $category->slug;
|
||||
|
||||
if ( $parent = $category->category_parent )
|
||||
if ( $parent = $category->parent )
|
||||
$category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename;
|
||||
|
||||
$catlink = str_replace('%category%', $category_nicename, $catlink);
|
||||
@ -45,31 +45,28 @@ function get_category_parents($id, $link = FALSE, $separator = '/', $nicename =
|
||||
$parent = &get_category($id);
|
||||
|
||||
if ( $nicename )
|
||||
$name = $parent->category_nicename;
|
||||
$name = $parent->slug;
|
||||
else
|
||||
$name = $parent->cat_name;
|
||||
|
||||
if ( $parent->category_parent && ($parent->category_parent != $parent->cat_ID) )
|
||||
$chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename);
|
||||
if ( $parent->parent && ($parent->parent != $parent->term_id) )
|
||||
$chain .= get_category_parents($parent->parent, $link, $separator, $nicename);
|
||||
|
||||
if ( $link )
|
||||
$chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
|
||||
$chain .= '<a href="' . get_category_link($parent->term_id) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
|
||||
else
|
||||
$chain .= $name.$separator;
|
||||
return $chain;
|
||||
}
|
||||
|
||||
function get_the_category($id = false) {
|
||||
global $post, $category_cache, $blog_id;
|
||||
global $post, $category_cache, $blog_id;
|
||||
|
||||
$id = (int) $id;
|
||||
if ( !$id )
|
||||
$id = (int) $post->ID;
|
||||
|
||||
if ( !isset($category_cache[$blog_id][$id]) )
|
||||
update_post_category_cache($id);
|
||||
|
||||
$categories = $category_cache[$blog_id][$id];
|
||||
$categories = get_object_terms($id, 'category');
|
||||
|
||||
if ( !empty($categories) )
|
||||
usort($categories, '_get_the_category_usort');
|
||||
@ -86,7 +83,7 @@ function _get_the_category_usort($a, $b) {
|
||||
function get_the_category_by_ID($cat_ID) {
|
||||
$cat_ID = (int) $cat_ID;
|
||||
$category = &get_category($cat_ID);
|
||||
return $category->cat_name;
|
||||
return $category->name;
|
||||
}
|
||||
|
||||
function get_the_category_list($separator = '', $parents='') {
|
||||
@ -104,19 +101,19 @@ function get_the_category_list($separator = '', $parents='') {
|
||||
$thelist .= "\n\t<li>";
|
||||
switch ( strtolower($parents) ) {
|
||||
case 'multiple':
|
||||
if ($category->category_parent)
|
||||
$thelist .= get_category_parents($category->category_parent, TRUE);
|
||||
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
|
||||
if ($category->parent)
|
||||
$thelist .= get_category_parents($category->parent, TRUE);
|
||||
$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a></li>';
|
||||
break;
|
||||
case 'single':
|
||||
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>';
|
||||
if ($category->category_parent)
|
||||
$thelist .= get_category_parents($category->category_parent, FALSE);
|
||||
$thelist .= $category->cat_name.'</a></li>';
|
||||
$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
|
||||
if ($category->parent)
|
||||
$thelist .= get_category_parents($category->parent, FALSE);
|
||||
$thelist .= $category->name.'</a></li>';
|
||||
break;
|
||||
case '':
|
||||
default:
|
||||
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
|
||||
$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
|
||||
}
|
||||
}
|
||||
$thelist .= '</ul>';
|
||||
@ -127,19 +124,19 @@ function get_the_category_list($separator = '', $parents='') {
|
||||
$thelist .= $separator . ' ';
|
||||
switch ( strtolower($parents) ) {
|
||||
case 'multiple':
|
||||
if ( $category->category_parent )
|
||||
$thelist .= get_category_parents($category->category_parent, TRUE);
|
||||
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>' . $category->cat_name.'</a>';
|
||||
if ( $category->parent )
|
||||
$thelist .= get_category_parents($category->parent, TRUE);
|
||||
$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a>';
|
||||
break;
|
||||
case 'single':
|
||||
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>';
|
||||
if ( $category->category_parent )
|
||||
$thelist .= get_category_parents($category->category_parent, FALSE);
|
||||
$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
|
||||
if ( $category->parent )
|
||||
$thelist .= get_category_parents($category->parent, FALSE);
|
||||
$thelist .= "$category->cat_name</a>";
|
||||
break;
|
||||
case '':
|
||||
default:
|
||||
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" ' . $rel . '>' . $category->cat_name.'</a>';
|
||||
$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a>';
|
||||
}
|
||||
++$i;
|
||||
}
|
||||
@ -165,7 +162,7 @@ function category_description($category = 0) {
|
||||
if ( !$category )
|
||||
$category = $cat;
|
||||
$category = & get_category($category);
|
||||
return apply_filters('category_description', $category->category_description, $category->cat_ID);
|
||||
return apply_filters('category_description', $category->description, $category->term_id);
|
||||
}
|
||||
|
||||
function wp_dropdown_categories($args = '') {
|
||||
|
@ -102,81 +102,6 @@ function cat_is_ancestor_of($cat1, $cat2) {
|
||||
return cat_is_ancestor_of($cat1, get_category($cat2->parent));
|
||||
}
|
||||
|
||||
//
|
||||
// Private
|
||||
//
|
||||
|
||||
function &_get_cat_children($category_id, $categories) {
|
||||
if ( empty($categories) )
|
||||
return array();
|
||||
|
||||
$category_list = array();
|
||||
$has_children = _get_category_hierarchy();
|
||||
|
||||
if ( ( 0 != $category_id ) && ! isset($has_children[$category_id]) )
|
||||
return array();
|
||||
|
||||
foreach ( $categories as $category ) {
|
||||
if ( $category->cat_ID == $category_id )
|
||||
continue;
|
||||
|
||||
if ( $category->category_parent == $category_id ) {
|
||||
$category_list[] = $category;
|
||||
|
||||
if ( !isset($has_children[$category->cat_ID]) )
|
||||
continue;
|
||||
|
||||
if ( $children = _get_cat_children($category->cat_ID, $categories) )
|
||||
$category_list = array_merge($category_list, $children);
|
||||
}
|
||||
}
|
||||
|
||||
return $category_list;
|
||||
}
|
||||
|
||||
// Recalculates link or post counts by including items from child categories
|
||||
// Assumes all relevant children are already in the $categories argument
|
||||
function _pad_category_counts($type, &$categories) {
|
||||
global $wpdb;
|
||||
|
||||
// Set up some useful arrays
|
||||
foreach ( $categories as $key => $cat ) {
|
||||
$cats[$cat->cat_ID] = & $categories[$key];
|
||||
$cat_IDs[] = $cat->cat_ID;
|
||||
}
|
||||
|
||||
// Get the relevant post2cat or link2cat records and stick them in a lookup table
|
||||
if ( $type == 'post' ) {
|
||||
$results = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2cat LEFT JOIN $wpdb->posts ON post_id = ID WHERE category_id IN (".join(',', $cat_IDs).") AND post_type = 'post' AND post_status = 'publish'");
|
||||
foreach ( $results as $row )
|
||||
++$cat_items[$row->category_id][$row->post_id];
|
||||
} else {
|
||||
$results = $wpdb->get_results("SELECT $wpdb->link2cat.link_id, category_id FROM $wpdb->link2cat LEFT JOIN $wpdb->links USING (link_id) WHERE category_id IN (".join(',', $cat_IDs).") AND link_visible = 'Y'");
|
||||
foreach ( $results as $row )
|
||||
++$cat_items[$row->category_id][$row->link_id];
|
||||
}
|
||||
|
||||
// Touch every ancestor's lookup row for each post in each category
|
||||
foreach ( $cat_IDs as $cat_ID ) {
|
||||
$child = $cat_ID;
|
||||
while ( $parent = $cats[$child]->category_parent ) {
|
||||
if ( !empty($cat_items[$cat_ID]) )
|
||||
foreach ( $cat_items[$cat_ID] as $item_id => $touches )
|
||||
++$cat_items[$parent][$item_id];
|
||||
$child = $parent;
|
||||
}
|
||||
}
|
||||
|
||||
// Transfer the touched cells
|
||||
foreach ( (array) $cat_items as $id => $items )
|
||||
if ( isset($cats[$id]) )
|
||||
$cats[$id]->{'link' == $type ? 'link_count' : 'category_count'} = count($items);
|
||||
}
|
||||
|
||||
function _get_category_hierarchy() {
|
||||
return _get_term_hierarchy('category');
|
||||
}
|
||||
|
||||
// Tags
|
||||
|
||||
function &get_tags($args = '') {
|
||||
|
@ -447,11 +447,8 @@ function wp_delete_post($postid = 0) {
|
||||
function wp_get_post_categories($post_id = 0) {
|
||||
$post_id = (int) $post_id;
|
||||
|
||||
$cats = &get_the_category($post_id);
|
||||
$cat_ids = array();
|
||||
foreach ( $cats as $cat )
|
||||
$cat_ids[] = (int) $cat->cat_ID;
|
||||
return array_unique($cat_ids);
|
||||
$cats = get_object_terms($post_id, 'category', 'get=ids');
|
||||
return $cats;
|
||||
}
|
||||
|
||||
function wp_get_post_tags( $post_id = 0 ) {
|
||||
@ -805,54 +802,10 @@ function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
|
||||
if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories))
|
||||
$post_categories = array(get_option('default_category'));
|
||||
|
||||
$post_categories = array_map('intval', $post_categories);
|
||||
$post_categories = array_unique($post_categories);
|
||||
|
||||
// First the old categories
|
||||
$old_categories = $wpdb->get_col("
|
||||
SELECT category_id
|
||||
FROM $wpdb->post2cat
|
||||
WHERE post_id = '$post_ID' AND rel_type = 'category'");
|
||||
|
||||
if (!$old_categories) {
|
||||
$old_categories = array();
|
||||
} else {
|
||||
$old_categories = array_unique($old_categories);
|
||||
}
|
||||
|
||||
// Delete any?
|
||||
$delete_cats = array_diff($old_categories,$post_categories);
|
||||
|
||||
if ($delete_cats) {
|
||||
foreach ($delete_cats as $del) {
|
||||
$wpdb->query("
|
||||
DELETE FROM $wpdb->post2cat
|
||||
WHERE category_id = '$del'
|
||||
AND post_id = '$post_ID' AND rel_type = 'category'
|
||||
");
|
||||
}
|
||||
}
|
||||
|
||||
// Add any?
|
||||
$add_cats = array_diff($post_categories, $old_categories);
|
||||
|
||||
if ($add_cats) {
|
||||
foreach ($add_cats as $new_cat) {
|
||||
$new_cat = (int) $new_cat;
|
||||
if ( !empty($new_cat) )
|
||||
$wpdb->query("
|
||||
INSERT INTO $wpdb->post2cat (post_id, category_id)
|
||||
VALUES ('$post_ID', '$new_cat')");
|
||||
}
|
||||
}
|
||||
|
||||
// Update category counts.
|
||||
$all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
|
||||
foreach ( $all_affected_cats as $cat_id ) {
|
||||
$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status = 'publish' AND post_type = 'post' AND category_id = '$cat_id' AND rel_type = 'category'");
|
||||
$wpdb->query("UPDATE $wpdb->categories SET category_count = '$count', type = type | " . TAXONOMY_CATEGORY . " WHERE cat_ID = '$cat_id'");
|
||||
clean_category_cache($cat_id);
|
||||
do_action('edit_category', $cat_id);
|
||||
}
|
||||
return wp_set_object_terms($post_ID, $post_categories, 'category');
|
||||
} // wp_set_post_categories()
|
||||
|
||||
//
|
||||
|
@ -224,6 +224,8 @@ function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) {
|
||||
if ( ! $append ) {
|
||||
$in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
|
||||
$old_terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($in_taxonomies) AND tr.object_id = '$object_id'");
|
||||
if ( empty($old_terms) )
|
||||
$old_terms = array();
|
||||
}
|
||||
|
||||
$tt_ids = array();
|
||||
@ -262,6 +264,7 @@ function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) {
|
||||
function get_object_terms($object_id, $taxonomy, $args = array()) {
|
||||
global $wpdb;
|
||||
$taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy;
|
||||
// TODO cast to int
|
||||
$object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id;
|
||||
|
||||
$defaults = array('orderby' => 'name', 'order' => 'ASC', 'get' => 'everything');
|
||||
|
Loading…
Reference in New Issue
Block a user