Completed
Push — update/admin-menu-sync-wpcom ( b0ac91...43248e )
by
unknown
124:17 queued 113:08
created

maybe_initialize_hooks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
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
	public function maybe_initialize_hooks() {
45
		if ( ! $this->can_be_displayed() ) {
46
			return;
47
		}
48
49
		add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) );
50
		add_action( 'admin_notices', array( $this, 'render_banner' ) );
51
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) );
52
	}
53
54
	/**
55
	 * Determines if the banner can be displayed
56
	 */
57
	public static function can_be_displayed() {
58
		if ( ! Jetpack_Recommendations::is_banner_enabled() ) {
59
			return false;
60
		}
61
62
		// Only the dashboard and plugins pages should see the banner.
63
		if ( ! in_array( get_current_screen()->id, array( 'dashboard', 'plugins' ), true ) ) {
64
			return false;
65
		}
66
67
		if ( ! current_user_can( 'jetpack_manage_modules' ) ) {
68
			return false;
69
		}
70
71
		if ( Jetpack_Options::get_option( 'recommendations_banner_dismissed' ) ) {
72
			return false;
73
		}
74
75
		if ( ! in_array(
76
			Jetpack_Options::get_option( 'recommendations_step', 'not-started' ),
77
			array(
78
				'not-started',
79
				'site-type-question',
80
			),
81
			true
82
		) ) {
83
			return false;
84
		}
85
86
		return true;
87
	}
88
89
	/**
90
	 * Handles storing the user responses in the banner.
91
	 */
92
	public static function ajax_callback() {
93
		check_ajax_referer( 'jp-recommendations-banner-nonce', 'nonce' );
94
95
		if ( ! current_user_can( 'jetpack_manage_modules' ) ) {
96
			wp_die();
97
		}
98
99
		$tracking = new Tracking();
100
101
		if ( isset( $_REQUEST['dismissBanner'] ) && 'true' === $_REQUEST['dismissBanner'] ) {
102
			Jetpack_Options::update_option( 'recommendations_banner_dismissed', 1 );
103
			$tracking->record_user_event( 'recommendations_banner_dismissed' );
104
			wp_send_json_success();
105
			wp_die();
106
		}
107
108
		$data = Jetpack_Recommendations::get_recommendations_data();
109
110
		$tracking_answers = array();
111
112 View Code Duplication
		if ( isset( $_REQUEST['personal'] ) && is_string( $_REQUEST['personal'] ) ) {
113
			$value                        = 'true' === $_REQUEST['personal'] ? true : false;
114
			$data['site-type-personal']   = $value;
115
			$tracking_answers['personal'] = $value;
116
		}
117
118 View Code Duplication
		if ( isset( $_REQUEST['business'] ) && is_string( $_REQUEST['business'] ) ) {
119
			$value                        = 'true' === $_REQUEST['business'] ? true : false;
120
			$data['site-type-business']   = $value;
121
			$tracking_answers['business'] = $value;
122
		}
123
124 View Code Duplication
		if ( isset( $_REQUEST['store'] ) && is_string( $_REQUEST['store'] ) ) {
125
			$value                     = 'true' === $_REQUEST['store'] ? true : false;
126
			$data['site-type-store']   = $value;
127
			$tracking_answers['store'] = $value;
128
		}
129
130 View Code Duplication
		if ( isset( $_REQUEST['other'] ) && is_string( $_REQUEST['other'] ) ) {
131
			$value                     = 'true' === $_REQUEST['other'] ? true : false;
132
			$data['site-type-other']   = $value;
133
			$tracking_answers['other'] = $value;
134
		}
135
136
		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...
137
		Jetpack_Options::update_option( 'recommendations_step', 'banner-completed' );
138
139
		$tracking->record_user_event( 'recommendations_banner_answered', $tracking_answers );
140
141
		wp_send_json_success();
142
		wp_die();
143
	}
144
145
	/**
146
	 * Enqueue JavaScript files.
147
	 */
148 View Code Duplication
	public function enqueue_banner_scripts() {
149
		wp_enqueue_script(
150
			'jetpack-recommendations-banner-js',
151
			Assets::get_file_url_for_environment(
152
				'_inc/build/jetpack-recommendations-banner.min.js',
153
				'_inc/jetpack-recommendations-banner.js'
154
			),
155
			array( 'jquery' ),
156
			JETPACK__VERSION,
157
			true
158
		);
159
160
		wp_localize_script(
161
			'jetpack-recommendations-banner-js',
162
			'jp_banner',
163
			array(
164
				'nonce'               => wp_create_nonce( 'jp-recommendations-banner-nonce' ),
165
				'ajax_url'            => admin_url( 'admin-ajax.php' ),
166
				'recommendations_url' => admin_url( 'admin.php?page=jetpack#/recommendations' ),
167
			)
168
		);
169
	}
170
171
	/**
172
	 * Include the needed styles
173
	 */
174
	public function admin_banner_styles() {
175
		wp_enqueue_style(
176
			'jetpack-recommendations-banner',
177
			Assets::get_file_url_for_environment(
178
				'css/jetpack-recommendations-banner.min.css',
179
				'css/jetpack-recommendations-banner.css'
180
			),
181
			array(),
182
			JETPACK__VERSION
183
		);
184
	}
185
186
	/**
187
	 * Renders the Recommendations Banner
188
	 */
189
	public function render_banner() {
190
		$jetpack_logo = new Jetpack_Logo();
191
		$site_name    = get_bloginfo( 'name' );
192
		?>
193
		<div id="jp-recommendations-banner-main" class="jp-recommendations-banner-main">
194
			<div class="jp-recommendations-banner__content">
195
				<div class="jp-recommendations-banner__logo">
196
					<?php
197
					echo $jetpack_logo->get_jp_emblem_larger(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
198
					?>
199
				</div>
200
				<h1 class="jp-recommendations-banner__question">
201
					<?php
202
					/* translators: placeholder is the name of the website */
203
					echo sprintf( esc_html__( 'What type of site is %s?', 'jetpack' ), esc_html( $site_name ) );
204
					?>
205
				</h1>
206
				<p class="jp-recommendations-banner__description">
207
					<?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' ); ?>
208
				</p>
209
				<div class="jp-recommendations-banner__answer">
210
					<form id="jp-recommendations-banner__form" class="jp-recommendations-banner__form">
211
						<div class="jp-recommendations-banner__checkboxes">
212
							<?php $this->render_checkbox( 'personal', __( 'Personal', 'jetpack' ) ); ?>
213
							<?php $this->render_checkbox( 'business', __( 'Business', 'jetpack' ) ); ?>
214
							<?php $this->render_checkbox( 'store', __( 'Store', 'jetpack' ) ); ?>
215
							<?php $this->render_checkbox( 'other', __( 'Other', 'jetpack' ) ); ?>
216
						</div>
217
					</form>
218
					<a id="jp-recommendations-banner__continue-button" class="jp-recommendations-banner__continue-button">
219
						<?php esc_html_e( 'Continue', 'jetpack' ); ?>
220
					</a>
221
					<div class="jp-recommendations-banner__continue-description">
222
						<?php esc_html_e( 'The following Jetpack recommendations are available to you later in the Jetpack dashboard.', 'jetpack' ); ?>
223
					</div>
224
				</div>
225
			</div>
226
			<div class="jp-recommendations-banner__illustration-container">
227
				<div id="jp-recommendations-banner__notice-dismiss" class="jp-recommendations-banner__notice-dismiss">
228
					<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">
229
						<mask id="jp-dismiss-mask0" mask-type="alpha" maskUnits="userSpaceOnUse" x="2" y="2" width="21" height="20">
230
						<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" />
231
						</mask><g mask="url(#jp-dismiss-mask0)"><rect x="0.582031" width="23.8823" height="24" /></g></svg>
232
					<span><?php esc_attr_e( 'Dismiss', 'jetpack' ); ?></span>
233
				</div>
234
				<img
235
						src="<?php echo esc_url( plugins_url( 'images/recommendations/background.svg', JETPACK__PLUGIN_FILE ), 'jetpack' ); ?>"
236
						class="jp-recommendations-banner__illustration-background"
237
				/>
238
				<img
239
						src="<?php echo esc_url( plugins_url( 'images/recommendations/site-type-illustration.jpg', JETPACK__PLUGIN_FILE ), 'jetpack' ); ?>"
240
						class="jp-recommendations-banner__illustration-foreground"
241
				/>
242
			</div>
243
		</div>
244
		<?php
245
	}
246
247
	/**
248
	 * Renders a checkbox.
249
	 *
250
	 * @param string $name The name to give the form input.
251
	 * @param string $title The title to put on the checkbox.
252
	 */
253
	private function render_checkbox( $name, $title ) {
254
		?>
255
		<label for="<?php echo esc_html( $name ); ?>" class="jp-recommendations-answer__checkbox-label">
256
			<input id="<?php echo esc_html( $name ); ?>" name="<?php echo esc_html( $name ); ?>" type="checkbox" tabindex="-1"/>
257
			<div class="jp-recommendations-answer__title">
258
				<?php echo esc_html( $title ); ?>
259
			</div>
260
		</label>
261
		<?php
262
	}
263
}
264