Skip to content

Commit a2f5fa5

Browse files
committed
Bookmark/link rework. WordPress#2499
git-svn-id: https://develop.svn.wordpress.org/trunk@3570 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fea381e commit a2f5fa5

24 files changed

Lines changed: 981 additions & 1260 deletions

wp-admin/admin-db.php

Lines changed: 135 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,21 @@ function wp_insert_category($catarr) {
105105
if (empty ($category_parent))
106106
$category_parent = 0;
107107

108+
if ( isset($posts_private) )
109+
$posts_private = (int) $posts_private;
110+
else
111+
$posts_private = 0;
112+
113+
if ( isset($links_private) )
114+
$links_private = (int) $links_private;
115+
else
116+
$links_private = 0;
117+
108118
if (!$update) {
109-
$wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent')");
119+
$wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent, links_private, posts_private) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_private', '$posts_private')");
110120
$cat_ID = $wpdb->insert_id;
111121
} else {
112-
$wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent' WHERE cat_ID = '$cat_ID'");
122+
$wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent', links_private = '$links_private', posts_private = '$posts_private' WHERE cat_ID = '$cat_ID'");
113123
}
114124

115125
if ( $category_nicename == '' ) {
@@ -153,7 +163,10 @@ function wp_delete_category($cat_ID) {
153163
$cat_ID = (int) $cat_ID;
154164

155165
// Don't delete the default cat.
156-
if (1 == $cat_ID)
166+
if ( $cat_ID == get_option('default_category') )
167+
return 0;
168+
169+
if ( $cat_ID == get_option('default_link_category') )
157170
return 0;
158171

159172
$category = get_category($cat_ID);
@@ -166,9 +179,29 @@ function wp_delete_category($cat_ID) {
166179
// Update children to point to new parent.
167180
$wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
168181

169-
// TODO: Only set categories to general if they're not in another category already
170-
$wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
182+
// Only set posts and links to the default category if they're not in another category already.
183+
$default_cat = get_option('default_category');
184+
$posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID'");
185+
if ( is_array($posts) ) foreach ($posts as $post_id) {
186+
$cats = wp_get_post_cats('', $post_id);
187+
if ( 1 == count($cats) )
188+
$cats = array($default_cat);
189+
else
190+
$cats = array_diff($cats, array($cat_ID));
191+
wp_set_post_cats('', $post_id, $cats);
192+
}
171193

194+
$default_link_cat = get_option('default_link_category');
195+
$links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'");
196+
if ( is_array($links) ) foreach ($links as $link_id) {
197+
$cats = wp_get_link_cats($link_id);
198+
if ( 1 == count($cats) )
199+
$cats = array($default_link_cat);
200+
else
201+
$cats = array_diff($cats, array($cat_ID));
202+
wp_set_link_cats($link_id, $cats);
203+
}
204+
172205
wp_cache_delete($cat_ID, 'category');
173206
wp_cache_delete('all_category_ids', 'category');
174207

@@ -244,6 +277,7 @@ function get_link($link_id, $output = OBJECT) {
244277
global $wpdb;
245278

246279
$link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
280+
$link->link_category = wp_get_link_cats($link_id);
247281

248282
if ( $output == OBJECT ) {
249283
return $link;
@@ -280,19 +314,26 @@ function wp_insert_link($linkdata) {
280314
if ( empty($link_notes) )
281315
$link_notes = '';
282316

317+
// Make sure we set a valid category
318+
if (0 == count($link_category) || !is_array($link_category)) {
319+
$link_category = array(get_option('default_category'));
320+
}
321+
283322
if ( $update ) {
284323
$wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
285324
link_name='$link_name', link_image='$link_image',
286-
link_target='$link_target', link_category='$link_category',
325+
link_target='$link_target',
287326
link_visible='$link_visible', link_description='$link_description',
288327
link_rating='$link_rating', link_rel='$link_rel',
289328
link_notes='$link_notes', link_rss = '$link_rss'
290329
WHERE link_id='$link_id'");
291330
} else {
292-
$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_category, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_category', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
331+
$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
293332
$link_id = $wpdb->insert_id;
294333
}
295334

335+
wp_set_link_cats($link_id, $link_category);
336+
296337
if ( $update )
297338
do_action('edit_link', $link_id);
298339
else
@@ -311,8 +352,16 @@ function wp_update_link($linkdata) {
311352
// Escape data pulled from DB.
312353
$link = add_magic_quotes($link);
313354

355+
// Passed link category list overwrites existing category list if not empty.
356+
if ( isset($linkdata['link_category']) && is_array($linkdata['link_category'])
357+
&& 0 != count($linkdata['link_category']) )
358+
$link_cats = $linkdata['link_category'];
359+
else
360+
$link_cats = $link['link_category'];
361+
314362
// Merge old and new fields with new fields overwriting old ones.
315363
$linkdata = array_merge($link, $linkdata);
364+
$linkdata['link_category'] = $link_cats;
316365

317366
return wp_insert_link($linkdata);
318367
}
@@ -321,9 +370,88 @@ function wp_delete_link($link_id) {
321370
global $wpdb;
322371

323372
do_action('delete_link', $link_id);
373+
374+
$categories = wp_get_link_cats($link_id);
375+
if( is_array( $categories ) ) {
376+
foreach ( $categories as $category ) {
377+
$wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$category'");
378+
wp_cache_delete($category, 'category');
379+
}
380+
}
381+
382+
$wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = '$link_id'");
324383
return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
325384
}
326385

386+
function wp_get_link_cats($link_ID = 0) {
387+
global $wpdb;
388+
389+
$sql = "SELECT category_id
390+
FROM $wpdb->link2cat
391+
WHERE link_id = $link_ID
392+
ORDER BY category_id";
393+
394+
$result = $wpdb->get_col($sql);
395+
396+
if ( !$result )
397+
$result = array();
398+
399+
return array_unique($result);
400+
}
401+
402+
function wp_set_link_cats($link_ID = 0, $link_categories = array()) {
403+
global $wpdb;
404+
// If $link_categories isn't already an array, make it one:
405+
if (!is_array($link_categories) || 0 == count($link_categories))
406+
$link_categories = array(get_option('default_category'));
407+
408+
$link_categories = array_unique($link_categories);
409+
410+
// First the old categories
411+
$old_categories = $wpdb->get_col("
412+
SELECT category_id
413+
FROM $wpdb->link2cat
414+
WHERE link_id = $link_ID");
415+
416+
if (!$old_categories) {
417+
$old_categories = array();
418+
} else {
419+
$old_categories = array_unique($old_categories);
420+
}
421+
422+
// Delete any?
423+
$delete_cats = array_diff($old_categories,$link_categories);
424+
425+
if ($delete_cats) {
426+
foreach ($delete_cats as $del) {
427+
$wpdb->query("
428+
DELETE FROM $wpdb->link2cat
429+
WHERE category_id = $del
430+
AND link_id = $link_ID
431+
");
432+
}
433+
}
434+
435+
// Add any?
436+
$add_cats = array_diff($link_categories, $old_categories);
437+
438+
if ($add_cats) {
439+
foreach ($add_cats as $new_cat) {
440+
$wpdb->query("
441+
INSERT INTO $wpdb->link2cat (link_id, category_id)
442+
VALUES ($link_ID, $new_cat)");
443+
}
444+
}
445+
446+
// Update category counts.
447+
$all_affected_cats = array_unique(array_merge($link_categories, $old_categories));
448+
foreach ( $all_affected_cats as $cat_id ) {
449+
$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'");
450+
$wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
451+
wp_cache_delete($cat_id, 'category');
452+
}
453+
} // wp_set_link_cats()
454+
327455
function post_exists($title, $content = '', $post_date = '') {
328456
global $wpdb;
329457

wp-admin/admin-functions.php

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ function get_link_to_edit($link_id) {
476476
$link->link_description = wp_specialchars($link->link_description);
477477
$link->link_notes = wp_specialchars($link->link_notes);
478478
$link->link_rss = wp_specialchars($link->link_rss);
479+
$link->post_category = $link->link_category;
479480

480481
return $link;
481482
}
@@ -491,6 +492,8 @@ function get_default_link_to_edit() {
491492
else
492493
$link->link_name = '';
493494

495+
$link->link_visible = 'Y';
496+
494497
return $link;
495498
}
496499

@@ -507,14 +510,7 @@ function edit_link($link_id = '') {
507510
$_POST['link_name'] = wp_specialchars($_POST['link_name']);
508511
$_POST['link_image'] = wp_specialchars($_POST['link_image']);
509512
$_POST['link_rss'] = wp_specialchars($_POST['link_rss']);
510-
$auto_toggle = get_autotoggle($_POST['link_category']);
511-
512-
// if we are in an auto toggle category and this one is visible then we
513-
// need to make the others invisible before we add this new one.
514-
// FIXME Add category toggle func.
515-
//if (($auto_toggle == 'Y') && ($link_visible == 'Y')) {
516-
// $wpdb->query("UPDATE $wpdb->links set link_visible = 'N' WHERE link_category = $link_category");
517-
//}
513+
$_POST['link_category'] = $_POST['post_category'];
518514

519515
if ( !empty($link_id) ) {
520516
$_POST['link_id'] = $link_id;
@@ -554,7 +550,7 @@ function sort_cats($cat1, $cat2) {
554550
}
555551

556552
function get_nested_categories($default = 0, $parent = 0) {
557-
global $post_ID, $mode, $wpdb;
553+
global $post_ID, $link_id, $mode, $wpdb;
558554

559555
if ($post_ID) {
560556
$checked_categories = $wpdb->get_col("
@@ -567,7 +563,17 @@ function get_nested_categories($default = 0, $parent = 0) {
567563
// No selected categories, strange
568564
$checked_categories[] = $default;
569565
}
566+
} else if ($link_id) {
567+
$checked_categories = $wpdb->get_col("
568+
SELECT category_id
569+
FROM $wpdb->categories, $wpdb->link2cat
570+
WHERE $wpdb->link2cat.category_id = cat_ID AND $wpdb->link2cat.link_id = '$link_id'
571+
");
570572

573+
if (count($checked_categories) == 0) {
574+
// No selected categories, strange
575+
$checked_categories[] = $default;
576+
}
571577
} else {
572578
$checked_categories[] = $default;
573579
}
@@ -620,9 +626,10 @@ function cat_rows($parent = 0, $level = 0, $categories = 0) {
620626
if ( current_user_can('manage_categories') ) {
621627
$edit = "<a href='categories.php?action=edit&amp;cat_ID=$category->cat_ID' class='edit'>".__('Edit')."</a></td>";
622628
$default_cat_id = get_option('default_category');
629+
$default_link_cat_id = get_option('default_link_category');
623630

624-
if ($category->cat_ID != $default_cat_id)
625-
$edit .= "<td><a href='categories.php?action=delete&amp;cat_ID=$category->cat_ID' onclick=\"return deleteSomething( 'cat', $category->cat_ID, '".sprintf(__("You are about to delete the category &quot;%s&quot;. All of its posts will go to the default category.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), wp_specialchars($category->cat_name, 1))."' );\" class='delete'>".__('Delete')."</a>";
631+
if ( ($category->cat_ID != $default_cat_id) && ($category->cat_ID != $default_link_cat_id) )
632+
$edit .= "<td><a href='categories.php?action=delete&amp;cat_ID=$category->cat_ID' onclick=\"return deleteSomething( 'cat', $category->cat_ID, '".sprintf(__("You are about to delete the category &quot;%s&quot;. All of its posts and bookmarks will go to the default categories.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), wp_specialchars($category->cat_name, 1))."' );\" class='delete'>".__('Delete')."</a>";
626633
else
627634
$edit .= "<td style='text-align:center'>".__("Default");
628635
}
@@ -633,6 +640,7 @@ function cat_rows($parent = 0, $level = 0, $categories = 0) {
633640
echo "<tr id='cat-$category->cat_ID' class='$class'><th scope='row'>$category->cat_ID</th><td>$pad $category->cat_name</td>
634641
<td>$category->category_description</td>
635642
<td>$category->category_count</td>
643+
<td>$category->link_count</td>
636644
<td>$edit</td>
637645
</tr>";
638646
cat_rows($category->cat_ID, $level +1, $categories);
@@ -687,7 +695,6 @@ function wp_dropdown_cats($currentcat = 0, $currentparent = 0, $parent = 0, $lev
687695
if ($categories) {
688696
foreach ($categories as $category) {
689697
if ($currentcat != $category->cat_ID && $parent == $category->category_parent) {
690-
$count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID");
691698
$pad = str_repeat('&#8211; ', $level);
692699
$category->cat_name = wp_specialchars($category->cat_name);
693700
echo "\n\t<option value='$category->cat_ID'";
@@ -702,21 +709,9 @@ function wp_dropdown_cats($currentcat = 0, $currentparent = 0, $parent = 0, $lev
702709
}
703710
}
704711

705-
function link_category_dropdown($fieldname, $selected = 0) {
712+
function return_link_categories_list($parent = 0) {
706713
global $wpdb;
707-
708-
$results = $wpdb->get_results("SELECT cat_id, cat_name, auto_toggle FROM $wpdb->linkcategories ORDER BY cat_id");
709-
echo "\n<select name='$fieldname' size='1'>\n";
710-
foreach ($results as $row) {
711-
echo "\n\t<option value='$row->cat_id'";
712-
if ($row->cat_id == $selected)
713-
echo " selected='selected'";
714-
echo ">$row->cat_id : " . wp_specialchars($row->cat_name);
715-
if ($row->auto_toggle == 'Y')
716-
echo ' (auto toggle)';
717-
echo "</option>";
718-
}
719-
echo "\n</select>\n";
714+
return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY link_count DESC LIMIT 100");
720715
}
721716

722717
function wp_create_thumbnail($file, $max_side, $effect = '') {

wp-admin/admin-header.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ function addLoadEvent(func) {if ( typeof wpOnload!='function'){wpOnload=func;}el
4040
var manager = new dbxManager('postmeta');
4141
<?php break; case 'page.php' : case 'page-new.php' : ?>
4242
var manager = new dbxManager('pagemeta');
43+
<?php break; case 'link.php' : case 'link-add.php' : ?>
44+
var manager = new dbxManager('linkmeta');
4345
<?php break; endswitch; ?>
4446
});
4547
//]]>

wp-admin/categories.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<th scope="col"><?php _e('Name') ?></th>
128128
<th scope="col"><?php _e('Description') ?></th>
129129
<th scope="col"><?php _e('# Posts') ?></th>
130+
<th scope="col"><?php _e('# Links') ?></th>
130131
<th colspan="2"><?php _e('Action') ?></th>
131132
</tr>
132133
<?php
@@ -140,7 +141,7 @@
140141

141142
<?php if ( current_user_can('manage_categories') ) : ?>
142143
<div class="wrap">
143-
<p><?php printf(__('<strong>Note:</strong><br />Deleting a category does not delete posts from that category, it will just set them back to the default category <strong>%s</strong>.'), get_catname(get_option('default_category'))) ?></p>
144+
<p><?php printf(__('<strong>Note:</strong><br />Deleting a category does not delete the posts and bookmarks in that category. Instead, posts in the deleted category are set to the category <strong>%s</strong> and bookmarks are set to <strong>%s</strong>.'), get_catname(get_option('default_category')), get_catname(get_option('default_link_category'))) ?></p>
144145
</div>
145146

146147
<div class="wrap">

0 commit comments

Comments
 (0)