WordPress.org

Make WordPress Core

Ticket #14511: wp-get-sites.2.php

File wp-get-sites.2.php, 4.4 KB (added by transom, 4 years ago)

Standalone version of wp_get_sites() - toss in functions.php to test

Line 
1if( !function_exists('wp_get_sites') ){
2/**
3 * Return a list of sites for the current network
4 *
5 * @since 3.1.0
6 *
7 * @param array|string $args Optional. Override default arguments.
8 * @return array site list and values
9 */
10function wp_get_sites($args=array()){
11// replacement for wp-includes/ms-deprecated.php#get_blog_list
12// see wp-admin/ms-sites.php#352
13//  also wp-includes/ms-functions.php#get_blogs_of_user
14//  also wp-includes/post-template.php#wp_list_pages
15        global $wpdb;
16       
17        $defaults = array(
18                'include_id'            =>'',                   // includes only these sites in the results, comma-delimited
19                'exclude_id'            =>'',                   // excludes these sites from the results, comma-delimted
20                'blogname_like'         =>'',                   // domain or path is like this value
21                'ip_like'                       =>'',                   // Match IP address
22                'reg_date_since'        =>'',                   // sites registered since (accepts pretty much any valid date like tomorrow, today, 5/12/2009, etc.)
23                'reg_date_before'       =>'',                   // sites registered before
24                'include_user_id'       =>'',                   // only sites owned by these users, comma-delimited
25                'exclude_user_id'       =>'',                   // don't include sites owned by these users, comma-delimited
26                'include_spam'          => false,               // Include sites marked as "spam"
27                'include_deleted'       => false,               // Include deleted sites
28                'include_archived'      => false,               // Include archived sites
29                'include_mature'        => false,               // Included blogs marked as mature
30                'public_only'           => true,                // Include only blogs marked as public
31                'sort_column'           => 'registered',// or registered, last_updated, blogname, site_id.
32                'order'                         => 'asc',               // or desc
33                'limit_results'         => '',                  // return this many results
34                'start'                         => '',                  // return results starting with this item
35        );
36        if( !function_exists('make_email_list_by_user_id')){
37                function make_email_list_by_user_id($user_ids){
38                        $the_users = explode(',',$user_ids);
39                        $the_emails = array();
40                        foreach( (array) $the_users as $user_id){
41                                $the_user = get_userdata($user_id);
42                                $the_emails[] = $the_user->user_email;
43                        }
44                        return $the_emails;
45                }
46        }
47       
48        // array_merge
49        $r = wp_parse_args( $args, $defaults );
50        extract( $r, EXTR_SKIP );
51
52        $query = "SELECT * FROM {$wpdb->blogs} as b ";
53        $query .= "LEFT JOIN {$wpdb->registration_log} as l ON b.`blog_id` = l.`blog_id` ";
54        $query .= "WHERE b.`site_id` = '{$wpdb->site_id}' ";
55
56        if ( !empty($include_id) ) {
57                $list = implode("','", explode(',', $include_id));
58                $query .= " AND b.blog_id IN ('{$list}') ";
59        }
60        if ( !empty($exclude_id) ) {
61                $list = implode("','", explode(',', $exclude_id));
62                $query .= " AND b.blog_id NOT IN ('{$list}') ";
63        }
64        if ( !empty($blogname_like) ) {
65                $query .= " AND ( b.domain LIKE '%".$blogname_like."%' OR b.path LIKE '%".$blogname_like."%' ) ";
66        }
67        if ( !empty($ip_like) ) {
68                $query .= " AND l.IP LIKE '%".$ip_like."%' ";
69        }
70        if( !empty($reg_date_since) ){
71                $query .= " AND unix_timestamp(b.date_registered) > '".strtotime($reg_date_since)."' ";
72        }
73        if( !empty($reg_date_before) ){
74                $query .= " AND unix_timestamp(b.date_registered) < '".strtotime($reg_date_before)."' ";
75        }
76        if ( !empty($include_user_id) ) {
77                $the_emails = make_email_list_by_user_id($include_user_id);
78                $list = implode("','", $the_emails);
79                $query .= " AND l.email IN ('{$list}') ";
80        }
81        if ( !empty($exclude_user_id) ) {
82                $the_emails = make_email_list_by_user_id($include_user_id);
83                $list = implode("','", $the_emails);
84                $query .= " AND l.email NOT IN ('{$list}') ";
85        }
86        if ( !empty($ip_like) ) {
87                $query .= " AND l.IP LIKE ('%".$ip_like."%') ";
88        }
89       
90        $query .= " AND b.public = ". (($public_only) ? "'1'" : "'0'");
91        $query .= " AND b.archived = ". (($include_archived) ? "'1'" : "'0'");
92        $query .= " AND b.mature = ". (($include_mature) ? "'1'" : "'0'");
93        $query .= " AND b.spam = ". (($include_spam) ? "'1'" : "'0'");
94        $query .= " AND b.deleted = ". (($include_deleted) ? "'1'" : "'0'");
95       
96        if ( $sort_column == 'site_id' ) {
97                $query .= ' ORDER BY b.`blog_id` ';
98        } elseif ( $sort_column == 'lastupdated' ) {
99                $query .= ' ORDER BY b.`last_updated` ';
100        } elseif ( $sort_column == 'blogname' ) {
101                $query .= ' ORDER BY b.`domain` ';
102        } else {
103                $sort_column = 'registered';
104                $query .= " ORDER BY b.`registered` ";
105        }
106
107        $order = ( 'desc' == $order ) ? "DESC" : "ASC";
108        $query .= $order;
109       
110        $limit = '';
111        if( !empty($limit_results) ){
112                if( !empty($start) ){
113                        $limit = $start." , ";
114                }
115                $query .= "LIMIT ".$limit.$limit_results;
116        }
117       
118        $sql = $wpdb->prepare($query);
119
120        $results = $wpdb->get_results($sql, ARRAY_A);
121       
122        return $results;       
123}
124}