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 |
||
| 19 | class Blocks { |
||
| 20 | /** |
||
| 21 | * Get CSS classes for a block. |
||
| 22 | * |
||
| 23 | * @since 9.0.0 |
||
| 24 | * |
||
| 25 | * @param string $slug Block slug. |
||
| 26 | * @param array $attr Block attributes. |
||
| 27 | * @param array $extra Potential extra classes you may want to provide. |
||
| 28 | * |
||
| 29 | * @return string $classes List of CSS classes for a block. |
||
| 30 | */ |
||
| 31 | public static function classes( $slug, $attr, $extra = array() ) { |
||
| 32 | if ( empty( $slug ) ) { |
||
| 33 | return ''; |
||
| 34 | } |
||
| 35 | |||
| 36 | // Basic block name class. |
||
| 37 | $classes = array( |
||
| 38 | 'wp-block-jetpack-' . $slug, |
||
| 39 | ); |
||
| 40 | |||
| 41 | // Add alignment if provided. |
||
| 42 | View Code Duplication | if ( |
|
| 43 | ! empty( $attr['align'] ) |
||
| 44 | && in_array( $attr['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true ) |
||
| 45 | ) { |
||
| 46 | $classes[] = 'align' . $attr['align']; |
||
| 47 | } |
||
| 48 | |||
| 49 | // Add custom classes if provided in the block editor. |
||
| 50 | if ( ! empty( $attr['className'] ) ) { |
||
| 51 | $classes[] = $attr['className']; |
||
| 52 | } |
||
| 53 | |||
| 54 | // Add any extra classes. |
||
| 55 | View Code Duplication | if ( is_array( $extra ) && ! empty( $extra ) ) { |
|
| 56 | $classes = array_merge( $classes, array_filter( $extra ) ); |
||
| 57 | } |
||
| 58 | |||
| 59 | return implode( ' ', $classes ); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Does the page return AMP content. |
||
| 64 | * |
||
| 65 | * @return bool $is_amp_request Are we on an AMP view. |
||
| 66 | */ |
||
| 67 | public static function is_amp_request() { |
||
| 73 | } |
||
| 74 | |||
| 75 |