Completed
Push — try/jetpack-logo-package ( f86071 )
by
unknown
49:45 queued 40:45
created

Jetpack_Connection_Banner::get_jetpack_logo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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