| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
function get_bookmark($bookmark_id, $output = OBJECT, $filter = 'raw') { |
|---|
| 4 |
global $wpdb; |
|---|
| 5 |
|
|---|
| 6 |
$bookmark_id = (int) $bookmark_id; |
|---|
| 7 |
$link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$bookmark_id' LIMIT 1"); |
|---|
| 8 |
$link->link_category = array_unique( wp_get_object_terms($link_id, 'link_category', 'fields=ids') ); |
|---|
| 9 |
|
|---|
| 10 |
$link = sanitize_bookmark($link, $filter); |
|---|
| 11 |
|
|---|
| 12 |
if ( $output == OBJECT ) { |
|---|
| 13 |
return $link; |
|---|
| 14 |
} elseif ( $output == ARRAY_A ) { |
|---|
| 15 |
return get_object_vars($link); |
|---|
| 16 |
} elseif ( $output == ARRAY_N ) { |
|---|
| 17 |
return array_values(get_object_vars($link)); |
|---|
| 18 |
} else { |
|---|
| 19 |
return $link; |
|---|
| 20 |
} |
|---|
| 21 |
} |
|---|
| 22 |
|
|---|
| 23 |
function get_bookmark_field( $field, $bookmark, $context = 'display' ) { |
|---|
| 24 |
$bookmark = (int) $bookmark; |
|---|
| 25 |
$bookmark = get_bookmark( $bookmark ); |
|---|
| 26 |
|
|---|
| 27 |
if ( is_wp_error($bookmark) ) |
|---|
| 28 |
return $bookmark; |
|---|
| 29 |
|
|---|
| 30 |
if ( !is_object($bookmark) ) |
|---|
| 31 |
return ''; |
|---|
| 32 |
|
|---|
| 33 |
if ( !isset($bookmark->$field) ) |
|---|
| 34 |
return ''; |
|---|
| 35 |
|
|---|
| 36 |
return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
function get_link($bookmark_id, $output = OBJECT) { |
|---|
| 41 |
return get_bookmark($bookmark_id, $output); |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
function get_bookmarks($args = '') { |
|---|
| 45 |
global $wpdb; |
|---|
| 46 |
|
|---|
| 47 |
$defaults = array( |
|---|
| 48 |
'orderby' => 'name', 'order' => 'ASC', |
|---|
| 49 |
'limit' => -1, 'category' => '', |
|---|
| 50 |
'category_name' => '', 'hide_invisible' => 1, |
|---|
| 51 |
'show_updated' => 0, 'include' => '', |
|---|
| 52 |
'exclude' => '' |
|---|
| 53 |
); |
|---|
| 54 |
|
|---|
| 55 |
$r = wp_parse_args( $args, $defaults ); |
|---|
| 56 |
extract( $r, EXTR_SKIP ); |
|---|
| 57 |
|
|---|
| 58 |
$key = md5( serialize( $r ) ); |
|---|
| 59 |
if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) |
|---|
| 60 |
if ( isset( $cache[ $key ] ) ) |
|---|
| 61 |
return apply_filters('get_bookmarks', $cache[ $key ], $r ); |
|---|
| 62 |
|
|---|
| 63 |
$inclusions = ''; |
|---|
| 64 |
if ( !empty($include) ) { |
|---|
| 65 |
$exclude = ''; |
|---|
| 66 |
$category = ''; |
|---|
| 67 |
$category_name = ''; |
|---|
| 68 |
$inclinks = preg_split('/[\s,]+/',$include); |
|---|
| 69 |
if ( count($inclinks) ) { |
|---|
| 70 |
foreach ( $inclinks as $inclink ) { |
|---|
| 71 |
if (empty($inclusions)) |
|---|
| 72 |
$inclusions = ' AND ( link_id = ' . intval($inclink) . ' '; |
|---|
| 73 |
else |
|---|
| 74 |
$inclusions .= ' OR link_id = ' . intval($inclink) . ' '; |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
} |
|---|
| 78 |
if (!empty($inclusions)) |
|---|
| 79 |
$inclusions .= ')'; |
|---|
| 80 |
|
|---|
| 81 |
$exclusions = ''; |
|---|
| 82 |
if ( !empty($exclude) ) { |
|---|
| 83 |
$exlinks = preg_split('/[\s,]+/',$exclude); |
|---|
| 84 |
if ( count($exlinks) ) { |
|---|
| 85 |
foreach ( $exlinks as $exlink ) { |
|---|
| 86 |
if (empty($exclusions)) |
|---|
| 87 |
$exclusions = ' AND ( link_id <> ' . intval($exlink) . ' '; |
|---|
| 88 |
else |
|---|
| 89 |
$exclusions .= ' AND link_id <> ' . intval($exlink) . ' '; |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
if (!empty($exclusions)) |
|---|
| 94 |
$exclusions .= ')'; |
|---|
| 95 |
|
|---|
| 96 |
if ( ! empty($category_name) ) { |
|---|
| 97 |
if ( $category = get_term_by('name', $category_name, 'link_category') ) |
|---|
| 98 |
$category = $category->term_id; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
$category_query = ''; |
|---|
| 102 |
$join = ''; |
|---|
| 103 |
if ( !empty($category) ) { |
|---|
| 104 |
$incategories = preg_split('/[\s,]+/',$category); |
|---|
| 105 |
if ( count($incategories) ) { |
|---|
| 106 |
foreach ( $incategories as $incat ) { |
|---|
| 107 |
if (empty($category_query)) |
|---|
| 108 |
$category_query = ' AND ( tt.term_id = ' . intval($incat) . ' '; |
|---|
| 109 |
else |
|---|
| 110 |
$category_query .= ' OR tt.term_id = ' . intval($incat) . ' '; |
|---|
| 111 |
} |
|---|
| 112 |
} |
|---|
| 113 |
} |
|---|
| 114 |
if (!empty($category_query)) { |
|---|
| 115 |
$category_query .= ") AND taxonomy = 'link_category'"; |
|---|
| 116 |
$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"; |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
if (get_option('links_recently_updated_time')) { |
|---|
| 120 |
$recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated "; |
|---|
| 121 |
} else { |
|---|
| 122 |
$recently_updated_test = ''; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
if ($show_updated) { |
|---|
| 126 |
$get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f "; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
$orderby = strtolower($orderby); |
|---|
| 130 |
$length = ''; |
|---|
| 131 |
switch ($orderby) { |
|---|
| 132 |
case 'length': |
|---|
| 133 |
$length = ", CHAR_LENGTH(link_name) AS length"; |
|---|
| 134 |
break; |
|---|
| 135 |
case 'rand': |
|---|
| 136 |
$orderby = 'rand()'; |
|---|
| 137 |
break; |
|---|
| 138 |
default: |
|---|
| 139 |
$orderby = "link_" . $orderby; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
if ( 'link_id' == $orderby ) |
|---|
| 143 |
$orderby = "$wpdb->links.link_id"; |
|---|
| 144 |
|
|---|
| 145 |
$visible = ''; |
|---|
| 146 |
if ( $hide_invisible ) |
|---|
| 147 |
$visible = "AND link_visible = 'Y'"; |
|---|
| 148 |
|
|---|
| 149 |
$query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; |
|---|
| 150 |
$query .= " $exclusions $inclusions"; |
|---|
| 151 |
$query .= " ORDER BY $orderby $order"; |
|---|
| 152 |
if ($limit != -1) |
|---|
| 153 |
$query .= " LIMIT $limit"; |
|---|
| 154 |
|
|---|
| 155 |
$results = $wpdb->get_results($query); |
|---|
| 156 |
|
|---|
| 157 |
$cache[ $key ] = $results; |
|---|
| 158 |
wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
|---|
| 159 |
|
|---|
| 160 |
return apply_filters('get_bookmarks', $results, $r); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
function sanitize_bookmark($bookmark, $context = 'display') { |
|---|
| 164 |
$fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category', |
|---|
| 165 |
'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated', |
|---|
| 166 |
'link_rel', 'link_notes', 'link_rss', ); |
|---|
| 167 |
|
|---|
| 168 |
$do_object = false; |
|---|
| 169 |
if ( is_object($bookmark) ) |
|---|
| 170 |
$do_object = true; |
|---|
| 171 |
|
|---|
| 172 |
foreach ( $fields as $field ) { |
|---|
| 173 |
if ( $do_object ) |
|---|
| 174 |
$bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context); |
|---|
| 175 |
else |
|---|
| 176 |
$bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $bookmark['link_id'], $context); |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
return $bookmark; |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
function sanitize_bookmark_field($field, $value, $bookmark_id, $context) { |
|---|
| 183 |
$int_fields = array('link_id', 'link_rating'); |
|---|
| 184 |
if ( in_array($field, $int_fields) ) |
|---|
| 185 |
$value = (int) $value; |
|---|
| 186 |
|
|---|
| 187 |
$yesno = array('link_visible'); |
|---|
| 188 |
if ( in_array($field, $yesno) ) |
|---|
| 189 |
$value = preg_replace('/[^YNyn]/', '', $value); |
|---|
| 190 |
|
|---|
| 191 |
if ( 'link_target' == $field ) { |
|---|
| 192 |
$targets = array('_top', '_blank'); |
|---|
| 193 |
if ( ! in_array($value, $targets) ) |
|---|
| 194 |
$value = ''; |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
if ( 'raw' == $context ) |
|---|
| 198 |
return $value; |
|---|
| 199 |
|
|---|
| 200 |
if ( 'edit' == $context ) { |
|---|
| 201 |
$format_to_edit = array('link_notes'); |
|---|
| 202 |
$value = apply_filters("edit_$field", $value, $bookmark_id); |
|---|
| 203 |
|
|---|
| 204 |
if ( in_array($field, $format_to_edit) ) { |
|---|
| 205 |
$value = format_to_edit($value); |
|---|
| 206 |
} else { |
|---|
| 207 |
$value = attribute_escape($value); |
|---|
| 208 |
} |
|---|
| 209 |
} else if ( 'db' == $context ) { |
|---|
| 210 |
$value = apply_filters("pre_$field", $value); |
|---|
| 211 |
} else { |
|---|
| 212 |
|
|---|
| 213 |
$value = apply_filters($field, $value, $bookmark_id, $context); |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
if ( 'attribute' == $context ) |
|---|
| 217 |
$value = attribute_escape($value); |
|---|
| 218 |
else if ( 'js' == $context ) |
|---|
| 219 |
$value = js_escape($value); |
|---|
| 220 |
|
|---|
| 221 |
return $value; |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
function delete_get_bookmark_cache() { |
|---|
| 225 |
wp_cache_delete( 'get_bookmarks', 'bookmark' ); |
|---|
| 226 |
} |
|---|
| 227 |
add_action( 'add_link', 'delete_get_bookmark_cache' ); |
|---|
| 228 |
add_action( 'edit_link', 'delete_get_bookmark_cache' ); |
|---|
| 229 |
add_action( 'delete_link', 'delete_get_bookmark_cache' ); |
|---|
| 230 |
|
|---|
| 231 |
?> |
|---|
| 232 |
|
|---|