Skip to content

Commit feb66bd

Browse files
committed
Start moving link categories to taxonomy. see WordPress#4189
git-svn-id: https://develop.svn.wordpress.org/trunk@5523 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7f3e74f commit feb66bd

4 files changed

Lines changed: 72 additions & 115 deletions

File tree

wp-admin/admin-db.php

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -463,78 +463,22 @@ function wp_delete_link($link_id) {
463463
do_action('deleted_link', $link_id);
464464
}
465465

466-
function wp_get_link_cats($link_ID = 0) {
467-
global $wpdb;
468-
469-
$sql = "SELECT category_id
470-
FROM $wpdb->link2cat
471-
WHERE link_id = $link_ID
472-
ORDER BY category_id";
473-
474-
$result = $wpdb->get_col($sql);
466+
function wp_get_link_cats($link_id = 0) {
475467

476-
if ( !$result )
477-
$result = array();
468+
$cats = get_object_terms($link_id, 'link_category', 'get=ids');
478469

479-
return array_unique($result);
470+
return array_unique($cats);
480471
}
481472

482-
function wp_set_link_cats($link_ID = 0, $link_categories = array()) {
483-
global $wpdb;
473+
function wp_set_link_cats($link_id = 0, $link_categories = array()) {
484474
// If $link_categories isn't already an array, make it one:
485475
if (!is_array($link_categories) || 0 == count($link_categories))
486476
$link_categories = array(get_option('default_link_category'));
487477

478+
$link_categories = array_map('intval', $link_categories);
488479
$link_categories = array_unique($link_categories);
489480

490-
// First the old categories
491-
$old_categories = $wpdb->get_col("
492-
SELECT category_id
493-
FROM $wpdb->link2cat
494-
WHERE link_id = '$link_ID'");
495-
496-
if (!$old_categories) {
497-
$old_categories = array();
498-
} else {
499-
$old_categories = array_unique($old_categories);
500-
}
501-
502-
// Delete any?
503-
$delete_cats = array_diff($old_categories,$link_categories);
504-
505-
if ($delete_cats) {
506-
foreach ($delete_cats as $del) {
507-
$del = (int) $del;
508-
$wpdb->query("
509-
DELETE FROM $wpdb->link2cat
510-
WHERE category_id = '$del'
511-
AND link_id = '$link_ID'
512-
");
513-
}
514-
}
515-
516-
// Add any?
517-
$add_cats = array_diff($link_categories, $old_categories);
518-
519-
if ($add_cats) {
520-
foreach ($add_cats as $new_cat) {
521-
$new_cat = (int) $new_cat;
522-
if ( !empty($new_cat) )
523-
$wpdb->query("
524-
INSERT INTO $wpdb->link2cat (link_id, category_id)
525-
VALUES ('$link_ID', '$new_cat')");
526-
}
527-
}
528-
529-
// Update category counts.
530-
$all_affected_cats = array_unique(array_merge($link_categories, $old_categories));
531-
foreach ( $all_affected_cats as $cat_id ) {
532-
$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'");
533-
$wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
534-
wp_cache_delete($cat_id, 'category');
535-
do_action('edit_category', $cat_id);
536-
}
537-
481+
wp_set_object_terms($link_id, $link_categories, 'link_category');
538482
} // wp_set_link_cats()
539483

540484
function post_exists($title, $content = '', $post_date = '') {

wp-admin/admin-functions.php

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -740,20 +740,11 @@ function dropdown_categories( $default = 0 ) {
740740
write_nested_categories( get_nested_categories( $default) );
741741
}
742742

743-
function return_link_categories_list( $parent = 0 ) {
744-
global $wpdb;
745-
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" );
746-
}
747-
748-
function get_nested_link_categories( $default = 0, $parent = 0 ) {
749-
global $post_ID, $link_id, $mode, $wpdb;
743+
function dropdown_link_categories( $default = 0 ) {
744+
global $link_id;
750745

751746
if ( $link_id ) {
752-
$checked_categories = $wpdb->get_col( "
753-
SELECT category_id
754-
FROM $wpdb->categories, $wpdb->link2cat
755-
WHERE $wpdb->link2cat.category_id = cat_ID AND $wpdb->link2cat.link_id = '$link_id'
756-
" );
747+
$checked_categories = wp_get_link_cats($link_id);
757748

758749
if ( count( $checked_categories ) == 0 ) {
759750
// No selected categories, strange
@@ -763,25 +754,17 @@ function get_nested_link_categories( $default = 0, $parent = 0 ) {
763754
$checked_categories[] = $default;
764755
}
765756

766-
$cats = return_link_categories_list( $parent);
767-
$result = array ();
757+
$categories = get_terms('link_category', 'orderby=count');
758+
759+
if ( empty($categories) )
760+
return;
768761

769-
if ( is_array( $cats ) ) {
770-
foreach ( $cats as $cat) {
771-
$result[$cat]['children'] = get_nested_link_categories( $default, $cat);
772-
$result[$cat]['cat_ID'] = $cat;
773-
$result[$cat]['checked'] = in_array( $cat, $checked_categories );
774-
$result[$cat]['cat_name'] = get_the_category_by_ID( $cat);
775-
}
762+
foreach ( $categories as $category ) {
763+
$cat_id = $category->term_id;
764+
$name = wp_specialchars( apply_filters('the_category', $category->name));
765+
$checked = in_array( $cat_id, $checked_categories );
766+
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>";
776767
}
777-
778-
usort( $result, 'sort_cats' );
779-
780-
return $result;
781-
}
782-
783-
function dropdown_link_categories( $default = 0 ) {
784-
write_nested_categories( get_nested_link_categories( $default) );
785768
}
786769

787770
// Dandy new recursive multiple category stuff.

wp-admin/link-manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ function checkAll(form)
159159
?><td><?php
160160
$cat_names = array();
161161
foreach ($link->link_category as $category) {
162-
$cat_name = get_the_category_by_ID($category);
163-
$cat_name = wp_specialchars(apply_filters('link_category', $cat_name));
162+
$cat = get_term($category, 'link_category');
163+
$cat_name = wp_specialchars(apply_filters('link_category', $cat->name));
164164
if ( $cat_id != $category )
165165
$cat_name = "<a href='link-manager.php?cat_id=$category'>$cat_name</a>";
166166
$cat_names[] = $cat_name;

wp-includes/taxonomy.php

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -182,35 +182,55 @@ function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) {
182182
* @param string|array $taxonomies The taxonomies to retrieve terms from.
183183
* @return array The requested term data.
184184
*/
185-
function get_object_terms($object_id, $taxonomy) {
185+
function get_object_terms($object_id, $taxonomy, $args = array()) {
186186
global $wpdb;
187187
$taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy;
188188
$object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id;
189189

190+
$defaults = array('orderby' => 'name', 'order' => 'ASC', 'get' => 'everything');
191+
$args = wp_parse_args( $args, $defaults );
192+
extract($args);
193+
194+
if ( 'count' == $orderby )
195+
$orderby = 'tt.count';
196+
else if ( 'name' == $orderby )
197+
$orderby = 't.name';
198+
190199
$taxonomies = "'" . implode("', '", $taxonomies) . "'";
191200
$object_ids = implode(', ', $object_ids);
192201

193-
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") ) {
194-
if ($single_taxonomy && $single_object) {
195-
// Just one kind of taxonomy for one object.
196-
return $taxonomy_data;
197-
} else {
198-
foreach ($taxonomy_data as $data) {
199-
if ($single_taxonomy) {
200-
// Many objects, one taxonomy type.
201-
$return[$data->object_id][] = $data;
202-
} elseif ($single_object) {
203-
// One object, many taxonomies.
204-
$return[$data->taxonomy][] = $data;
205-
} else {
206-
// Many objects, many taxonomies.
207-
$return[$data->object_id][$data->taxonomy][] = $data;
208-
}
202+
if ( 'everything' == $get )
203+
$select_this = 't.*';
204+
else if ( 'ids' == $get )
205+
$select_this = 't.term_id';
206+
207+
$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";
208+
209+
if ( 'everything' == $get )
210+
$taxonomy_data = $wpdb->get_results($query);
211+
else if ( 'ids' == $get )
212+
$taxonomy_data = $wpdb->get_col($query);
213+
214+
if ( ! $taxonomy_data )
215+
return array();
216+
217+
if ($single_taxonomy && $single_object) {
218+
// Just one kind of taxonomy for one object.
219+
return $taxonomy_data;
220+
} else {
221+
foreach ($taxonomy_data as $data) {
222+
if ($single_taxonomy) {
223+
// Many objects, one taxonomy type.
224+
$return[$data->object_id][] = $data;
225+
} elseif ($single_object) {
226+
// One object, many taxonomies.
227+
$return[$data->taxonomy][] = $data;
228+
} else {
229+
// Many objects, many taxonomies.
230+
$return[$data->object_id][$data->taxonomy][] = $data;
209231
}
210-
return $return;
211232
}
212-
} else {
213-
return array();
233+
return $return;
214234
}
215235
}
216236

@@ -223,7 +243,7 @@ function &get_terms($taxonomies, $args = '') {
223243

224244
$defaults = array('orderby' => 'name', 'order' => 'ASC',
225245
'hide_empty' => true, 'exclude' => '', 'include' => '',
226-
'number' => '');
246+
'number' => '', 'get' => 'everything');
227247
$args = wp_parse_args( $args, $defaults );
228248
$args['number'] = (int) $args['number'];
229249
extract($args);
@@ -278,7 +298,17 @@ function &get_terms($taxonomies, $args = '') {
278298
else
279299
$number = '';
280300

281-
$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");
301+
if ( 'everything' == $get )
302+
$select_this = 't.*, tt.*';
303+
else if ( 'ids' == $get )
304+
$select_this = 't.term_id';
305+
306+
$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";
307+
308+
if ( 'everything' == $get )
309+
$terms = $wpdb->get_results($query);
310+
else if ( 'ids' == $get )
311+
$terms = $wpdb->get_col($query);
282312

283313
if ( empty($terms) )
284314
return array();

0 commit comments

Comments
 (0)