Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Gutenberg 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 Jetpack_Gutenberg, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class Jetpack_Gutenberg { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Only these extensions can be registered. Used to control availability of beta blocks. |
||
| 59 | * |
||
| 60 | * @var array Extensions whitelist |
||
| 61 | */ |
||
| 62 | private static $extensions = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Keeps track of the reasons why a given extension is unavailable. |
||
| 66 | * |
||
| 67 | * @var array Extensions availability information |
||
| 68 | */ |
||
| 69 | private static $availability = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Since there is no `register_plugin()` counterpart to `register_block_type()` in Gutenberg, |
||
| 73 | * we have to keep track of plugin registration ourselves |
||
| 74 | * |
||
| 75 | * @var array Plugin registry |
||
| 76 | */ |
||
| 77 | private static $registered_plugins = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Prepend the 'jetpack/' prefix to a block name |
||
| 81 | * |
||
| 82 | * @param string $block_name The block name. |
||
| 83 | * |
||
| 84 | * @return string The prefixed block name. |
||
| 85 | */ |
||
| 86 | private static function prepend_block_prefix( $block_name ) { |
||
| 87 | return 'jetpack/' . $block_name; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Whether two arrays share at least one item |
||
| 92 | * |
||
| 93 | * @param array $a An array. |
||
| 94 | * @param array $b Another array. |
||
| 95 | * |
||
| 96 | * @return boolean True if $a and $b share at least one item |
||
| 97 | */ |
||
| 98 | protected static function share_items( $a, $b ) { |
||
| 99 | return count( array_intersect( $a, $b ) ) > 0; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Register a block |
||
| 104 | * |
||
| 105 | * If the block isn't whitelisted, set its unavailability reason instead. |
||
| 106 | * |
||
| 107 | * @param string $slug Slug of the block. |
||
| 108 | * @param array $args Arguments that are passed into register_block_type(). |
||
| 109 | */ |
||
| 110 | public static function register_block( $slug, $args ) { |
||
| 111 | $prefixed_extensions = array_map( array( __CLASS__, 'prepend_block_prefix' ), self::$extensions ); |
||
| 112 | |||
| 113 | // Register the block if it's whitelisted, or if it's a child block, and one of its parents is whitelisted. |
||
| 114 | if ( in_array( $slug, self::$extensions, true ) || ( isset( $args['parent'] ) && self::share_items( $args['parent'], $prefixed_extensions ) ) ) { |
||
| 115 | register_block_type( 'jetpack/' . $slug, $args ); |
||
| 116 | } elseif ( ! isset( $args['parent'] ) ) { |
||
| 117 | // Don't set availability information for child blocks -- we infer it from their parents. |
||
| 118 | self::set_extension_unavailability_reason( $slug, 'not_whitelisted' ); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Register a plugin |
||
| 124 | * |
||
| 125 | * If the plugin isn't whitelisted, set its unavailability reason instead. |
||
| 126 | * |
||
| 127 | * @param string $slug Slug of the plugin. |
||
| 128 | */ |
||
| 129 | public static function register_plugin( $slug ) { |
||
| 130 | if ( in_array( $slug, self::$extensions, true ) ) { |
||
| 131 | self::$registered_plugins[] = 'jetpack-' . $slug; |
||
| 132 | } else { |
||
| 133 | self::set_extension_unavailability_reason( $slug, 'not_whitelisted' ); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Register a block |
||
| 139 | * |
||
| 140 | * @deprecated 7.0.0 Use register_block() instead |
||
| 141 | * |
||
| 142 | * @param string $slug Slug of the block. |
||
| 143 | * @param array $args Arguments that are passed into the register_block_type. |
||
| 144 | * @param array $availability array containing if a block is available and the reason when it is not. |
||
| 145 | */ |
||
| 146 | public static function register( $slug, $args, $availability ) { |
||
| 147 | if ( isset( $availability['available'] ) && ! $availability['available'] ) { |
||
| 148 | self::set_extension_unavailability_reason( $slug, $availability['unavailable_reason'] ); |
||
| 149 | } else { |
||
| 150 | self::register_block( $slug, $args ); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set the reason why an extension (block or plugin) is unavailable |
||
| 156 | * |
||
| 157 | * @param string $slug Slug of the extension. |
||
| 158 | * @param string $reason A string representation of why the extension is unavailable. |
||
| 159 | */ |
||
| 160 | public static function set_extension_unavailability_reason( $slug, $reason ) { |
||
| 161 | self::$availability[ $slug ] = $reason; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Set up a whitelist of allowed block editor extensions |
||
| 166 | * |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | public static function init() { |
||
| 170 | if ( ! self::is_gutenberg_available() ) { |
||
| 171 | return; |
||
| 172 | } |
||
| 173 | |||
| 174 | if ( ! self::should_load() ) { |
||
| 175 | return; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks. |
||
| 180 | * |
||
| 181 | * @since 6.9.0 |
||
| 182 | * |
||
| 183 | * @param boolean |
||
| 184 | */ |
||
| 185 | if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) { |
||
| 186 | Jetpack_Constants::set_constant( 'JETPACK_BETA_BLOCKS', true ); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Filter the whitelist of block editor extensions that are available through Jetpack. |
||
| 191 | * |
||
| 192 | * @since 7.0.0 |
||
| 193 | * |
||
| 194 | * @param array |
||
| 195 | */ |
||
| 196 | self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_jetpack_gutenberg_extensions_whitelist() ); |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Filter the whitelist of block editor plugins that are available through Jetpack. |
||
| 200 | * |
||
| 201 | * @deprecated 7.0.0 Use jetpack_set_available_extensions instead |
||
| 202 | * |
||
| 203 | * @since 6.8.0 |
||
| 204 | * |
||
| 205 | * @param array |
||
| 206 | */ |
||
| 207 | self::$extensions = apply_filters( 'jetpack_set_available_blocks', self::$extensions ); |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Filter the whitelist of block editor plugins that are available through Jetpack. |
||
| 211 | * |
||
| 212 | * @deprecated 7.0.0 Use jetpack_set_available_extensions instead |
||
| 213 | * |
||
| 214 | * @since 6.9.0 |
||
| 215 | * |
||
| 216 | * @param array |
||
| 217 | */ |
||
| 218 | self::$extensions = apply_filters( 'jetpack_set_available_plugins', self::$extensions ); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Resets the class to its original state |
||
| 223 | * |
||
| 224 | * Used in unit tests |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | public static function reset() { |
||
| 229 | self::$extensions = array(); |
||
| 230 | self::$availability = array(); |
||
| 231 | self::$registered_plugins = array(); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return the Gutenberg extensions (blocks and plugins) directory |
||
| 236 | * |
||
| 237 | * @return string The Gutenberg extensions directory |
||
| 238 | */ |
||
| 239 | public static function get_blocks_directory() { |
||
| 240 | /** |
||
| 241 | * Filter to select Gutenberg blocks directory |
||
| 242 | * |
||
| 243 | * @since 6.9.0 |
||
| 244 | * |
||
| 245 | * @param string default: '_inc/blocks/' |
||
| 246 | */ |
||
| 247 | return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' ); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Checks for a given .json file in the blocks folder. |
||
| 252 | * |
||
| 253 | * @param string $preset The name of the .json file to look for. |
||
| 254 | * |
||
| 255 | * @return bool True if the file is found. |
||
| 256 | */ |
||
| 257 | public static function preset_exists( $preset ) { |
||
| 258 | return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Decodes JSON loaded from a preset file in the blocks folder |
||
| 263 | * |
||
| 264 | * @param string $preset The name of the .json file to load. |
||
| 265 | * |
||
| 266 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
||
| 267 | */ |
||
| 268 | public static function get_preset( $preset ) { |
||
| 269 | return json_decode( file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ) ); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
||
| 274 | * |
||
| 275 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
||
| 276 | */ |
||
| 277 | public static function get_jetpack_gutenberg_extensions_whitelist() { |
||
| 278 | $preset_extensions_manifest = self::preset_exists( 'index' ) ? self::get_preset( 'index' ) : (object) array(); |
||
| 279 | |||
| 280 | $preset_extensions = isset( $preset_extensions_manifest->production ) ? (array) $preset_extensions_manifest->production : array(); |
||
| 281 | |||
| 282 | if ( Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) { |
||
| 283 | $beta_extensions = isset( $preset_extensions_manifest->beta ) ? (array) $preset_extensions_manifest->beta : array(); |
||
| 284 | return array_unique( array_merge( $preset_extensions, $beta_extensions ) ); |
||
| 285 | } |
||
| 286 | |||
| 287 | return $preset_extensions; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get availability of each block / plugin. |
||
| 292 | * |
||
| 293 | * @return array A list of block and plugins and their availablity status |
||
| 294 | */ |
||
| 295 | public static function get_availability() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Fires before Gutenberg extensions availability is computed. |
||
| 299 | * |
||
| 300 | * In the function call you supply, use `jetpack_register_block()` and `jetpack_register_plugin()`, respectively. |
||
| 301 | * Alternatively, use `jetpack_set_extension_unavailability_reason()` if the block or plugin should not be registered but |
||
| 302 | * marked as unavailable. |
||
| 303 | * |
||
| 304 | * @since 7.0.0 |
||
| 305 | */ |
||
| 306 | do_action( 'jetpack_register_gutenberg_extensions' ); |
||
| 307 | |||
| 308 | $available_extensions = array(); |
||
| 309 | |||
| 310 | foreach ( self::$extensions as $extension ) { |
||
| 311 | $is_available = WP_Block_Type_Registry::get_instance()->is_registered( 'jetpack/' . $extension ) || |
||
| 312 | in_array( 'jetpack-' . $extension, self::$registered_plugins, true ); |
||
| 313 | |||
| 314 | $available_extensions[ $extension ] = array( |
||
| 315 | 'available' => $is_available, |
||
| 316 | ); |
||
| 317 | |||
| 318 | if ( ! $is_available ) { |
||
| 319 | $reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ] : 'missing_module'; |
||
| 320 | $available_extensions[ $extension ]['unavailable_reason'] = $reason; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | $unwhitelisted = array_fill_keys( |
||
| 325 | array_diff( array_keys( self::$availability ), self::$extensions ), |
||
| 326 | array( |
||
| 327 | 'available' => false, |
||
| 328 | 'unavailable_reason' => 'not_whitelisted', |
||
| 329 | ) |
||
| 330 | ); |
||
| 331 | |||
| 332 | return array_merge( $available_extensions, $unwhitelisted ); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Check if Gutenberg editor is available |
||
| 337 | * |
||
| 338 | * @since 6.7.0 |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public static function is_gutenberg_available() { |
||
| 343 | return function_exists( 'register_block_type' ); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
||
| 348 | * |
||
| 349 | * Loading blocks and plugins is enabled by default and may be disabled via filter: |
||
| 350 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
| 351 | * |
||
| 352 | * @since 6.9.0 |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | public static function should_load() { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Only enqueue block assets when needed. |
||
| 373 | * |
||
| 374 | * @param string $type slug of the block. |
||
| 375 | * @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued. |
||
| 376 | * |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Check if an asset exists for a block. |
||
| 411 | * |
||
| 412 | * @param string $file Path of the file we are looking for. |
||
| 413 | * |
||
| 414 | * @return bool $block_has_asset Does the file exist. |
||
| 415 | */ |
||
| 416 | public static function block_has_asset( $file ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
| 422 | * |
||
| 423 | * @param string $file Path of the file we are looking for. |
||
| 424 | * |
||
| 425 | * @return string $script_version Version number. |
||
| 426 | */ |
||
| 427 | public static function get_asset_version( $file ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Load Gutenberg editor assets |
||
| 435 | * |
||
| 436 | * @since 6.7.0 |
||
| 437 | * |
||
| 438 | * @return void |
||
| 439 | */ |
||
| 440 | public static function enqueue_block_editor_assets() { |
||
| 441 | if ( ! self::should_load() ) { |
||
| 442 | return; |
||
| 443 | } |
||
| 444 | |||
| 445 | $rtl = is_rtl() ? '.rtl' : ''; |
||
| 446 | $beta = Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : ''; |
||
| 502 | } |
||
| 503 |