Completed
Push — add/module-registration-refact... ( af2fe6...74b9d5 )
by
unknown
295:53 queued 288:51
created

Jetpack_WordAds_Gutenblock::gutenblock_render()   C

Complexity

Conditions 12
Paths 7

Size

Total Lines 36

Duplication

Lines 3
Ratio 8.33 %

Importance

Changes 0
Metric Value
cc 12
nc 7
nop 1
dl 3
loc 36
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Ads Block.
4
 *
5
 * @since 7.1.0
6
 *
7
 * @package Jetpack
8
 */
9
class Jetpack_WordAds_Gutenblock {
10
	const BLOCK_NAME = 'jetpack/wordads';
11
12
	/**
13
	 * Check if site is on WP.com Simple.
14
	 *
15
	 * @return bool
16
	 */
17
	private static function is_wpcom() {
18
		return defined( 'IS_WPCOM' ) && IS_WPCOM;
19
	}
20
	/**
21
	 * Check if the WordAds module is active.
22
	 *
23
	 * @return bool
24
	 */
25
	private static function is_jetpack_module_active() {
26
		return method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'wordads' );
27
	}
28
29
	/**
30
	 * Check if the site is approved for ads for WP.com Simple sites.
31
	 *
32
	 * @return bool
33
	 */
34
	private static function is_available() {
35
		if ( self::is_wpcom() ) {
36
			return has_any_blog_stickers( array( 'wordads', 'wordads-approved', 'wordads-approved-misfits' ), get_current_blog_id() );
37
		}
38
39
		return self::is_jetpack_module_active();
40
	}
41
42
	/**
43
	 * Register the WordAds block.
44
	 */
45
	public static function register() {
46
		if ( self::is_available() ) {
47
			jetpack_register_block(
48
				self::BLOCK_NAME,
49
				array(
50
					'render_callback' => array( 'Jetpack_WordAds_Gutenblock', 'gutenblock_render' ),
51
				)
52
			);
53
		}
54
	}
55
56
	/**
57
	 * Set if the WordAds block is available.
58
	 */
59
	public static function set_availability() {
60
		if ( ! self::is_available() ) {
61
			Jetpack_Gutenberg::set_extension_unavailable( self::BLOCK_NAME, 'WordAds unavailable' );
62
			return;
63
		}
64
		// Make the block available. Just in case it wasn't registed before.
65
		Jetpack_Gutenberg::set_extension_available( self::BLOCK_NAME );
66
	}
67
68
	/**
69
	 * Renders the WordAds block.
70
	 *
71
	 * @param array $attr Block attributes.
72
	 *
73
	 * @return string Block HTML.
74
	 */
75
	public static function gutenblock_render( $attr ) {
76
		global $wordads;
77
78
		/** This filter is already documented in modules/wordads/wordads.php `insert_ad()` */
79
		if ( empty( $wordads ) || is_feed() || apply_filters( 'wordads_inpost_disable', false ) ) {
80
			return '';
81
		}
82
83
		if ( ! empty( $attr['hideMobile'] ) && $wordads->params->is_mobile() ) {
84
			return '';
85
		}
86
87
		if ( ! self::is_wpcom() && $wordads->option( 'wordads_house' ) ) {
88
			return $wordads->get_ad( 'inline', 'house' );
89
		}
90
91
		// section_id is mostly depricated at this point, but it helps us (devs) keep track of which ads end up where
92
		// 6 is to keep track of gutenblock ads.
93
		$section_id = $wordads->params->blog_id . '6';
94
		$align      = 'center';
95 View Code Duplication
		if ( isset( $attr['align'] ) && in_array( $attr['align'], array( 'left', 'center', 'right' ), true ) ) {
96
			$align = $attr['align'];
97
		}
98
		$align = 'align' . $align;
99
100
		$ad_tag_ids = $wordads->get_ad_tags();
101
		$format     = 'mrec';
102
		if ( isset( $attr['format'] ) && in_array( $attr['format'], array_keys( $ad_tag_ids ), true ) ) {
103
			$format = $attr['format'];
104
		}
105
106
		$height  = $ad_tag_ids[ $format ]['height'];
107
		$width   = $ad_tag_ids[ $format ]['width'];
108
		$snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'inline', $wordads->get_solo_unit_css() );
109
		return $wordads->get_ad_div( 'inline', $snippet, array( $align ) );
110
	}
111
}
112
113
add_action(
114
	'init',
115
	array( 'Jetpack_WordAds_Gutenblock', 'register' )
116
);
117
118
add_action(
119
	'jetpack_register_gutenberg_extensions',
120
	array( 'Jetpack_WordAds_Gutenblock', 'set_availability' )
121
);
122