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() ) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Check if an extension/block is already registered |
||
| 107 | * |
||
| 108 | * @since 9.0.0 |
||
| 109 | * |
||
| 110 | * @param string $slug Name of extension/block to check. |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public static function is_registered( $slug ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Remove the 'jetpack/' or jetpack-' prefix from an extension name |
||
| 120 | * |
||
| 121 | * @since 9.0.0 |
||
| 122 | * |
||
| 123 | * @param string $extension_name The extension name. |
||
| 124 | * |
||
| 125 | * @return string The unprefixed extension name. |
||
| 126 | */ |
||
| 127 | public static function remove_extension_prefix( $extension_name ) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in |
||
| 136 | * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can |
||
| 137 | * optionally fall back to that. |
||
| 138 | * |
||
| 139 | * @since 9.0.0 |
||
| 140 | * |
||
| 141 | * @param array $version_requirements { |
||
| 142 | * An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version. |
||
| 143 | * |
||
| 144 | * @type string $gutenberg Gutenberg version. |
||
| 145 | * @type string $wp Optional. WordPress version. |
||
| 146 | * } |
||
| 147 | * @param string $slug The slug of the block or plugin that has the Gutenberg version requirement. |
||
| 148 | * |
||
| 149 | * @return boolean True if the version of Gutenberg required by the block or plugin is available. |
||
| 150 | */ |
||
| 151 | View Code Duplication | public static function is_gutenberg_version_available( $version_requirements, $slug ) { |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Get CSS classes for a block. |
||
| 197 | * |
||
| 198 | * @since 9.0.0 |
||
| 199 | * |
||
| 200 | * @param string $slug Block slug. |
||
| 201 | * @param array $attr Block attributes. |
||
| 202 | * @param array $extra Potential extra classes you may want to provide. |
||
| 203 | * |
||
| 204 | * @return string $classes List of CSS classes for a block. |
||
| 205 | */ |
||
| 206 | public static function classes( $slug, $attr, $extra = array() ) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Does the page return AMP content. |
||
| 239 | * |
||
| 240 | * @since 9.0.0 |
||
| 241 | * |
||
| 242 | * @return bool $is_amp_request Are we on an AMP view. |
||
| 243 | */ |
||
| 244 | public static function is_amp_request() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Is the current theme an FSE/Site Editor theme. |
||
| 253 | * |
||
| 254 | * @since 9.8.0 |
||
| 255 | * |
||
| 256 | * @return bool True if the current theme is an FSE/Site Editor theme. |
||
| 257 | */ |
||
| 258 | public static function is_fse_theme() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Check whether or the block being registered is a standalone block, |
||
| 273 | * running in a context outside of the Jetpack plugin. |
||
| 274 | * |
||
| 275 | * @since 9.6.0 |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | public static function is_standalone_block() { |
||
| 291 | } |
||
| 292 |