summaryrefslogtreecommitdiff
blob: f1797d88112ea3047d9f0b0112d3dc84eb18179e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
 * Wordads shortcode.
 *
 * Examples:
 * [wordads]
 *
 * @package Jetpack
 */

/**
 * Embed WordAds 'ad' in post
 */
class Jetpack_WordAds_Shortcode {

	/**
	 * Used to determine whether scripts and styles have been enqueued already.
	 *
	 * @var bool false Should we enqueue scripts and styles.
	 */
	private $scripts_and_style_included = false;

	/**
	 * Initialize.
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'action_init' ) );
	}

	/**
	 * Register our shortcode and enqueue necessary files.
	 */
	public function action_init() {
		global $wordads;

		if ( empty( $wordads ) ) {
			return null;
		}

		add_shortcode( 'wordads', array( $this, 'wordads_shortcode' ) );
	}

	/**
	 * Our [wordads] shortcode.
	 * Prints a WordAds Ad.
	 *
	 * @param array  $atts    Array of shortcode attributes.
	 * @param string $content Post content.
	 *
	 * @return string HTML for WordAds shortcode.
	 */
	public static function wordads_shortcode( $atts, $content = '' ) {
		$atts = shortcode_atts( array(), $atts, 'wordads' );

		return self::wordads_shortcode_html( $atts, $content );
	}

	/**
	 * The shortcode output
	 *
	 * @param array  $atts    Array of shortcode attributes.
	 * @param string $content Post content.
	 *
	 * @return string HTML output
	 */
	private static function wordads_shortcode_html( $atts, $content = '' ) {
		global $wordads;

		if ( empty( $wordads ) ) {
			return '<div>' . __( 'The WordAds module is not active', 'jetpack' ) . '</div>';
		}

		$html = '<div class="jetpack-wordad" itemscope itemtype="https://schema.org/WPAdBlock"></div>';
		$html = $wordads->insert_inline_ad( $html );

		return $html;
	}
}

new Jetpack_WordAds_Shortcode();