Add edit and delete links to the Content->Links rows. Add get_edit_bookmark_link(). Add caching for individual bookmarks. see #7552
git-svn-id: http://svn.automattic.com/wordpress/trunk@8758 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
6e95d9a803
commit
bcdb29372d
|
@ -51,6 +51,8 @@ function wp_delete_link($link_id) {
|
|||
|
||||
do_action('deleted_link', $link_id);
|
||||
|
||||
clean_bookmark_cache($link_id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -65,7 +67,7 @@ function get_link_to_edit( $link_id ) {
|
|||
return get_bookmark( $link_id, OBJECT, 'edit' );
|
||||
}
|
||||
|
||||
function wp_insert_link($linkdata) {
|
||||
function wp_insert_link($linkdata, $wp_error = false) {
|
||||
global $wpdb, $current_user;
|
||||
|
||||
$defaults = array('link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );
|
||||
|
@ -119,14 +121,24 @@ function wp_insert_link($linkdata) {
|
|||
}
|
||||
|
||||
if ( $update ) {
|
||||
$wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_url = %s,
|
||||
if ( false === $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_url = %s,
|
||||
link_name = %s, link_image = %s, link_target = %s,
|
||||
link_visible = %s, link_description = %s, link_rating = %s,
|
||||
link_rel = %s, link_notes = %s, link_rss = %s
|
||||
WHERE link_id = %s", $link_url, $link_name, $link_image, $link_target, $link_visible, $link_description, $link_rating, $link_rel, $link_notes, $link_rss, $link_id) );
|
||||
WHERE link_id = %s", $link_url, $link_name, $link_image, $link_target, $link_visible, $link_description, $link_rating, $link_rel, $link_notes, $link_rss, $link_id) ) ) {
|
||||
if ( $wp_error )
|
||||
return new WP_Error('db_update_error', __('Could not update link in the database'), $wpdb->last_error);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
$wpdb->query( $wpdb->prepare("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(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
||||
$link_url,$link_name, $link_image, $link_target, $link_description, $link_visible, $link_owner, $link_rating, $link_rel, $link_notes, $link_rss) );
|
||||
if ( false === $wpdb->query( $wpdb->prepare("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(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
||||
$link_url,$link_name, $link_image, $link_target, $link_description, $link_visible, $link_owner, $link_rating, $link_rel, $link_notes, $link_rss) ) ) {
|
||||
if ( $wp_error )
|
||||
return new WP_Error('db_insert_error', __('Could not insert link into the database'), $wpdb->last_error);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
$link_id = (int) $wpdb->insert_id;
|
||||
}
|
||||
|
||||
|
@ -137,6 +149,8 @@ function wp_insert_link($linkdata) {
|
|||
else
|
||||
do_action('add_link', $link_id);
|
||||
|
||||
clean_bookmark_cache($link_id);
|
||||
|
||||
return $link_id;
|
||||
}
|
||||
|
||||
|
@ -149,6 +163,8 @@ function wp_set_link_cats($link_id = 0, $link_categories = array()) {
|
|||
$link_categories = array_unique($link_categories);
|
||||
|
||||
wp_set_object_terms($link_id, $link_categories, 'link_category');
|
||||
|
||||
clean_bookmark_cache($link_id);
|
||||
} // wp_set_link_cats()
|
||||
|
||||
function wp_update_link($linkdata) {
|
||||
|
|
|
@ -163,7 +163,7 @@ if ( $links ) {
|
|||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$i = 0; // It is slower incrementing an undefined and valueless variable.
|
||||
$alt = 0;
|
||||
|
||||
foreach ($links as $link) {
|
||||
$link = sanitize_bookmark($link);
|
||||
|
@ -175,18 +175,28 @@ if ( $links ) {
|
|||
$short_url = substr($short_url, 0, -1);
|
||||
if (strlen($short_url) > 35)
|
||||
$short_url = substr($short_url, 0, 32).'...';
|
||||
|
||||
$visible = ($link->link_visible == 'Y') ? __('Yes') : __('No');
|
||||
++ $i;
|
||||
$style = ($i % 2) ? '' : ' class="alternate"';
|
||||
$style = ($alt % 2) ? '' : ' class="alternate"';
|
||||
++ $alt;
|
||||
$edit_link = get_edit_bookmark_link();
|
||||
?><tr id="link-<?php echo $link->link_id; ?>" valign="middle" <?php echo $style; ?>><?php
|
||||
echo '<th scope="row" class="check-column"><input type="checkbox" name="linkcheck[]" value="'.$link->link_id.'" /></th>';
|
||||
foreach($link_columns as $column_name=>$column_display_name) {
|
||||
switch($column_name) {
|
||||
case 'name':
|
||||
|
||||
echo "<td><strong><a class='row-title' href='link.php?link_id=$link->link_id&action=edit' title='" . attribute_escape(sprintf(__('Edit "%s"'), $link->link_name)) . "' class='edit'>$link->link_name</a></strong><br />";
|
||||
echo $link->link_description . "</td>";
|
||||
echo "<td><strong><a class='row-title' href='$edit_link' title='" . attribute_escape(sprintf(__('Edit "%s"'), $link->link_name)) . "' class='edit'>$link->link_name</a></strong><br />";
|
||||
$actions = array();
|
||||
$actions['edit'] = '<a href="' . $edit_link . '">' . __('Edit') . '</a>';
|
||||
$actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url("link.php?action=delete&link_id=$link->link_id", 'delete-bookmark_' . $link->link_id) . "' onclick=\"if ( confirm('" . js_escape(sprintf( __("You are about to delete this link '%s'\n 'Cancel' to stop, 'OK' to delete."), $link->link_name )) . "') ) { return true;}return false;\">" . __('Delete') . "</a>";
|
||||
$action_count = count($actions);
|
||||
$i = 0;
|
||||
foreach ( $actions as $action => $link ) {
|
||||
++$i;
|
||||
( $i == $action_count ) ? $sep = '' : $sep = ' | ';
|
||||
echo "<span class='$action'>$link$sep</span>";
|
||||
}
|
||||
echo '</td>';
|
||||
break;
|
||||
case 'url':
|
||||
echo "<td><a href='$link->link_url' title='".sprintf(__('Visit %s'), $link->link_name)."'>$short_url</a></td>";
|
||||
|
|
|
@ -17,22 +17,37 @@
|
|||
* @param string $filter Optional, default is 'raw'.
|
||||
* @return array|object Type returned depends on $output value.
|
||||
*/
|
||||
function get_bookmark($bookmark_id, $output = OBJECT, $filter = 'raw') {
|
||||
function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
|
||||
global $wpdb;
|
||||
|
||||
$link = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark_id));
|
||||
$link->link_category = array_unique( wp_get_object_terms($link->link_id, 'link_category', 'fields=ids') );
|
||||
if ( empty($bookmark) ) {
|
||||
if ( isset($GLOBALS['link']) )
|
||||
$_bookmark = & $GLOBALS['link'];
|
||||
else
|
||||
$_bookmark = null;
|
||||
} elseif ( is_object($bookmark) ) {
|
||||
wp_cache_add($bookmark->link_id, $bookmark, 'bookmark');
|
||||
$_bookmark = $bookmark;
|
||||
} else {
|
||||
if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $link) ) {
|
||||
$_bookmark = & $GLOBALS['link'];
|
||||
} elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) {
|
||||
$_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));
|
||||
$_bookmark->link_category = array_unique( wp_get_object_terms($_bookmark->link_id, 'link_category', 'fields=ids') );
|
||||
wp_cache_add($_bookmark->link_id, $_bookmark, 'bookmark');
|
||||
}
|
||||
}
|
||||
|
||||
$link = sanitize_bookmark($link, $filter);
|
||||
$_bookmark = sanitize_bookmark($_bookmark, $filter);
|
||||
|
||||
if ( $output == OBJECT ) {
|
||||
return $link;
|
||||
return $_bookmark;
|
||||
} elseif ( $output == ARRAY_A ) {
|
||||
return get_object_vars($link);
|
||||
return get_object_vars($_bookmark);
|
||||
} elseif ( $output == ARRAY_N ) {
|
||||
return array_values(get_object_vars($link));
|
||||
return array_values(get_object_vars($_bookmark));
|
||||
} else {
|
||||
return $link;
|
||||
return $_bookmark;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -331,16 +346,14 @@ function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Deletes entire bookmark cache
|
||||
* Deletes bookmark cache
|
||||
*
|
||||
* @since 2.1.0
|
||||
* @since 2.7.0
|
||||
* @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
|
||||
*/
|
||||
function delete_get_bookmark_cache() {
|
||||
function clean_bookmark_cache($bookmark_id) {
|
||||
wp_cache_delete( $bookmark_id, 'bookmark' );
|
||||
wp_cache_delete( 'get_bookmarks', 'bookmark' );
|
||||
}
|
||||
add_action( 'add_link', 'delete_get_bookmark_cache' );
|
||||
add_action( 'edit_link', 'delete_get_bookmark_cache' );
|
||||
add_action( 'delete_link', 'delete_get_bookmark_cache' );
|
||||
|
||||
?>
|
||||
|
|
|
@ -547,6 +547,29 @@ function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) {
|
|||
echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
|
||||
}
|
||||
|
||||
function get_edit_bookmark_link( $link = 0 ) {
|
||||
$link = &get_bookmark( $link );
|
||||
|
||||
if ( !current_user_can('manage_links') )
|
||||
return;
|
||||
|
||||
$location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
|
||||
return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
|
||||
}
|
||||
|
||||
function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
|
||||
$bookmark = get_bookmark($bookmark);
|
||||
|
||||
if ( !current_user_can('manage_links') )
|
||||
return;
|
||||
|
||||
if ( empty($link) )
|
||||
$link = __('Edit This');
|
||||
|
||||
$link = '<a href="' . get_edit_bookmark_link( $link ) . '" title="' . __( 'Edit link' ) . '">' . $link . '</a>';
|
||||
echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
|
||||
}
|
||||
|
||||
// Navigation links
|
||||
|
||||
function get_previous_post($in_same_cat = false, $excluded_categories = '') {
|
||||
|
|
Loading…
Reference in New Issue