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