WP_Site::__get()   C
last analyzed

Complexity

Conditions 9
Paths 17

Size

Total Lines 23
Code Lines 17

Duplication

Lines 9
Ratio 39.13 %

Importance

Changes 0
Metric Value
cc 9
eloc 17
nc 17
nop 1
dl 9
loc 23
rs 5.8541
c 0
b 0
f 0
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
 * @property int    $id
19
 * @property int    $network_id
20
 * @property string $blogname
21
 * @property string $siteurl
22
 * @property int    $post_count
23
 * @property string $home
24
 */
25
final class WP_Site {
26
27
	/**
28
	 * Site ID.
29
	 *
30
	 * A numeric string, for compatibility reasons.
31
	 *
32
	 * @since 4.5.0
33
	 * @access public
34
	 * @var string
35
	 */
36
	public $blog_id;
37
38
	/**
39
	 * Domain of the site.
40
	 *
41
	 * @since 4.5.0
42
	 * @access public
43
	 * @var string
44
	 */
45
	public $domain = '';
46
47
	/**
48
	 * Path of the site.
49
	 *
50
	 * @since 4.5.0
51
	 * @access public
52
	 * @var string
53
	 */
54
	public $path = '';
55
56
	/**
57
	 * The ID of the site's parent network.
58
	 *
59
	 * Named "site" vs. "network" for legacy reasons. An individual site's "site" is
60
	 * its network.
61
	 *
62
	 * A numeric string, for compatibility reasons.
63
	 *
64
	 * @since 4.5.0
65
	 * @access public
66
	 * @var string
67
	 */
68
	public $site_id = '0';
69
70
	/**
71
	 * The date on which the site was created or registered.
72
	 *
73
	 * @since 4.5.0
74
	 * @access public
75
	 * @var string Date in MySQL's datetime format.
76
	 */
77
	public $registered = '0000-00-00 00:00:00';
78
79
	/**
80
	 * The date and time on which site settings were last updated.
81
	 *
82
	 * @since 4.5.0
83
	 * @access public
84
	 * @var string Date in MySQL's datetime format.
85
	 */
86
	public $last_updated = '0000-00-00 00:00:00';
87
88
	/**
89
	 * Whether the site should be treated as public.
90
	 *
91
	 * A numeric string, for compatibility reasons.
92
	 *
93
	 * @since 4.5.0
94
	 * @access public
95
	 * @var string
96
	 */
97
	public $public = '1';
98
99
	/**
100
	 * Whether the site should be treated as archived.
101
	 *
102
	 * A numeric string, for compatibility reasons.
103
	 *
104
	 * @since 4.5.0
105
	 * @access public
106
	 * @var string
107
	 */
108
	public $archived = '0';
109
110
	/**
111
	 * Whether the site should be treated as mature.
112
	 *
113
	 * Handling for this does not exist throughout WordPress core, but custom
114
	 * implementations exist that require the property to be present.
115
	 *
116
	 * A numeric string, for compatibility reasons.
117
	 *
118
	 * @since 4.5.0
119
	 * @access public
120
	 * @var string
121
	 */
122
	public $mature = '0';
123
124
	/**
125
	 * Whether the site should be treated as spam.
126
	 *
127
	 * A numeric string, for compatibility reasons.
128
	 *
129
	 * @since 4.5.0
130
	 * @access public
131
	 * @var string
132
	 */
133
	public $spam = '0';
134
135
	/**
136
	 * Whether the site should be treated as deleted.
137
	 *
138
	 * A numeric string, for compatibility reasons.
139
	 *
140
	 * @since 4.5.0
141
	 * @access public
142
	 * @var string
143
	 */
144
	public $deleted = '0';
145
146
	/**
147
	 * The language pack associated with this site.
148
	 *
149
	 * A numeric string, for compatibility reasons.
150
	 *
151
	 * @since 4.5.0
152
	 * @access public
153
	 * @var string
154
	 */
155
	public $lang_id = '0';
156
157
	/**
158
	 * Retrieves a site from the database by its ID.
159
	 *
160
	 * @static
161
	 * @since 4.5.0
162
	 * @access public
163
	 *
164
	 * @global wpdb $wpdb WordPress database abstraction object.
165
	 *
166
	 * @param int $site_id The ID of the site to retrieve.
167
	 * @return WP_Site|false The site's object if found. False if not.
168
	 */
169 View Code Duplication
	public static function get_instance( $site_id ) {
170
		global $wpdb;
171
172
		$site_id = (int) $site_id;
173
		if ( ! $site_id ) {
174
			return false;
175
		}
176
177
		$_site = wp_cache_get( $site_id, 'sites' );
178
179
		if ( ! $_site ) {
180
			$_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );
181
182
			if ( empty( $_site ) || is_wp_error( $_site ) ) {
183
				return false;
184
			}
185
186
			wp_cache_add( $site_id, $_site, 'sites' );
187
		}
188
189
		return new WP_Site( $_site );
190
	}
191
192
	/**
193
	 * Creates a new WP_Site object.
194
	 *
195
	 * Will populate object properties from the object provided and assign other
196
	 * default properties based on that information.
197
	 *
198
	 * @since 4.5.0
199
	 * @access public
200
	 *
201
	 * @param WP_Site|object $site A site object.
202
	 */
203
	public function __construct( $site ) {
204
		foreach( get_object_vars( $site ) as $key => $value ) {
205
			$this->$key = $value;
206
		}
207
	}
208
209
	/**
210
	 * Converts an object to array.
211
	 *
212
	 * @since 4.6.0
213
	 * @access public
214
	 *
215
	 * @return array Object as array.
216
	 */
217
	public function to_array() {
218
		return get_object_vars( $this );
219
	}
220
221
	/**
222
	 * Getter.
223
	 *
224
	 * Allows current multisite naming conventions when getting properties.
225
	 * Allows access to extended site properties.
226
	 *
227
	 * @since 4.6.0
228
	 * @access public
229
	 *
230
	 * @param string $key Property to get.
231
	 * @return mixed Value of the property. Null if not available.
232
	 */
233
	public function __get( $key ) {
234
		switch ( $key ) {
235
			case 'id':
236
				return (int) $this->blog_id;
237
			case 'network_id':
238
				return (int) $this->site_id;
239
			case 'blogname':
240
			case 'siteurl':
241
			case 'post_count':
242
			case 'home':
243 View Code Duplication
			default: // Custom properties added by 'site_details' filter.
244
				if ( ! did_action( 'ms_loaded' ) ) {
245
					return null;
246
				}
247
248
				$details = $this->get_details();
249
				if ( isset( $details->$key ) ) {
250
					return $details->$key;
251
				}
252
		}
253
254
		return null;
255
	}
256
257
	/**
258
	 * Isset-er.
259
	 *
260
	 * Allows current multisite naming conventions when checking for properties.
261
	 * Checks for extended site properties.
262
	 *
263
	 * @since 4.6.0
264
	 * @access public
265
	 *
266
	 * @param string $key Property to check if set.
267
	 * @return bool Whether the property is set.
268
	 */
269
	public function __isset( $key ) {
270
		switch ( $key ) {
271
			case 'id':
272
			case 'network_id':
273
				return true;
274
			case 'blogname':
275
			case 'siteurl':
276
			case 'post_count':
277
			case 'home':
278
				if ( ! did_action( 'ms_loaded' ) ) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) did_action('ms_loaded');.
Loading history...
279
					return false;
280
				}
281
				return true;
282 View Code Duplication
			default: // Custom properties added by 'site_details' filter.
283
				if ( ! did_action( 'ms_loaded' ) ) {
284
					return false;
285
				}
286
287
				$details = $this->get_details();
288
				if ( isset( $details->$key ) ) {
289
					return true;
290
				}
291
		}
292
293
		return false;
294
	}
295
296
	/**
297
	 * Setter.
298
	 *
299
	 * Allows current multisite naming conventions while setting properties.
300
	 *
301
	 * @since 4.6.0
302
	 * @access public
303
	 *
304
	 * @param string $key   Property to set.
305
	 * @param mixed  $value Value to assign to the property.
306
	 */
307 View Code Duplication
	public function __set( $key, $value ) {
308
		switch ( $key ) {
309
			case 'id':
310
				$this->blog_id = (string) $value;
311
				break;
312
			case 'network_id':
313
				$this->site_id = (string) $value;
314
				break;
315
			default:
316
				$this->$key = $value;
317
		}
318
	}
319
320
	/**
321
	 * Retrieves the details for this site.
322
	 *
323
	 * This method is used internally to lazy-load the extended properties of a site.
324
	 *
325
	 * @since 4.6.0
326
	 * @access private
327
	 *
328
	 * @see WP_Site::__get()
329
	 *
330
	 * @return stdClass A raw site object with all details included.
331
	 */
332
	private function get_details() {
333
		$details = wp_cache_get( $this->blog_id, 'site-details' );
334
335
		if ( false === $details ) {
336
337
			switch_to_blog( $this->blog_id );
338
			// Create a raw copy of the object for backwards compatibility with the filter below.
339
			$details = new stdClass();
340
			foreach ( get_object_vars( $this ) as $key => $value ) {
341
				$details->$key = $value;
342
			}
343
			$details->blogname   = get_option( 'blogname' );
344
			$details->siteurl    = get_option( 'siteurl' );
345
			$details->post_count = get_option( 'post_count' );
346
			$details->home       = get_option( 'home' );
347
			restore_current_blog();
348
349
			wp_cache_set( $this->blog_id, $details, 'site-details' );
350
		}
351
352
		/** This filter is documented in wp-includes/ms-blogs.php */
353
		$details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
354
355
		/**
356
		 * Filters a site's extended properties.
357
		 *
358
		 * @since 4.6.0
359
		 *
360
		 * @param stdClass $details The site details.
361
		 */
362
		$details = apply_filters( 'site_details', $details );
363
364
		return $details;
365
	}
366
}
367