Completed
Push — updates/infinity-vanilla-js ( fda899...9466ff )
by
unknown
07:55
created

WordAds   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 109
Duplicated Lines 2.75 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 3
loc 109
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A is_wpcom() 0 3 2
A is_jetpack_module_active() 0 3 2
A is_available() 0 7 2
A register() 0 10 2
A set_availability() 0 8 2
C gutenblock_render() 3 41 13

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * Ads Block.
4
 *
5
 * @since 7.1.0
6
 *
7
 * @package Jetpack
8
 */
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() {
30
		return defined( 'IS_WPCOM' ) && IS_WPCOM;
31
	}
32
	/**
33
	 * Check if the WordAds module is active.
34
	 *
35
	 * @return bool
36
	 */
37
	private static function is_jetpack_module_active() {
38
		return method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'wordads' );
39
	}
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() {
47
		if ( self::is_wpcom() ) {
48
			return has_any_blog_stickers( array( 'wordads', 'wordads-approved', 'wordads-approved-misfits' ), get_current_blog_id() );
49
		}
50
51
		return self::is_jetpack_module_active();
52
	}
53
54
	/**
55
	 * Register the WordAds block.
56
	 */
57
	public static function register() {
58
		if ( self::is_available() ) {
59
			jetpack_register_block(
60
				self::BLOCK_NAME,
61
				array(
62
					'render_callback' => array( __CLASS__, 'gutenblock_render' ),
63
				)
64
			);
65
		}
66
	}
67
68
	/**
69
	 * Set if the WordAds block is available.
70
	 */
71
	public static function set_availability() {
72
		if ( ! self::is_available() ) {
73
			Jetpack_Gutenberg::set_extension_unavailable( self::BLOCK_NAME, 'WordAds unavailable' );
74
			return;
75
		}
76
		// Make the block available. Just in case it wasn't registed before.
77
		Jetpack_Gutenberg::set_extension_available( self::BLOCK_NAME );
78
	}
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();
118
		$format     = 'mrec';
119
		if ( isset( $attr['format'] ) && in_array( $attr['format'], array_keys( $ad_tag_ids ), true ) ) {
120
			$format = $attr['format'];
121
		}
122
123
		$height  = $ad_tag_ids[ $format ]['height'];
124
		$width   = $ad_tag_ids[ $format ]['width'];
125
		$snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'gutenberg', $wordads->get_solo_unit_css() );
126
		return $wordads->get_ad_div( 'inline', $snippet, array( $align ) );
127
	}
128
}
129
130
add_action( 'init', array( 'Automattic\\Jetpack\\Extensions\\WordAds', 'register' ) );
131
add_action( 'jetpack_register_gutenberg_extensions', array( 'Automattic\\Jetpack\\Extensions\\WordAds', 'set_availability' ) );
132