Completed
Push — add/react-based-connect-button... ( 618ed0 )
by
unknown
06:30
created

enqueue_connect_button_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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