Completed
Push — add/tracks-events-to-setup-wiz... ( 720ead )
by
unknown
80:38 queued 62:43
created

Jetpack_Wizard_Banner::ajax_callback()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 16
nop 0
dl 0
loc 28
rs 8.8497
c 0
b 0
f 0
1
<?php
2
/**
3
 * Displays the first page of the Wizard in a banner form
4
 *
5
 * @package Jetpack
6
 */
7
8
use Automattic\Jetpack\Assets;
9
use Automattic\Jetpack\Assets\Logo as Jetpack_Logo;
10
use Automattic\Jetpack\Tracking;
11
12
/**
13
 * Jetpack_Wizard_Banner
14
 **/
15
class Jetpack_Wizard_Banner {
16
	/**
17
	 * Jetpack_Wizard_Banner
18
	 *
19
	 * @var Jetpack_Wizard_Banner
20
	 **/
21
	private static $instance = null;
22
23
	/**
24
	 * Factory method
25
	 */
26
	public static function init() {
27
		if ( is_null( self::$instance ) ) {
28
			self::$instance = new Jetpack_Wizard_Banner();
29
		}
30
31
		return self::$instance;
32
	}
33
34
	/**
35
	 * Jetpack_Wizard_Banner constructor.
36
	 */
37
	private function __construct() {
38
		add_action( 'current_screen', array( $this, 'maybe_initialize_hooks' ) );
39
	}
40
41
	/**
42
	 * Initialize hooks to display the banner
43
	 */
44
	public function maybe_initialize_hooks() {
45
		if ( ! $this->can_be_displayed() ) {
46
			return;
47
		}
48
49
		add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) );
50
		add_action( 'admin_notices', array( $this, 'render_banner' ) );
51
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) );
52
	}
53
54
	/**
55
	 * Can we display the banner?
56
	 */
57
	private function can_be_displayed() {
58
		if ( ! Jetpack_Wizard::can_be_displayed() ) {
59
			return false;
60
		}
61
62
		// Only the dashboard and plugins pages should see the banner.
63
		if ( ! in_array( get_current_screen()->id, array( 'dashboard', 'plugins' ), true ) ) {
64
			return false;
65
		}
66
67
		if ( ! current_user_can( 'jetpack_manage_modules' ) ) {
68
			return false;
69
		}
70
71
		// Kill if banner has been dismissed.
72
		if ( Jetpack_Options::get_option( 'dismissed_wizard_banner' ) ) {
73
			return false;
74
		}
75
76
		return true;
77
	}
78
79
	/**
80
	 * Enqueue JavaScript files.
81
	 */
82 View Code Duplication
	public function enqueue_banner_scripts() {
83
		wp_enqueue_script(
84
			'jetpack-wizard-banner-js',
85
			Assets::get_file_url_for_environment(
86
				'_inc/build/jetpack-wizard-banner.min.js',
87
				'_inc/jetpack-wizard-banner.js'
88
			),
89
			array( 'jquery' ),
90
			JETPACK__VERSION,
91
			true
92
		);
93
94
		wp_localize_script(
95
			'jetpack-wizard-banner-js',
96
			'jp_banner',
97
			array(
98
				'ajax_url'          => admin_url( 'admin-ajax.php' ),
99
				'wizardBannerNonce' => wp_create_nonce( 'jp-wizard-banner-nonce' ),
100
			)
101
		);
102
	}
103
104
	/**
105
	 * Include the needed styles
106
	 */
107
	public function admin_banner_styles() {
108
		wp_enqueue_style(
109
			'jetpack-wizard-banner',
110
			Assets::get_file_url_for_environment(
111
				'css/jetpack-wizard-banner.min.css',
112
				'css/jetpack-wizard-banner.css'
113
			),
114
			array(),
115
			JETPACK__VERSION
116
		);
117
	}
118
119
	/**
120
	 * AJAX callback
121
	 */
122
	public static function ajax_callback() {
123
		check_ajax_referer( 'jp-wizard-banner-nonce', 'nonce' );
124
125
		$tracking = new Tracking();
126
127
		if ( isset( $_REQUEST['personal'] ) ) {
128
			$tracking->record_user_event( 'jetpack_setup_wizard_banner_click', array( 'button' => 'personal' ) );
129
		}
130
131
		if ( isset( $_REQUEST['business'] ) ) {
132
			$tracking->record_user_event( 'jetpack_setup_wizard_banner_click', array( 'button' => 'business' ) );
133
		}
134
135
		if ( isset( $_REQUEST['skip'] ) ) {
136
			$tracking->record_user_event( 'jetpack_setup_wizard_banner_click', array( 'button' => 'skip' ) );
137
		}
138
139
		if (
140
			current_user_can( 'jetpack_manage_modules' )
141
			&& isset( $_REQUEST['dismissBanner'] )
142
		) {
143
			Jetpack_Options::update_option( 'dismissed_wizard_banner', 1 );
144
			$tracking->record_user_event( 'jetpack_setup_wizard_banner_dismiss' );
145
			wp_send_json_success();
146
		}
147
148
		wp_die();
149
	}
150
151
	/**
152
	 * Renders the Wizard Banner
153
	 *
154
	 * Since this HTML replicates the contents of _inc/client/setup-wizard/intro-page/index.jsx,
155
	 * every time one is changed, the other should also be.
156
	 */
157
	public function render_banner() {
158
		$jetpack_logo     = new Jetpack_Logo();
159
		$powering_up_logo = plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE );
160
161
		?>
162
		<div id="jp-wizard-banner" class="jp-wizard-banner">
163
			<div class="jp-wizard-banner-grid">
164
				<div class="jp-wizard-banner-grid-a">
165
					<div class="jp-emblem">
166
						<?php
167
							echo $jetpack_logo->get_jp_emblem_larger(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
168
						?>
169
					</div>
170
					<h2 class="jp-wizard-banner-wizard-header">
171
						<?php esc_html_e( 'Set up Jetpack for better site security, performance, and more.', 'jetpack' ); ?>
172
					</h2>
173
					<p class="jp-wizard-banner-wizard-paragraph">
174
						<?php esc_html_e( 'Jetpack is a cloud-powered tool built by Automattic.', 'jetpack' ); ?>
175
					</p>
176
					<p class="jp-wizard-banner-wizard-paragraph">
177
						<?php esc_html_e( 'Answer a few questions and we’ll help you secure, speed up, customize, and grow your WordPress website.', 'jetpack' ); ?>
178
					</p>
179
					<div class="jp-wizard-banner-wizard-intro-question">
180
						<h2>
181
							<?php
182
							printf(
183
								/* translators: %s is the site name */
184
								esc_html__( 'What will %s be used for?', 'jetpack' ),
185
								esc_html( get_bloginfo( 'name' ) )
186
							);
187
							?>
188
						</h2>
189
						<div class="jp-wizard-banner-wizard-answer-buttons">
190
							<a
191
								id="jp-wizard-banner-personal-button"
192
								class="button button-primary jp-wizard-banner-wizard-button"
193
								href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/income?use=personal' ) ); ?>"
194
							>
195
							<?php esc_html_e( 'Personal Use', 'jetpack' ); ?>
196
							</a>
197
							<a
198
								id="jp-wizard-banner-business-button"
199
								class="button button-primary jp-wizard-banner-wizard-button"
200
								href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/income?use=business' ) ); ?>"
201
							>
202
								<?php esc_html_e( 'Business Use', 'jetpack' ); ?>
203
							</a>
204
						</div>
205
						<a
206
							class="jp-wizard-banner-wizard-skip-link"
207
							href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/features' ) ); ?>"
208
						>
209
							<?php esc_html_e( 'Skip to recommended features', 'jetpack' ); ?>
210
						</a>
211
					</div>
212
				</div>
213
214
215
				<div class="jp-wizard-banner-grid-b">
216
					<img
217
						class="powering-up-img"
218
						width="200px"
219
						height="200px"
220
						src="<?php echo esc_url( $powering_up_logo ); ?>"
221
						alt="<?php esc_attr_e( 'A jetpack site powering up', 'jetpack' ); ?>"
222
					/>
223
				</div>
224
225
				<span
226
					class="notice-dismiss wizard-banner-dismiss"
227
					title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
228
				</span>
229
230
			</div>
231
232
		</div>
233
		<?php
234
	}
235
}
236