MslsBlogCollection::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
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 MslsBlog[]
32
	 */
33
	private $objects = [];
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
	 * Container for hreflang-mapping
49
	 * @var array
50
	 */
51
	private $hreflangmap = [];
52
53
	/**
54
	 * Constructor
55
	 */
56
	public function __construct() {
57
		if ( ! has_filter( 'msls_blog_collection_description' ) ) {
58
			add_filter( 'msls_blog_collection_description', [ $this, 'get_configured_blog_description' ], 10, 2 );
59
		}
60
61
		$this->current_blog_id = get_current_blog_id();
62
63
		$options = MslsOptions::instance();
64
65
		$this->current_blog_output = isset( $options->output_current_blog );
66
		$this->objects_order       = $options->get_order();
67
68
		if ( ! $options->is_excluded() ) {
69
			/**
70
			 * Returns custom filtered blogs of the blogs_collection
71
			 * @since 0.9.8
72
			 *
73
			 * @param array $blogs_collection
74
			 */
75
			$blogs_collection = (array) apply_filters(
76
				'msls_blog_collection_construct',
77
				$this->get_blogs_of_reference_user( $options )
78
			);
79
80
			foreach ( $blogs_collection as $blog ) {
81
				$description = false;
82
				if ( $blog->userblog_id == $this->current_blog_id ) {
83
					$description = $options->description;
84
				} elseif ( ! $this->is_plugin_active( $blog->userblog_id ) ) {
85
					continue;
86
				}
87
88
				$description = apply_filters(
89
					'msls_blog_collection_description',
90
					$blog->userblog_id,
91
					$description
92
				);
93
94
				if ( false !== $description ) {
95
					$this->objects[ $blog->userblog_id ] = new MslsBlog( $blog, $description );
96
				}
97
			}
98
			uasort( $this->objects, [ MslsBlog::class, $this->objects_order ] );
99
		}
100
	}
101
102
	/**
103
	 * Returns the description of an configured blog or false if it is not configured
104
	 *
105
	 * @param int $blog_id
106
	 * @param string|bool $description
107
	 *
108
	 * @return string|bool
109
	 */
110
	public static function get_configured_blog_description( $blog_id, $description = false ) {
111
		if ( false != $description ) {
112
			return $description;
113
		}
114
115
		$temp = get_blog_option( $blog_id, 'msls' );
116
		if ( is_array( $temp ) && empty( $temp['exclude_current_blog'] ) ) {
117
			return $temp['description'];
118
		}
119
120
		return false;
121
	}
122
123
	/**
124
	 * Gets the list of the blogs of the reference user
125
	 * The first available user of the blog will be used if there is no
126
	 * refrence user configured
127
	 *
128
	 * @param MslsOptions $options
129
	 *
130
	 * @return array
131
	 */
132
	public function get_blogs_of_reference_user( MslsOptions $options ) {
133
		$reference_user = $options->has_value( 'reference_user' ) ? $options->reference_user : current( $this->get_users( 'ID', 1 ) );
134
		$blogs          = get_blogs_of_user( $reference_user );
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
		$path = MslsPlugin::path();
239
		if ( isset( $this->active_plugins[ $path ] ) ) {
240
			return true;
241
		}
242
243
		$plugins = get_blog_option( $blog_id, 'active_plugins', [] );
244
245
		return in_array( $path, $plugins );
246
	}
247
248
	/**
249
	 * Gets only blogs where the plugin is active
250
	 * @return array
251
	 */
252
	public function get_plugin_active_blogs() {
253
		$arr = [];
254
255
		foreach ( $this->get_objects() as $blog ) {
256
			if ( $this->is_plugin_active( $blog->userblog_id ) ) {
257
				$arr[] = $blog;
258
			}
259
		}
260
261
		return $arr;
262
	}
263
264
	/**
265
	 * Gets an array of all - but not the current - blog-objects
266
	 * @return MslsBlog[]
267
	 */
268
	public function get() {
269
		$objects = $this->get_objects();
270
271
		if ( $this->has_current_blog() ) {
272
			unset( $objects[ $this->current_blog_id ] );
273
		}
274
275
		return apply_filters( 'msls_blog_collection_get', $objects );
276
	}
277
278
	/**
279
	 * Gets an array with filtered blog-objects
280
	 *
281
	 * @param bool $filter
282
	 *
283
	 * @return MslsBlog[]
284
	 */
285
	public function get_filtered( $filter = false ) {
286
		if ( ! $filter && $this->current_blog_output ) {
287
			return $this->get_objects();
288
		}
289
290
		return $this->get();
291
	}
292
293
	/**
294
	 * Gets the registered users of the current blog
295
	 *
296
	 * @param string $fields
297
	 * @param int|string $number
298
	 *
299
	 * @return array
300
	 */
301
	public function get_users( $fields = 'all', $number = '' ) {
302
		$args = [
303
			'blog_id' => $this->current_blog_id,
304
			'orderby' => 'registered',
305
			'fields'  => $fields,
306
			'number'  => $number,
307
			'count_total' => false,
308
		];
309
310
		$args = (array) apply_filters( 'msls_get_users', $args );
311
312
		return get_users( $args );
313
	}
314
315
	/**
316
	 * Returns a specific blog language.
317
	 *
318
	 * @param int $blog_id
319
	 * @param string $default
320
	 *
321
	 * @return string
322
	 */
323
	public static function get_blog_language( $blog_id = null, $default = 'en_US' ) {
324
		if ( null === $blog_id ) {
325
			$blog_id = get_current_blog_id();
326
		}
327
328
		$language = ( string ) get_blog_option( $blog_id, 'WPLANG' );
329
330
		return '' !== $language ? $language : $default;
331
	}
332
333
}
334