Completed
Push — update/remove-setup-wizard ( 682b52...319559 )
by
unknown
595:57 queued 587:10
created

Jetpack_Wizard_Banner::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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