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:
| 1 | <?php |
||
| 21 | class Blocks { |
||
| 22 | /** |
||
| 23 | * Wrapper function to safely register a Gutenberg block type |
||
| 24 | * |
||
| 25 | * @see register_block_type |
||
| 26 | * @see Automattic\Jetpack\Blocks::is_gutenberg_version_available |
||
| 27 | * |
||
| 28 | * @since 9.0.0 |
||
| 29 | * |
||
| 30 | * @param string $slug Slug of the block. |
||
| 31 | * @param array $args { |
||
| 32 | * Arguments that are passed into register_block_type. |
||
| 33 | * See register_block_type for full list of arguments. |
||
| 34 | * Can also include 2 extra arguments not currently supported by register_block_type. |
||
| 35 | * |
||
| 36 | * @type array $version_requirements Array containing required Gutenberg version and, if known, the WordPress version that was released with this minimum version. |
||
| 37 | * @type bool $plan_check Should we check for a specific plan before registering the block. |
||
| 38 | * } |
||
| 39 | * |
||
| 40 | * @return WP_Block_Type|false The registered block type on success, or false on failure. |
||
| 41 | */ |
||
| 42 | public static function jetpack_register_block( $slug, $args = array() ) { |
||
| 43 | if ( 0 !== strpos( $slug, 'jetpack/' ) && ! strpos( $slug, '/' ) ) { |
||
| 44 | _doing_it_wrong( 'jetpack_register_block', 'Prefix the block with jetpack/ ', 'Jetpack 9.0.0' ); |
||
| 45 | $slug = 'jetpack/' . $slug; |
||
| 46 | } |
||
| 47 | |||
| 48 | if ( |
||
| 49 | isset( $args['version_requirements'] ) |
||
| 50 | && ! self::is_gutenberg_version_available( $args['version_requirements'], $slug ) |
||
| 51 | ) { |
||
| 52 | return false; |
||
| 53 | } |
||
| 54 | |||
| 55 | // Checking whether block is registered to ensure it isn't registered twice. |
||
| 56 | if ( self::is_registered( $slug ) ) { |
||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | $feature_name = self::remove_extension_prefix( $slug ); |
||
| 61 | |||
| 62 | // This is only useful in Jetpack. |
||
| 63 | if ( ! self::is_standalone_block() ) { |
||
| 64 | // If the block is dynamic, and a Jetpack block, wrap the render_callback to check availability. |
||
| 65 | if ( ! empty( $args['plan_check'] ) ) { |
||
| 66 | if ( isset( $args['render_callback'] ) ) { |
||
| 67 | $args['render_callback'] = Jetpack_Gutenberg::get_render_callback_with_availability_check( $feature_name, $args['render_callback'] ); |
||
| 68 | } |
||
| 69 | $method_name = 'set_availability_for_plan'; |
||
| 70 | } else { |
||
| 71 | $method_name = 'set_extension_available'; |
||
| 72 | } |
||
| 73 | |||
| 74 | add_action( |
||
| 75 | 'jetpack_register_gutenberg_extensions', |
||
| 76 | function () use ( $feature_name, $method_name ) { |
||
| 77 | call_user_func( array( 'Jetpack_Gutenberg', $method_name ), $feature_name ); |
||
| 78 | } |
||
| 79 | ); |
||
| 80 | |||
| 81 | // Ensure editor styles are registered so that the site editor knows about the |
||
| 82 | // editor style dependency when copying styles to the editor iframe. |
||
| 83 | if ( ! isset( $args['editor_style'] ) ) { |
||
| 84 | $args['editor_style'] = 'jetpack-blocks-editor'; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | return register_block_type( $slug, $args ); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Check if an extension/block is already registered |
||
| 93 | * |
||
| 94 | * @since 9.0.0 |
||
| 95 | * |
||
| 96 | * @param string $slug Name of extension/block to check. |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | public static function is_registered( $slug ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Remove the 'jetpack/' or jetpack-' prefix from an extension name |
||
| 106 | * |
||
| 107 | * @since 9.0.0 |
||
| 108 | * |
||
| 109 | * @param string $extension_name The extension name. |
||
| 110 | * |
||
| 111 | * @return string The unprefixed extension name. |
||
| 112 | */ |
||
| 113 | public static function remove_extension_prefix( $extension_name ) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in |
||
| 122 | * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can |
||
| 123 | * optionally fall back to that. |
||
| 124 | * |
||
| 125 | * @since 9.0.0 |
||
| 126 | * |
||
| 127 | * @param array $version_requirements { |
||
| 128 | * An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version. |
||
| 129 | * |
||
| 130 | * @type string $gutenberg Gutenberg version. |
||
| 131 | * @type string $wp Optional. WordPress version. |
||
| 132 | * } |
||
| 133 | * @param string $slug The slug of the block or plugin that has the Gutenberg version requirement. |
||
| 134 | * |
||
| 135 | * @return boolean True if the version of Gutenberg required by the block or plugin is available. |
||
| 136 | */ |
||
| 137 | View Code Duplication | public static function is_gutenberg_version_available( $version_requirements, $slug ) { |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Get CSS classes for a block. |
||
| 183 | * |
||
| 184 | * @since 9.0.0 |
||
| 185 | * |
||
| 186 | * @param string $slug Block slug. |
||
| 187 | * @param array $attr Block attributes. |
||
| 188 | * @param array $extra Potential extra classes you may want to provide. |
||
| 189 | * |
||
| 190 | * @return string $classes List of CSS classes for a block. |
||
| 191 | */ |
||
| 192 | public static function classes( $slug, $attr, $extra = array() ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Does the page return AMP content. |
||
| 225 | * |
||
| 226 | * @since 9.0.0 |
||
| 227 | * |
||
| 228 | * @return bool $is_amp_request Are we on an AMP view. |
||
| 229 | */ |
||
| 230 | public static function is_amp_request() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Check whether or the block being registered is a standalone block, |
||
| 239 | * running in a context outside of the Jetpack plugin. |
||
| 240 | * |
||
| 241 | * @since 9.6.0 |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public static function is_standalone_block() { |
||
| 257 | } |
||
| 258 |