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 //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
71 | class Jetpack_Gutenberg { |
||
72 | |||
73 | /** |
||
74 | * Only these extensions can be registered. Used to control availability of beta blocks. |
||
75 | * |
||
76 | * @var array Extensions allowed list. |
||
77 | */ |
||
78 | private static $extensions = array(); |
||
79 | |||
80 | /** |
||
81 | * Keeps track of the reasons why a given extension is unavailable. |
||
82 | * |
||
83 | * @var array Extensions availability information |
||
84 | */ |
||
85 | private static $availability = array(); |
||
86 | |||
87 | /** |
||
88 | * A cached array of the fully processed availability data. Keeps track of |
||
89 | * reasons why an extension is unavailable or missing. |
||
90 | * |
||
91 | * @var array Extensions availability information. |
||
92 | */ |
||
93 | private static $cached_availability = null; |
||
94 | |||
95 | /** |
||
96 | * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in |
||
97 | * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can |
||
98 | * optionally fall back to that. |
||
99 | * |
||
100 | * @param array $version_requirements An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version. |
||
101 | * @param string $slug The slug of the block or plugin that has the gutenberg version requirement. |
||
102 | * |
||
103 | * @since 8.3.0 |
||
104 | * |
||
105 | * @return boolean True if the version of gutenberg required by the block or plugin is available. |
||
106 | */ |
||
107 | View Code Duplication | public static function is_gutenberg_version_available( $version_requirements, $slug ) { |
|
147 | |||
148 | /** |
||
149 | * Prepend the 'jetpack/' prefix to a block name |
||
150 | * |
||
151 | * @param string $block_name The block name. |
||
152 | * |
||
153 | * @return string The prefixed block name. |
||
154 | */ |
||
155 | private static function prepend_block_prefix( $block_name ) { |
||
158 | |||
159 | /** |
||
160 | * Remove the 'jetpack/' or jetpack-' prefix from an extension name |
||
161 | * |
||
162 | * @param string $extension_name The extension name. |
||
163 | * |
||
164 | * @return string The unprefixed extension name. |
||
165 | */ |
||
166 | public static function remove_extension_prefix( $extension_name ) { |
||
172 | |||
173 | /** |
||
174 | * Whether two arrays share at least one item |
||
175 | * |
||
176 | * @param array $a An array. |
||
177 | * @param array $b Another array. |
||
178 | * |
||
179 | * @return boolean True if $a and $b share at least one item |
||
180 | */ |
||
181 | protected static function share_items( $a, $b ) { |
||
184 | |||
185 | /** |
||
186 | * Register a plugin |
||
187 | * |
||
188 | * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead |
||
189 | * |
||
190 | * @param string $slug Slug of the plugin. |
||
191 | */ |
||
192 | public static function register_plugin( $slug ) { |
||
197 | |||
198 | /** |
||
199 | * Set a (non-block) extension as available |
||
200 | * |
||
201 | * @param string $slug Slug of the extension. |
||
202 | */ |
||
203 | public static function set_extension_available( $slug ) { |
||
206 | |||
207 | /** |
||
208 | * Set the reason why an extension (block or plugin) is unavailable |
||
209 | * |
||
210 | * @param string $slug Slug of the extension. |
||
211 | * @param string $reason A string representation of why the extension is unavailable. |
||
212 | * @param array $details A free-form array containing more information on why the extension is unavailable. |
||
213 | */ |
||
214 | public static function set_extension_unavailable( $slug, $reason, $details = array() ) { |
||
245 | |||
246 | /** |
||
247 | * Set the reason why an extension (block or plugin) is unavailable |
||
248 | * |
||
249 | * @deprecated 7.1.0 Use set_extension_unavailable() instead |
||
250 | * |
||
251 | * @param string $slug Slug of the extension. |
||
252 | * @param string $reason A string representation of why the extension is unavailable. |
||
253 | */ |
||
254 | public static function set_extension_unavailability_reason( $slug, $reason ) { |
||
259 | |||
260 | /** |
||
261 | * Set up a list of allowed block editor extensions |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | public static function init() { |
||
323 | |||
324 | /** |
||
325 | * Resets the class to its original state |
||
326 | * |
||
327 | * Used in unit tests |
||
328 | * |
||
329 | * @return void |
||
330 | */ |
||
331 | public static function reset() { |
||
336 | |||
337 | /** |
||
338 | * Return the Gutenberg extensions (blocks and plugins) directory |
||
339 | * |
||
340 | * @return string The Gutenberg extensions directory |
||
341 | */ |
||
342 | public static function get_blocks_directory() { |
||
352 | |||
353 | /** |
||
354 | * Checks for a given .json file in the blocks folder. |
||
355 | * |
||
356 | * @param string $preset The name of the .json file to look for. |
||
357 | * |
||
358 | * @return bool True if the file is found. |
||
359 | */ |
||
360 | public static function preset_exists( $preset ) { |
||
363 | |||
364 | /** |
||
365 | * Decodes JSON loaded from a preset file in the blocks folder |
||
366 | * |
||
367 | * @param string $preset The name of the .json file to load. |
||
368 | * |
||
369 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
||
370 | */ |
||
371 | public static function get_preset( $preset ) { |
||
377 | |||
378 | /** |
||
379 | * Returns a list of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
||
380 | * |
||
381 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
||
382 | */ |
||
383 | public static function get_jetpack_gutenberg_extensions_allowed_list() { |
||
391 | |||
392 | /** |
||
393 | * Returns a list of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
||
394 | * |
||
395 | * @deprecated 8.7.0 Use get_jetpack_gutenberg_extensions_allowed_list() |
||
396 | * |
||
397 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
||
398 | */ |
||
399 | public static function get_jetpack_gutenberg_extensions_whitelist() { |
||
403 | |||
404 | /** |
||
405 | * Returns a diff from a combined list of allowed extensions and extensions determined to be excluded |
||
406 | * |
||
407 | * @param array $allowed_extensions An array of allowed extensions. |
||
408 | * |
||
409 | * @return array A list of blocks: eg array( 'publicize', 'markdown' ) |
||
410 | */ |
||
411 | public static function get_available_extensions( $allowed_extensions = null ) { |
||
417 | |||
418 | /** |
||
419 | * Return true if the extension has been registered and there's nothing in the availablilty array. |
||
420 | * |
||
421 | * @param string $extension The name of the extension. |
||
422 | * |
||
423 | * @return bool whether the extension has been registered and there's nothing in the availablilty array. |
||
424 | */ |
||
425 | public static function is_registered_and_no_entry_in_availability( $extension ) { |
||
428 | |||
429 | /** |
||
430 | * Return true if the extension has a true entry in the availablilty array. |
||
431 | * |
||
432 | * @param string $extension The name of the extension. |
||
433 | * |
||
434 | * @return bool whether the extension has a true entry in the availablilty array. |
||
435 | */ |
||
436 | public static function is_available( $extension ) { |
||
439 | |||
440 | /** |
||
441 | * Get the availability of each block / plugin, or return the cached availability |
||
442 | * if it has already been calculated. Avoids re-registering extensions when not |
||
443 | * necessary. |
||
444 | * |
||
445 | * @return array A list of block and plugins and their availability status. |
||
446 | */ |
||
447 | public static function get_cached_availability() { |
||
453 | |||
454 | /** |
||
455 | * Get availability of each block / plugin. |
||
456 | * |
||
457 | * @return array A list of block and plugins and their availablity status |
||
458 | */ |
||
459 | public static function get_availability() { |
||
490 | |||
491 | /** |
||
492 | * Check if an extension/block is already registered |
||
493 | * |
||
494 | * @since 7.2 |
||
495 | * |
||
496 | * @param string $slug Name of extension/block to check. |
||
497 | * |
||
498 | * @return bool |
||
499 | */ |
||
500 | public static function is_registered( $slug ) { |
||
503 | |||
504 | /** |
||
505 | * Check if Gutenberg editor is available |
||
506 | * |
||
507 | * @since 6.7.0 |
||
508 | * |
||
509 | * @return bool |
||
510 | */ |
||
511 | public static function is_gutenberg_available() { |
||
514 | |||
515 | /** |
||
516 | * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
||
517 | * |
||
518 | * Loading blocks and plugins is enabled by default and may be disabled via filter: |
||
519 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
520 | * |
||
521 | * @since 6.9.0 |
||
522 | * |
||
523 | * @return bool |
||
524 | */ |
||
525 | public static function should_load() { |
||
539 | |||
540 | /** |
||
541 | * Only enqueue block assets when needed. |
||
542 | * |
||
543 | * @param string $type Slug of the block. |
||
544 | * @param array $script_dependencies Script dependencies. Will be merged with automatically |
||
545 | * detected script dependencies from the webpack build. |
||
546 | * |
||
547 | * @return void |
||
548 | */ |
||
549 | public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
||
559 | |||
560 | /** |
||
561 | * Only enqueue block sytles when needed. |
||
562 | * |
||
563 | * @param string $type Slug of the block. |
||
564 | * |
||
565 | * @since 7.2.0 |
||
566 | * |
||
567 | * @return void |
||
568 | */ |
||
569 | public static function load_styles_as_required( $type ) { |
||
592 | |||
593 | /** |
||
594 | * Only enqueue block scripts when needed. |
||
595 | * |
||
596 | * @param string $type Slug of the block. |
||
597 | * @param array $script_dependencies Script dependencies. Will be merged with automatically |
||
598 | * detected script dependencies from the webpack build. |
||
599 | * |
||
600 | * @since 7.2.0 |
||
601 | * |
||
602 | * @return void |
||
603 | */ |
||
604 | public static function load_scripts_as_required( $type, $script_dependencies = array() ) { |
||
640 | |||
641 | /** |
||
642 | * Check if an asset exists for a block. |
||
643 | * |
||
644 | * @param string $file Path of the file we are looking for. |
||
645 | * |
||
646 | * @return bool $block_has_asset Does the file exist. |
||
647 | */ |
||
648 | public static function block_has_asset( $file ) { |
||
651 | |||
652 | /** |
||
653 | * Get the version number to use when loading the file. Allows us to bypass cache when developing. |
||
654 | * |
||
655 | * @param string $file Path of the file we are looking for. |
||
656 | * |
||
657 | * @return string $script_version Version number. |
||
658 | */ |
||
659 | public static function get_asset_version( $file ) { |
||
664 | |||
665 | /** |
||
666 | * Load Gutenberg editor assets |
||
667 | * |
||
668 | * @since 6.7.0 |
||
669 | * |
||
670 | * @return void |
||
671 | */ |
||
672 | public static function enqueue_block_editor_assets() { |
||
762 | |||
763 | /** |
||
764 | * Some blocks do not depend on a specific module, |
||
765 | * and can consequently be loaded outside of the usual modules. |
||
766 | * We will look for such modules in the extensions/ directory. |
||
767 | * |
||
768 | * @since 7.1.0 |
||
769 | */ |
||
770 | public static function load_independent_blocks() { |
||
784 | |||
785 | /** |
||
786 | * Loads PHP components of extended-blocks. |
||
787 | * |
||
788 | * @since 8.9.0 |
||
789 | */ |
||
790 | public static function load_extended_blocks() { |
||
804 | |||
805 | /** |
||
806 | * Get CSS classes for a block. |
||
807 | * |
||
808 | * @since 7.7.0 |
||
809 | * |
||
810 | * @param string $slug Block slug. |
||
811 | * @param array $attr Block attributes. |
||
812 | * @param array $extra Potential extra classes you may want to provide. |
||
813 | * |
||
814 | * @return string $classes List of CSS classes for a block. |
||
815 | */ |
||
816 | public static function block_classes( $slug, $attr, $extra = array() ) { |
||
820 | |||
821 | /** |
||
822 | * Determine whether a site should use the default set of blocks, or a custom set. |
||
823 | * Possible variations are currently beta, experimental, and production. |
||
824 | * |
||
825 | * @since 8.1.0 |
||
826 | * |
||
827 | * @return string $block_varation production|beta|experimental |
||
828 | */ |
||
829 | public static function blocks_variation() { |
||
853 | |||
854 | /** |
||
855 | * Get a list of extensions available for the variation you chose. |
||
856 | * |
||
857 | * @since 8.1.0 |
||
858 | * |
||
859 | * @param obj $preset_extensions_manifest List of extensions available in Jetpack. |
||
860 | * @param string $blocks_variation Subset of blocks. production|beta|experimental. |
||
861 | * |
||
862 | * @return array $preset_extensions Array of extensions for that variation |
||
863 | */ |
||
864 | public static function get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ) { |
||
902 | |||
903 | /** |
||
904 | * Validate a URL used in a SSR block. |
||
905 | * |
||
906 | * @since 8.3.0 |
||
907 | * |
||
908 | * @param string $url URL saved as an attribute in block. |
||
909 | * @param array $allowed Array of allowed hosts for that block, or regexes to check against. |
||
910 | * @param bool $is_regex Array of regexes matching the URL that could be used in block. |
||
911 | * |
||
912 | * @return bool|string |
||
913 | */ |
||
914 | public static function validate_block_embed_url( $url, $allowed = array(), $is_regex = false ) { |
||
968 | |||
969 | /** |
||
970 | * Determines whether a preview of the block with an upgrade nudge should |
||
971 | * be displayed for admins on the site frontend. |
||
972 | * |
||
973 | * @since 8.4.0 |
||
974 | * |
||
975 | * @param array $availability_for_block The availability for the block. |
||
976 | * |
||
977 | * @return bool |
||
978 | */ |
||
979 | public static function should_show_frontend_preview( $availability_for_block ) { |
||
986 | |||
987 | /** |
||
988 | * Output an UpgradeNudge Component on the frontend of a site. |
||
989 | * |
||
990 | * @since 8.4.0 |
||
991 | * |
||
992 | * @param string $plan The plan that users need to purchase to make the block work. |
||
993 | * |
||
994 | * @return string |
||
995 | */ |
||
996 | public static function upgrade_nudge( $plan ) { |
||
1004 | |||
1005 | /** |
||
1006 | * Output a notice within a block. |
||
1007 | * |
||
1008 | * @since 8.6.0 |
||
1009 | * |
||
1010 | * @param string $message Notice we want to output. |
||
1011 | * @param string $status Status of the notice. Can be one of success, info, warning, error. info by default. |
||
1012 | * @param string $classes List of CSS classes. |
||
1013 | * |
||
1014 | * @return string |
||
1015 | */ |
||
1016 | public static function notice( $message, $status = 'info', $classes = '' ) { |
||
1055 | |||
1056 | /** |
||
1057 | * Set the availability of the block as the editor |
||
1058 | * is loaded. |
||
1059 | * |
||
1060 | * @param string $slug Slug of the block. |
||
1061 | */ |
||
1062 | public static function set_availability_for_plan( $slug ) { |
||
1110 | |||
1111 | /** |
||
1112 | * Wraps the suplied render_callback in a function to check |
||
1113 | * the availability of the block before rendering it. |
||
1114 | * |
||
1115 | * @param string $slug The block slug, used to check for availability. |
||
1116 | * @param callable $render_callback The render_callback that will be called if the block is available. |
||
1117 | */ |
||
1118 | public static function get_render_callback_with_availability_check( $slug, $render_callback ) { |
||
1144 | } |
||
1145 | |||
1156 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.