Completed
Push — master ( 196f44...96e9ab )
by Dennis
01:21
created

MslsBlogCollection::get_objects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsBlogCollection
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Collection of blog-objects
12
 *
13
 * @package Msls
14
 */
15
class MslsBlogCollection extends MslsRegistryInstance {
16
17
	/**
18
	 * ID of the current blog
19
	 * @var int
20
	 */
21
	private $current_blog_id;
22
23
	/**
24
	 * True if the current blog should be in the output
25
	 * @var bool
26
	 */
27
	private $current_blog_output;
28
29
	/**
30
	 * Collection of MslsBlog-objects
31
	 * @var array
32
	 */
33
	private $objects = array();
34
35
	/**
36
	 * Order output by language or description
37
	 * @var string
38
	 */
39
	private $objects_order;
40
41
	/**
42
	 * Active plugins in the whole network
43
	 * @var array
44
	 */
45
	private $active_plugins;
46
47
	/**
48
	 * Constructor
49
	 */
50
	public function __construct() {
51
		if ( ! has_filter( 'msls_blog_collection_description' ) ) {
52
			add_filter( 'msls_blog_collection_description', [ $this, 'get_configured_blog_description' ], 10, 2 );
53
		}
54
55
		$this->current_blog_id = get_current_blog_id();
56
57
		$options = MslsOptions::instance();
58
59
		$this->current_blog_output = isset( $options->output_current_blog );
60
		$this->objects_order       = $options->get_order();
61
62
		if ( ! $options->is_excluded() ) {
63
			/**
64
			 * Returns custom filtered blogs of the blogs_collection
65
			 * @since 0.9.8
66
			 *
67
			 * @param array $blogs_collection
68
			 */
69
			$blogs_collection = (array) apply_filters(
70
				'msls_blog_collection_construct',
71
				$this->get_blogs_of_reference_user( $options )
72
			);
73
74
			foreach ( $blogs_collection as $blog ) {
75
				$description = false;
76
				if ( $blog->userblog_id == $this->current_blog_id ) {
77
					$description = $options->description;
78
				} elseif ( ! $this->is_plugin_active( $blog->userblog_id ) ) {
79
					continue;
80
				}
81
82
				$description = apply_filters(
83
					'msls_blog_collection_description',
84
					$blog->userblog_id,
85
					$description
86
				);
87
88
				if ( false !== $description ) {
89
					$this->objects[ $blog->userblog_id ] = new MslsBlog(
90
						$blog,
91
						$description
92
					);
93
				}
94
			}
95
			uasort( $this->objects, [ MslsBlog::class, $this->objects_order ] );
96
		}
97
	}
98
99
	/**
100
	 * Returns the description of an configured blog or false if it is not configured
101
	 *
102
	 * @param int $blog_id
103
	 * @param string|bool $description
104
	 *
105
	 * @return string|bool
106
	 */
107
	public static function get_configured_blog_description( $blog_id, $description = false ) {
108
		if ( false != $description ) {
109
			return $description;
110
		}
111
112
		$temp = get_blog_option( $blog_id, 'msls' );
113
		if ( is_array( $temp ) && empty( $temp['exclude_current_blog'] ) ) {
114
			return $temp['description'];
115
		}
116
117
		return false;
118
	}
119
120
	/**
121
	 * Gets the list of the blogs of the reference user
122
	 * The first available user of the blog will be used if there is no
123
	 * refrence user configured
124
	 *
125
	 * @param MslsOptions $options
126
	 *
127
	 * @return array
128
	 */
129
	public function get_blogs_of_reference_user( MslsOptions $options ) {
130
		$blogs = get_blogs_of_user(
131
			$options->has_value( 'reference_user' ) ?
132
				$options->reference_user :
133
				current( $this->get_users( 'ID', 1 ) )
134
		);
135
136
		/**
137
		 * @todo Check if this is still useful
138
		 */
139
		if ( is_array( $blogs ) ) {
140
			foreach ( $blogs as $key => $blog ) {
141
				$blogs[ $key ]->blog_id = $blog->userblog_id;
142
			}
143
		}
144
145
		return $blogs;
146
	}
147
148
	/**
149
	 * Get blog by language
150
	 *
151
	 * @param string $language
152
	 *
153
	 * @return MslsBlog|null
154
	 */
155
	public function get_blog( $language ) {
156
		$blog = null;
157
158
		foreach ( $this->get_objects() as $item ) {
159
			if ( $language == $item->get_language() ) {
160
				$blog = $item;
161
				break;
162
			}
163
		}
164
165
		return apply_filters( 'msls_blog_collection_get_blog', $blog, $language );
166
	}
167
168
	/**
169
	 * Gets blog_id by language
170
	 *
171
	 * @param string $language
172
	 *
173
	 * @return string|null
174
	 */
175
	public function get_blog_id( $language ) {
176
		$blog    = $this->get_blog( $language );
177
		$blog_id = ! is_null( $blog ) ? $blog->userblog_id : null;
178
179
		return apply_filters( 'msls_blog_collection_get_blog_id', $blog_id, $language );
180
	}
181
182
	/**
183
	 * Get the id of the current blog
184
	 * @return int
185
	 */
186
	public function get_current_blog_id() {
187
		return $this->current_blog_id;
188
	}
189
190
	/**
191
	 * Checks if blog is the current blog
192
	 *
193
	 * @param MslsBlog $blog
194
	 *
195
	 * @return bool
196
	 */
197
	public function is_current_blog( MslsBlog $blog ) {
198
		return $blog->userblog_id === $this->get_current_blog_id();
199
	}
200
201
	/**
202
	 * Checks if current blog is in the collection
203
	 *
204
	 * @return bool
205
	 */
206
	public function has_current_blog() {
207
		return isset( $this->objects[ $this->get_current_blog_id() ] );
208
	}
209
210
	/**
211
	 * Gets current blog as object
212
	 * @return MslsBlog|null
213
	 */
214
	public function get_current_blog() {
215
		return $this->has_current_blog() ? $this->objects[ $this->get_current_blog_id() ] : null;
216
	}
217
218
	/**
219
	 * Gets an array with all blog-objects
220
	 * @return MslsBlog[]
221
	 */
222
	public function get_objects() {
223
		return apply_filters( 'msls_blog_collection_get_objects', $this->objects );
224
	}
225
226
	/**
227
	 * Is plugin active in the blog with that blog_id
228
	 *
229
	 * @param int $blog_id
230
	 *
231
	 * @return bool
232
	 */
233
	public function is_plugin_active( $blog_id ) {
234
		if ( ! is_array( $this->active_plugins ) ) {
235
			$this->active_plugins = get_site_option( 'active_sitewide_plugins', [] );
236
		}
237
238
		if ( isset( $this->active_plugins[ MSLS_PLUGIN_PATH ] ) ) {
239
			return true;
240
		}
241
242
		$plugins = get_blog_option( $blog_id, 'active_plugins', [] );
243
244
		return in_array( MSLS_PLUGIN_PATH, $plugins );
245
	}
246
247
	/**
248
	 * Gets only blogs where the plugin is active
249
	 * @return array
250
	 */
251
	public function get_plugin_active_blogs() {
252
		$arr = array();
253
254
		foreach ( $this->get_objects() as $id => $blog ) {
255
			if ( $this->is_plugin_active( $blog->userblog_id ) ) {
256
				$arr[] = $blog;
257
			}
258
		}
259
260
		return $arr;
261
	}
262
263
	/**
264
	 * Gets an array of all - but not the current - blog-objects
265
	 * @return MslsBlog[]
266
	 */
267
	public function get() {
268
		$objects = $this->get_objects();
269
270
		if ( $this->has_current_blog() ) {
271
			unset( $objects[ $this->current_blog_id ] );
272
		}
273
274
		return apply_filters( 'msls_blog_collection_get', $objects );
275
	}
276
277
	/**
278
	 * Gets an array with filtered blog-objects
279
	 *
280
	 * @param bool $filter
281
	 *
282
	 * @return MslsBlog[]
283
	 */
284
	public function get_filtered( $filter = false ) {
285
		if ( ! $filter && $this->current_blog_output ) {
286
			return $this->get_objects();
287
		}
288
289
		return $this->get();
290
	}
291
292
	/**
293
	 * Gets the registered users of the current blog
294
	 *
295
	 * @param string $fields
296
	 * @param int|string $number
297
	 *
298
	 * @return array
299
	 */
300
	public function get_users( $fields = 'all', $number = '' ) {
301
		$args = [
302
			'blog_id' => $this->current_blog_id,
303
			'orderby' => 'registered',
304
			'fields'  => $fields,
305
			'number'  => $number,
306
			'count_total' => false,
307
		];
308
309
		$args = (array) apply_filters( 'msls_get_users', $args );
310
311
		return get_users( $args );
312
	}
313
314
	/**
315
	 * Returns a specific blog language.
316
	 *
317
	 * @param int $blog_id
318
	 * @param string $default
319
	 *
320
	 * @return string
321
	 */
322
	public static function get_blog_language( $blog_id = null, $default = 'en_US' ) {
323
		if ( null === $blog_id ) {
324
			$blog_id = get_current_blog_id();
325
		}
326
327
		$language = ( string ) get_blog_option( $blog_id, 'WPLANG' );
328
329
		return '' !== $language ? $language : $default;
330
	}
331
332
}
333