Completed
Push — update/wpcom-premium-block-reg... ( ceb48a )
by Jeremy
07:15
created

Jetpack_WordAds_Gutenblock::register()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
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
	 * Register the WordAds block.
31
	 */
32
	public static function register() {
33
		// On WordPress.com the WordAds module is always active.
34
		if ( self::is_jetpack_module_active() ) {
35
			jetpack_register_block(
36
				self::BLOCK_NAME,
37
				array(
38
					'render_callback' => array( 'Jetpack_WordAds_Gutenblock', 'gutenblock_render' ),
39
				),
40
				array(
41
					'wpcom'   => 'wordads',
42
					'jetpack' => 'wordads-jetpack',
43
				)
44
			);
45
		}
46
	}
47
48
	/**
49
	 * Renders the WordAds block.
50
	 *
51
	 * @param array $attr Block attributes.
52
	 *
53
	 * @return string Block HTML.
54
	 */
55
	public static function gutenblock_render( $attr ) {
56
		global $wordads;
57
58
		/** This filter is already documented in modules/wordads/wordads.php `insert_ad()` */
59
		if (
60
			empty( $wordads )
61
			|| empty( $wordads->params )
62
			|| is_feed()
63
			|| apply_filters( 'wordads_inpost_disable', false )
64
		) {
65
			return '';
66
		}
67
68
		if ( ! empty( $attr['hideMobile'] ) && $wordads->params->is_mobile() ) {
69
			return '';
70
		}
71
72
		if ( ! self::is_wpcom() && $wordads->option( 'wordads_house' ) ) {
73
			return $wordads->get_ad( 'inline', 'house' );
74
		}
75
76
		// section_id is mostly depricated at this point, but it helps us (devs) keep track of which ads end up where
77
		// 6 is to keep track of gutenblock ads.
78
		$section_id = $wordads->params->blog_id . '6';
79
		$align      = 'center';
80 View Code Duplication
		if ( isset( $attr['align'] ) && in_array( $attr['align'], array( 'left', 'center', 'right' ), true ) ) {
81
			$align = $attr['align'];
82
		}
83
		$align = 'align' . $align;
84
85
		$ad_tag_ids = $wordads->get_ad_tags();
86
		$format     = 'mrec';
87
		if ( isset( $attr['format'] ) && in_array( $attr['format'], array_keys( $ad_tag_ids ), true ) ) {
88
			$format = $attr['format'];
89
		}
90
91
		$height  = $ad_tag_ids[ $format ]['height'];
92
		$width   = $ad_tag_ids[ $format ]['width'];
93
		$snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'gutenberg', $wordads->get_solo_unit_css() );
94
		return $wordads->get_ad_div( 'inline', $snippet, array( $align ) );
95
	}
96
}
97
98
add_action(
99
	'init',
100
	array( 'Jetpack_WordAds_Gutenblock', 'register' )
101
);
102