Completed
Push — master ( d24e06...1436e2 )
by Dennis
02:14
created

MslsBlogCollection::get_objects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * MslsBlogCollection
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
/**
9
 * Collection of blog-objects
10
 *
11
 * Implements the interface IMslsRegistryInstance because we want to
12
 * work with a singleton instance of MslsBlogCollection all the time.
13
 * @package Msls
14
 */
15
class MslsBlogCollection implements IMslsRegistryInstance {
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', array( $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
				}
79
				elseif ( ! $this->is_plugin_active( $blog->userblog_id ) ) {
80
					continue;
81
				}
82
83
				$description = apply_filters(
84
					'msls_blog_collection_description',
85
					$blog->userblog_id,
86
					$description
87
				);
88
89
				if ( false !== $description ) {
90
					$this->objects[ $blog->userblog_id ] = new MslsBlog(
91
						$blog,
92
						$description
93
					);
94
				}
95
			}
96
			uasort( $this->objects, array( 'MslsBlog', $this->objects_order ) );
97
		}
98
	}
99
100
	/**
101
	 * Returns the description of an configured blog or false if it is not configured
102
	 *
103
	 * @param int $blog_id
104
	 * @param string|bool $description
105
	 *
106
	 * @return string|bool
107
	 */
108
	public static function get_configured_blog_description( $blog_id, $description = false ) {
109
		if ( false != $description ) {
110
			return $description;
111
		}
112
113
		$temp = get_blog_option( $blog_id, 'msls' );
114
		if ( is_array( $temp ) && empty( $temp['exclude_current_blog'] ) ) {
115
			return $temp['description'];
116
		}
117
118
		return false;
119
	}
120
121
	/**
122
	 * Gets the list of the blogs of the reference user
123
	 * The first available user of the blog will be used if there is no
124
	 * refrence user configured
125
	 *
126
	 * @param MslsOptions $options
127
	 *
128
	 * @return array
129
	 */
130
	public function get_blogs_of_reference_user( MslsOptions $options ) {
131
		$blogs = get_blogs_of_user(
132
			$options->has_value( 'reference_user' ) ?
133
			$options->reference_user :
134
			current( $this->get_users( 'ID', 1 ) )
135
		);
136
137
		/**
138
		 * @todo Check if this is still useful
139
		 */
140
		if ( is_array( $blogs ) ) {
141
			foreach ( $blogs as $key => $blog ) {
142
				$blogs[ $key ]->blog_id = $blog->userblog_id;
143
			}
144
		}
145
146
		return $blogs;
147
	}
148
149
	/**
150
	 * Gets blog(s) by language
151
	 */
152
	public function get_blog_id( $language ) {
153
		foreach ( $this->get_objects() as $blog ) {
154
			if ( $language == $blog->get_language() ) {
155
				return $blog->userblog_id;
156
			}
157
		}
158
159
		return null;
160
	}
161
162
	/**
163
	 * Get the id of the current blog
164
	 * @return int
165
	 */
166
	public function get_current_blog_id() {
167
		return $this->current_blog_id;
168
	}
169
170
	/**
171
	 * Checks if current blog is in the collection
172
	 *
173
	 * @return bool
174
	 */
175
	public function has_current_blog() {
176
		return ( isset( $this->objects[ $this->current_blog_id ] ) );
177
	}
178
179
	/**
180
	 * Gets current blog as object
181
	 * @return MslsBlog|null
182
	 */
183
	public function get_current_blog() {
184
		return (
185
			$this->has_current_blog() ?
186
			$this->objects[ $this->current_blog_id ] :
187
			null
188
		);
189
	}
190
191
	/**
192
	 * Gets an array with all blog-objects
193
	 * @return MslsBlog[]
194
	 */
195
	public function get_objects() {
196
		return $this->objects;
197
	}
198
199
	/**
200
	 * Is plugin active in the blog with that blog_id
201
	 *
202
	 * @param int $blog_id
203
	 *
204
	 * @return bool
205
	 */
206
	public function is_plugin_active( $blog_id ) {
207
		if ( ! is_array( $this->active_plugins ) ) {
208
			$this->active_plugins = get_site_option(
209
				'active_sitewide_plugins',
210
				array()
211
			);
212
		}
213
214
		if ( isset( $this->active_plugins[ MSLS_PLUGIN_PATH ] ) ) {
215
			return true;
216
		}
217
218
		$plugins = get_blog_option( $blog_id, 'active_plugins', array() );
219
220
		return ( in_array( MSLS_PLUGIN_PATH, $plugins ) );
221
	}
222
223
	/**
224
	 * Gets only blogs where the plugin is active
225
	 * @return array
226
	 */
227
	public function get_plugin_active_blogs() {
228
		$arr = array();
229
230
		foreach ( $this->get_objects() as $id => $blog ) {
231
			if ( $this->is_plugin_active( $blog->userblog_id ) ) {
232
				$arr[] = $blog;
233
			}
234
		}
235
236
		return $arr;
237
	}
238
239
	/**
240
	 * Gets an array of all - but not the current - blog-objects
241
	 * @return array
242
	 */
243
	public function get() {
244
		$objects = $this->get_objects();
245
		if ( $this->has_current_blog() ) {
246
			unset( $objects[ $this->current_blog_id ] );
247
		}
248
249
		return $objects;
250
	}
251
252
	/**
253
	 * Gets an array with filtered blog-objects
254
	 *
255
	 * @param bool $filter
256
	 *
257
	 * @return array
258
	 */
259
	public function get_filtered( $filter = false ) {
260
		if ( ! $filter && $this->current_blog_output ) {
261
			return $this->get_objects();
262
		}
263
264
		return $this->get();
265
	}
266
267
	/**
268
	 * Gets the registered users of the current blog
269
	 *
270
	 * @param string $fields
271
	 * @param int|string $number
272
	 *
273
	 * @return array
274
	 */
275
	public function get_users( $fields = 'all', $number = '' ) {
276
		$args = array(
277
			'blog_id' => $this->current_blog_id,
278
			'orderby' => 'registered',
279
			'fields'  => $fields,
280
			'number'  => $number,
281
		);
282
283
		return get_users( $args );
284
	}
285
286
	/**
287
	 * Gets or creates an instance of MslsBlogCollection
288
	 * @todo Until PHP 5.2 is not longer the minimum for WordPress ...
289
	 * @return MslsBlogCollection
290
	 */
291
	public static function instance() {
292
		if ( ! ( $obj = MslsRegistry::get_object( 'MslsBlogCollection' ) ) ) {
293
			$obj = new self();
294
			MslsRegistry::set_object( 'MslsBlogCollection', $obj );
295
		}
296
297
		return $obj;
298
	}
299
300
}
301