Completed
Push — master ( 551434...e0a666 )
by Dennis
03:35
created

MslsBlogCollection::get_reference_user()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

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