Completed
Push — update/gutenberg-unit-tests-re... ( 29e10f...0256b9 )
by
unknown
30:54 queued 23:09
created

Jetpack_Connection_Banner::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Connection_Banner {
4
	/**
5
	 * @var Jetpack_Connection_Banner
6
	 **/
7
	private static $instance = null;
8
9
	static function init() {
10
		if ( is_null( self::$instance ) ) {
11
			self::$instance = new Jetpack_Connection_Banner();
12
		}
13
14
		return self::$instance;
15
	}
16
17
	/**
18
	 * Jetpack_Connection_Banner constructor.
19
	 *
20
	 * Since we call the Jetpack_Connection_Banner:init() method from the `Jetpack` class, and after
21
	 * the admin_init action fires, we know that the admin is initialized at this point.
22
	 */
23
	private function __construct() {
24
		add_action( 'current_screen', array( $this, 'maybe_initialize_hooks' ) );
25
	}
26
27
	/**
28
	 * Given a string for the the banner was added, and an int that represents the slide to
29
	 * a URL for, this function returns a connection URL with a from parameter that will
30
	 * support split testing.
31
	 *
32
	 * @since 7.2   Event key format is now banner-connect-banner-72-dashboard or connect-banner-72-plugins.
33
	 *              The param $slide_num was removed since we removed all slides but the first one.
34
	 * @since 4.4.0
35
	 *
36
	 * @param string     $jp_version_banner_added A short version of when the banner was added. Ex. 44
37
	 *
38
	 * @return string
39
	 */
40
	function build_connect_url_for_slide( $jp_version_banner_added ) {
41
		global $current_screen;
42
		$url = Jetpack::init()->build_connect_url(
43
			true,
44
			false,
45
			sprintf( 'connect-banner-%s-%s', $jp_version_banner_added, $current_screen->base )
46
		);
47
		// Add a tracks event corresponding to the A/B version displayed
48
		$ab_test = Jetpack_Options::get_option( 'ab_connect_banner_green_bar' );
49
		if ( in_array( $ab_test, array( 'a', 'b' ), true ) ) {
50
			$url = add_query_arg( 'ab_connect_banner_green_bar', $ab_test, $url );
51
		}
52
		return add_query_arg( 'auth_approved', 'true', $url );
53
	}
54
55
	/**
56
	 * Return an img HTML tag pointing to the Jetpack logo. Includes alt text.
57
	 *
58
	 * @since 7.2
59
	 *
60
	 * @return string
61
	 */
62
	public static function get_jetpack_logo() {
63
		return sprintf(
64
			'<img src="%s" class="jetpack-logo" alt="%s" />',
65
			esc_url( plugins_url( 'images/jetpack-logo-green.svg', JETPACK__PLUGIN_FILE ) ),
66
			esc_attr__(
67
				'Jetpack is a free plugin that utilizes powerful WordPress.com servers to enhance your site and simplify managing it',
68
				'jetpack'
69
			)
70
		);
71
	}
72
73
	/**
74
	 * Will initialize hooks to display the new (as of 4.4) connection banner if the current user can
75
	 * connect Jetpack, if Jetpack has not been deactivated, and if the current page is the plugins page.
76
	 *
77
	 * This method should not be called if the site is connected to WordPress.com or if the site is in development mode.
78
	 *
79
	 * @since 4.4.0
80
	 * @since 4.5.0 Made the new (as of 4.4) connection banner display to everyone by default.
81
	 * @since 5.3.0 Running another split test between 4.4 banner and a new one in 5.3.
82
	 * @since 7.2   B test was removed.
83
	 *
84
	 * @param $current_screen
85
	 */
86
	function maybe_initialize_hooks( $current_screen ) {
87
		// Kill if banner has been dismissed
88
		if ( Jetpack_Options::get_option( 'dismissed_connection_banner' ) ) {
89
			return;
90
		}
91
92
		// Don't show the connect notice anywhere but the plugins.php after activating
93
		if ( 'plugins' !== $current_screen->base && 'dashboard' !== $current_screen->base ) {
94
			return;
95
		}
96
97
		if ( ! current_user_can( 'jetpack_connect' ) ) {
98
			return;
99
		}
100
101
		add_action( 'admin_notices', array( $this, 'render_banner' ) );
102
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) );
103
		add_action( 'admin_print_styles', array( Jetpack::init(), 'admin_banner_styles' ) );
104
105
		if ( Jetpack::state( 'network_nag' ) ) {
106
			add_action( 'network_admin_notices', array( $this, 'network_connect_notice' ) );
107
		}
108
109
		// Only fires immediately after plugin activation
110
		if ( get_transient( 'activated_jetpack' ) ) {
111
			add_action( 'admin_notices', array( $this, 'render_connect_prompt_full_screen' ) );
112
			delete_transient( 'activated_jetpack' );
113
		}
114
	}
115
116
	/**
117
	 * Enqueues JavaScript for new connection banner.
118
	 *
119
	 * @since 4.4.0
120
	 */
121 View Code Duplication
	public static function enqueue_banner_scripts() {
122
		wp_enqueue_script(
123
			'jetpack-connection-banner-js',
124
			Jetpack::get_file_url_for_environment(
125
				'_inc/build/jetpack-connection-banner.min.js',
126
				'_inc/jetpack-connection-banner.js'
127
			),
128
			array( 'jquery' ),
129
			JETPACK__VERSION,
130
			true
131
		);
132
133
		wp_localize_script(
134
			'jetpack-connection-banner-js',
135
			'jp_banner',
136
			array(
137
				'ajax_url' => admin_url( 'admin-ajax.php' ),
138
				'connectionBannerNonce' => wp_create_nonce( 'jp-connection-banner-nonce' ),
139
			)
140
		);
141
	}
142
143
	/**
144
	 * Performs an A/B test showing or hiding the green bar at the top of the connection dialog displayed in Dashboard or Plugins.
145
	 * We save which version we're showing so we always show the same to the same user.
146
	 * The "A" version displays the green bar at the top.
147
	 * The "B" version doesn't display it.
148
	 *
149
	 * @return void
150
	 */
151
	function get_ab_banner_top_bar() {
152
		$ab_test = Jetpack_Options::get_option( 'ab_connect_banner_green_bar' );
153
		// If it doesn't exist yet, generate it for later use and save it, so we always show the same to this user
154
		if ( ! $ab_test ) {
155
			$ab_test = 1 === rand( 1, 2 ) ? 'a' : 'b';
156
			Jetpack_Options::update_option( 'ab_connect_banner_green_bar', $ab_test);
157
		}
158
		if ( 'a' === $ab_test ) {
159
			?>
160
			<div class="jp-wpcom-connect__container-top-text">
161
				<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="0" fill="none" width="24" height="24"/><g><path d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm1 15h-2v-2h2v2zm0-4h-2l-.5-6h3l-.5 6z"/></g></svg>
162
				<span>
163
			    <?php esc_html_e( 'You’re almost done. Set up Jetpack to enable powerful security and performance tools for WordPress.', 'jetpack' ); ?>
164
				</span>
165
			</div>
166
			<?php
167
		}
168
	}
169
170
	/**
171
	 * Renders the new connection banner as of 4.4.0.
172
	 *
173
	 * @since 7.2   Copy and visual elements reduced to show the new focus of Jetpack on Security and Performance.
174
	 * @since 4.4.0
175
	 */
176
	function render_banner() { ?>
177
		<div id="message" class="updated jp-wpcom-connect__container">
178
			<?php $this->get_ab_banner_top_bar(); ?>
179
			<div class="jp-wpcom-connect__inner-container">
180
				<span
181
					class="notice-dismiss connection-banner-dismiss"
182
					title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
183
				</span>
184
185
				<div class="jp-wpcom-connect__content-container">
186
187
					<!-- slide 1: intro -->
188
					<div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active">
189
190
						<div class="jp-wpcom-connect__content-icon jp-connect-illo">
191
							<?php echo self::get_jetpack_logo(); ?>
192
							<img
193
								src="<?php echo plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ); ?>"
194
								class="jp-wpcom-connect__hide-phone-and-smaller"
195
								alt="<?php esc_attr_e(
196
									'Jetpack premium services offer even more powerful performance, security, ' .
197
									'and revenue tools to help you keep your site safe, fast, and help generate income.',
198
									'jetpack'
199
								); ?>"
200
								height="auto"
201
								width="225"
202
								/>
203
						</div>
204
205
						<div class="jp-wpcom-connect__slide-text">
206
							<h2><?php esc_html_e( 'Simplify your site security and performance with Jetpack', 'jetpack' ) ?></h2>
207
208
							<p>
209
								<?php
210
								esc_html_e(
211
									'Jetpack protects you against brute force attacks and unauthorized logins. Basic protection ' .
212
									'is always free, while premium plans add unlimited backups of your whole site, spam protection, ' .
213
									'malware scanning, and automated fixes.',
214
									'jetpack'
215
								);
216
								?>
217
							</p>
218
219
							<p>
220
								<?php
221
								esc_html_e(
222
									'Activate site accelerator tools and watch your page load times and hosting costs drop – we’ll ' .
223
									'optimize your images and serve them from our own powerful global network of servers, ' .
224
									'and speed up your mobile site to reduce bandwidth usage.',
225
									'jetpack'
226
								);
227
								?>
228
							</p>
229
230
							<div class="jp-banner__button-container">
231
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
232
								<a
233
									href="<?php echo esc_url( $this->build_connect_url_for_slide( '72' ) ); ?>"
234
									class="dops-button is-primary">
235
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
236
								</a>
237
							</div>
238
						</div>
239
					</div> <!-- end slide 1 -->
240
				</div>
241
			</div>
242
		</div>
243
		<?php
244
	}
245
246
	/**
247
	 * Renders the full-screen connection prompt.  Only shown once and on plugin activation.
248
	 */
249
	public static function render_connect_prompt_full_screen() {
250
		$current_screen = get_current_screen();
251
		if ( 'plugins' === $current_screen->base ) {
252
			$bottom_connect_url_from = 'full-screen-prompt';
253
		} else {
254
			$bottom_connect_url_from = 'landing-page-bottom';
255
		}
256
		?>
257
		<div class="jp-connect-full__container"><div class="jp-connect-full__container-card">
258
259
				<?php if ( 'plugins' === $current_screen->base ) : ?>
260
					<?php echo self::get_jetpack_logo(); ?>
261
262
					<div class="jp-connect-full__dismiss">
263
						<svg class="jp-connect-full__svg-dismiss" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><title>Dismiss Jetpack Connection Window</title><rect x="0" fill="none" /><g><path d="M17.705 7.705l-1.41-1.41L12 10.59 7.705 6.295l-1.41 1.41L10.59 12l-4.295 4.295 1.41 1.41L12 13.41l4.295 4.295 1.41-1.41L13.41 12l4.295-4.295z"/></g></svg>
264
					</div>
265
				<?php endif; ?>
266
267
				<div class="jp-connect-full__step-header">
268
					<h2 class="jp-connect-full__step-header-title"><?php esc_html_e( 'Activate essential WordPress security and performance tools by setting up Jetpack', 'jetpack' ) ?></h2>
269
				</div>
270
271
				<div class="jp-connect-full__row">
272
					<div class="jp-connect-full__slide">
273
						<div class="jp-connect-full__slide-card illustration">
274
							<img
275
									src="<?php echo plugins_url( 'images/security.svg', JETPACK__PLUGIN_FILE ); ?>"
276
									alt="<?php esc_attr_e( 'Security & Backups', 'jetpack' ); ?>"
277
							/>
278
						</div>
279
						<div class="jp-connect-full__slide-card">
280
							<p><?php
281
								esc_html_e(
282
									'Jetpack protects you against brute force attacks and unauthorized logins. ' .
283
									'Basic protection is always free, while premium plans add unlimited backups of your whole site, ' .
284
									'spam protection, malware scanning, and automated fixes.',
285
									'jetpack'
286
								);
287
								?></p>
288
						</div>
289
					</div>
290
					<div class="jp-connect-full__slide">
291
						<div class="jp-connect-full__slide-card illustration">
292
							<img
293
									src="<?php echo plugins_url( 'images/jetpack-speed.svg', JETPACK__PLUGIN_FILE ); ?>"
294
									alt="<?php esc_attr_e( 'Built-in Performance', 'jetpack' ); ?>"
295
							/>
296
						</div>
297
						<div class="jp-connect-full__slide-card">
298
							<p><?php
299
								esc_html_e(
300
									"Activate site accelerator tools and watch your page load times and hosting costs drop—" .
301
									"we'll optimize your images and serve them from our own powerful global network of servers, " .
302
									"and speed up your mobile site to reduce bandwidth usage.",
303
									'jetpack'
304
								);
305
								?></p>
306
						</div>
307
					</div>
308
				</div>
309
310
				<p class="jp-connect-full__tos-blurb">
311
					<?php jetpack_render_tos_blurb(); ?>
312
				</p>
313
				<p class="jp-connect-full__button-container">
314
					<a href="<?php echo esc_url( Jetpack::init()->build_connect_url( true, false, $bottom_connect_url_from ) ); ?>" class="dops-button is-primary">
315
						<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
316
					</a>
317
				</p>
318
				<?php if ( 'plugins' === $current_screen->base ) : ?>
319
					<p class="jp-connect-full__dismiss-paragraph">
320
						<a>
321
							<?php echo esc_html_x(
322
								'Not now, thank you.', 'a link that closes the modal window that offers to connect Jetpack', 'jetpack'
323
							); ?>
324
						</a>
325
					</p>
326
				<?php endif; ?>
327
			</div></div>
328
		<?php
329
	}
330
331
	/**
332
	 * Renders the legacy network connection banner.
333
	 */
334
	function network_connect_notice() {
335
		?>
336
		<div id="message" class="updated jetpack-message">
337
			<div class="squeezer">
338
				<h2>
339
					<?php
340
						echo wp_kses(
341
							__(
342
								'<strong>Jetpack is activated!</strong> Each site on your network must be connected individually by an admin on that site.',
343
								'jetpack'
344
							),
345
							array( 'strong' => array() )
346
						);
347
					?>
348
				</h2>
349
			</div>
350
		</div>
351
		<?php
352
	}
353
}
354