Completed
Push — try/target-existing-faux-inlin... ( c498a5 )
by
unknown
11:06
created

Inline_Help::add_fab_styles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Inline Help.
4
 *
5
 * Handles providing a LiveChat icon within WPAdmin until such time
6
 * as the full live chat experience can be run in a non-Calypso environment.
7
 *
8
 * @package automattic/jetpack
9
 */
10
11
namespace Automattic\Jetpack\Dashboard_Customizations;
12
13
/**
14
 * Class Inline_Help.
15
 */
16
class Inline_Help {
17
18
	/**
19
	 * Inline_Help constructor.
20
	 */
21
	public function __construct() {
22
		$this->register_actions();
23
	}
24
25
	/**
26
	 * Registers actions.
27
	 *
28
	 * @return void
29
	 */
30
	public function register_actions() {
31
		// phpcs:disable WordPress.Security.NonceVerification.Recommended
32
		// Do not inject the FAB icon on embedded screens since the parent window may already contain a FAB icon.
33
		$is_framed = ! empty( $_GET['frame-nonce'] );
34
35
		if ( $is_framed ) {
36
			return;
37
		}
38
		// phpcs:enable WordPress.Security.NonceVerification.Recommended
39
40
		add_action( 'admin_footer', array( $this, 'add_fab_icon' ) );
41
42
		add_action( 'admin_enqueue_scripts', array( $this, 'add_fab_styles' ) );
43
44
		add_action( 'admin_enqueue_scripts', array( $this, 'add_fab_scripts' ) );
45
	}
46
47
	/**
48
	 * Outputs "FAB" icon markup and SVG.
49
	 *
50
	 * @return void|string the HTML markup for the FAB or early exit.
51
	 */
52
	public function add_fab_icon() {
53
54
		if ( wp_doing_ajax() ) {
55
			return;
56
		}
57
58
		$svg_allowed = array(
59
			'svg'   => array(
60
				'id'              => true,
61
				'class'           => true,
62
				'aria-hidden'     => true,
63
				'aria-labelledby' => true,
64
				'role'            => true,
65
				'xmlns'           => true,
66
				'width'           => true,
67
				'height'          => true,
68
				'viewbox'         => true, // <= Must be lower case!
69
			),
70
			'g'     => array( 'fill' => true ),
71
			'title' => array( 'title' => true ),
72
			'path'  => array(
73
				'd'    => true,
74
				'fill' => true,
75
			),
76
		);
77
78
		$gridicon_help = file_get_contents( __DIR__ . '/gridicon-help.svg', true );
79
80
		// Add tracking data to link to be picked up by Calypso for GA and Tracks usage.
81
		$tracking_href = add_query_arg(
82
			array(
83
				'utm_source'  => 'wp_admin',
84
				'utm_medium'  => 'other',
85
				'utm_content' => 'jetpack_masterbar_inline_help_click',
86
				'flags'       => 'a8c-analytics.on',
87
			),
88
			'https://wordpress.com/help'
89
		);
90
91
		// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
92
		// We trust that output in the template has been escaped.
93
		echo load_template(
94
			__DIR__ . '/inline-help-template.php',
95
			true,
96
			array(
97
				'href'        => $tracking_href,
98
				'icon'        => $gridicon_help,
99
				'svg_allowed' => $svg_allowed,
100
			)
101
		);
102
		// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
103
104
	}
105
106
	/**
107
	 * Enqueues FAB CSS styles.
108
	 *
109
	 * @return void
110
	 */
111
	public function add_fab_styles() {
112
		wp_enqueue_style( 'a8c-faux-inline-help', plugins_url( 'inline-help.css', __FILE__ ), array(), JETPACK__VERSION );
113
	}
114
115
	public function add_fab_scripts() {
116
		wp_enqueue_script(
117
			'a8c-faux-inline-help',
118
			plugins_url( 'inline-help.js', __FILE__ ),
119
			array(),
120
			JETPACK__VERSION,
121
			true
122
		);
123
	}
124
}
125