| 1 | <?php |
| 2 | /** |
| 3 | * Site API: WP_Site class |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage Multisite |
| 7 | * @since 4.5.0 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Core class used for interacting with a multisite site. |
| 12 | * |
| 13 | * This class is used during load to populate the `$current_blog` global and |
| 14 | * setup the current site. |
| 15 | * |
| 16 | * @since 4.5.0 |
| 17 | */ |
| 18 | final class WP_Site { |
| 19 | |
| 20 | /** |
| 21 | * Site ID. |
| 22 | * |
| 23 | * @since 4.5.0 |
| 24 | * @access public |
| 25 | * @var int |
| 26 | */ |
| 27 | public $blog_id; |
| 28 | |
| 29 | /** |
| 30 | * Domain of the site. |
| 31 | * |
| 32 | * @since 4.5.0 |
| 33 | * @access public |
| 34 | * @var string |
| 35 | */ |
| 36 | public $domain = ''; |
| 37 | |
| 38 | /** |
| 39 | * Path of the site. |
| 40 | * |
| 41 | * @since 4.5.0 |
| 42 | * @access public |
| 43 | * @var string |
| 44 | */ |
| 45 | public $path = ''; |
| 46 | |
| 47 | /** |
| 48 | * The ID of the site's parent network. |
| 49 | * |
| 50 | * Named "site" vs. "network" for legacy reasons. An individual site's "site" is |
| 51 | * its network. |
| 52 | * |
| 53 | * @since 4.5.0 |
| 54 | * @access public |
| 55 | * @var int |
| 56 | */ |
| 57 | public $site_id = 0; |
| 58 | |
| 59 | /** |
| 60 | * The date on which the site was created or registered. |
| 61 | * |
| 62 | * @since 4.5.0 |
| 63 | * @access public |
| 64 | * @var string Date in MySQL's datetime format. |
| 65 | */ |
| 66 | public $registered = '0000-00-00 00:00:00'; |
| 67 | |
| 68 | /** |
| 69 | * The date and time on which site settings were last updated. |
| 70 | * |
| 71 | * @since 4.5.0 |
| 72 | * @access public |
| 73 | * @var string Date in MySQL's datetime format. |
| 74 | */ |
| 75 | public $last_updated = '0000-00-00 00:00:00'; |
| 76 | |
| 77 | /** |
| 78 | * Whether the site should be treated as public. |
| 79 | * |
| 80 | * @since 4.5.0 |
| 81 | * @access public |
| 82 | * @var int |
| 83 | */ |
| 84 | public $public = 1; |
| 85 | |
| 86 | /** |
| 87 | * Whether the site should be treated as archived. |
| 88 | * |
| 89 | * @since 4.5.0 |
| 90 | * @access public |
| 91 | * @var int |
| 92 | */ |
| 93 | public $archived = 0; |
| 94 | |
| 95 | /** |
| 96 | * Whether the site should be treated as mature. |
| 97 | * |
| 98 | * Handling for this does not exist throughout WordPress core, but custom |
| 99 | * implementations exist that require the property to be present. |
| 100 | * |
| 101 | * @since 4.5.0 |
| 102 | * @access public |
| 103 | * @var int |
| 104 | */ |
| 105 | public $mature = 0; |
| 106 | |
| 107 | /** |
| 108 | * Whether the site should be treated as spam. |
| 109 | * |
| 110 | * @since 4.5.0 |
| 111 | * @access public |
| 112 | * @var int |
| 113 | */ |
| 114 | public $spam = 0; |
| 115 | |
| 116 | /** |
| 117 | * Whether the site should be treated as deleted. |
| 118 | * |
| 119 | * @since 4.5.0 |
| 120 | * @access public |
| 121 | * @var int |
| 122 | */ |
| 123 | public $deleted = 0; |
| 124 | |
| 125 | /** |
| 126 | * The language pack associated with this site. |
| 127 | * |
| 128 | * @since 4.5.0 |
| 129 | * @access public |
| 130 | * @var int |
| 131 | */ |
| 132 | public $lang_id = 0; |
| 133 | |
| 134 | /** |
| 135 | * Retrieve a site from the database by its ID. |
| 136 | * |
| 137 | * @since 4.5.0 |
| 138 | * @access public |
| 139 | * |
| 140 | * @global wpdb $wpdb WordPress database abstraction object. |
| 141 | * |
| 142 | * @param int $site_id The ID of the site to retrieve. |
| 143 | * @return WP_Site|bool The site's object if found. False if not. |
| 144 | */ |
| 145 | public static function get_instance( $site_id ) { |
| 146 | global $wpdb; |
| 147 | |
| 148 | $site_id = (int) $site_id; |
| 149 | if ( ! $site_id ) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | $_site = wp_cache_get( $site_id, 'sites' ); |
| 154 | |
| 155 | if ( ! $_site ) { |
| 156 | $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) ); |
| 157 | |
| 158 | if ( empty( $_site ) || is_wp_error( $_site ) ) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | wp_cache_add( $site_id, $_site, 'sites' ); |
| 163 | } |
| 164 | |
| 165 | return new WP_Site( $_site ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Create a new WP_Site object. |
| 170 | * |
| 171 | * Will populate object properties from the object provided and assign other |
| 172 | * default properties based on that information. |
| 173 | * |
| 174 | * @since 4.5.0 |
| 175 | * @access public |
| 176 | * |
| 177 | * @param WP_Site|object $site A site object. |
| 178 | */ |
| 179 | public function __construct( $site ) { |
| 180 | foreach( get_object_vars( $site ) as $key => $value ) { |
| 181 | $this->$key = $value; |
| 182 | } |
| 183 | } |
| 184 | } |