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 //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 9 | |||
| 10 | namespace Automattic\Jetpack\Extensions; |
||
| 11 | |||
| 12 | use Jetpack; |
||
| 13 | use Jetpack_Gutenberg; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Jetpack's Ads Block class. |
||
| 17 | * |
||
| 18 | * @since 7.1.0 |
||
| 19 | */ |
||
| 20 | class WordAds { |
||
| 21 | const FEATURE_NAME = 'wordads'; |
||
| 22 | const BLOCK_NAME = 'jetpack/' . self::FEATURE_NAME; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Check if site is on WP.com Simple. |
||
| 26 | * |
||
| 27 | * @return bool |
||
| 28 | */ |
||
| 29 | private static function is_wpcom() { |
||
| 32 | /** |
||
| 33 | * Check if the WordAds module is active. |
||
| 34 | * |
||
| 35 | * @return bool |
||
| 36 | */ |
||
| 37 | private static function is_jetpack_module_active() { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Check if the site is approved for ads for WP.com Simple sites. |
||
| 43 | * |
||
| 44 | * @return bool |
||
| 45 | */ |
||
| 46 | private static function is_available() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Register the WordAds block. |
||
| 56 | */ |
||
| 57 | public static function register() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set if the WordAds block is available. |
||
| 70 | */ |
||
| 71 | public static function set_availability() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Renders the WordAds block. |
||
| 82 | * |
||
| 83 | * @param array $attr Block attributes. |
||
| 84 | * |
||
| 85 | * @return string Block HTML. |
||
| 86 | */ |
||
| 87 | public static function gutenblock_render( $attr ) { |
||
| 88 | global $wordads; |
||
| 89 | |||
| 90 | /** This filter is already documented in modules/wordads/wordads.php `insert_ad()` */ |
||
| 91 | if ( |
||
| 92 | empty( $wordads ) |
||
| 93 | || empty( $wordads->params ) |
||
| 94 | || is_feed() |
||
| 95 | || apply_filters( 'wordads_inpost_disable', false ) |
||
| 96 | ) { |
||
| 97 | return ''; |
||
| 98 | } |
||
| 99 | |||
| 100 | if ( ! empty( $attr['hideMobile'] ) && $wordads->params->is_mobile() ) { |
||
| 101 | return ''; |
||
| 102 | } |
||
| 103 | |||
| 104 | if ( ! self::is_wpcom() && $wordads->option( 'wordads_house' ) ) { |
||
| 105 | return $wordads->get_ad( 'inline', 'house' ); |
||
| 106 | } |
||
| 107 | |||
| 108 | // section_id is mostly depricated at this point, but it helps us (devs) keep track of which ads end up where |
||
| 109 | // 6 is to keep track of gutenblock ads. |
||
| 110 | $section_id = $wordads->params->blog_id . '6'; |
||
| 111 | $align = 'center'; |
||
| 112 | View Code Duplication | if ( isset( $attr['align'] ) && in_array( $attr['align'], array( 'left', 'center', 'right' ), true ) ) { |
|
| 113 | $align = $attr['align']; |
||
| 114 | } |
||
| 115 | $align = 'align' . $align; |
||
| 116 | |||
| 117 | $ad_tag_ids = $wordads->get_ad_tags(); |
||
| 132 |