Completed
Push — remove/publicize-google-plus ( 287652...15e298 )
by Jeremy
14:15 queued 05:42
created

Jetpack_WordAds_Gutenblock   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 20
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 set_availability() 0 8 2
B gutenblock_render() 0 32 10
A register() 0 10 2
1
<?php
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
	private static function is_wpcom() {
13
		return defined( 'IS_WPCOM' ) && IS_WPCOM;
14
	}
15
16
	private static function is_jetpack_module_active() {
17
		return method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'wordads' );
18
	}
19
20
	private static function is_available() {
21
		if ( self::is_wpcom() ) {
22
			return has_any_blog_stickers( array( 'wordads', 'wordads-approved', 'wordads-approved-misfits' ), get_current_blog_id() );
23
		}
24
25
		return self::is_jetpack_module_active();
26
	}
27
28
	public static function register() {
29
		if ( self::is_available() ) {
30
			jetpack_register_block(
31
				self::BLOCK_NAME,
32
				array(
33
					'render_callback' => array( 'Jetpack_WordAds_Gutenblock', 'gutenblock_render' ),
34
				)
35
			);
36
		}
37
	}
38
39
	public static function set_availability() {
40
		if ( ! self::is_available() ) {
41
			Jetpack_Gutenberg::set_extension_unavailable( self::BLOCK_NAME, 'WordAds unavailable' );
42
			return;
43
		}
44
		// Make the block available. Just in case it wasn't registed before.
45
		Jetpack_Gutenberg::set_extension_available( self::BLOCK_NAME );
46
	}
47
48
	public static function gutenblock_render( $attr ) {
49
		global $wordads;
50
51
		/** This filter is already documented in modules/wordads/wordads.php `insert_ad()` */
52
		if ( empty( $wordads ) || is_feed() || apply_filters( 'wordads_inpost_disable', false ) ) {
53
			return '';
54
		}
55
56
		if ( ! self::is_wpcom() && $wordads->option( 'wordads_house' ) ) {
57
			return $wordads->get_ad( 'inline', 'house' );
58
		}
59
60
		// section_id is mostly depricated at this point, but it helps us (devs) keep track of which ads end up where
61
		// 6 is to keep track of gutenblock ads
62
		$section_id = $wordads->params->blog_id . '6';
63
		$align      = 'center';
64
		if ( isset( $attr['align'] ) && in_array( $attr['align'], array( 'left', 'center', 'right' ) ) ) {
65
			$align = $attr['align'];
66
		}
67
		$align = 'align' . $align;
68
69
		$ad_tag_ids = $wordads->get_ad_tags();
70
		$format     = 'mrec';
71
		if ( isset( $attr['format'] ) && in_array( $attr['format'], array_keys( $ad_tag_ids ) ) ) {
72
			$format = $attr['format'];
73
		}
74
75
		$height  = $ad_tag_ids[ $format ]['height'];
76
		$width   = $ad_tag_ids[ $format ]['width'];
77
		$snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'inline', $wordads->get_solo_unit_css() );
78
		return $wordads->get_ad_div( 'inline', $snippet, array( $align ) );
79
	}
80
}
81
82
add_action(
83
	'init',
84
	array( 'Jetpack_WordAds_Gutenblock', 'register' )
85
);
86
87
add_action(
88
	'jetpack_register_gutenberg_extensions',
89
	array( 'Jetpack_WordAds_Gutenblock', 'set_availability' )
90
);
91