Completed
Push — delete/shortcopy-b-test-connec... ( 900da5 )
by
unknown
53:08 queued 46:34
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
		return add_query_arg( 'auth_approved', 'true', $url );
48
	}
49
50
	/**
51
	 * Return an img HTML tag pointing to the Jetpack logo. Includes alt text.
52
	 *
53
	 * @since 7.2
54
	 *
55
	 * @return string
56
	 */
57
	public static function get_jetpack_logo() {
58
		return sprintf(
59
			'<img src="%s" class="jetpack-logo" alt="%s" />',
60
			esc_url( plugins_url( 'images/jetpack-logo-green.svg', JETPACK__PLUGIN_FILE ) ),
61
			esc_attr__(
62
				'Jetpack is a free plugin that utilizes powerful WordPress.com servers to enhance your site and simplify managing it',
63
				'jetpack'
64
			)
65
		);
66
	}
67
68
	/**
69
	 * Will initialize hooks to display the new (as of 4.4) connection banner if the current user can
70
	 * connect Jetpack, if Jetpack has not been deactivated, and if the current page is the plugins page.
71
	 *
72
	 * This method should not be called if the site is connected to WordPress.com or if the site is in development mode.
73
	 *
74
	 * @since 4.4.0
75
	 * @since 4.5.0 Made the new (as of 4.4) connection banner display to everyone by default.
76
	 * @since 5.3.0 Running another split test between 4.4 banner and a new one in 5.3.
77
	 * @since 7.2   B test was removed.
78
	 *
79
	 * @param $current_screen
80
	 */
81
	function maybe_initialize_hooks( $current_screen ) {
82
		// Kill if banner has been dismissed
83
		if ( Jetpack_Options::get_option( 'dismissed_connection_banner' ) ) {
84
			return;
85
		}
86
87
		// Don't show the connect notice anywhere but the plugins.php after activating
88
		if ( 'plugins' !== $current_screen->base && 'dashboard' !== $current_screen->base ) {
89
			return;
90
		}
91
92
		if ( ! current_user_can( 'jetpack_connect' ) ) {
93
			return;
94
		}
95
96
		add_action( 'admin_notices', array( $this, 'render_banner' ) );
97
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) );
98
		add_action( 'admin_print_styles', array( Jetpack::init(), 'admin_banner_styles' ) );
99
100
		if ( Jetpack::state( 'network_nag' ) ) {
101
			add_action( 'network_admin_notices', array( $this, 'network_connect_notice' ) );
102
		}
103
104
		// Only fires immediately after plugin activation
105
		if ( get_transient( 'activated_jetpack' ) ) {
106
			add_action( 'admin_notices', array( $this, 'render_connect_prompt_full_screen' ) );
107
			delete_transient( 'activated_jetpack' );
108
		}
109
	}
110
111
	/**
112
	 * Enqueues JavaScript for new connection banner.
113
	 *
114
	 * @since 4.4.0
115
	 */
116 View Code Duplication
	public static function enqueue_banner_scripts() {
117
		wp_enqueue_script(
118
			'jetpack-connection-banner-js',
119
			Jetpack::get_file_url_for_environment(
120
				'_inc/build/jetpack-connection-banner.min.js',
121
				'_inc/jetpack-connection-banner.js'
122
			),
123
			array( 'jquery' ),
124
			JETPACK__VERSION,
125
			true
126
		);
127
128
		wp_localize_script(
129
			'jetpack-connection-banner-js',
130
			'jp_banner',
131
			array(
132
				'ajax_url' => admin_url( 'admin-ajax.php' ),
133
				'connectionBannerNonce' => wp_create_nonce( 'jp-connection-banner-nonce' ),
134
			)
135
		);
136
	}
137
138
	/**
139
	 * Renders the new connection banner as of 4.4.0.
140
	 *
141
	 * @since 7.2   Copy and visual elements reduced to show the new focus of Jetpack on Security and Performance.
142
	 * @since 4.4.0
143
	 */
144
	function render_banner() { ?>
145
		<div id="message" class="updated jp-wpcom-connect__container">
146
			<div class="jp-wpcom-connect__container-top-text">
147
				<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>
148
				<span><?php esc_html_e( 'You’re almost done. Set up Jetpack to boost your site performance and unlock powerful customization, marketing, and security tools.', 'jetpack' ); ?></span>
149
			</div>
150
			<div class="jp-wpcom-connect__inner-container">
151
				<span
152
					class="notice-dismiss connection-banner-dismiss"
153
					title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
154
				</span>
155
156
				<div class="jp-wpcom-connect__content-container">
157
158
					<!-- slide 1: intro -->
159
					<div class="jp-wpcom-connect__slide jp-wpcom-connect__slide-one jp__slide-is-active">
160
161
						<div class="jp-wpcom-connect__content-icon jp-connect-illo">
162
							<?php echo self::get_jetpack_logo(); ?>
163
							<img
164
								src="<?php echo plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE ); ?>"
165
								class="jp-wpcom-connect__hide-phone-and-smaller"
166
								alt="<?php esc_attr_e(
167
									'Jetpack premium services offer even more powerful performance, security, ' .
168
									'and revenue tools to help you keep your site safe, fast, and help generate income.',
169
									'jetpack'
170
								); ?>"
171
								height="auto"
172
								width="225"
173
								/>
174
						</div>
175
176
						<div class="jp-wpcom-connect__slide-text">
177
							<h2><?php esc_html_e( 'Simplify your site security and performance with Jetpack', 'jetpack' ) ?></h2>
178
179
							<p>
180
								<?php
181
								esc_html_e(
182
									'Jetpack free protects your site against brute force attacks and unauthorized logins. Our premium security servives also include unlimited backups of your entire site, spam protection, malware scanning, and automated fixes.',
183
									'jetpack'
184
								);
185
								?>
186
							</p>
187
188
							<p>
189
								<?php
190
								esc_html_e(
191
									'Activate Jetpack’s site accelerator to load pages faster, optimize your images, and serve your images and static files (like CSS and JavaScript) from our global network of WordPress.com servers. Speed up your site for mobile viewers and reduce bandwidth usage, which may lead to lower hosting costs.',
192
									'jetpack'
193
								);
194
								?>
195
							</p>
196
197
							<div class="jp-banner__button-container">
198
								<span class="jp-banner__tos-blurb"><?php jetpack_render_tos_blurb(); ?></span>
199
								<a
200
									href="<?php echo esc_url( $this->build_connect_url_for_slide( '72' ) ); ?>"
201
									class="dops-button is-primary">
202
									<?php esc_html_e( 'Set up Jetpack', 'jetpack' ); ?>
203
								</a>
204
							</div>
205
						</div>
206
					</div> <!-- end slide 1 -->
207
				</div>
208
			</div>
209
		</div>
210
		<?php
211
	}
212
213
	/**
214
	 * Renders the legacy network connection banner.
215
	 */
216
	function network_connect_notice() {
217
		?>
218
		<div id="message" class="updated jetpack-message">
219
			<div class="squeezer">
220
				<h2>
221
					<?php
222
						echo wp_kses(
223
							__(
224
								'<strong>Jetpack is activated!</strong> Each site on your network must be connected individually by an admin on that site.',
225
								'jetpack'
226
							),
227
							array( 'strong' => array() )
228
						);
229
					?>
230
				</h2>
231
			</div>
232
		</div>
233
		<?php
234
	}
235
}
236