Completed
Push — try/minimal-authorize ( 0f284f )
by
unknown
06:39
created

Jetpack_Connection_Banner::enqueue_button_styles()   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
30
	/**
31
	 * Given a string for the the banner was added, and an int that represents the slide to
32
	 * a URL for, this function returns a connection URL with a from parameter that will
33
	 * support split testing.
34
	 *
35
	 * @since 7.2   Event key format is now banner-connect-banner-72-dashboard or connect-banner-72-plugins.
36
	 *              The param $slide_num was removed since we removed all slides but the first one.
37
	 * @since 4.4.0
38
	 *
39
	 * @param string     $jp_version_banner_added A short version of when the banner was added. Ex. 44
40
	 *
41
	 * @return string
42
	 */
43
	function build_connect_url_for_slide( $jp_version_banner_added ) {
44
		global $current_screen;
45
		$url = Jetpack::init()->build_connect_url(
46
			true,
47
			false,
48
			sprintf( 'connect-banner-%s-%s', $jp_version_banner_added, $current_screen->base )
49
		);
50
		// Add a tracks event corresponding to the A/B version displayed
51
		$ab_test = Jetpack_Options::get_option( 'ab_connect_banner_green_bar' );
52
		if ( in_array( $ab_test, array( 'a', 'b' ), true ) ) {
53
			$url = add_query_arg( 'ab_connect_banner_green_bar', $ab_test, $url );
54
		}
55
		return add_query_arg( 'auth_approved', 'true', $url );
56
	}
57
58
	/**
59
	 * Will initialize hooks to display the new (as of 4.4) connection banner if the current user can
60
	 * connect Jetpack, if Jetpack has not been deactivated, and if the current page is the plugins page.
61
	 *
62
	 * This method should not be called if the site is connected to WordPress.com or if the site is in development mode.
63
	 *
64
	 * @since 4.4.0
65
	 * @since 4.5.0 Made the new (as of 4.4) connection banner display to everyone by default.
66
	 * @since 5.3.0 Running another split test between 4.4 banner and a new one in 5.3.
67
	 * @since 7.2   B test was removed.
68
	 *
69
	 * @param $current_screen
70
	 */
71
	function maybe_initialize_hooks( $current_screen ) {
72
		// Kill if banner has been dismissed
73
		if ( Jetpack_Options::get_option( 'dismissed_connection_banner' ) ) {
74
			return;
75
		}
76
77
		// Don't show the connect notice anywhere but the plugins.php after activating
78
		if ( 'plugins' !== $current_screen->base && 'dashboard' !== $current_screen->base ) {
79
			return;
80
		}
81
82
		if ( ! current_user_can( 'jetpack_connect' ) ) {
83
			return;
84
		}
85
86
		add_action( 'admin_notices', array( $this, 'render_banner' ) );
87
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) );
88
89
		// new fancy connect button
90
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_button_scripts' ) );
91
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_button_styles' ) );
92
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 ( true ) {//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_button_scripts() {
134
		// TODO - move this somewhere more central, where it can be a dependency of other stuff?
135
		$script_deps_path    = JETPACK__PLUGIN_DIR . '_inc/build/connect-button.deps.json';
136
		$script_dependencies = file_exists( $script_deps_path )
137
			? json_decode( file_get_contents( $script_deps_path ) )
138
			: array();
139
		$script_dependencies[] = 'wp-polyfill';
140
141
		wp_enqueue_script(
142
			'jetpack-connect-button',
143
			Assets::get_file_url_for_environment(
144
				'_inc/build/connect-button.js', // TODO - minify?
145
				'_inc/build/connect-button.js'
146
			),
147
			$script_dependencies,
148
			JETPACK__VERSION,
149
			true
150
		);
151
152
		// hacked from admin.js
153
		$jetpack_react_page = new Jetpack_React_Page();
154
		wp_localize_script( 'jetpack-connect-button', 'Initial_State', $jetpack_react_page->get_initial_state() );
155
	}
156
157
	public static function enqueue_button_styles() {
158
		wp_enqueue_style(
159
			'jetpack-connect-button',
160
			Assets::get_file_url_for_environment(
161
				'_inc/build/connect-button.css', // TODO - minify?
162
				'_inc/build/connect-button.css'
163
			),
164
			array(),
165
			JETPACK__VERSION,
166
			true
167
		);
168
	}
169
170
	/**
171
	 * Performs an A/B test showing or hiding the green bar at the top of the connection dialog displayed in Dashboard or Plugins.
172
	 * We save which version we're showing so we always show the same to the same user.
173
	 * The "A" version displays the green bar at the top.
174
	 * The "B" version doesn't display it.
175
	 *
176
	 * @return void
177
	 */
178
	function get_ab_banner_top_bar() {
179
		$ab_test = Jetpack_Options::get_option( 'ab_connect_banner_green_bar' );
180
		// If it doesn't exist yet, generate it for later use and save it, so we always show the same to this user
181
		if ( ! $ab_test ) {
182
			$ab_test = 1 === rand( 1, 2 ) ? 'a' : 'b';
183
			Jetpack_Options::update_option( 'ab_connect_banner_green_bar', $ab_test);
184
		}
185
		if ( 'a' === $ab_test ) {
186
			?>
187
			<div class="jp-wpcom-connect__container-top-text">
188
				<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>
189
				<span>
190
			    <?php esc_html_e( 'You’re almost done. Set up Jetpack to enable powerful security and performance tools for WordPress.', 'jetpack' ); ?>
191
				</span>
192
			</div>
193
			<?php
194
		}
195
	}
196
197
	/**
198
	 * Renders the new connection banner as of 4.4.0.
199
	 *
200
	 * @since 7.2   Copy and visual elements reduced to show the new focus of Jetpack on Security and Performance.
201
	 * @since 4.4.0
202
	 */
203
	function render_banner() { ?>
204
		<div id="message" class="updated jp-wpcom-connect__container">
205
			<?php $this->get_ab_banner_top_bar(); ?>
206
			<div class="jp-wpcom-connect__inner-container">
207
				<span
208
					class="notice-dismiss connection-banner-dismiss"
209
					title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
210
				</span>
211
212
				<div class="jp-wpcom-connect__content-container">
213
214
					<!-- slide 1: intro -->
215
					<div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active">
216
217
						<div class="jp-wpcom-connect__content-icon jp-connect-illo">
218
							<?php
219
							$logo = new Logo();
220
							echo $logo->render();
221
							?>
222
							<img
223
								src="<?php echo plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ); ?>"
224
								class="jp-wpcom-connect__hide-phone-and-smaller"
225
								alt="<?php esc_attr_e(
226
									'Jetpack premium services offer even more powerful performance, security, ' .
227
									'and revenue tools to help you keep your site safe, fast, and help generate income.',
228
									'jetpack'
229
								); ?>"
230
								height="auto"
231
								width="225"
232
								/>
233
						</div>
234
235
						<div class="jp-wpcom-connect__slide-text">
236
							<h2><?php esc_html_e( 'Simplify your site security and performance with Jetpack', 'jetpack' ) ?></h2>
237
238
							<p>
239
								<?php
240
								esc_html_e(
241
									'Jetpack protects you against brute force attacks and unauthorized logins. Basic protection ' .
242
									'is always free, while premium plans add unlimited backups of your whole site, spam protection, ' .
243
									'malware scanning, and automated fixes.',
244
									'jetpack'
245
								);
246
								?>
247
							</p>
248
249
							<p>
250
								<?php
251
								esc_html_e(
252
									'Activate site accelerator tools and watch your page load times decrease—we’ll ' .
253
									'optimize your images and serve them from our own powerful global network of servers, ' .
254
									'and speed up your mobile site to reduce bandwidth usage.',
255
									'jetpack'
256
								);
257
								?>
258
							</p>
259
260
							<div class="jp-banner__button-container">
261
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
262
								<a
263
									href="<?php echo esc_url( $this->build_connect_url_for_slide( '72' ) ); ?>"
264
									class="dops-button is-primary">
265
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
266
								</a>
267
							</div>
268
						</div>
269
					</div> <!-- end slide 1 -->
270
				</div>
271
			</div>
272
		</div>
273
		<?php
274
	}
275
276
	/**
277
	 * Renders the full-screen connection prompt.  Only shown once and on plugin activation.
278
	 */
279
	public static function render_connect_prompt_full_screen() {
280
		$current_screen = get_current_screen();
281
		if ( 'plugins' === $current_screen->base ) {
282
			$bottom_connect_url_from = 'full-screen-prompt';
0 ignored issues
show
Unused Code introduced by
$bottom_connect_url_from is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
283
		} else {
284
			$bottom_connect_url_from = 'landing-page-bottom';
0 ignored issues
show
Unused Code introduced by
$bottom_connect_url_from is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
285
		}
286
		?>
287
		<div class="jp-connect-full__container"><div class="jp-connect-full__container-card">
288
289
				<?php if ( 'plugins' === $current_screen->base ) : ?>
290
					<?php
291
					$logo = new Logo();
292
					echo $logo->render();
293
					?>
294
295
					<div class="jp-connect-full__dismiss">
296
						<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>
297
					</div>
298
				<?php endif; ?>
299
300
				<div class="jp-connect-full__step-header">
301
					<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>
302
				</div>
303
304
				<div class="jp-connect-full__row">
305
					<div class="jp-connect-full__slide">
306
						<div class="jp-connect-full__slide-card illustration">
307
							<img
308
									src="<?php echo plugins_url( 'images/security.svg', JETPACK__PLUGIN_FILE ); ?>"
309
									alt="<?php esc_attr_e( 'Security & Backups', 'jetpack' ); ?>"
310
							/>
311
						</div>
312
						<div class="jp-connect-full__slide-card">
313
							<p><?php
314
								esc_html_e(
315
									'Jetpack protects you against brute force attacks and unauthorized logins. ' .
316
									'Basic protection is always free, while premium plans add unlimited backups of your whole site, ' .
317
									'spam protection, malware scanning, and automated fixes.',
318
									'jetpack'
319
								);
320
								?></p>
321
						</div>
322
					</div>
323
					<div class="jp-connect-full__slide">
324
						<div class="jp-connect-full__slide-card illustration">
325
							<img
326
									src="<?php echo plugins_url( 'images/jetpack-speed.svg', JETPACK__PLUGIN_FILE ); ?>"
327
									alt="<?php esc_attr_e( 'Built-in Performance', 'jetpack' ); ?>"
328
							/>
329
						</div>
330
						<div class="jp-connect-full__slide-card">
331
							<p><?php
332
								esc_html_e(
333
									"Activate site accelerator tools and watch your page load times decrease—" .
334
									"we'll optimize your images and serve them from our own powerful global network of servers, " .
335
									"and speed up your mobile site to reduce bandwidth usage.",
336
									'jetpack'
337
								);
338
								?></p>
339
						</div>
340
					</div>
341
				</div>
342
343
				<div id="jp-connect-full__button-container">
344
				</div>
345
346
				<?php if ( 'plugins' === $current_screen->base ) : ?>
347
					<p class="jp-connect-full__dismiss-paragraph">
348
						<a>
349
							<?php echo esc_html_x(
350
								'Not now, thank you.', 'a link that closes the modal window that offers to connect Jetpack', 'jetpack'
351
							); ?>
352
						</a>
353
					</p>
354
				<?php endif; ?>
355
			</div></div>
356
		<?php
357
	}
358
359
	/**
360
	 * Renders the legacy network connection banner.
361
	 */
362
	function network_connect_notice() {
363
		?>
364
		<div id="message" class="updated jetpack-message">
365
			<div class="squeezer">
366
				<h2>
367
					<?php
368
						echo wp_kses(
369
							__(
370
								'<strong>Jetpack is activated!</strong> Each site on your network must be connected individually by an admin on that site.',
371
								'jetpack'
372
							),
373
							array( 'strong' => array() )
374
						);
375
					?>
376
				</h2>
377
			</div>
378
		</div>
379
		<?php
380
	}
381
}
382