Index: wp-includes/template-functions-category.php
===================================================================
--- wp-includes/template-functions-category.php (revision 3568)
+++ wp-includes/template-functions-category.php (working copy)
@@ -410,4 +410,60 @@
return false;
}
+function &get_categories($args = '') {
+ global $wpdb, $category_links;
+
+ parse_str($args, $r);
+
+ if ( !isset($r['type']) ) // 'post' or 'link'
+ $r['type'] = 'post';
+ if ( !isset($r['child_of']) )
+ $r['child_of'] = 0;
+ if ( !isset($r['orderby']) )
+ $r['orderby'] = 'name';
+ if ( !isset($r['order']) )
+ $r['order'] = 'ASC';
+ if ( !isset($r['hide_empty']) )
+ $r['hide_empty'] = true;
+
+ $r['orderby'] = "cat_" . $r['orderby'];
+
+ $exclusions = '';
+ if ( !empty($r['exclude']) ) {
+ $excategories = preg_split('/[\s,]+/',$r['exclude']);
+ if ( count($excategories) ) {
+ foreach ( $excategories as $excat ) {
+ $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
+ }
+ }
+ }
+
+ $categories = $wpdb->get_results("SELECT * " .
+ "FROM $wpdb->categories " .
+ "$exclusions " .
+ "ORDER BY " . $r['orderby'] . " " . $r['order']);
+
+ if ( empty($categories) )
+ return array();
+
+ if ( $r['hide_empty'] ) {
+ foreach ( $categories as $category ) {
+ $count = 0;
+ if ( 'link' == $r['type'] ) {
+ $count = $category->link_count;
+ } else {
+ $count = $category->category_count;
+ }
+ if ( $count )
+ $the_categories[] = $category;
+ }
+ $categories = $the_categories;
+ }
+
+ /* if ( $r['child_of'] )
+ $categories = & get_category_children($r['child_of'], $categories); */
+
+ return $categories;
+}
+
?>
Index: wp-includes/links.php
===================================================================
--- wp-includes/links.php (revision 3568)
+++ wp-includes/links.php (working copy)
@@ -28,21 +28,16 @@
$limit = -1, $show_updated = 0) {
global $wpdb;
$cat_id = -1;
- $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
+ $results = $wpdb->get_results("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
if ($results) {
foreach ($results as $result) {
- $cat_id = $result->cat_id;
+ $cat_id = $result->cat_ID;
}
}
get_links($cat_id, $before, $after, $between, $show_images, $orderby,
$show_description, $show_rating, $limit, $show_updated);
}
-function bool_from_yn($yn) {
- if ($yn == 'Y') return 1;
- return 0;
-}
-
/** function wp_get_linksbyname()
** Gets the links associated with the named category.
** Parameters:
@@ -51,26 +46,13 @@
function wp_get_linksbyname($category, $args = '') {
global $wpdb;
- $cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
- . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
- . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_name='$category'");
+ $cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category' LIMIT 1");
- if (! $cat) {
+ if (! $cat_id)
return;
- }
- if (empty($args)) {
- if ($cat->sort_desc == 'Y') {
- $cat->sort_order = '_'.$cat->sort_order;
- }
- get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
- $cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
- bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
- $cat->list_limit, bool_from_yn($cat->show_updated));
- } else {
- $args = add_query_arg('category', $cat->cat_id, $args);
- wp_get_links($args);
- }
+ $args = add_query_arg('category', $cat_id, $args);
+ wp_get_links($args);
} // end wp_get_linksbyname
/** function wp_get_links()
@@ -83,38 +65,29 @@
function wp_get_links($args = '') {
global $wpdb;
- if (!empty($args) && false === strpos($args, '=')) {
- // If args is not a query string, it's a category id.
- $category = $args;
- $cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
- . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
- . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_id=$category");
- if ($cat) {
- if ($cat->sort_desc == 'Y') {
- $cat->sort_order = '_'.$cat->sort_order;
- }
- get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
- $cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
- bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
- $cat->list_limit, bool_from_yn($cat->show_updated));
- }
- } else {
- parse_str($args);
+ if ( empty($args) )
+ return;
- if (! isset($category)) $category = -1;
- if (! isset($before)) $before = '';
- if (! isset($after)) $after = '
';
- if (! isset($between)) $between = ' ';
- if (! isset($show_images)) $show_images = true;
- if (! isset($orderby)) $orderby = 'name';
- if (! isset($show_description)) $show_description = true;
- if (! isset($show_rating)) $show_rating = false;
- if (! isset($limit)) $limit = -1;
- if (! isset($show_updated)) $show_updated = 1;
- if (! isset($echo)) $echo = true;
-
- return get_links($category, $before, $after, $between, $show_images, $orderby, $show_description, $show_rating, $limit, $show_updated, $echo);
+ if ( false === strpos($args, '=') ) {
+ $cat_id = $args;
+ $args = add_query_arg('category', $cat_id, $args);
}
+
+ parse_str($args);
+
+ if (! isset($category)) $category = -1;
+ if (! isset($before)) $before = '';
+ if (! isset($after)) $after = '
';
+ if (! isset($between)) $between = ' ';
+ if (! isset($show_images)) $show_images = true;
+ if (! isset($orderby)) $orderby = 'name';
+ if (! isset($show_description)) $show_description = true;
+ if (! isset($show_rating)) $show_rating = false;
+ if (! isset($limit)) $limit = -1;
+ if (! isset($show_updated)) $show_updated = 1;
+ if (! isset($echo)) $echo = true;
+
+ return get_links($category, $before, $after, $between, $show_images, $orderby, $show_description, $show_rating, $limit, $show_updated, $echo);
} // end wp_get_links
/** function get_links()
@@ -154,53 +127,13 @@
global $wpdb;
- $direction = ' ASC';
- $category_query = '';
- if ($category != -1) {
- $category_query = " AND link_category = $category ";
- }
- if (get_settings('links_recently_updated_time')) {
- $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_settings('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
- } else {
- $recently_updated_test = '';
- }
- if ($show_updated) {
- $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
- }
+ $results = get_linkz("orderby=$orderby&show_updated=$show_updated&limit=$limit");
- $orderby = strtolower($orderby);
- if ($orderby == '')
- $orderby = 'id';
- if (substr($orderby, 0, 1) == '_') {
- $direction = ' DESC';
- $orderby = substr($orderby, 1);
- }
-
- switch($orderby) {
- case 'length':
- $length = ", CHAR_LENGTH(link_name) AS length";
- break;
- case 'rand':
- $orderby = 'rand()';
- break;
- default:
- $orderby = " link_" . $orderby;
- }
-
- if (!isset($length)) {
- $length = '';
- }
-
- $sql = "SELECT link_url, link_name, link_image, link_target, link_description, link_rating, link_rel $length $recently_updated_test $get_updated FROM $wpdb->links WHERE link_visible = 'Y' " . $category_query;
- $sql .= ' ORDER BY ' . $orderby . $direction;
- /* The next 2 lines implement LIMIT TO processing */
- if ($limit != -1)
- $sql .= " LIMIT $limit";
- $results = $wpdb->get_results($sql);
if (!$results) {
return;
}
+
$output = '';
foreach ($results as $row) {
@@ -290,10 +223,12 @@
** echo '
'.$link->link_name.'';
** }
**/
+// Deprecate in favor of get_linkz().
function get_linkobjectsbyname($cat_name = "noname" , $orderby = 'name', $limit = -1) {
global $wpdb;
$cat_id = -1;
- $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
+ //$results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
+ // TODO: Fix me.
if ($results) {
foreach ($results as $result) {
$cat_id = $result->cat_id;
@@ -337,6 +272,7 @@
** link_rel
** link_notes
**/
+// Deprecate in favor of get_linkz().
function get_linkobjects($category = -1, $orderby = 'name', $limit = -1) {
global $wpdb;
@@ -445,12 +381,18 @@
** uses 0
*/
function get_linkcatname($id = 0) {
- global $wpdb;
- $cat_name = '';
- if ('' != $id) {
- $cat_name = $wpdb->get_var("SELECT cat_name FROM $wpdb->linkcategories WHERE cat_id=$id");
- }
- return $cat_name;
+ if ( empty($id) )
+ return '';
+
+ $cats = wp_get_link_cats($id);
+
+ if ( empty($cats) || ! is_array($cats) )
+ return '';
+
+ $cat_id = $cats[0]; // Take the first cat.
+
+ $cat = get_category($cat_id);
+ return $cat->cat_name;
}
/** function get_get_autotoggle()
@@ -459,11 +401,7 @@
** uses 0
*/
function get_autotoggle($id = 0) {
- global $wpdb;
- $auto_toggle = $wpdb->get_var("SELECT auto_toggle FROM $wpdb->linkcategories WHERE cat_id=$id");
- if ('' == $auto_toggle)
- $auto_toggle = 'N';
- return $auto_toggle;
+ return 0;
}
/** function links_popup_script()
@@ -511,50 +449,29 @@
* hide_if_empty (default true) - Supress listing empty link categories
*/
function get_links_list($order = 'name', $hide_if_empty = 'obsolete') {
- global $wpdb;
-
$order = strtolower($order);
// Handle link category sorting
+ $direction = 'ASC';
if (substr($order,0,1) == '_') {
- $direction = ' DESC';
+ $direction = 'DESC';
$order = substr($order,1);
}
- // if 'name' wasn't specified, assume 'id':
- $cat_order = ('name' == $order) ? 'cat_name' : 'cat_id';
-
if (!isset($direction)) $direction = '';
- // Fetch the link category data as an array of hashesa
- $cats = $wpdb->get_results("
- SELECT DISTINCT link_category, cat_name, show_images,
- show_description, show_rating, show_updated, sort_order,
- sort_desc, list_limit
- FROM `$wpdb->links`
- LEFT JOIN `$wpdb->linkcategories` ON (link_category = cat_id)
- WHERE link_visible = 'Y'
- AND list_limit <> 0
- ORDER BY $cat_order $direction ", ARRAY_A);
+ $cats = get_categories("type=link&orderby=$order&order=$direction");
+
// Display each category
if ($cats) {
foreach ($cats as $cat) {
// Handle each category.
- // First, fix the sort_order info
- $orderby = $cat['sort_order'];
- $orderby = (bool_from_yn($cat['sort_desc'])?'_':'') . $orderby;
// Display the category name
- echo ' ' . $cat['cat_name'] . "
\n\t\n";
+ echo ' ' . $cat->cat_name . "
\n\t\n";
// Call get_links() with all the appropriate params
- get_links($cat['link_category'],
- '- ',"
","\n",
- bool_from_yn($cat['show_images']),
- $orderby,
- bool_from_yn($cat['show_description']),
- bool_from_yn($cat['show_rating']),
- $cat['list_limit'],
- bool_from_yn($cat['show_updated']));
+ get_links($cat->cat_ID,
+ '- ',"
","\n");
// Close the last category
echo "\n\t
\n \n";
@@ -562,4 +479,83 @@
}
}
+function get_linkz($args = '') {
+ global $wpdb;
+
+ parse_str($args, $r);
+
+ if ( !isset($r['orderby']) )
+ $r['orderby'] = 'name';
+ if ( !isset($r['order']) )
+ $r['order'] = 'ASC';
+ if ( !isset($r['limit']) )
+ $r['limit'] = -1;
+ if ( !isset($r['category']) )
+ $r['category'] = -1;
+ if ( !isset($r['category_name']) )
+ $r['category_name'] = '';
+ if ( !isset($r['hide_invisible']) )
+ $r['hide_invisible'] = 1;
+ if ( !isset($r['show_updates']) )
+ $r['show_updated'] = 0;
+
+ $exclusions = '';
+ if ( !empty($r['exclude']) ) {
+ $exlinks = preg_split('/[\s,]+/',$r['exclude']);
+ if ( count($exlinks) ) {
+ foreach ( $exlinks as $exlink ) {
+ $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
+ }
+ }
+ }
+
+ extract($r);
+
+ if ( ! empty($category_name) ) {
+ if ( $cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category_name' LIMIT 1") )
+ $category = $cat_id;
+ }
+
+ $category_query = '';
+ $join = '';
+ if ( $category != -1 && !empty($category) ) {
+ $join = " LEFT JOIN $wpdb->link2cat ON ($wpdb->links.link_id = $wpdb->link2cat.link_id) ";
+
+ $category_query = " AND category_id = $category ";
+ }
+
+ if (get_settings('links_recently_updated_time')) {
+ $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_settings('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
+ } else {
+ $recently_updated_test = '';
+ }
+
+ if ($show_updated) {
+ $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
+ }
+
+ $orderby = strtolower($r['orderby']);
+ $length = '';
+ switch ($orderby) {
+ case 'length':
+ $length = ", CHAR_LENGTH(link_name) AS length";
+ break;
+ case 'rand':
+ $orderby = 'rand()';
+ break;
+ default:
+ $orderby = "link_" . $orderby;
+ }
+
+ $visible = '';
+ if ( $hide_invisible )
+ $visible = "AND link_visible = 'Y'";
+
+ $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
+ $query .= " ORDER BY $orderby $order";
+ if ($limit != -1)
+ $query .= " LIMIT $limit";
+
+ return $wpdb->get_results($query);
+}
?>
\ No newline at end of file
Index: wp-includes/version.php
===================================================================
--- wp-includes/version.php (revision 3568)
+++ wp-includes/version.php (working copy)
@@ -3,6 +3,6 @@
// This just holds the version number, in a separate file so we can bump it without cluttering the SVN
$wp_version = '2.1-alpha1';
-$wp_db_version = 3548;
+$wp_db_version = 3569;
?>
\ No newline at end of file
Index: wp-includes/functions.php
===================================================================
--- wp-includes/functions.php (revision 3568)
+++ wp-includes/functions.php (working copy)
@@ -2409,4 +2409,9 @@
else
return '';
}
+
+function bool_from_yn($yn) {
+ if ($yn == 'Y') return 1;
+ return 0;
+}
?>
Index: wp-settings.php
===================================================================
--- wp-settings.php (revision 3568)
+++ wp-settings.php (working copy)
@@ -78,6 +78,7 @@
$wpdb->categories = $table_prefix . 'categories';
$wpdb->post2cat = $table_prefix . 'post2cat';
$wpdb->comments = $table_prefix . 'comments';
+$wpdb->link2cat = $table_prefix . 'link2cat';
$wpdb->links = $table_prefix . 'links';
$wpdb->linkcategories = $table_prefix . 'linkcategories';
$wpdb->options = $table_prefix . 'options';
@@ -98,6 +99,7 @@
$tablecategories = $wpdb->categories;
$tablepost2cat = $wpdb->post2cat;
$tablecomments = $wpdb->comments;
+$tablelink2cat = $wpdb->link2cat;
$tablelinks = $wpdb->links;
$tablelinkcategories = $wpdb->linkcategories;
$tableoptions = $wpdb->options;
Index: wp-admin/upgrade-functions.php
===================================================================
--- wp-admin/upgrade-functions.php (revision 3568)
+++ wp-admin/upgrade-functions.php (working copy)
@@ -1,6 +1,7 @@
flush_rules();
@@ -366,6 +367,43 @@
foreach ( $posts as $post )
wp_schedule_event(mysql2date('U', $post->post_date), 'once', 'publish_future_post', $post->ID);
}
+ if ( $wp_current_db_version < 3569 ) {
+ // Create categories for link categories if a category with the same
+ // name doesn't exist. Create a map of link cat IDs to cat IDs.
+ $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories");
+ foreach ( $link_cats as $link_cat) {
+ if ( $cat_id = category_exists($link_cat->cat_name) ) {
+ $link_cat_id_map[$link_cat->cat_id] = $cat_id;
+ $default_link_cat = $cat_id;
+ } else {
+ $link_cat_id_map[$link_cat->cat_id] = wp_create_category($link_cat->cat_name);
+ $default_link_cat = $link_cat_id_map[$link_cat->cat_id];
+ }
+ }
+
+ // Associate links to cats.
+ $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
+ foreach ( $links as $link ) {
+ $link_cat = $link_cat_id_map[$link->link_category];
+ $cat = $wpdb->get_row("SELECT * FROM $wpdb->link2cat WHERE link_id = $link->link_id AND category_id = $link_cat");
+ if (!$cat && 0 != $link->link_category) {
+ $wpdb->query("INSERT INTO $wpdb->link2cat (link_id, category_id)
+ VALUES ('$link->link_id', '$link_cat')");
+ }
+ }
+
+ // Set default to the last category we grabbed during the upgrade loop.
+ update_option('default_link_category', $default_link_cat);
+
+ // Count links per category.
+ if ( 0 == $wpdb->get_var("SELECT SUM(link_count) FROM $wpdb->categories") ) {
+ $categories = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
+ foreach ( $categories 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'");
+ }
+ }
+ }
}
// The functions we use to actually do stuff
Index: wp-admin/admin-functions.php
===================================================================
--- wp-admin/admin-functions.php (revision 3568)
+++ wp-admin/admin-functions.php (working copy)
@@ -476,6 +476,7 @@
$link->link_description = wp_specialchars($link->link_description);
$link->link_notes = wp_specialchars($link->link_notes);
$link->link_rss = wp_specialchars($link->link_rss);
+ $link->post_category = $link->link_category;
return $link;
}
@@ -507,15 +508,7 @@
$_POST['link_name'] = wp_specialchars($_POST['link_name']);
$_POST['link_image'] = wp_specialchars($_POST['link_image']);
$_POST['link_rss'] = wp_specialchars($_POST['link_rss']);
- $auto_toggle = get_autotoggle($_POST['link_category']);
- // if we are in an auto toggle category and this one is visible then we
- // need to make the others invisible before we add this new one.
- // FIXME Add category toggle func.
- //if (($auto_toggle == 'Y') && ($link_visible == 'Y')) {
- // $wpdb->query("UPDATE $wpdb->links set link_visible = 'N' WHERE link_category = $link_category");
- //}
-
if ( !empty($link_id) ) {
$_POST['link_id'] = $link_id;
return wp_update_link($_POST);
@@ -643,6 +636,44 @@
}
}
+// Dandy new recursive multiple category stuff.
+function link_cat_rows($parent = 0, $level = 0, $categories = 0) {
+ global $wpdb, $class;
+
+ if (!$categories)
+ $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name");
+
+ if ($categories) {
+ foreach ($categories as $category) {
+ if ($category->category_parent == $parent) {
+ $category->cat_name = wp_specialchars($category->cat_name);
+ $pad = str_repeat('— ', $level);
+ if ( current_user_can('manage_categories') ) {
+ $edit = "".__('Edit')."";
+ $default_cat_id = get_option('default_link_category');
+
+ if ($category->cat_ID != $default_cat_id)
+ $edit .= "cat_ID, '".sprintf(__("You are about to delete the category "%s". All of its posts will go to the default category.\\n"OK" to delete, "Cancel" to stop."), wp_specialchars($category->cat_name, 1))."' );\" class='delete'>".__('Delete')."";
+ else
+ $edit .= " | ".__("Default");
+ }
+ else
+ $edit = '';
+
+ $class = ('alternate' == $class) ? '' : 'alternate';
+ echo " | $category->cat_ID | $pad $category->cat_name |
+ $category->category_description |
+ $category->link_count |
+ $edit |
+
";
+ link_cat_rows($category->cat_ID, $level +1, $categories);
+ }
+ }
+ } else {
+ return false;
+ }
+}
+
function page_rows($parent = 0, $level = 0, $pages = 0, $hierarchy = true) {
global $wpdb, $class, $post;
@@ -702,23 +733,63 @@
}
}
-function link_category_dropdown($fieldname, $selected = 0) {
+function return_link_categories_list($parent = 0) {
global $wpdb;
+ return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY link_count DESC LIMIT 100");
+}
- $results = $wpdb->get_results("SELECT cat_id, cat_name, auto_toggle FROM $wpdb->linkcategories ORDER BY cat_id");
- echo "\n\n";
+
+ $cats = return_link_categories_list($parent);
+ $result = array ();
+
+ 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);
+ }
+ }
+
+ usort($result, 'sort_cats');
+
+ return $result;
}
+function write_nested_link_categories($categories) {
+ foreach ($categories as $category) {
+ echo '\n";
+
+ if (isset ($category['children'])) {
+ echo "\n\n";
+ write_nested_link_categories($category['children']);
+ echo "\n";
+ }
+ }
+}
+
+function dropdown_link_categories($default = 0) {
+ write_nested_link_categories(get_nested_link_categories($default));
+}
+
function wp_create_thumbnail($file, $max_side, $effect = '') {
// 1 = GIF, 2 = JPEG, 3 = PNG
Index: wp-admin/admin-db.php
===================================================================
--- wp-admin/admin-db.php (revision 3568)
+++ wp-admin/admin-db.php (working copy)
@@ -105,11 +105,14 @@
if (empty ($category_parent))
$category_parent = 0;
+ if ( ! isset($links_visible) )
+ $links_visible = 1;
+
if (!$update) {
- $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')");
+ $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent, links_visible) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_visible')");
$cat_ID = $wpdb->insert_id;
} else {
- $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'");
+ $wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent', links_visible = '$links_visible' WHERE cat_ID = '$cat_ID'");
}
if ( $category_nicename == '' ) {
@@ -168,6 +171,7 @@
// TODO: Only set categories to general if they're not in another category already
$wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
+ $wpdb->query("UPDATE $wpdb->link2cat SET category_id='1' WHERE category_id='$cat_ID'");
wp_cache_delete($cat_ID, 'category');
wp_cache_delete('all_category_ids', 'category');
@@ -244,6 +248,7 @@
global $wpdb;
$link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
+ $link->link_category = wp_get_link_cats($link_id);
if ( $output == OBJECT ) {
return $link;
@@ -280,19 +285,26 @@
if ( empty($link_notes) )
$link_notes = '';
+ // Make sure we set a valid category
+ if (0 == count($link_category) || !is_array($link_category)) {
+ $link_category = array(get_option('default_category'));
+ }
+
if ( $update ) {
$wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
link_name='$link_name', link_image='$link_image',
- link_target='$link_target', link_category='$link_category',
+ link_target='$link_target',
link_visible='$link_visible', link_description='$link_description',
link_rating='$link_rating', link_rel='$link_rel',
link_notes='$link_notes', link_rss = '$link_rss'
WHERE link_id='$link_id'");
} else {
- $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')");
+ $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')");
$link_id = $wpdb->insert_id;
}
+ wp_set_link_cats($link_id, $link_category);
+
if ( $update )
do_action('edit_link', $link_id);
else
@@ -311,8 +323,16 @@
// Escape data pulled from DB.
$link = add_magic_quotes($link);
+ // Passed link category list overwrites existing category list if not empty.
+ if ( isset($linkdata['link_category']) && is_array($linkdata['link_category'])
+ && 0 != count($linkdata['link_category']) )
+ $link_cats = $linkdata['link_category'];
+ else
+ $link_cats = $link['link_category'];
+
// Merge old and new fields with new fields overwriting old ones.
$linkdata = array_merge($link, $linkdata);
+ $linkdata['link_category'] = $link_cats;
return wp_insert_link($linkdata);
}
@@ -321,9 +341,88 @@
global $wpdb;
do_action('delete_link', $link_id);
+
+ $categories = wp_get_link_cats('', $link_id);
+ if( is_array( $categories ) ) {
+ foreach ( $categories as $cat_id ) {
+ $wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$cat_id'");
+ wp_cache_delete($cat_id, 'category');
+ }
+ }
+
+ $wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = $link_id");
return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
}
+function wp_get_link_cats($link_ID = 0) {
+ global $wpdb;
+
+ $sql = "SELECT category_id
+ FROM $wpdb->link2cat
+ WHERE link_id = $link_ID
+ ORDER BY category_id";
+
+ $result = $wpdb->get_col($sql);
+
+ if ( !$result )
+ $result = array();
+
+ return array_unique($result);
+}
+
+function wp_set_link_cats($link_ID = 0, $link_categories = array()) {
+ global $wpdb;
+ // 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_category'));
+
+ $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) {
+ $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) {
+ $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');
+ }
+} // wp_set_link_cats()
+
function post_exists($title, $content = '', $post_date = '') {
global $wpdb;
Index: wp-admin/edit-link-form.php
===================================================================
--- wp-admin/edit-link-form.php (revision 3568)
+++ wp-admin/edit-link-form.php (working copy)
@@ -1,14 +1,12 @@
';
+ $form = '