| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
function get_bookmark($bookmark_id, $output = OBJECT, $filter = 'raw') { |
|---|
| 21 |
global $wpdb; |
|---|
| 22 |
|
|---|
| 23 |
$link = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark_id)); |
|---|
| 24 |
$link->link_category = array_unique( wp_get_object_terms($link->link_id, 'link_category', 'fields=ids') ); |
|---|
| 25 |
|
|---|
| 26 |
$link = sanitize_bookmark($link, $filter); |
|---|
| 27 |
|
|---|
| 28 |
if ( $output == OBJECT ) { |
|---|
| 29 |
return $link; |
|---|
| 30 |
} elseif ( $output == ARRAY_A ) { |
|---|
| 31 |
return get_object_vars($link); |
|---|
| 32 |
} elseif ( $output == ARRAY_N ) { |
|---|
| 33 |
return array_values(get_object_vars($link)); |
|---|
| 34 |
} else { |
|---|
| 35 |
return $link; |
|---|
| 36 |
} |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
function get_bookmark_field( $field, $bookmark, $context = 'display' ) { |
|---|
| 52 |
$bookmark = (int) $bookmark; |
|---|
| 53 |
$bookmark = get_bookmark( $bookmark ); |
|---|
| 54 |
|
|---|
| 55 |
if ( is_wp_error($bookmark) ) |
|---|
| 56 |
return $bookmark; |
|---|
| 57 |
|
|---|
| 58 |
if ( !is_object($bookmark) ) |
|---|
| 59 |
return ''; |
|---|
| 60 |
|
|---|
| 61 |
if ( !isset($bookmark->$field) ) |
|---|
| 62 |
return ''; |
|---|
| 63 |
|
|---|
| 64 |
return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
function get_link($bookmark_id, $output = OBJECT, $filter = 'raw') { |
|---|
| 79 |
return get_bookmark($bookmark_id, $output, $filter); |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
function get_bookmarks($args = '') { |
|---|
| 117 |
global $wpdb; |
|---|
| 118 |
|
|---|
| 119 |
$defaults = array( |
|---|
| 120 |
'orderby' => 'name', 'order' => 'ASC', |
|---|
| 121 |
'limit' => -1, 'category' => '', |
|---|
| 122 |
'category_name' => '', 'hide_invisible' => 1, |
|---|
| 123 |
'show_updated' => 0, 'include' => '', |
|---|
| 124 |
'exclude' => '', 'search' => '' |
|---|
| 125 |
); |
|---|
| 126 |
|
|---|
| 127 |
$r = wp_parse_args( $args, $defaults ); |
|---|
| 128 |
extract( $r, EXTR_SKIP ); |
|---|
| 129 |
|
|---|
| 130 |
$key = md5( serialize( $r ) ); |
|---|
| 131 |
if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) |
|---|
| 132 |
if ( isset( $cache[ $key ] ) ) |
|---|
| 133 |
return apply_filters('get_bookmarks', $cache[ $key ], $r ); |
|---|
| 134 |
|
|---|
| 135 |
$inclusions = ''; |
|---|
| 136 |
if ( !empty($include) ) { |
|---|
| 137 |
$exclude = ''; |
|---|
| 138 |
$category = ''; |
|---|
| 139 |
$category_name = ''; |
|---|
| 140 |
$inclinks = preg_split('/[\s,]+/',$include); |
|---|
| 141 |
if ( count($inclinks) ) { |
|---|
| 142 |
foreach ( $inclinks as $inclink ) { |
|---|
| 143 |
if (empty($inclusions)) |
|---|
| 144 |
$inclusions = ' AND ( link_id = ' . intval($inclink) . ' '; |
|---|
| 145 |
else |
|---|
| 146 |
$inclusions .= ' OR link_id = ' . intval($inclink) . ' '; |
|---|
| 147 |
} |
|---|
| 148 |
} |
|---|
| 149 |
} |
|---|
| 150 |
if (!empty($inclusions)) |
|---|
| 151 |
$inclusions .= ')'; |
|---|
| 152 |
|
|---|
| 153 |
$exclusions = ''; |
|---|
| 154 |
if ( !empty($exclude) ) { |
|---|
| 155 |
$exlinks = preg_split('/[\s,]+/',$exclude); |
|---|
| 156 |
if ( count($exlinks) ) { |
|---|
| 157 |
foreach ( $exlinks as $exlink ) { |
|---|
| 158 |
if (empty($exclusions)) |
|---|
| 159 |
$exclusions = ' AND ( link_id <> ' . intval($exlink) . ' '; |
|---|
| 160 |
else |
|---|
| 161 |
$exclusions .= ' AND link_id <> ' . intval($exlink) . ' '; |
|---|
| 162 |
} |
|---|
| 163 |
} |
|---|
| 164 |
} |
|---|
| 165 |
if (!empty($exclusions)) |
|---|
| 166 |
$exclusions .= ')'; |
|---|
| 167 |
|
|---|
| 168 |
if ( ! empty($category_name) ) { |
|---|
| 169 |
if ( $category = get_term_by('name', $category_name, 'link_category') ) |
|---|
| 170 |
$category = $category->term_id; |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
if ( ! empty($search) ) { |
|---|
| 174 |
$search = like_escape($search); |
|---|
| 175 |
$search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) "; |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
$category_query = ''; |
|---|
| 179 |
$join = ''; |
|---|
| 180 |
if ( !empty($category) ) { |
|---|
| 181 |
$incategories = preg_split('/[\s,]+/',$category); |
|---|
| 182 |
if ( count($incategories) ) { |
|---|
| 183 |
foreach ( $incategories as $incat ) { |
|---|
| 184 |
if (empty($category_query)) |
|---|
| 185 |
$category_query = ' AND ( tt.term_id = ' . intval($incat) . ' '; |
|---|
| 186 |
else |
|---|
| 187 |
$category_query .= ' OR tt.term_id = ' . intval($incat) . ' '; |
|---|
| 188 |
} |
|---|
| 189 |
} |
|---|
| 190 |
} |
|---|
| 191 |
if (!empty($category_query)) { |
|---|
| 192 |
$category_query .= ") AND taxonomy = 'link_category'"; |
|---|
| 193 |
$join = " INNER JOIN $wpdb->term_relationships AS tr ON ($wpdb->links.link_id = tr.object_id) INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id"; |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
if (get_option('links_recently_updated_time')) { |
|---|
| 197 |
$recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated "; |
|---|
| 198 |
} else { |
|---|
| 199 |
$recently_updated_test = ''; |
|---|
| 200 |
} |
|---|
| 201 |
|
|---|
| 202 |
$get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : ''; |
|---|
| 203 |
|
|---|
| 204 |
$orderby = strtolower($orderby); |
|---|
| 205 |
$length = ''; |
|---|
| 206 |
switch ($orderby) { |
|---|
| 207 |
case 'length': |
|---|
| 208 |
$length = ", CHAR_LENGTH(link_name) AS length"; |
|---|
| 209 |
break; |
|---|
| 210 |
case 'rand': |
|---|
| 211 |
$orderby = 'rand()'; |
|---|
| 212 |
break; |
|---|
| 213 |
default: |
|---|
| 214 |
$orderby = "link_" . $orderby; |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
if ( 'link_id' == $orderby ) |
|---|
| 218 |
$orderby = "$wpdb->links.link_id"; |
|---|
| 219 |
|
|---|
| 220 |
$visible = ''; |
|---|
| 221 |
if ( $hide_invisible ) |
|---|
| 222 |
$visible = "AND link_visible = 'Y'"; |
|---|
| 223 |
|
|---|
| 224 |
$query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; |
|---|
| 225 |
$query .= " $exclusions $inclusions $search"; |
|---|
| 226 |
$query .= " ORDER BY $orderby $order"; |
|---|
| 227 |
if ($limit != -1) |
|---|
| 228 |
$query .= " LIMIT $limit"; |
|---|
| 229 |
|
|---|
| 230 |
$results = $wpdb->get_results($query); |
|---|
| 231 |
|
|---|
| 232 |
$cache[ $key ] = $results; |
|---|
| 233 |
wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
|---|
| 234 |
|
|---|
| 235 |
return apply_filters('get_bookmarks', $results, $r); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
function sanitize_bookmark($bookmark, $context = 'display') { |
|---|
| 249 |
$fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category', |
|---|
| 250 |
'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated', |
|---|
| 251 |
'link_rel', 'link_notes', 'link_rss', ); |
|---|
| 252 |
|
|---|
| 253 |
$do_object = false; |
|---|
| 254 |
if ( is_object($bookmark) ) |
|---|
| 255 |
$do_object = true; |
|---|
| 256 |
|
|---|
| 257 |
foreach ( $fields as $field ) { |
|---|
| 258 |
if ( $do_object ) |
|---|
| 259 |
$bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context); |
|---|
| 260 |
else |
|---|
| 261 |
$bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $bookmark['link_id'], $context); |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
return $bookmark; |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
function sanitize_bookmark_field($field, $value, $bookmark_id, $context) { |
|---|
| 292 |
$int_fields = array('link_id', 'link_rating'); |
|---|
| 293 |
if ( in_array($field, $int_fields) ) |
|---|
| 294 |
$value = (int) $value; |
|---|
| 295 |
|
|---|
| 296 |
$yesno = array('link_visible'); |
|---|
| 297 |
if ( in_array($field, $yesno) ) |
|---|
| 298 |
$value = preg_replace('/[^YNyn]/', '', $value); |
|---|
| 299 |
|
|---|
| 300 |
if ( 'link_target' == $field ) { |
|---|
| 301 |
$targets = array('_top', '_blank'); |
|---|
| 302 |
if ( ! in_array($value, $targets) ) |
|---|
| 303 |
$value = ''; |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
if ( 'raw' == $context ) |
|---|
| 307 |
return $value; |
|---|
| 308 |
|
|---|
| 309 |
if ( 'edit' == $context ) { |
|---|
| 310 |
$format_to_edit = array('link_notes'); |
|---|
| 311 |
$value = apply_filters("edit_$field", $value, $bookmark_id); |
|---|
| 312 |
|
|---|
| 313 |
if ( in_array($field, $format_to_edit) ) { |
|---|
| 314 |
$value = format_to_edit($value); |
|---|
| 315 |
} else { |
|---|
| 316 |
$value = attribute_escape($value); |
|---|
| 317 |
} |
|---|
| 318 |
} else if ( 'db' == $context ) { |
|---|
| 319 |
$value = apply_filters("pre_$field", $value); |
|---|
| 320 |
} else { |
|---|
| 321 |
|
|---|
| 322 |
$value = apply_filters($field, $value, $bookmark_id, $context); |
|---|
| 323 |
} |
|---|
| 324 |
|
|---|
| 325 |
if ( 'attribute' == $context ) |
|---|
| 326 |
$value = attribute_escape($value); |
|---|
| 327 |
else if ( 'js' == $context ) |
|---|
| 328 |
$value = js_escape($value); |
|---|
| 329 |
|
|---|
| 330 |
return $value; |
|---|
| 331 |
} |
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
function delete_get_bookmark_cache() { |
|---|
| 340 |
wp_cache_delete( 'get_bookmarks', 'bookmark' ); |
|---|
| 341 |
} |
|---|
| 342 |
add_action( 'add_link', 'delete_get_bookmark_cache' ); |
|---|
| 343 |
add_action( 'edit_link', 'delete_get_bookmark_cache' ); |
|---|
| 344 |
add_action( 'delete_link', 'delete_get_bookmark_cache' ); |
|---|
| 345 |
|
|---|
| 346 |
?> |
|---|
| 347 |
|
|---|