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 ( class_exists( Jetpack_Gutenberg::class ) ) { |
||
| 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 | return register_block_type( $slug, $args ); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Check if an extension/block is already registered |
||
| 86 | * |
||
| 87 | * @since 9.0.0 |
||
| 88 | * |
||
| 89 | * @param string $slug Name of extension/block to check. |
||
| 90 | * |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | public static function is_registered( $slug ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Remove the 'jetpack/' or jetpack-' prefix from an extension name |
||
| 99 | * |
||
| 100 | * @since 9.0.0 |
||
| 101 | * |
||
| 102 | * @param string $extension_name The extension name. |
||
| 103 | * |
||
| 104 | * @return string The unprefixed extension name. |
||
| 105 | */ |
||
| 106 | public static function remove_extension_prefix( $extension_name ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in |
||
| 115 | * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can |
||
| 116 | * optionally fall back to that. |
||
| 117 | * |
||
| 118 | * @since 9.0.0 |
||
| 119 | * |
||
| 120 | * @param array $version_requirements { |
||
| 121 | * An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version. |
||
| 122 | * |
||
| 123 | * @type string $gutenberg Gutenberg version. |
||
| 124 | * @type string $wp Optional. WordPress version. |
||
| 125 | * } |
||
| 126 | * @param string $slug The slug of the block or plugin that has the Gutenberg version requirement. |
||
| 127 | * |
||
| 128 | * @return boolean True if the version of Gutenberg required by the block or plugin is available. |
||
| 129 | */ |
||
| 130 | View Code Duplication | public static function is_gutenberg_version_available( $version_requirements, $slug ) { |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Get CSS classes for a block. |
||
| 176 | * |
||
| 177 | * @since 9.0.0 |
||
| 178 | * |
||
| 179 | * @param string $slug Block slug. |
||
| 180 | * @param array $attr Block attributes. |
||
| 181 | * @param array $extra Potential extra classes you may want to provide. |
||
| 182 | * |
||
| 183 | * @return string $classes List of CSS classes for a block. |
||
| 184 | */ |
||
| 185 | public static function classes( $slug, $attr, $extra = array() ) { |
||
| 186 | if ( empty( $slug ) ) { |
||
| 187 | return ''; |
||
| 188 | } |
||
| 189 | |||
| 190 | // Basic block name class. |
||
| 191 | $classes = array( |
||
| 192 | 'wp-block-jetpack-' . $slug, |
||
| 193 | ); |
||
| 194 | |||
| 195 | // Add alignment if provided. |
||
| 196 | if ( |
||
| 197 | ! empty( $attr['align'] ) |
||
| 198 | && in_array( $attr['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true ) |
||
| 199 | ) { |
||
| 200 | $classes[] = 'align' . $attr['align']; |
||
| 201 | } |
||
| 202 | |||
| 203 | // Add custom classes if provided in the block editor. |
||
| 204 | if ( ! empty( $attr['className'] ) ) { |
||
| 205 | $classes[] = $attr['className']; |
||
| 206 | } |
||
| 207 | |||
| 208 | // Add any extra classes. |
||
| 209 | if ( is_array( $extra ) && ! empty( $extra ) ) { |
||
| 210 | $classes = array_merge( $classes, array_filter( $extra ) ); |
||
| 211 | } |
||
| 212 | |||
| 213 | return implode( ' ', $classes ); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Does the page return AMP content. |
||
| 218 | * |
||
| 219 | * @since 9.0.0 |
||
| 220 | * |
||
| 221 | * @return bool $is_amp_request Are we on an AMP view. |
||
| 222 | */ |
||
| 223 | public static function is_amp_request() { |
||
| 229 | } |
||
| 230 |