Make WordPress Core

Ticket #14511: wp-get-sites.php

File wp-get-sites.php, 4.5 KB (added by transom, 14 years ago)

Source for wp_get_sites

Line 
1<?php
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){
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        function make_email_list_by_user_id($user_ids){
37                $the_users = explode(',',$user_ids);
38                $the_emails = array();
39                foreach( (array) $the_users as $user_id){
40                        $the_user = get_userdata($user_id);
41                        $the_emails[] = $the_user->user_email;
42                }
43                return $the_emails;
44        }
45       
46
47        // array_merge
48        $r = wp_parse_args( $args, $defaults );
49        extract( $r, EXTR_SKIP );
50
51        $query = "SELECT * FROM {$wpdb->blogs}, {$wpdb->registration_log} WHERE site_id = '{$wpdb->siteid}' AND {$wpdb->blogs}.blog_id = {$wpdb->registration_log}.blog_id ";
52
53        if ( isset($include_id) ) {
54                $list = implode("','", explode(',', $include_id));
55                $query .= " AND {$wpdb->blogs}.blog_id IN ('{$list}') ";
56        }
57        if ( isset($exclude_id) ) {
58                $list = implode("','", explode(',', $exclude_id));
59                $query .= " AND {$wpdb->blogs}.blog_id NOT IN ('{$list}') ";
60        }
61        if ( isset($blogname_like) ) {
62                $query .= " AND ( {$wpdb->blogs}.domain LIKE '%".$blogname_like."%' OR {$wpdb->blogs}.path LIKE '%".$blogname_like."%' ) ";
63        }
64        if ( isset($ip_like) ) {
65                $query .= " AND {$wpdb->registration_log}.IP LIKE '%".$ip_like."%' ";
66        }
67        if( isset($reg_date_since) ){
68                $query .= " AND unix_timestamp({$wpdb->registration_log}.date_registered) > '".strtotime($reg_date_since)."' ";
69        }
70        if( isset($reg_date_before) ){
71                $query .= " AND unix_timestamp({$wpdb->registration_log}.date_registered) < '".strtotime($reg_date_before)."' ";
72        }
73        if ( isset($include_user_id) ) {
74                $the_emails = make_email_list_by_user_id($include_user_id);
75                $list = implode("','", $the_emails);
76                $query .= " AND {$wpdb->registration_log}.email IN ('{$list}') ";
77        }
78        if ( isset($exclude_user_id) ) {
79                $the_emails = make_email_list_by_user_id($include_user_id);
80                $list = implode("','", $the_emails);
81                $query .= " AND {$wpdb->registration_log}.email NOT IN ('{$list}') ";
82        }
83        if ( isset($ip_like) ) {
84                $query .= " AND {$wpdb->registration_log}.IP LIKE ('%".$ip_like."%') ";
85        }
86       
87        $query .= " AND {$wpdb->blogs}.public = ". (($public_only) ? "'1'" : "'0'");
88        $query .= " AND {$wpdb->blogs}.archived = ". (($include_archived) ? "'1'" : "'0'");
89        $query .= " AND {$wpdb->blogs}.mature = ". (($include_mature) ? "'1'" : "'0'");
90        $query .= " AND {$wpdb->blogs}.spam = ". (($include_spam) ? "'1'" : "'0'");
91        $query .= " AND {$wpdb->blogs}.deleted = ". (($include_deleted) ? "'1'" : "'0'");
92       
93        if ( $sort_column == 'site_id' ) {
94                $query .= ' ORDER BY {$wpdb->blogs}.blog_id ';
95        } elseif ( $sort_column == 'lastupdated' ) {
96                $query .= ' ORDER BY last_updated ';
97        } elseif ( $sort_column == 'blogname' ) {
98                $query .= ' ORDER BY domain ';
99        } else {
100                $sort_column = 'registered';
101                $query .= " ORDER BY {$wpdb->blogs}.registered ";
102        }
103
104        $order = ( 'desc' == $order ) ? "DESC" : "ASC";
105        $query .= $order;
106       
107        $limit = '';
108        if( isset($limit_results) ){
109                if( isset($start) ){
110                        $limit = $start." , ";
111                }
112                $query .= "LIMIT ".$limit.$limit_results;
113        }
114       
115        $sql = $wpdb->prepare($query);
116
117        $results = $wpdb->get_results($sql, ARRAY_A);
118       
119        return $results;       
120}