Completed
Push — add/setup-wizard-url-states ( 042f0b...d2405a )
by
unknown
14:54 queued 07:18
created

Jetpack_Wizard_Banner::enqueue_banner_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 21
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
/**
3
 * Displays the first page of the Wizard in a banner form
4
 *
5
 * @package Jetpack
6
 */
7
8
use Automattic\Jetpack\Assets;
9
use Automattic\Jetpack\Assets\Logo as Jetpack_Logo;
10
11
/**
12
 * Jetpack_Wizard_Banner
13
 **/
14
class Jetpack_Wizard_Banner {
15
	/**
16
	 * Jetpack_Wizard_Banner
17
	 *
18
	 * @var Jetpack_Wizard_Banner
19
	 **/
20
	private static $instance = null;
21
22
	/**
23
	 * Factory method
24
	 */
25
	public static function init() {
26
		if ( is_null( self::$instance ) ) {
27
			self::$instance = new Jetpack_Wizard_Banner();
28
		}
29
30
		return self::$instance;
31
	}
32
33
	/**
34
	 * Jetpack_Wizard_Banner constructor.
35
	 */
36
	private function __construct() {
37
		add_action( 'current_screen', array( $this, 'maybe_initialize_hooks' ) );
38
	}
39
40
	/**
41
	 * Initialize hooks to display the banner
42
	 */
43
	public function maybe_initialize_hooks() {
44
		if ( ! $this->can_be_displayed() ) {
45
			return;
46
		}
47
48
		add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) );
49
		add_action( 'admin_notices', array( $this, 'render_banner' ) );
50
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) );
51
	}
52
53
	/**
54
	 * Can we display the banner?
55
	 */
56
	private function can_be_displayed() {
57
		if ( ! Jetpack_Wizard::can_be_displayed() ) {
58
			return false;
59
		}
60
61
		// We already display the wizard at the Jetpack area.
62
		if ( false !== strpos( get_current_screen()->id, 'jetpack' ) ) {
63
			return false;
64
		}
65
66
		if ( ! current_user_can( 'jetpack_manage_modules' ) ) {
67
			return false;
68
		}
69
70
		// Kill if banner has been dismissed.
71
		if ( Jetpack_Options::get_option( 'dismissed_wizard_banner' ) ) {
72
			return false;
73
		}
74
75
		if ( 'not-started' !== Jetpack_Options::get_option( 'setup_wizard_status', 'not-started' ) ) {
76
			return false;
77
		}
78
79
		return true;
80
	}
81
82
	/**
83
	 * Enqueue JavaScript files.
84
	 */
85 View Code Duplication
	public function enqueue_banner_scripts() {
86
		wp_enqueue_script(
87
			'jetpack-wizard-banner-js',
88
			Assets::get_file_url_for_environment(
89
				'_inc/build/jetpack-wizard-banner.min.js',
90
				'_inc/jetpack-wizard-banner.js'
91
			),
92
			array( 'jquery' ),
93
			JETPACK__VERSION,
94
			true
95
		);
96
97
		wp_localize_script(
98
			'jetpack-wizard-banner-js',
99
			'jp_banner',
100
			array(
101
				'ajax_url'          => admin_url( 'admin-ajax.php' ),
102
				'wizardBannerNonce' => wp_create_nonce( 'jp-wizard-banner-nonce' ),
103
			)
104
		);
105
	}
106
107
	/**
108
	 * Include the needed styles
109
	 */
110
	public function admin_banner_styles() {
111
		wp_enqueue_style(
112
			'jetpack-wizard-banner',
113
			Assets::get_file_url_for_environment(
114
				'css/jetpack-wizard-banner.min.css',
115
				'css/jetpack-wizard-banner.css'
116
			),
117
			array(),
118
			JETPACK__VERSION
119
		);
120
	}
121
122
	/**
123
	 * AJAX callback
124
	 */
125 View Code Duplication
	public static function ajax_callback() {
126
		check_ajax_referer( 'jp-wizard-banner-nonce', 'nonce' );
127
128
		if (
129
			current_user_can( 'jetpack_manage_modules' )
130
			&& isset( $_REQUEST['dismissBanner'] )
131
		) {
132
			Jetpack_Options::update_option( 'dismissed_wizard_banner', 1 );
133
			wp_send_json_success();
134
		}
135
136
		wp_die();
137
	}
138
139
	/**
140
	 * Renders the Wizard Banner
141
	 *
142
	 * Since this HTML replicates the contents of _inc/client/setup-wizard/intro-page/index.jsx,
143
	 * every time one is changed, the other should also be.
144
	 */
145
	public function render_banner() {
146
		$jetpack_logo     = new Jetpack_Logo();
147
		$powering_up_logo = plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE );
148
149
		?>
150
		<div id="jp-wizard-banner" class="jp-wizard-banner">
151
			<div class="jp-wizard-banner-grid">
152
				<div class="jp-wizard-banner-grid-a">
153
					<div class="jp-emblem">
154
						<?php
155
							echo $jetpack_logo->get_jp_emblem_larger(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
156
						?>
157
					</div>
158
					<h2 class="jp-wizard-banner-wizard-header">
159
						<?php esc_html_e( 'Set up Jetpack for better site security, performance, and more.', 'jetpack' ); ?>
160
					</h2>
161
					<p class="jp-wizard-banner-wizard-paragraph">
162
						<?php esc_html_e( 'Jetpack is a cloud-powered tool built by Automattic.', 'jetpack' ); ?>
163
					</p>
164
					<p class="jp-wizard-banner-wizard-paragraph">
165
						<?php esc_html_e( 'Answer a few questions and we’ll help you secure, speed up, customize, and grow your WordPress website.', 'jetpack' ); ?>
166
					</p>
167
					<div class="jp-wizard-banner-wizard-intro-question">
168
						<h2>
169
							<?php
170
							printf(
171
								/* translators: %s is the site name */
172
								esc_html__( 'What will %s be used for?', 'jetpack' ),
173
								esc_html( get_bloginfo( 'name' ) )
174
							);
175
							?>
176
						</h2>
177
						<div class="jp-wizard-banner-wizard-answer-buttons">
178
							<a
179
								class="button button-primary jp-wizard-banner-wizard-button"
180
								href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/income?use=personal' ) ); ?>"
181
							>
182
							<?php esc_html_e( 'Personal Use', 'jetpack' ); ?>
183
							</a>
184
							<a
185
								class="button button-primary jp-wizard-banner-wizard-button"
186
								href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/income?use=business' ) ); ?>"
187
							>
188
								<?php esc_html_e( 'Business Use', 'jetpack' ); ?>
189
							</a>
190
						</div>
191
						<a
192
							class="jp-wizard-banner-wizard-skip-link"
193
							href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/features' ) ); ?>"
194
						>
195
							<?php esc_html_e( 'Skip to recommended features', 'jetpack' ); ?>
196
						</a>
197
					</div>
198
				</div>
199
200
201
				<div class="jp-wizard-banner-grid-b">
202
					<img
203
						class="powering-up-img"
204
						width="200px"
205
						height="200px"
206
						src="<?php echo esc_url( $powering_up_logo ); ?>"
207
						alt="<?php esc_attr_e( 'A jetpack site powering up', 'jetpack' ); ?>"
208
					/>
209
				</div>
210
211
				<span
212
					class="notice-dismiss wizard-banner-dismiss"
213
					title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
214
				</span>
215
216
			</div>
217
218
		</div>
219
		<?php
220
	}
221
}
222