Completed
Push — add/wordads-shortcode ( 83ee94...b5b352 )
by
unknown
40:01 queued 27:38
created

Jetpack_WordAds_Shortcode::add_scripts()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 49
Code Lines 31

Duplication

Lines 49
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
eloc 31
nc 10
nop 0
dl 49
loc 49
rs 6.7272
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Jetpack_WordAds_Shortcode::wordads_shortcode() 0 8 1
A Jetpack_WordAds_Shortcode::wordads_shortcode_html() 0 15 2
A Jetpack_WordAds_Shortcode::admin_head() 0 4 1
1
<?php
2
3
/**
4
 * Embed WordAds 'ad' in post
5
 *
6
 */
7
class Jetpack_WordAds_Shortcode {
8
9
	private $scripts_and_style_included = false;
0 ignored issues
show
Unused Code introduced by
The property $scripts_and_style_included is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11
	function __construct() {
12
		add_action( 'init', array( $this, 'action_init' ) );
13
	}
14
15
	/**
16
	 * Register our shortcode and enqueue necessary files.
17
	 */
18
	function action_init() {
19
		add_shortcode( 'wordad', array( $this, 'wordads_shortcode' ) );
20
	}
21
22
	/**
23
	 * Add hooks according to screen.
24
	 *
25
	 * @param WP_Screen $screen Data about current screen.
26
	 */
27
	public static function add_hooks( $screen ) {
28 View Code Duplication
		if ( isset( $screen->base ) && 'post' === $screen->base ) {
29
			add_action( 'admin_notices', array( __CLASS__, 'handle_editor_view_js' ) );
30
			add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
31
		}
32
	}
33
34
	/**
35
	 * WordPress Shortcode Editor View JS Code
36
	 */
37
	public static function handle_editor_view_js() {
38
		add_filter( 'mce_external_plugins', array( __CLASS__, 'mce_external_plugins' ) );
39
		add_filter( 'mce_buttons', array( __CLASS__, 'mce_buttons' ) );
40
		wp_enqueue_script(
41
			'wordads-shortcode-editor-view',
42
			Jetpack::get_file_url_for_environment(
43
				'_inc/build/wordads/js/wordads-shortcode.min.js',
44
				'modules/wordads/js/wordads-shortcode.js'
45
			),
46
			array( 'wp-util', 'jquery', 'quicktags' ),
47
			false,
48
			true
49
		);
50
	}
51
52
	/**
53
	 * Our [wordad] shortcode.
54
	 * Prints a WordAds Ad.
55
	 *
56
	 * @param array  $atts    Array of shortcode attributes.
57
	 * @param string $content Post content.
58
	 *
59
	 * @return string HTML for WordAds shortcode.
60
	 */
61
	static function wordads_shortcode( $atts, $content = '' ) {
62
		$atts = shortcode_atts(
63
			array(
64
			), $atts, 'wordads'
65
		);
66
67
		return self::wordads_shortcode_html( $atts, $content );
68
	}
69
70
	/**
71
	 * The shortcode output
72
	 *
73
	 * @param array  $atts    Array of shortcode attributes.
74
	 * @param string $content Post content.
75
	 *
76
	 * @return string HTML output
77
	 */
78
	static function wordads_shortcode_html( $atts, $content = '' ) {
79
		global $wordads;
80
81
		if ( empty( $wordads ) ) {
82
			return __( '<div>The WordAds module is not active</div>' );
83
		}
84
85
		$html = '<div class="jetpack-wordad" itemscope itemtype="https://schema.org/WPAdBlock">';
86
87
		$html .= '</div>';
88
89
		$html = $wordads->insert_ad( $html );
90
91
		return $html;
92
	}
93
94
	public static function admin_head() {
95
		remove_action( 'media_buttons', 'wordads_media_button', 9999 );
96
		add_action( 'media_buttons', array( __CLASS__, 'wordads_media_button' ), 9999 );
97
	}
98
99 View Code Duplication
	public static function wordads_media_button() {
100
		$title = __( 'Insert Ad', 'jetpack' );
101
		?>
102
103
		<button type="button" id="insert-jetpack-wordads-inline-ad" class="button" title="<?php echo esc_attr( $title ); ?>" href="javascript:;">
104
			<span class="jetpack-wordads-inline-ad-icon jetpack-contact-form-icon"></span>
105
			<?php echo esc_html( $title ); ?>
106
		</button>
107
108
		<?php
109
	}
110
}
111
112
new Jetpack_WordAds_Shortcode();
113
add_action( 'current_screen', array( 'Jetpack_WordAds_Shortcode', 'add_hooks' ) );
114