Completed
Push — add/implement_wizard_banner ( 2cf485...af534f )
by
unknown
14:54 queued 08:11
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
 * 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
			<span
148
				class="notice-dismiss wizard-banner-dismiss"
149
				title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>">
150
			</span>
151
			<div class="jp-emblem">
152
			<?php
153
				echo $jetpack_logo->get_jp_emblem_larger(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
154
			?>
155
			</div>
156
			<img
157
				class="powering-up-img"
158
				width="200px"
159
				height="200px"
160
				src="<?php echo esc_url( $powering_up_logo ); ?>"
161
				alt="<?php esc_attr_e( 'A jetpack site powering up', 'jetpack' ); ?>"
162
			/>
163
			<h2 class="jp-wizard-banner-wizard-header">
164
				<?php esc_html_e( 'Set up Jetpack for better site security, performance, and more', 'jetpack' ); ?>
165
			</h2>
166
			<p class="jp-wizard-banner-wizard-paragraph">
167
				<?php esc_html_e( 'Jetpack is a cloud-powered tool built by Automattic.', 'jetpack' ); ?>
168
			</p>
169
			<p class="jp-wizard-banner-wizard-paragraph">
170
				<?php esc_html_e( 'Answer a few questions and we’ll help you secure, speed up, customize, and grow your WordPress website.', 'jetpack' ); ?>
171
			</p>
172
			<div class="jp-wizard-banner-wizard-intro-question">
173
				<h2>
174
					<?php
175
					/* translators: %s is the site name */
176
					printf(
177
						/* translators: %s is the site name */
178
						esc_html__( 'What will %s be used for?', 'jetpack' ),
179
						esc_html( get_bloginfo( 'name' ) )
180
					);
181
					?>
182
				</h2>
183
				<div class="jp-wizard-banner-wizard-answer-buttons">
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=personal' ) ); ?>"
187
					>
188
					<?php esc_html_e( 'Personal Use', 'jetpack' ); ?>
189
					</a>
190
					<a
191
						class="button button-primary jp-wizard-banner-wizard-button"
192
						href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/income?use=business' ) ); ?>"
193
					>
194
						<?php esc_html_e( 'Business Use', 'jetpack' ); ?>
195
					</a>
196
				</div>
197
				<a
198
					class="jp-wizard-banner-wizard-skip-link"
199
					href="<?php echo esc_url( Jetpack::admin_url( 'page=jetpack#/setup/features' ) ); ?>"
200
				>
201
					<?php esc_html_e( 'Skip to recommended features', 'jetpack' ); ?>
202
				</a>
203
			</div>
204
		</div>
205
		<?php
206
	}
207
}
208