Complex classes like MslsBlogCollection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MslsBlogCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 MslsBlog[] |
||
| 32 | */ |
||
| 33 | private $objects = []; |
||
| 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 | * Container for hreflang-mapping |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $hreflangmap = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor |
||
| 55 | */ |
||
| 56 | public function __construct() { |
||
| 57 | if ( ! has_filter( 'msls_blog_collection_description' ) ) { |
||
| 58 | add_filter( 'msls_blog_collection_description', [ $this, 'get_configured_blog_description' ], 10, 2 ); |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->current_blog_id = get_current_blog_id(); |
||
| 62 | |||
| 63 | $options = MslsOptions::instance(); |
||
| 64 | |||
| 65 | $this->current_blog_output = isset( $options->output_current_blog ); |
||
| 66 | $this->objects_order = $options->get_order(); |
||
| 67 | |||
| 68 | if ( ! $options->is_excluded() ) { |
||
| 69 | /** |
||
| 70 | * Returns custom filtered blogs of the blogs_collection |
||
| 71 | * @since 0.9.8 |
||
| 72 | * |
||
| 73 | * @param array $blogs_collection |
||
| 74 | */ |
||
| 75 | $blogs_collection = (array) apply_filters( |
||
| 76 | 'msls_blog_collection_construct', |
||
| 77 | $this->get_blogs_of_reference_user( $options ) |
||
| 78 | ); |
||
| 79 | |||
| 80 | foreach ( $blogs_collection as $blog ) { |
||
| 81 | $description = false; |
||
| 82 | if ( $blog->userblog_id == $this->current_blog_id ) { |
||
| 83 | $description = $options->description; |
||
| 84 | } elseif ( ! $this->is_plugin_active( $blog->userblog_id ) ) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | |||
| 88 | $description = apply_filters( |
||
| 89 | 'msls_blog_collection_description', |
||
| 90 | $blog->userblog_id, |
||
| 91 | $description |
||
| 92 | ); |
||
| 93 | |||
| 94 | if ( false !== $description ) { |
||
| 95 | $this->objects[ $blog->userblog_id ] = new MslsBlog( $blog, $description ); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | uasort( $this->objects, [ MslsBlog::class, $this->objects_order ] ); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Returns the description of an configured blog or false if it is not configured |
||
| 104 | * |
||
| 105 | * @param int $blog_id |
||
| 106 | * @param string|bool $description |
||
| 107 | * |
||
| 108 | * @return string|bool |
||
| 109 | */ |
||
| 110 | public static function get_configured_blog_description( $blog_id, $description = false ) { |
||
| 111 | if ( false != $description ) { |
||
| 112 | return $description; |
||
| 113 | } |
||
| 114 | |||
| 115 | $temp = get_blog_option( $blog_id, 'msls' ); |
||
| 116 | if ( is_array( $temp ) && empty( $temp['exclude_current_blog'] ) ) { |
||
| 117 | return $temp['description']; |
||
| 118 | } |
||
| 119 | |||
| 120 | return false; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Gets the list of the blogs of the reference user |
||
| 125 | * The first available user of the blog will be used if there is no |
||
| 126 | * refrence user configured |
||
| 127 | * |
||
| 128 | * @param MslsOptions $options |
||
| 129 | * |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function get_blogs_of_reference_user( MslsOptions $options ) { |
||
| 133 | $reference_user = $options->has_value( 'reference_user' ) ? $options->reference_user : current( $this->get_users( 'ID', 1 ) ); |
||
| 134 | $blogs = get_blogs_of_user( $reference_user ); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @todo Check if this is still useful |
||
| 138 | */ |
||
| 139 | if ( is_array( $blogs ) ) { |
||
| 140 | foreach ( $blogs as $key => $blog ) { |
||
| 141 | $blogs[ $key ]->blog_id = $blog->userblog_id; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return $blogs; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get blog by language |
||
| 150 | * |
||
| 151 | * @param string $language |
||
| 152 | * |
||
| 153 | * @return MslsBlog|null |
||
| 154 | */ |
||
| 155 | public function get_blog( $language ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Gets blog_id by language |
||
| 170 | * |
||
| 171 | * @param string $language |
||
| 172 | * |
||
| 173 | * @return string|null |
||
| 174 | */ |
||
| 175 | public function get_blog_id( $language ) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get the id of the current blog |
||
| 184 | * @return int |
||
| 185 | */ |
||
| 186 | public function get_current_blog_id() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Checks if blog is the current blog |
||
| 192 | * |
||
| 193 | * @param MslsBlog $blog |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | public function is_current_blog( MslsBlog $blog ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Checks if current blog is in the collection |
||
| 203 | * |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function has_current_blog() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Gets current blog as object |
||
| 212 | * @return MslsBlog|null |
||
| 213 | */ |
||
| 214 | public function get_current_blog() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Gets an array with all blog-objects |
||
| 220 | * @return MslsBlog[] |
||
| 221 | */ |
||
| 222 | public function get_objects() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Is plugin active in the blog with that blog_id |
||
| 228 | * |
||
| 229 | * @param int $blog_id |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function is_plugin_active( $blog_id ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Gets only blogs where the plugin is active |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | public function get_plugin_active_blogs() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Gets an array of all - but not the current - blog-objects |
||
| 266 | * @return MslsBlog[] |
||
| 267 | */ |
||
| 268 | public function get() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Gets an array with filtered blog-objects |
||
| 280 | * |
||
| 281 | * @param bool $filter |
||
| 282 | * |
||
| 283 | * @return MslsBlog[] |
||
| 284 | */ |
||
| 285 | public function get_filtered( $filter = false ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Gets the registered users of the current blog |
||
| 295 | * |
||
| 296 | * @param string $fields |
||
| 297 | * @param int|string $number |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function get_users( $fields = 'all', $number = '' ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Returns a specific blog language. |
||
| 317 | * |
||
| 318 | * @param int $blog_id |
||
| 319 | * @param string $default |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public static function get_blog_language( $blog_id = null, $default = 'en_US' ) { |
||
| 332 | |||
| 333 | } |
||
| 334 |