Completed
Push — add/testing-info ( be1095...03b7e9 )
by
unknown
09:20
created

enqueue_banner_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 22
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
/**
3
 * Displays the site type recommendations question as a banner.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
use Automattic\Jetpack\Assets;
9
use Automattic\Jetpack\Assets\Logo as Jetpack_Logo;
10
use Automattic\Jetpack\Tracking;
11
12
/**
13
 * Jetpack_Recommendations_Banner
14
 **/
15
class Jetpack_Recommendations_Banner {
16
	/**
17
	 * Jetpack_Recommendations_Banner
18
	 *
19
	 * @var Jetpack_Recommendations_Banner
20
	 **/
21
	private static $instance = null;
22
23
	/**
24
	 * Factory method
25
	 */
26
	public static function init() {
27
		if ( is_null( self::$instance ) ) {
28
			self::$instance = new Jetpack_Recommendations_Banner();
29
		}
30
31
		return self::$instance;
32
	}
33
34
	/**
35
	 * Jetpack_Recommendations_Banner constructor.
36
	 */
37
	private function __construct() {
38
		add_action( 'current_screen', array( $this, 'maybe_initialize_hooks' ) );
39
	}
40
41
	/**
42
	 * Initialize hooks to display the banner
43
	 *
44
	 * @since 9.7 Added the $current_screen parameter.
45
	 *
46
	 * @param \WP_Screen $current_screen Current WordPress screen.
47
	 */
48
	public function maybe_initialize_hooks( $current_screen ) {
49
		if ( ! $this->can_be_displayed() ) {
50
			return;
51
		}
52
53
		if ( Jetpack_Connection_Banner::can_be_displayed( $current_screen ) ) {
54
			// We don't want to overcrowd the screen with both the Connection banner and the Recommendations banner.
55
			return;
56
		}
57
58
		add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) );
59
		add_action( 'admin_notices', array( $this, 'render_banner' ) );
60
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) );
61
	}
62
63
	/**
64
	 * Determines if the banner can be displayed
65
	 */
66
	public static function can_be_displayed() {
67
		if ( ! Jetpack_Recommendations::is_banner_enabled() ) {
68
			return false;
69
		}
70
71
		// Only the dashboard and plugins pages should see the banner.
72
		if ( ! in_array( get_current_screen()->id, array( 'dashboard', 'plugins' ), true ) ) {
73
			return false;
74
		}
75
76
		if ( ! current_user_can( 'jetpack_manage_modules' ) ) {
77
			return false;
78
		}
79
80
		if ( Jetpack_Options::get_option( 'recommendations_banner_dismissed' ) ) {
81
			return false;
82
		}
83
84
		if ( ! in_array(
85
			Jetpack_Options::get_option( 'recommendations_step', 'not-started' ),
86
			array(
87
				'not-started',
88
				'site-type-question',
89
			),
90
			true
91
		) ) {
92
			return false;
93
		}
94
95
		return true;
96
	}
97
98
	/**
99
	 * Handles storing the user responses in the banner.
100
	 */
101
	public static function ajax_callback() {
102
		check_ajax_referer( 'jp-recommendations-banner-nonce', 'nonce' );
103
104
		if ( ! current_user_can( 'jetpack_manage_modules' ) ) {
105
			wp_die();
106
		}
107
108
		$tracking = new Tracking();
109
110
		if ( isset( $_REQUEST['dismissBanner'] ) && 'true' === $_REQUEST['dismissBanner'] ) {
111
			Jetpack_Options::update_option( 'recommendations_banner_dismissed', 1 );
112
			$tracking->record_user_event( 'recommendations_banner_dismissed' );
113
			wp_send_json_success();
114
			wp_die();
115
		}
116
117
		$data = Jetpack_Recommendations::get_recommendations_data();
118
119
		$tracking_answers = array();
120
121 View Code Duplication
		if ( isset( $_REQUEST['personal'] ) && is_string( $_REQUEST['personal'] ) ) {
122
			$value                        = 'true' === $_REQUEST['personal'] ? true : false;
123
			$data['site-type-personal']   = $value;
124
			$tracking_answers['personal'] = $value;
125
		}
126
127 View Code Duplication
		if ( isset( $_REQUEST['business'] ) && is_string( $_REQUEST['business'] ) ) {
128
			$value                        = 'true' === $_REQUEST['business'] ? true : false;
129
			$data['site-type-business']   = $value;
130
			$tracking_answers['business'] = $value;
131
		}
132
133 View Code Duplication
		if ( isset( $_REQUEST['store'] ) && is_string( $_REQUEST['store'] ) ) {
134
			$value                     = 'true' === $_REQUEST['store'] ? true : false;
135
			$data['site-type-store']   = $value;
136
			$tracking_answers['store'] = $value;
137
		}
138
139 View Code Duplication
		if ( isset( $_REQUEST['other'] ) && is_string( $_REQUEST['other'] ) ) {
140
			$value                     = 'true' === $_REQUEST['other'] ? true : false;
141
			$data['site-type-other']   = $value;
142
			$tracking_answers['other'] = $value;
143
		}
144
145
		Jetpack_Recommendations::update_recommendations_data( $data );
0 ignored issues
show
Documentation introduced by
$data is of type array, but the function expects a object<WP_REST_Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146
		Jetpack_Options::update_option( 'recommendations_step', 'banner-completed' );
147
148
		$tracking->record_user_event( 'recommendations_banner_answered', $tracking_answers );
149
150
		wp_send_json_success();
151
		wp_die();
152
	}
153
154
	/**
155
	 * Enqueue JavaScript files.
156
	 */
157 View Code Duplication
	public function enqueue_banner_scripts() {
158
		wp_enqueue_script(
159
			'jetpack-recommendations-banner-js',
160
			Assets::get_file_url_for_environment(
161
				'_inc/build/jetpack-recommendations-banner.min.js',
162
				'_inc/jetpack-recommendations-banner.js'
163
			),
164
			array( 'jquery' ),
165
			JETPACK__VERSION,
166
			true
167
		);
168
169
		wp_localize_script(
170
			'jetpack-recommendations-banner-js',
171
			'jp_banner',
172
			array(
173
				'nonce'               => wp_create_nonce( 'jp-recommendations-banner-nonce' ),
174
				'ajax_url'            => admin_url( 'admin-ajax.php' ),
175
				'recommendations_url' => admin_url( 'admin.php?page=jetpack#/recommendations' ),
176
			)
177
		);
178
	}
179
180
	/**
181
	 * Include the needed styles
182
	 */
183
	public function admin_banner_styles() {
184
		wp_enqueue_style(
185
			'jetpack-recommendations-banner',
186
			Assets::get_file_url_for_environment(
187
				'css/jetpack-recommendations-banner.min.css',
188
				'css/jetpack-recommendations-banner.css'
189
			),
190
			array(),
191
			JETPACK__VERSION
192
		);
193
	}
194
195
	/**
196
	 * Renders the Recommendations Banner
197
	 */
198
	public function render_banner() {
199
		$jetpack_logo = new Jetpack_Logo();
200
		$site_name    = get_bloginfo( 'name' );
201
		?>
202
		<div id="jp-recommendations-banner-main" class="jp-recommendations-banner-main">
203
			<div class="jp-recommendations-banner__content">
204
				<div class="jp-recommendations-banner__logo">
205
					<?php
206
					echo $jetpack_logo->get_jp_emblem_larger(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
207
					?>
208
				</div>
209
				<h1 class="jp-recommendations-banner__question">
210
					<?php
211
					/* translators: placeholder is the name of the website */
212
					echo sprintf( esc_html__( 'What type of site is %s?', 'jetpack' ), esc_html( $site_name ) );
213
					?>
214
				</h1>
215
				<p class="jp-recommendations-banner__description">
216
					<?php esc_html_e( 'This assistant will help you get the most from Jetpack. Tell us more about your goals and we’ll recommend relevant features to help you succeed.', 'jetpack' ); ?>
217
				</p>
218
				<div class="jp-recommendations-banner__answer">
219
					<form id="jp-recommendations-banner__form" class="jp-recommendations-banner__form">
220
						<div class="jp-recommendations-banner__checkboxes">
221
							<?php $this->render_checkbox( 'personal', __( 'Personal', 'jetpack' ) ); ?>
222
							<?php $this->render_checkbox( 'business', __( 'Business', 'jetpack' ) ); ?>
223
							<?php $this->render_checkbox( 'store', __( 'Store', 'jetpack' ) ); ?>
224
							<?php $this->render_checkbox( 'other', __( 'Other', 'jetpack' ) ); ?>
225
						</div>
226
					</form>
227
					<a id="jp-recommendations-banner__continue-button" class="jp-recommendations-banner__continue-button">
228
						<?php esc_html_e( 'Continue', 'jetpack' ); ?>
229
					</a>
230
					<div class="jp-recommendations-banner__continue-description">
231
						<?php esc_html_e( 'The following Jetpack recommendations are available to you later in the Jetpack dashboard.', 'jetpack' ); ?>
232
					</div>
233
				</div>
234
			</div>
235
			<div class="jp-recommendations-banner__illustration-container">
236
				<div id="jp-recommendations-banner__notice-dismiss" class="jp-recommendations-banner__notice-dismiss">
237
					<svg class="jp-recommendations-banner__svg-dismiss" width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
238
						<mask id="jp-dismiss-mask0" mask-type="alpha" maskUnits="userSpaceOnUse" x="2" y="2" width="21" height="20">
239
						<path fill-rule="evenodd" clip-rule="evenodd" d="M12.5232 2C7.02034 2 2.57227 6.47 2.57227 12C2.57227 17.53 7.02034 22 12.5232 22C18.0261 22 22.4742 17.53 22.4742 12C22.4742 6.47 18.0261 2 12.5232 2ZM15.1005 8L12.5232 10.59L9.94591 8L8.54283 9.41L11.1201 12L8.54283 14.59L9.94591 16L12.5232 13.41L15.1005 16L16.5036 14.59L13.9263 12L16.5036 9.41L15.1005 8ZM4.56245 12C4.56245 16.41 8.13484 20 12.5232 20C16.9116 20 20.484 16.41 20.484 12C20.484 7.59 16.9116 4 12.5232 4C8.13484 4 4.56245 7.59 4.56245 12Z" />
240
						</mask><g mask="url(#jp-dismiss-mask0)"><rect x="0.582031" width="23.8823" height="24" /></g></svg>
241
					<span><?php esc_attr_e( 'Dismiss', 'jetpack' ); ?></span>
242
				</div>
243
				<img
244
						src="<?php echo esc_url( plugins_url( 'images/recommendations/background.svg', JETPACK__PLUGIN_FILE ), 'jetpack' ); ?>"
245
						class="jp-recommendations-banner__illustration-background"
246
				/>
247
				<img
248
						src="<?php echo esc_url( plugins_url( 'images/recommendations/site-type-illustration.jpg', JETPACK__PLUGIN_FILE ), 'jetpack' ); ?>"
249
						class="jp-recommendations-banner__illustration-foreground"
250
				/>
251
			</div>
252
		</div>
253
		<?php
254
	}
255
256
	/**
257
	 * Renders a checkbox.
258
	 *
259
	 * @param string $name The name to give the form input.
260
	 * @param string $title The title to put on the checkbox.
261
	 */
262
	private function render_checkbox( $name, $title ) {
263
		?>
264
		<label for="<?php echo esc_html( $name ); ?>" class="jp-recommendations-answer__checkbox-label">
265
			<input id="<?php echo esc_html( $name ); ?>" name="<?php echo esc_html( $name ); ?>" type="checkbox" tabindex="-1"/>
266
			<div class="jp-recommendations-answer__title">
267
				<?php echo esc_html( $title ); ?>
268
			</div>
269
		</label>
270
		<?php
271
	}
272
}
273