These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 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', [ $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, [ MslsBlog::class, $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 | $blog_id = null; |
||
| 154 | |||
| 155 | foreach ( $this->get_objects() as $blog ) { |
||
| 156 | if ( $language == $blog->get_language() ) { |
||
| 157 | $blog_id = $blog->userblog_id; |
||
| 158 | break; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | return apply_filters( 'msls_blog_collection_get_blog_id', $blog_id, $language ); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the id of the current blog |
||
| 167 | * @return int |
||
| 168 | */ |
||
| 169 | public function get_current_blog_id() { |
||
| 170 | return $this->current_blog_id; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Checks if current blog is in the collection |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function has_current_blog() { |
||
| 179 | return ( isset( $this->objects[ $this->current_blog_id ] ) ); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Gets current blog as object |
||
| 184 | * @return MslsBlog|null |
||
| 185 | */ |
||
| 186 | public function get_current_blog() { |
||
| 187 | return ( |
||
| 188 | $this->has_current_blog() ? |
||
| 189 | $this->objects[ $this->current_blog_id ] : |
||
| 190 | null |
||
| 191 | ); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Gets an array with all blog-objects |
||
| 196 | * @return MslsBlog[] |
||
| 197 | */ |
||
| 198 | public function get_objects() { |
||
| 199 | return $this->objects; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Is plugin active in the blog with that blog_id |
||
| 204 | * |
||
| 205 | * @param int $blog_id |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function is_plugin_active( $blog_id ) { |
||
| 210 | if ( ! is_array( $this->active_plugins ) ) { |
||
| 211 | $this->active_plugins = get_site_option( |
||
| 212 | 'active_sitewide_plugins', |
||
| 213 | array() |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | if ( isset( $this->active_plugins[ MSLS_PLUGIN_PATH ] ) ) { |
||
| 218 | return true; |
||
| 219 | } |
||
| 220 | |||
| 221 | $plugins = get_blog_option( $blog_id, 'active_plugins', array() ); |
||
| 222 | |||
| 223 | return ( in_array( MSLS_PLUGIN_PATH, $plugins ) ); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Gets only blogs where the plugin is active |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function get_plugin_active_blogs() { |
||
| 231 | $arr = array(); |
||
| 232 | |||
| 233 | foreach ( $this->get_objects() as $id => $blog ) { |
||
| 234 | if ( $this->is_plugin_active( $blog->userblog_id ) ) { |
||
| 235 | $arr[] = $blog; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | return $arr; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Gets an array of all - but not the current - blog-objects |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function get() { |
||
| 247 | $objects = $this->get_objects(); |
||
| 248 | if ( $this->has_current_blog() ) { |
||
| 249 | unset( $objects[ $this->current_blog_id ] ); |
||
| 250 | } |
||
| 251 | |||
| 252 | return $objects; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Gets an array with filtered blog-objects |
||
| 257 | * |
||
| 258 | * @param bool $filter |
||
| 259 | * |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function get_filtered( $filter = false ) { |
||
| 263 | if ( ! $filter && $this->current_blog_output ) { |
||
| 264 | return $this->get_objects(); |
||
| 265 | } |
||
| 266 | |||
| 267 | return $this->get(); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Gets the registered users of the current blog |
||
| 272 | * |
||
| 273 | * @param string $fields |
||
| 274 | * @param int|string $number |
||
| 275 | * |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public function get_users( $fields = 'all', $number = '' ) { |
||
| 279 | $args = array( |
||
| 280 | 'blog_id' => $this->current_blog_id, |
||
| 281 | 'orderby' => 'registered', |
||
| 282 | 'fields' => $fields, |
||
| 283 | 'number' => $number, |
||
| 284 | ); |
||
| 285 | |||
| 286 | return get_users( $args ); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Returns a specific blog language. |
||
| 291 | * |
||
| 292 | * @param int $blog_id |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public static function get_blog_language( $blog_id = null ) { |
||
| 297 | if ( null === $blog_id ) { |
||
| 298 | $blog_id = get_current_blog_id(); |
||
| 299 | } |
||
| 300 | |||
| 301 | $language = ( string ) get_blog_option( |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 302 | $blog_id, 'WPLANG' |
||
| 303 | ); |
||
| 304 | |||
| 305 | return '' !== $language ? $language : 'en_US'; |
||
| 306 | } |
||
| 307 | |||
| 308 | } |
||
| 309 |