root/tags/2.1/wp-includes/bookmark.php

Revision 4529, 4.1 kB (checked in by markjaquith, 2 years ago)

whitespace tidying by Viper007Bond. fixes #3385

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