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