Start moving link categories to taxonomy. see #4189
git-svn-id: http://svn.automattic.com/wordpress/trunk@5523 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
b450105bfd
commit
4bbddb6559
|
@ -463,78 +463,22 @@ function wp_delete_link($link_id) {
|
|||
do_action('deleted_link', $link_id);
|
||||
}
|
||||
|
||||
function wp_get_link_cats($link_ID = 0) {
|
||||
global $wpdb;
|
||||
function wp_get_link_cats($link_id = 0) {
|
||||
|
||||
$sql = "SELECT category_id
|
||||
FROM $wpdb->link2cat
|
||||
WHERE link_id = $link_ID
|
||||
ORDER BY category_id";
|
||||
$cats = get_object_terms($link_id, 'link_category', 'get=ids');
|
||||
|
||||
$result = $wpdb->get_col($sql);
|
||||
|
||||
if ( !$result )
|
||||
$result = array();
|
||||
|
||||
return array_unique($result);
|
||||
return array_unique($cats);
|
||||
}
|
||||
|
||||
function wp_set_link_cats($link_ID = 0, $link_categories = array()) {
|
||||
global $wpdb;
|
||||
function wp_set_link_cats($link_id = 0, $link_categories = array()) {
|
||||
// If $link_categories isn't already an array, make it one:
|
||||
if (!is_array($link_categories) || 0 == count($link_categories))
|
||||
$link_categories = array(get_option('default_link_category'));
|
||||
|
||||
$link_categories = array_map('intval', $link_categories);
|
||||
$link_categories = array_unique($link_categories);
|
||||
|
||||
// First the old categories
|
||||
$old_categories = $wpdb->get_col("
|
||||
SELECT category_id
|
||||
FROM $wpdb->link2cat
|
||||
WHERE link_id = '$link_ID'");
|
||||
|
||||
if (!$old_categories) {
|
||||
$old_categories = array();
|
||||
} else {
|
||||
$old_categories = array_unique($old_categories);
|
||||
}
|
||||
|
||||
// Delete any?
|
||||
$delete_cats = array_diff($old_categories,$link_categories);
|
||||
|
||||
if ($delete_cats) {
|
||||
foreach ($delete_cats as $del) {
|
||||
$del = (int) $del;
|
||||
$wpdb->query("
|
||||
DELETE FROM $wpdb->link2cat
|
||||
WHERE category_id = '$del'
|
||||
AND link_id = '$link_ID'
|
||||
");
|
||||
}
|
||||
}
|
||||
|
||||
// Add any?
|
||||
$add_cats = array_diff($link_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->link2cat (link_id, category_id)
|
||||
VALUES ('$link_ID', '$new_cat')");
|
||||
}
|
||||
}
|
||||
|
||||
// Update category counts.
|
||||
$all_affected_cats = array_unique(array_merge($link_categories, $old_categories));
|
||||
foreach ( $all_affected_cats as $cat_id ) {
|
||||
$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->link2cat, $wpdb->links WHERE $wpdb->links.link_id = $wpdb->link2cat.link_id AND category_id = '$cat_id'");
|
||||
$wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
|
||||
wp_cache_delete($cat_id, 'category');
|
||||
do_action('edit_category', $cat_id);
|
||||
}
|
||||
|
||||
wp_set_object_terms($link_id, $link_categories, 'link_category');
|
||||
} // wp_set_link_cats()
|
||||
|
||||
function post_exists($title, $content = '', $post_date = '') {
|
||||
|
|
|
@ -740,20 +740,11 @@ function dropdown_categories( $default = 0 ) {
|
|||
write_nested_categories( get_nested_categories( $default) );
|
||||
}
|
||||
|
||||
function return_link_categories_list( $parent = 0 ) {
|
||||
global $wpdb;
|
||||
return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( type & " . TAXONOMY_CATEGORY . " != 0 ) AND ( category_count = 0 OR link_count != 0 ) ORDER BY link_count DESC" );
|
||||
}
|
||||
|
||||
function get_nested_link_categories( $default = 0, $parent = 0 ) {
|
||||
global $post_ID, $link_id, $mode, $wpdb;
|
||||
function dropdown_link_categories( $default = 0 ) {
|
||||
global $link_id;
|
||||
|
||||
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_link_cats($link_id);
|
||||
|
||||
if ( count( $checked_categories ) == 0 ) {
|
||||
// No selected categories, strange
|
||||
|
@ -763,25 +754,17 @@ function get_nested_link_categories( $default = 0, $parent = 0 ) {
|
|||
$checked_categories[] = $default;
|
||||
}
|
||||
|
||||
$cats = return_link_categories_list( $parent);
|
||||
$result = array ();
|
||||
$categories = get_terms('link_category', 'orderby=count');
|
||||
|
||||
if ( empty($categories) )
|
||||
return;
|
||||
|
||||
if ( is_array( $cats ) ) {
|
||||
foreach ( $cats as $cat) {
|
||||
$result[$cat]['children'] = get_nested_link_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);
|
||||
}
|
||||
foreach ( $categories as $category ) {
|
||||
$cat_id = $category->term_id;
|
||||
$name = wp_specialchars( apply_filters('the_category', $category->name));
|
||||
$checked = in_array( $cat_id, $checked_categories );
|
||||
echo '<li id="category-', $cat_id, '"><label for="in-category-', $cat_id, '" class="selectit"><input value="', $cat_id, '" type="checkbox" name="post_category[]" id="in-category-', $cat_id, '"', ($checked ? ' checked="checked"' : "" ), '/> ', $name, "</label></li>";
|
||||
}
|
||||
|
||||
usort( $result, 'sort_cats' );
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function dropdown_link_categories( $default = 0 ) {
|
||||
write_nested_categories( get_nested_link_categories( $default) );
|
||||
}
|
||||
|
||||
// Dandy new recursive multiple category stuff.
|
||||
|
|
|
@ -159,8 +159,8 @@ if ( $links ) {
|
|||
?><td><?php
|
||||
$cat_names = array();
|
||||
foreach ($link->link_category as $category) {
|
||||
$cat_name = get_the_category_by_ID($category);
|
||||
$cat_name = wp_specialchars(apply_filters('link_category', $cat_name));
|
||||
$cat = get_term($category, 'link_category');
|
||||
$cat_name = wp_specialchars(apply_filters('link_category', $cat->name));
|
||||
if ( $cat_id != $category )
|
||||
$cat_name = "<a href='link-manager.php?cat_id=$category'>$cat_name</a>";
|
||||
$cat_names[] = $cat_name;
|
||||
|
|
|
@ -182,35 +182,55 @@ function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) {
|
|||
* @param string|array $taxonomies The taxonomies to retrieve terms from.
|
||||
* @return array The requested term data.
|
||||
*/
|
||||
function get_object_terms($object_id, $taxonomy) {
|
||||
function get_object_terms($object_id, $taxonomy, $args = array()) {
|
||||
global $wpdb;
|
||||
$taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy;
|
||||
$object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id;
|
||||
|
||||
$defaults = array('orderby' => 'name', 'order' => 'ASC', 'get' => 'everything');
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
extract($args);
|
||||
|
||||
if ( 'count' == $orderby )
|
||||
$orderby = 'tt.count';
|
||||
else if ( 'name' == $orderby )
|
||||
$orderby = 't.name';
|
||||
|
||||
$taxonomies = "'" . implode("', '", $taxonomies) . "'";
|
||||
$object_ids = implode(', ', $object_ids);
|
||||
|
||||
if ( $taxonomy_data = $wpdb->get_results("SELECT t.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY t.name") ) {
|
||||
if ($single_taxonomy && $single_object) {
|
||||
// Just one kind of taxonomy for one object.
|
||||
return $taxonomy_data;
|
||||
} else {
|
||||
foreach ($taxonomy_data as $data) {
|
||||
if ($single_taxonomy) {
|
||||
// Many objects, one taxonomy type.
|
||||
$return[$data->object_id][] = $data;
|
||||
} elseif ($single_object) {
|
||||
// One object, many taxonomies.
|
||||
$return[$data->taxonomy][] = $data;
|
||||
} else {
|
||||
// Many objects, many taxonomies.
|
||||
$return[$data->object_id][$data->taxonomy][] = $data;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
} else {
|
||||
if ( 'everything' == $get )
|
||||
$select_this = 't.*';
|
||||
else if ( 'ids' == $get )
|
||||
$select_this = 't.term_id';
|
||||
|
||||
$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY $orderby $order";
|
||||
|
||||
if ( 'everything' == $get )
|
||||
$taxonomy_data = $wpdb->get_results($query);
|
||||
else if ( 'ids' == $get )
|
||||
$taxonomy_data = $wpdb->get_col($query);
|
||||
|
||||
if ( ! $taxonomy_data )
|
||||
return array();
|
||||
|
||||
if ($single_taxonomy && $single_object) {
|
||||
// Just one kind of taxonomy for one object.
|
||||
return $taxonomy_data;
|
||||
} else {
|
||||
foreach ($taxonomy_data as $data) {
|
||||
if ($single_taxonomy) {
|
||||
// Many objects, one taxonomy type.
|
||||
$return[$data->object_id][] = $data;
|
||||
} elseif ($single_object) {
|
||||
// One object, many taxonomies.
|
||||
$return[$data->taxonomy][] = $data;
|
||||
} else {
|
||||
// Many objects, many taxonomies.
|
||||
$return[$data->object_id][$data->taxonomy][] = $data;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -223,7 +243,7 @@ function &get_terms($taxonomies, $args = '') {
|
|||
|
||||
$defaults = array('orderby' => 'name', 'order' => 'ASC',
|
||||
'hide_empty' => true, 'exclude' => '', 'include' => '',
|
||||
'number' => '');
|
||||
'number' => '', 'get' => 'everything');
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
$args['number'] = (int) $args['number'];
|
||||
extract($args);
|
||||
|
@ -278,7 +298,17 @@ function &get_terms($taxonomies, $args = '') {
|
|||
else
|
||||
$number = '';
|
||||
|
||||
$terms = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $number");
|
||||
if ( 'everything' == $get )
|
||||
$select_this = 't.*, tt.*';
|
||||
else if ( 'ids' == $get )
|
||||
$select_this = 't.term_id';
|
||||
|
||||
$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $number";
|
||||
|
||||
if ( 'everything' == $get )
|
||||
$terms = $wpdb->get_results($query);
|
||||
else if ( 'ids' == $get )
|
||||
$terms = $wpdb->get_col($query);
|
||||
|
||||
if ( empty($terms) )
|
||||
return array();
|
||||
|
|
Loading…
Reference in New Issue