Completed
Push — update/deactivation-modal-stat... ( 03d6ae...763765 )
by
unknown
07:34
created

Jetpack_Wizard_Banner::can_be_displayed()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 0
dl 0
loc 21
rs 9.2728
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
		return true;
76
	}
77
78
	/**
79
	 * Enqueue JavaScript files.
80
	 */
81 View Code Duplication
	public function enqueue_banner_scripts() {
82
		wp_enqueue_script(
83
			'jetpack-wizard-banner-js',
84
			Assets::get_file_url_for_environment(
85
				'_inc/build/jetpack-wizard-banner.min.js',
86
				'_inc/jetpack-wizard-banner.js'
87
			),
88
			array( 'jquery' ),
89
			JETPACK__VERSION,
90
			true
91
		);
92
93
		wp_localize_script(
94
			'jetpack-wizard-banner-js',
95
			'jp_banner',
96
			array(
97
				'ajax_url'          => admin_url( 'admin-ajax.php' ),
98
				'wizardBannerNonce' => wp_create_nonce( 'jp-wizard-banner-nonce' ),
99
			)
100
		);
101
	}
102
103
	/**
104
	 * Include the needed styles
105
	 */
106
	public function admin_banner_styles() {
107
		wp_enqueue_style(
108
			'jetpack-wizard-banner',
109
			Assets::get_file_url_for_environment(
110
				'css/jetpack-wizard-banner.min.css',
111
				'css/jetpack-wizard-banner.css'
112
			),
113
			array(),
114
			JETPACK__VERSION
115
		);
116
	}
117
118
	/**
119
	 * AJAX callback
120
	 */
121 View Code Duplication
	public static function ajax_callback() {
122
		check_ajax_referer( 'jp-wizard-banner-nonce', 'nonce' );
123
124
		if (
125
			current_user_can( 'jetpack_manage_modules' )
126
			&& isset( $_REQUEST['dismissBanner'] )
127
		) {
128
			Jetpack_Options::update_option( 'dismissed_wizard_banner', 1 );
129
			wp_send_json_success();
130
		}
131
132
		wp_die();
133
	}
134
135
	/**
136
	 * Renders the Wizard Banner
137
	 *
138
	 * Since this HTML replicates the contents of _inc/client/setup-wizard/intro-page/index.jsx,
139
	 * every time one is changed, the other should also be.
140
	 */
141
	public function render_banner() {
142
		$jetpack_logo     = new Jetpack_Logo();
143
		$powering_up_logo = plugins_url( 'images/jetpack-powering-up.svg', JETPACK__PLUGIN_FILE );
144
145
		?>
146
		<div id="jp-wizard-banner" class="jp-wizard-banner">
147
			<div class="jp-wizard-banner-grid">
148
				<div class="jp-wizard-banner-grid-a">
149
					<div class="jp-emblem">
150
						<?php
151
							echo $jetpack_logo->get_jp_emblem_larger(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
152
						?>
153
					</div>
154
					<h2 class="jp-wizard-banner-wizard-header">
155
						<?php esc_html_e( 'Set up Jetpack for better site security, performance, and more.', 'jetpack' ); ?>
156
					</h2>
157
					<p class="jp-wizard-banner-wizard-paragraph">
158
						<?php esc_html_e( 'Jetpack is a cloud-powered tool built by Automattic.', 'jetpack' ); ?>
159
					</p>
160
					<p class="jp-wizard-banner-wizard-paragraph">
161
						<?php esc_html_e( 'Answer a few questions and we’ll help you secure, speed up, customize, and grow your WordPress website.', 'jetpack' ); ?>
162
					</p>
163
					<div class="jp-wizard-banner-wizard-intro-question">
164
						<h2>
165
							<?php
166
							printf(
167
								/* translators: %s is the site name */
168
								esc_html__( 'What will %s be used for?', 'jetpack' ),
169
								esc_html( get_bloginfo( 'name' ) )
170
							);
171
							?>
172
						</h2>
173
						<div class="jp-wizard-banner-wizard-answer-buttons">
174
							<a
175
								class="button button-primary jp-wizard-banner-wizard-button"
176
								href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/income?use=personal' ) ); ?>"
177
							>
178
							<?php esc_html_e( 'Personal Use', 'jetpack' ); ?>
179
							</a>
180
							<a
181
								class="button button-primary jp-wizard-banner-wizard-button"
182
								href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/income?use=business' ) ); ?>"
183
							>
184
								<?php esc_html_e( 'Business Use', 'jetpack' ); ?>
185
							</a>
186
						</div>
187
						<a
188
							class="jp-wizard-banner-wizard-skip-link"
189
							href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/features' ) ); ?>"
190
						>
191
							<?php esc_html_e( 'Skip to recommended features', 'jetpack' ); ?>
192
						</a>
193
					</div>
194
				</div>
195
196
197
				<div class="jp-wizard-banner-grid-b">
198
					<img
199
						class="powering-up-img"
200
						width="200px"
201
						height="200px"
202
						src="<?php echo esc_url( $powering_up_logo ); ?>"
203
						alt="<?php esc_attr_e( 'A jetpack site powering up', 'jetpack' ); ?>"
204
					/>
205
				</div>
206
207
				<span
208
					class="notice-dismiss wizard-banner-dismiss"
209
					title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
210
				</span>
211
212
			</div>
213
214
		</div>
215
		<?php
216
	}
217
}
218