Completed
Push — add/calypso-nudges ( 2bd96c )
by Kirk
09:44
created

Jetpack_Calypso_Nudges::get_cookie_group()   D

Complexity

Conditions 18
Paths 18

Size

Total Lines 40
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 38
nc 18
nop 1
dl 0
loc 40
rs 4.947
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class Jetpack_Calypso_Nudges {
4
	public function __construct() {
5
		$enabled = apply_filters( 'jetpack_calypso_nudges_enable', '__return_true' );
6
		if ( ! $enabled ) {
7
			return;
8
		}
9
10
		add_action( 'admin_notices', array( $this, 'nudge_to_calypso' ) );
11
		add_action( 'admin_post_calypso_nudge', array( $this, 'redirect_to_calypso' ) );
12
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
13
		add_action( 'wp_ajax_calypso_nudges_register_dismiss_stats', array( $this, 'register_dismiss_stats' ) );
14
		add_filter( 'allowed_redirect_hosts', array( $this, 'update_allowed_redirect_hosts' ) );
15
	}
16
17
	public function nudge_to_calypso() {
18
		$screen = $this->get_screen();
19
		if ( ! $screen || $this->is_dismissed( $screen ) ) {
20
			return;
21
		}
22
23
		$url = $this->get_nudge_url( $screen );
24
25
		if ( 'add' === $screen['type'] ) {
26
			$this->display_editor_nudge( $url, $screen['desc'] );
27
		} else {
28
			$this->display_management_nudge( $url, $screen['desc'] );
29
		}
30
	}
31
32
	protected function get_screen() {
33
		switch ( get_current_screen()->id ) {
34
			case 'edit-post':
35
				$type = 'manage';
36
				$desc = 'post';
37
				break;
38
			case 'post':
39
				$type = 'add';
40
				$desc = 'post';
41
				break;
42
			case 'edit-category':
43
				$type = 'manage';
44
				$desc = 'category';
45
				break;
46
			case 'edit-post_tag':
47
				$type = 'manage';
48
				$desc = 'tag';
49
				break;
50
			case 'upload':
51
				$type = 'manage';
52
				$desc = 'media';
53
				break;
54
			case 'media':
55
				$type = 'add';
56
				$desc = 'media';
57
				break;
58
			case 'edit-page':
59
				$type = 'manage';
60
				$desc = 'page';
61
				break;
62
			case 'page':
63
				$type = 'add';
64
				$desc = 'page';
65
				break;
66
			case 'edit-comments':
67
				$type = 'manage';
68
				$desc = 'comment';
69
				break;
70
			case 'edit-jetpack-testimonial':
71
				$type = 'manage';
72
				$desc = 'testimonial';
73
				break;
74
			case 'jetpack-testimonial':
75
				$type = 'add';
76
				$desc = 'testimonial';
77
				break;
78
			case 'edit-jetpack-portfolio':
79
				$type = 'manage';
80
				$desc = 'portfolio';
81
				break;
82
			case 'jetpack-portfolio':
83
				$type = 'add';
84
				$desc = 'portfolio';
85
				break;
86
			case 'edit-nova_menu_item':
87
				$type = 'manage';
88
				$desc = 'food_menu';
89
				break;
90
			case 'nova_menu_item':
91
				$type = 'add';
92
				$desc = 'food_menu';
93
				break;
94
			case 'edit-jetpack-comic':
95
				$type = 'manage';
96
				$desc = 'comic';
97
				break;
98
			case 'jetpack-comic':
99
				$type = 'add';
100
				$desc = 'comic';
101
				break;
102
			default:
103
				return false;
104
		}
105
		return array(
106
			'id' => "{$type}_{$desc}",
107
			'type' => $type,
108
			'desc' => $desc,
109
		);
110
	}
111
112
	protected function get_nudge_url( $screen ) {
113
		$nudge_url = get_admin_url( null, 'admin-post.php' );
114
115
		$nudge_url = add_query_arg( array(
116
			'action' => 'calypso_nudge',
117
			'screen' => $screen['id'],
118
			'calypso_nudge_nonce' => wp_create_nonce( $screen['id'] )
119
		), $nudge_url );
120
121
		return $nudge_url;
122
	}
123
124
	protected function display_editor_nudge( $nudge_url, $desc ) {
125
		switch ( $desc ) {
126
			case 'post':
127
				$notice = __( 'There\'s an easier way to create posts on WordPress.com.' );
128
				break;
129
			case 'page':
130
				$notice = __( 'There\'s an easier way to create pages on WordPress.com.' );
131
				break;
132
			case 'media':
133
				$notice = __( 'There\'s an easier way to add media on WordPress.com.' );
134
				break;
135
			case 'tag':
136
				$notice = __( 'There\'s an easier way to create tags on WordPress.com.' );
137
				break;
138
			case 'category':
139
				$notice = __( 'There\'s an easier way to create categories on WordPress.com.' );
140
				break;
141
			case 'testimonial':
142
				$notice = __( 'There\'s an easier way to create testimonials on WordPress.com.' );
143
				break;
144
			case 'portfolio':
145
				$notice = __( 'There\'s an easier way to create portfolio projects on WordPress.com.' );
146
				break;
147
			case 'food_menu':
148
				$notice = __( 'There\'s an easier way to create menu items on WordPress.com.' );
149
				break;
150
			case 'comic':
151
				$notice = __( 'There\'s an easier way to create comics on WordPress.com.' );
152
				break;
153
			default:
154
				$notice = __( 'There\'s an easier way to create on WordPress.com.' );
155
		}
156
		?>
157
		<div class="jetpack-calypso-nudge notice notice-info is-dismissible">
158
			<p>
159
				<?php echo esc_html( $notice ); ?>
160
				<a href="<?php echo esc_url( $nudge_url ); ?>">
161
					<?php esc_html_e('Switch to the improved editor.'); ?><span class="dashicons dashicons-external"></span>
162
				</a>
163
				<?php static $script_included = true; //wpcom_hide_tip_link( 'calypso_nudges' ); ?>
164
			</p>
165
		</div>
166
		<?php
167
	}
168
169
	protected function display_management_nudge( $nudge_url, $desc ) {
170
		switch ( $desc ) {
171
			case 'post':
172
				$notice = __( 'There\'s an easier way to manage posts on WordPress.com.' );
173
				break;
174
			case 'page':
175
				$notice = __( 'There\'s an easier way to manage pages on WordPress.com.' );
176
				break;
177
			case 'comment':
178
				$notice = __( 'There\'s an easier way to manage comments on WordPress.com.' );
179
				break;
180
			case 'media':
181
				$notice = __( 'There\'s an easier way to manage media on WordPress.com.' );
182
				break;
183
			case 'tag':
184
				$notice = __( 'There\'s an easier way to manage tags on WordPress.com.' );
185
				break;
186
			case 'category':
187
				$notice = __( 'There\'s an easier way to manage categories on WordPress.com.' );
188
				break;
189
			case 'testimonial':
190
				$notice = __( 'There\'s an easier way to manage testimonials on WordPress.com.' );
191
				break;
192
			case 'portfolio':
193
				$notice = __( 'There\'s an easier way to manage portfolio projects on WordPress.com.' );
194
				break;
195
			case 'food_menu':
196
				$notice = __( 'There\'s an easier way to manage menu items on WordPress.com.' );
197
				break;
198
			case 'comic':
199
				$notice = __( 'There\'s an easier way to manage comics on WordPress.com.' );
200
				break;
201
			default:
202
				$notice = __( 'There\'s an easier way to manage on WordPress.com.' );
203
		}
204
		?>
205
		<div class="jetpack-calypso-nudge notice notice-info is-dismissible">
206
			<p>
207
				<?php echo esc_html( $notice ); ?>
208
				<a href="<?php echo esc_url( $nudge_url ); ?>">
209
					<?php esc_html_e('Switch to the improved experience.'); ?><span class="dashicons dashicons-external"></span>
210
				</a>
211
				<?php static $script_included = true; //wpcom_hide_tip_link( 'calypso_nudges' ); ?>
212
			</p>
213
		</div>
214
		<?php
215
	}
216
217
	public function redirect_to_calypso() {
218
		$screen = $_GET['screen'];
219
		$this->verify_nonce( $screen );
220
		$this->register_follow_stats( $screen );
221
		$this->redirect_with_slug( $screen );
222
	}
223
224
	protected function verify_nonce( $screen ) {
225
		if ( isset( $_GET['calypso_nudge_nonce'] )
226
			&& wp_verify_nonce( $_GET['calypso_nudge_nonce'], $screen ) ) {
227
			return;
228
		}
229
230
		// redirect to dashboard if nonce is not set
231
		wp_safe_redirect( get_dashboard_url() );
232
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method verify_nonce() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
233
	}
234
235
	protected function register_follow_stats( $screen ) {
236
		// Make $event a full string for better discoverability.
237
		switch ( $screen ) {
238
			case 'manage_post':
239
				$event = 'jetpack_calypso_nudge_follow_manage_post';
240
				break;
241
			case 'add_post':
242
				$event = 'jetpack_calypso_nudge_follow_add_post';
243
				break;
244
			case 'manage_page':
245
				$event = 'jetpack_calypso_nudge_follow_manage_page';
246
				break;
247
			case 'add_page':
248
				$event = 'jetpack_calypso_nudge_follow_add_page';
249
				break;
250
			case 'manage_media':
251
				$event = 'jetpack_calypso_nudge_follow_manage_media';
252
				break;
253
			case 'add_media':
254
				$event = 'jetpack_calypso_nudge_follow_add_media';
255
				break;
256
			case 'manage_testimonial':
257
				$event = 'jetpack_calypso_nudge_follow_manage_testimonial';
258
				break;
259
			case 'add_testimonial':
260
				$event = 'jetpack_calypso_nudge_follow_add_testimonial';
261
				break;
262
			case 'manage_portfolio':
263
				$event = 'jetpack_calypso_nudge_follow_manage_portfolio';
264
				break;
265
			case 'add_portfolio':
266
				$event = 'jetpack_calypso_nudge_follow_add_portfolio';
267
				break;
268
			case 'manage_food_menu':
269
				$event = 'jetpack_calypso_nudge_follow_manage_food_menu';
270
				break;
271
			case 'add_food_menu':
272
				$event = 'jetpack_calypso_nudge_follow_add_food_menu';
273
				break;
274
			case 'manage_comic':
275
				$event = 'jetpack_calypso_nudge_follow_manage_comic';
276
				break;
277
			case 'add_comic':
278
				$event = 'jetpack_calypso_nudge_follow_add_comic';
279
				break;
280
			case 'manage_comment':
281
				$event = 'jetpack_calypso_nudge_follow_manage_comment';
282
				break;
283
			case 'manage_category':
284
				$event = 'jetpack_calypso_nudge_follow_manage_category';
285
				break;
286
			case 'manage_tag':
287
				$event = 'jetpack_calypso_nudge_follow_manage_tag';
288
				break;
289
			default:
290
				return;
291
		}
292
293
		// record in Tracks
294
		jetpack_tracks_record_event( wp_get_current_user(), $event );
295
	}
296
297
	public function register_dismiss_stats() {
298
		check_ajax_referer( 'jetpack-calypso-nudges-dismiss-nonce', 'nonce' );
299
300
		// Make $event a full string for better discoverability.
301
		switch ( $_GET['cookieGroup'] ) {
302
			case 'posts':
303
				$event = 'jetpack_calypso_nudge_dismiss_posts';
304
				break;
305
			case 'pages':
306
				$event = 'jetpack_calypso_nudge_dismiss_pages';
307
				break;
308
			case 'media':
309
				$event = 'jetpack_calypso_nudge_dismiss_media';
310
				break;
311
			case 'testimonials':
312
				$event = 'jetpack_calypso_nudge_dismiss_testimonials';
313
				break;
314
			case 'portfolios':
315
				$event = 'jetpack_calypso_nudge_dismiss_portfolios';
316
				break;
317
			case 'food_menus':
318
				$event = 'jetpack_calypso_nudge_dismiss_food_menus';
319
				break;
320
			case 'comics':
321
				$event = 'jetpack_calypso_nudge_dismiss_comics';
322
				break;
323
			case 'comments':
324
				$event = 'jetpack_calypso_nudge_dismiss_comments';
325
				break;
326
			case 'taxonomies':
327
				$event = 'jetpack_calypso_nudge_dismiss_taxonomies';
328
				break;
329
			default:
330
				return;
331
		}
332
333
		// record in Tracks
334
		jetpack_tracks_record_event( wp_get_current_user(), $event );
335
	}
336
337
	protected function redirect_with_slug( $screen ) {
338
		switch ( $screen ) {
339
			case 'manage_post':
340
				$url = 'https://wordpress.com/posts/';
341
				break;
342
			case 'add_post':
343
				$url = 'https://wordpress.com/post/';
344
				break;
345
			case 'manage_page':
346
				$url = 'https://wordpress.com/pages/';
347
				break;
348
			case 'add_page':
349
				$url = 'https://wordpress.com/page/';
350
				break;
351
			case 'manage_media':
352
			case 'add_media':
353
				$url = 'https://wordpress.com/media/';
354
				break;
355
			case 'manage_testimonial':
356
				$url = 'https://wordpress.com/types/jetpack-testimonial/';
357
				break;
358
			case 'add_testimonial':
359
				$url = 'https://wordpress.com/edit/jetpack-testimonial/';
360
				break;
361
			case 'manage_portfolio':
362
				$url = 'https://wordpress.com/types/jetpack-portfolio/';
363
				break;
364
			case 'add_portfolio':
365
				$url = 'https://wordpress.com/edit/jetpack-portfolio/';
366
				break;
367
			case 'manage_food_menu':
368
				$url = 'https://wordpress.com/types/nova_menu_item/';
369
				break;
370
			case 'add_food_menu':
371
				$url = 'https://wordpress.com/edit/nova_menu_item/';
372
				break;
373
			case 'manage_comic':
374
				$url = 'https://wordpress.com/types/jetpack-comic/';
375
				break;
376
			case 'add_comic':
377
				$url = 'https://wordpress.com/edit/jetpack-comic/';
378
				break;
379
			case 'manage_comment':
380
				$url = 'https://wordpress.com/comments/all/';
381
				break;
382
			case 'manage_category':
383
				$url = 'https://wordpress.com/settings/taxonomies/category/';
384
				break;
385
			case 'manage_tag':
386
				$url = 'https://wordpress.com/settings/taxonomies/post_tag/';
387
				break;
388
			default:
389
				wp_safe_redirect( get_dashboard_url() );
390
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method redirect_with_slug() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
391
		}
392
393
		$site_slug = Jetpack::build_raw_urls( get_home_url() );
394
		wp_safe_redirect( $url . $site_slug );
395
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method redirect_with_slug() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
396
	}
397
398
	public function enqueue_scripts() {
399
		$screen = $this->get_screen();
400
		if ( ! $screen ) {
401
			return;
402
		}
403
404
		wp_enqueue_script( 'jetpack-calypso-nudges-js', plugins_url( 'calypso-nudges.js', __FILE__ ), array(), '20171114', true );
405
		wp_enqueue_style( 'jetpack-calypso-nudges-css', plugins_url( 'calypso-nudges.css' , __FILE__ ), array(), '20171114' );
406
		wp_style_add_data( 'jetpack-calypso-nudges-css', 'rtl', 'replace' );
407
408
		wp_localize_script( 'jetpack-calypso-nudges-js', 'jetpackCalypsoNudges', array(
409
			'cookieGroup' => $this->get_cookie_group( $screen['id'] ),
410
			'nonce' => wp_create_nonce( 'jetpack-calypso-nudges-dismiss-nonce' ),
411
		) );
412
	}
413
414
	protected function is_dismissed( $screen ) {
415
		$group = $this->get_cookie_group( $screen['id']);
416
		$cookie = "jetpack_nudge_dismissed_{$group}";
417
		return ! empty( $_COOKIE[ $cookie ] );
418
	}
419
420
	protected function get_cookie_group( $screen_id ) {
421
		switch ( $screen_id ) {
422
			case 'manage_post':
423
			case 'add_post':
424
				return 'posts';
425
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
426
			case 'manage_page':
427
			case 'add_page':
428
				return 'pages';
429
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
430
			case 'manage_media':
431
			case 'add_media':
432
				return 'media';
433
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
434
			case 'manage_testimonial':
435
			case 'add_testimonial':
436
				return 'testimonials';
437
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
438
			case 'manage_portfolio':
439
			case 'add_portfolio':
440
				return 'portfolios';				
441
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
442
			case 'manage_food_menu':
443
			case 'add_food_menu':
444
				return 'food_menus';				
445
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
446
			case 'manage_comic':
447
			case 'add_comic':
448
				return 'comics';				
449
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
450
			case 'manage_comment':
451
				return 'comments';
452
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
453
			case 'manage_category':
454
			case 'manage_tag':
455
				return 'taxonomies';
456
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
457
		}
458
		return false;
459
	}
460
461
	public function update_allowed_redirect_hosts( $allowed ) {
462
		array_push( $allowed, 'wordpress.com' );
463
		return array_unique( $allowed );
464
	}
465
}
466
467
new Jetpack_Calypso_Nudges();
468