| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
function get_bookmark($bookmark_id, $output = OBJECT) { |
|---|
| 4 |
global $wpdb; |
|---|
| 5 |
|
|---|
| 6 |
$bookmark_id = (int) $bookmark_id; |
|---|
| 7 |
$link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$bookmark_id'"); |
|---|
| 8 |
$link->link_category = wp_get_link_cats($bookmark_id); |
|---|
| 9 |
|
|---|
| 10 |
if ( $output == OBJECT ) { |
|---|
| 11 |
return $link; |
|---|
| 12 |
} elseif ( $output == ARRAY_A ) { |
|---|
| 13 |
return get_object_vars($link); |
|---|
| 14 |
} elseif ( $output == ARRAY_N ) { |
|---|
| 15 |
return array_values(get_object_vars($link)); |
|---|
| 16 |
} else { |
|---|
| 17 |
return $link; |
|---|
| 18 |
} |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
function get_link($bookmark_id, $output = OBJECT) { |
|---|
| 23 |
return get_bookmark($bookmark_id, $output); |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
function get_bookmarks($args = '') { |
|---|
| 27 |
global $wpdb; |
|---|
| 28 |
|
|---|
| 29 |
if ( is_array($args) ) |
|---|
| 30 |
$r = &$args; |
|---|
| 31 |
else |
|---|
| 32 |
parse_str($args, $r); |
|---|
| 33 |
|
|---|
| 34 |
$defaults = array('orderby' => 'name', 'order' => 'ASC', 'limit' => -1, 'category' => '', |
|---|
| 35 |
'category_name' => '', 'hide_invisible' => 1, 'show_updated' => 0, 'include' => '', 'exclude' => ''); |
|---|
| 36 |
$r = array_merge($defaults, $r); |
|---|
| 37 |
extract($r); |
|---|
| 38 |
|
|---|
| 39 |
$key = md5( serialize( $r ) ); |
|---|
| 40 |
if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) |
|---|
| 41 |
if ( isset( $cache[ $key ] ) ) |
|---|
| 42 |
return apply_filters('get_bookmarks', $cache[ $key ], $r ); |
|---|
| 43 |
|
|---|
| 44 |
$inclusions = ''; |
|---|
| 45 |
if ( !empty($include) ) { |
|---|
| 46 |
$exclude = ''; |
|---|
| 47 |
$category = ''; |
|---|
| 48 |
$category_name = ''; |
|---|
| 49 |
$inclinks = preg_split('/[\s,]+/',$include); |
|---|
| 50 |
if ( count($inclinks) ) { |
|---|
| 51 |
foreach ( $inclinks as $inclink ) { |
|---|
| 52 |
if (empty($inclusions)) |
|---|
| 53 |
$inclusions = ' AND ( link_id = ' . intval($inclink) . ' '; |
|---|
| 54 |
else |
|---|
| 55 |
$inclusions .= ' OR link_id = ' . intval($inclink) . ' '; |
|---|
| 56 |
} |
|---|
| 57 |
} |
|---|
| 58 |
} |
|---|
| 59 |
if (!empty($inclusions)) |
|---|
| 60 |
$inclusions .= ')'; |
|---|
| 61 |
|
|---|
| 62 |
$exclusions = ''; |
|---|
| 63 |
if ( !empty($exclude) ) { |
|---|
| 64 |
$exlinks = preg_split('/[\s,]+/',$exclude); |
|---|
| 65 |
if ( count($exlinks) ) { |
|---|
| 66 |
foreach ( $exlinks as $exlink ) { |
|---|
| 67 |
if (empty($exclusions)) |
|---|
| 68 |
$exclusions = ' AND ( link_id <> ' . intval($exlink) . ' '; |
|---|
| 69 |
else |
|---|
| 70 |
$exclusions .= ' AND link_id <> ' . intval($exlink) . ' '; |
|---|
| 71 |
} |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|
| 74 |
if (!empty($exclusions)) |
|---|
| 75 |
$exclusions .= ')'; |
|---|
| 76 |
|
|---|
| 77 |
if ( ! empty($category_name) ) { |
|---|
| 78 |
if ( $cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category_name' LIMIT 1") ) |
|---|
| 79 |
$category = $cat_id; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
$category_query = ''; |
|---|
| 83 |
$join = ''; |
|---|
| 84 |
if ( !empty($category) ) { |
|---|
| 85 |
$incategories = preg_split('/[\s,]+/',$category); |
|---|
| 86 |
if ( count($incategories) ) { |
|---|
| 87 |
foreach ( $incategories as $incat ) { |
|---|
| 88 |
if (empty($category_query)) |
|---|
| 89 |
$category_query = ' AND ( category_id = ' . intval($incat) . ' '; |
|---|
| 90 |
else |
|---|
| 91 |
$category_query .= ' OR category_id = ' . intval($incat) . ' '; |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|
| 95 |
if (!empty($category_query)) { |
|---|
| 96 |
$category_query .= ')'; |
|---|
| 97 |
$join = " LEFT JOIN $wpdb->link2cat ON ($wpdb->links.link_id = $wpdb->link2cat.link_id) "; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
if (get_option('links_recently_updated_time')) { |
|---|
| 101 |
$recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated "; |
|---|
| 102 |
} else { |
|---|
| 103 |
$recently_updated_test = ''; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
if ($show_updated) { |
|---|
| 107 |
$get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f "; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
$orderby = strtolower($orderby); |
|---|
| 111 |
$length = ''; |
|---|
| 112 |
switch ($orderby) { |
|---|
| 113 |
case 'length': |
|---|
| 114 |
$length = ", CHAR_LENGTH(link_name) AS length"; |
|---|
| 115 |
break; |
|---|
| 116 |
case 'rand': |
|---|
| 117 |
$orderby = 'rand()'; |
|---|
| 118 |
break; |
|---|
| 119 |
default: |
|---|
| 120 |
$orderby = "link_" . $orderby; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
if ( 'link_id' == $orderby ) |
|---|
| 124 |
$orderby = "$wpdb->links.link_id"; |
|---|
| 125 |
|
|---|
| 126 |
$visible = ''; |
|---|
| 127 |
if ( $hide_invisible ) |
|---|
| 128 |
$visible = "AND link_visible = 'Y'"; |
|---|
| 129 |
|
|---|
| 130 |
$query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; |
|---|
| 131 |
$query .= " $exclusions $inclusions"; |
|---|
| 132 |
$query .= " ORDER BY $orderby $order"; |
|---|
| 133 |
if ($limit != -1) |
|---|
| 134 |
$query .= " LIMIT $limit"; |
|---|
| 135 |
|
|---|
| 136 |
$results = $wpdb->get_results($query); |
|---|
| 137 |
|
|---|
| 138 |
$cache[ $key ] = $results; |
|---|
| 139 |
wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
|---|
| 140 |
|
|---|
| 141 |
return apply_filters('get_bookmarks', $results, $r); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
function delete_get_bookmark_cache() { |
|---|
| 145 |
wp_cache_delete( 'get_bookmarks', 'bookmark' ); |
|---|
| 146 |
} |
|---|
| 147 |
add_action( 'add_link', 'delete_get_bookmark_cache' ); |
|---|
| 148 |
add_action( 'edit_link', 'delete_get_bookmark_cache' ); |
|---|
| 149 |
add_action( 'delete_link', 'delete_get_bookmark_cache' ); |
|---|
| 150 |
|
|---|
| 151 |
?> |
|---|