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