Completed
Push — add/changelog-71 ( b70f6c...ac535c )
by Jeremy
10:29
created

Jetpack_WordAds_Shortcode   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A action_init() 0 12 2
A wordads_shortcode() 0 5 1
A wordads_shortcode_html() 0 12 2
B gutenblock_render() 0 34 9
1
<?php
2
3
/**
4
 * Embed WordAds 'ad' in post
5
 */
6
class Jetpack_WordAds_Shortcode {
7
8
	private $scripts_and_style_included = false;
9
10
	function __construct() {
11
		add_action( 'init', array( $this, 'action_init' ) );
12
	}
13
14
	/**
15
	 * Register our shortcode and enqueue necessary files.
16
	 */
17
	function action_init() {
18
		global $wordads;
19
20
		if ( empty( $wordads ) ) {
21
			return null;
22
		}
23
		add_shortcode( 'wordads', array( $this, 'wordads_shortcode' ) );
24
25
		jetpack_register_block( 'jetpack/wordads', array(
26
			'render_callback' => array( $this, 'gutenblock_render' ),
27
		) );
28
	}
29
30
	/**
31
	 * Our [wordads] shortcode.
32
	 * Prints a WordAds Ad.
33
	 *
34
	 * @param array  $atts    Array of shortcode attributes.
35
	 * @param string $content Post content.
36
	 *
37
	 * @return string HTML for WordAds shortcode.
38
	 */
39
	static function wordads_shortcode( $atts, $content = '' ) {
40
		$atts = shortcode_atts( array(), $atts, 'wordads' );
41
42
		return self::wordads_shortcode_html( $atts, $content );
43
	}
44
45
	/**
46
	 * The shortcode output
47
	 *
48
	 * @param array  $atts    Array of shortcode attributes.
49
	 * @param string $content Post content.
50
	 *
51
	 * @return string HTML output
52
	 */
53
	static function wordads_shortcode_html( $atts, $content = '' ) {
54
		global $wordads;
55
56
		if ( empty( $wordads ) ) {
57
			return '<div>' . __( 'The WordAds module is not active', 'jetpack' ) . '</div>';
58
		}
59
60
		$html = '<div class="jetpack-wordad" itemscope itemtype="https://schema.org/WPAdBlock"></div>';
61
		$html = $wordads->insert_inline_ad( $html );
62
63
		return $html;
64
	}
65
66
	public static function gutenblock_render( $attr ) {
67
		global $wordads;
68
69
		/** This filter is already documented in modules/wordads/wordads.php `insert_ad()` */
70
		if ( is_feed() || apply_filters( 'wordads_inpost_disable', false ) ) {
71
			return '';
72
		}
73
74
		if ( $wordads->option( 'wordads_house' ) ) {
75
			return $wordads->get_ad( 'inline', 'house' );
76
		}
77
78
		// section_id is mostly depricated at this point, but it helps us (devs) keep track of which ads end up where
79
		// 6 is to keep track of gutenblock ads
80
		$section_id = 0 === $wordads->params->blog_id ?
81
			WORDADS_API_TEST_ID :
82
			$wordads->params->blog_id . '6';
83
84
		$align = 'center';
85
		if ( isset( $attr['align'] ) && in_array( $attr['align'], array( 'left', 'center', 'right' ) ) ) {
86
			$align = $attr['align'];
87
		}
88
		$align = 'align' . $align;
89
90
		$format = 'mrec';
91
		if ( isset( $attr['format'] ) && in_array( $attr['format'], array_keys( WordAds::$ad_tag_ids ) ) ) {
92
			$format = $attr['format'];
93
		}
94
95
		$height  = WordAds::$ad_tag_ids[ $format ]['height'];
96
		$width   = WordAds::$ad_tag_ids[ $format ]['width'];
97
		$snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'inline', WordAds::$SOLO_UNIT_CSS );
98
		return $wordads->get_ad_div( 'inline', $snippet, array( $align ) );
99
	}
100
}
101
102
new Jetpack_WordAds_Shortcode();
103