Completed
Push — master ( ee928d...1fc1bc )
by Dennis
02:34
created

MslsBlogCollection::is_plugin_active()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 16
rs 9.7333
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
	 * Gets blog(s) by language
150
	 */
151
	public function get_blog_id( $language ) {
152
		$blog_id = null;
153
154
		foreach ( $this->get_objects() as $blog ) {
155
			if ( $language == $blog->get_language() ) {
156
				$blog_id = $blog->userblog_id;
157
				break;
158
			}
159
		}
160
161
		return apply_filters( 'msls_blog_collection_get_blog_id', $blog_id, $language );
162
	}
163
164
	/**
165
	 * Get the id of the current blog
166
	 * @return int
167
	 */
168
	public function get_current_blog_id() {
169
		return $this->current_blog_id;
170
	}
171
172
	/**
173
	 * Checks if blog is the current blog
174
	 * @param MslsBlog $blog
175
	 *
176
	 * @return bool
177
	 */
178
	public function is_current_blog( MslsBlog $blog ) {
179
		return $blog->userblog_id === $this->get_current_blog_id();
180
	}
181
182
	/**
183
	 * Checks if current blog is in the collection
184
	 *
185
	 * @return bool
186
	 */
187
	public function has_current_blog() {
188
		return isset( $this->objects[ $this->get_current_blog_id() ] );
189
	}
190
191
	/**
192
	 * Gets current blog as object
193
	 * @return MslsBlog|null
194
	 */
195
	public function get_current_blog() {
196
		return $this->has_current_blog() ? $this->objects[ $this->get_current_blog_id() ] : null;
197
	}
198
199
	/**
200
	 * Gets an array with all blog-objects
201
	 * @return MslsBlog[]
202
	 */
203
	public function get_objects() {
204
		return apply_filters( 'msls_blog_collection_get_objects', $this->objects );
205
	}
206
207
	/**
208
	 * Is plugin active in the blog with that blog_id
209
	 *
210
	 * @param int $blog_id
211
	 *
212
	 * @return bool
213
	 */
214
	public function is_plugin_active( $blog_id ) {
215
		if ( ! is_array( $this->active_plugins ) ) {
216
			$this->active_plugins = get_site_option(
217
				'active_sitewide_plugins',
218
				array()
219
			);
220
		}
221
222
		if ( isset( $this->active_plugins[ MSLS_PLUGIN_PATH ] ) ) {
223
			return true;
224
		}
225
226
		$plugins = get_blog_option( $blog_id, 'active_plugins', array() );
227
228
		return ( in_array( MSLS_PLUGIN_PATH, $plugins ) );
229
	}
230
231
	/**
232
	 * Gets only blogs where the plugin is active
233
	 * @return array
234
	 */
235
	public function get_plugin_active_blogs() {
236
		$arr = array();
237
238
		foreach ( $this->get_objects() as $id => $blog ) {
239
			if ( $this->is_plugin_active( $blog->userblog_id ) ) {
240
				$arr[] = $blog;
241
			}
242
		}
243
244
		return $arr;
245
	}
246
247
	/**
248
	 * Gets an array of all - but not the current - blog-objects
249
	 * @return MslsBlog[]
250
	 */
251
	public function get() {
252
		$objects = $this->get_objects();
253
254
		if ( $this->has_current_blog() ) {
255
			unset( $objects[ $this->current_blog_id ] );
256
		}
257
258
		return apply_filters( 'msls_blog_collection_get', $objects );
259
	}
260
261
	/**
262
	 * Gets an array with filtered blog-objects
263
	 *
264
	 * @param bool $filter
265
	 *
266
	 * @return MslsBlog[]
267
	 */
268
	public function get_filtered( $filter = false ) {
269
		if ( ! $filter && $this->current_blog_output ) {
270
			return $this->get_objects();
271
		}
272
273
		return $this->get();
274
	}
275
276
	/**
277
	 * Gets the registered users of the current blog
278
	 *
279
	 * @param string $fields
280
	 * @param int|string $number
281
	 *
282
	 * @return array
283
	 */
284
	public function get_users( $fields = 'all', $number = '' ) {
285
		$args = [
286
			'blog_id' => $this->current_blog_id,
287
			'orderby' => 'registered',
288
			'fields'  => $fields,
289
			'number'  => $number,
290
		];
291
292
		return get_users( $args );
293
	}
294
295
	/**
296
	 * Returns a specific blog language.
297
	 *
298
	 * @param int $blog_id
299
	 *
300
	 * @return string
301
	 */
302
	public static function get_blog_language( $blog_id = null ) {
303
		if ( null === $blog_id ) {
304
			$blog_id = get_current_blog_id();
305
		}
306
307
		$language = ( string ) get_blog_option(
0 ignored issues
show
introduced by
Cast statements must not contain whitespace; expected "(string)" but found "( string )"
Loading history...
308
			$blog_id, 'WPLANG'
309
		);
310
311
		return '' !== $language ? $language : 'en_US';
312
	}
313
314
}
315