Completed
Push — add/wordads-shortcode ( b5b352...d5d630 )
by
unknown
25:10 queued 12:01
created

Jetpack_WordAds_Shortcode::mce_external_plugins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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
		global $wordads;
20
21
		if ( empty( $wordads ) ) {
22
			return null;
23
		}
24
		add_shortcode( 'wordad', array( $this, 'wordads_shortcode' ) );
25
	}
26
27
	/**
28
	 * Add hooks according to screen.
29
	 *
30
	 * @param WP_Screen $screen Data about current screen.
31
	 */
32
	public static function add_hooks( $screen ) {
33 View Code Duplication
		if ( isset( $screen->base ) && 'post' === $screen->base ) {
34
			add_action( 'admin_notices', array( __CLASS__, 'handle_editor_view_js' ) );
35
			add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
36
		}
37
	}
38
39
	public static function mce_external_plugins( $plugin_array ) {
40
		$plugin_array['wordads'] = Jetpack::get_file_url_for_environment(
41
			'_inc/build/wordads/js/tinymce-plugin-wordads-button.min.js',
42
			'modules/wordads/js/tinymce-plugin-wordads-button.js'
43
		);
44
		return $plugin_array;
45
	}
46
47
	/**
48
	 * WordPress Shortcode Editor View JS Code
49
	 */
50
	public static function handle_editor_view_js() {
51
		add_filter( 'mce_external_plugins', array( __CLASS__, 'mce_external_plugins' ) );
52
		add_filter( 'mce_buttons', array( __CLASS__, 'mce_buttons' ) );
53
		wp_enqueue_script(
54
			'wordads-shortcode-editor-view',
55
			Jetpack::get_file_url_for_environment(
56
				'_inc/build/wordads/js/editor-view.min.js',
57
				'modules/wordads/js/editor-view.js'
58
			),
59
			array( 'wp-util', 'jquery', 'quicktags' ),
60
			false,
61
			true
62
		);
63
		wp_localize_script( 'wordads-shortcode-editor-view', 'wordadsEditorView', array(
64
			'labels' => array(
65
				'tinymce_label' => __( 'Insert Ad', 'jetpack' ),
66
			)
67
		) );
68
	}
69
70
	/**
71
	 * Our [wordad] shortcode.
72
	 * Prints a WordAds Ad.
73
	 *
74
	 * @param array  $atts    Array of shortcode attributes.
75
	 * @param string $content Post content.
76
	 *
77
	 * @return string HTML for WordAds shortcode.
78
	 */
79
	static function wordads_shortcode( $atts, $content = '' ) {
80
		$atts = shortcode_atts(
81
			array(
82
			), $atts, 'wordads'
83
		);
84
85
		return self::wordads_shortcode_html( $atts, $content );
86
	}
87
88
	/**
89
	 * The shortcode output
90
	 *
91
	 * @param array  $atts    Array of shortcode attributes.
92
	 * @param string $content Post content.
93
	 *
94
	 * @return string HTML output
95
	 */
96
	static function wordads_shortcode_html( $atts, $content = '' ) {
97
		global $wordads;
98
99
		if ( empty( $wordads ) ) {
100
			return __( '<div>The WordAds module is not active</div>' );
101
		}
102
103
		$html = '<div class="jetpack-wordad" itemscope itemtype="https://schema.org/WPAdBlock">';
104
105
		$html .= '</div>';
106
107
		$html = $wordads->insert_inline_ad( $html );
108
109
		return $html;
110
	}
111
112
	public static function admin_head() {
113
		remove_action( 'media_buttons', 'wordads_media_button', 9999 );
114
		add_action( 'media_buttons', array( __CLASS__, 'wordads_media_button' ), 9999 );
115
	}
116
117 View Code Duplication
	public static function wordads_media_button() {
118
		$title = __( 'Insert Ad', 'jetpack' );
119
		?>
120
121
		<button type="button" id="insert-jetpack-wordads-inline-ad" class="button" title="<?php echo esc_attr( $title ); ?>" href="javascript:;">
122
			<span class="jetpack-wordads-inline-ad-icon jetpack-contact-form-icon"></span>
123
			<?php echo esc_html( $title ); ?>
124
		</button>
125
126
		<?php
127
	}
128
}
129
130
new Jetpack_WordAds_Shortcode();
131
add_action( 'current_screen', array( 'Jetpack_WordAds_Shortcode', 'add_hooks' ) );
132