Completed
Push — jetpack-fusion-mock-files ( e51750...3b1561 )
by
unknown
13:31
created

admin.php ➔ grunion_ajax_shortcode()   C

Complexity

Conditions 11
Paths 7

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 7
nop 0
dl 0
loc 42
rs 5.2653
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
 * Add a contact form button to the post composition screen
4
 */
5
add_action( 'media_buttons', 'grunion_media_button', 999 );
6
function grunion_media_button( ) {
7
	global $post_ID, $temp_ID, $pagenow;
8
9
	if ( 'press-this.php' === $pagenow ) {
10
		return;
11
	}
12
13
	$iframe_post_id = (int) (0 == $post_ID ? $temp_ID : $post_ID);
14
	$title = __( 'Add Contact Form', 'jetpack' );
15
	$plugin_url = esc_url( GRUNION_PLUGIN_URL );
0 ignored issues
show
Unused Code introduced by
$plugin_url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
16
	$site_url = esc_url( admin_url( "/admin-ajax.php?post_id={$iframe_post_id}&action=grunion_form_builder&TB_iframe=true&width=768" ) );
17
	?>
18
19
	<a id="insert-jetpack-contact-form" class="button thickbox" title="<?php echo esc_attr( $title ); ?>" data-editor="content" href="<?php echo $site_url ?>&id=add_form">
20
		<span class="jetpack-contact-form-icon"></span> <?php echo esc_html( $title ); ?>
21
	</a>
22
23
	<?php
24
}
25
26
add_action( 'wp_ajax_grunion_form_builder', 'grunion_display_form_view' );
27
28
function grunion_display_form_view() {
29
	if ( current_user_can( 'edit_posts' ) ) {
30
		require_once GRUNION_PLUGIN_DIR . 'grunion-form-view.php';
31
	}
32
	exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_display_form_view() 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...
33
}
34
35
// feedback specific css items
36
add_action( 'admin_print_styles', 'grunion_admin_css' );
37
function grunion_admin_css() {
38
	global $current_screen;
39
	if ( is_null( $current_screen ) ) {
40
		return;
41
	}
42
	if ( 'edit-feedback' !== $current_screen->id ) {
43
		return;
44
	}
45
46
	wp_enqueue_script( 'wp-lists' );
47
?>
48
49
<style type='text/css'>
50
.add-new-h2, .view-switch, body.no-js .tablenav select[name^=action], body.no-js #doaction, body.no-js #doaction2 {
51
	display: none
52
}
53
54
.column-feedback_from img {
55
	float:left;
56
	margin-right:10px;
57
	margin-top:3px;
58
}
59
60
.widefat .column-feedback_from {
61
	width: 17%;
62
}
63
.widefat .column-feedback_date {
64
	width: 17%;
65
}
66
67
.spam a {
68
	color: #BC0B0B;
69
}
70
71
.untrash a {
72
	color: #D98500;
73
}
74
75
.unspam a {
76
color: #D98500;
77
}
78
79
</style>
80
81
<?php
82
}
83
84
/**
85
 * Hack a 'Bulk Spam' option for bulk edit in other than spam view
86
 * Hack a 'Bulk Delete' option for bulk edit in spam view
87
 *
88
 * There isn't a better way to do this until
89
 * http://core.trac.wordpress.org/changeset/17297 is resolved
90
 */
91
add_action( 'admin_head', 'grunion_add_bulk_edit_option' );
92
function grunion_add_bulk_edit_option() {
93
94
	$screen = get_current_screen();
95
96
	if ( is_null( $screen ) ) {
97
		return;
98
	}
99
100
	if ( 'edit-feedback' != $screen->id ) {
101
		return;
102
	}
103
104
	// When viewing spam we want to be able to be able to bulk delete
105
	// When viewing anything we want to be able to bulk move to spam
106
	if ( isset( $_GET['post_status'] ) && 'spam' == $_GET['post_status'] ) {
107
		// Create Delete Permanently bulk item
108
		$option_val = 'delete';
109
		$option_txt = __( 'Delete Permanently', 'jetpack' );
110
		$pseudo_selector = 'last-child';
111
112
	} else {
113
		// Create Mark Spam bulk item
114
		$option_val = 'spam';
115
		$option_txt = __( 'Mark as Spam', 'jetpack' );
116
		$pseudo_selector = 'first-child';
117
	}
118
119
	?>
120
		<script type="text/javascript">
121
			jQuery(document).ready(function($) {
122
				$('#posts-filter .actions select').filter('[name=action], [name=action2]').find('option:<?php echo $pseudo_selector; ?>').after('<option value="<?php echo $option_val; ?>"><?php echo esc_attr( $option_txt ); ?></option>' );
123
			})
124
		</script>
125
	<?php
126
}
127
128
/**
129
 * Hack an 'Empty Spam' button to spam view
130
 *
131
 * Leverages core's delete_all functionality
132
 */
133
add_action( 'admin_head', 'grunion_add_empty_spam_button' );
134
function grunion_add_empty_spam_button() {
135
	$screen = get_current_screen();
136
137
	if ( is_null( $screen ) ) {
138
		return;
139
	}
140
141
	// Only add to feedback, only to spam view
142 View Code Duplication
	if ( 'edit-feedback' != $screen->id
143
	|| empty( $_GET['post_status'] )
144
	|| 'spam' !== $_GET['post_status'] ) {
145
		return;
146
	}
147
148
	// Get HTML for the button
149
	$button_html = wp_nonce_field( 'bulk-destroy', '_destroy_nonce', true, false );
150
	$button_html .= get_submit_button( __( 'Empty Spam', 'jetpack' ), 'apply', 'delete_all', false );
151
152
	// Add the button next to the filter button via js
153
	?>
154
		<script type="text/javascript">
155
			jQuery(document).ready(function($) {
156
				$('#posts-filter #post-query-submit').after('<?php echo $button_html; ?>' );
157
			})
158
		</script>
159
	<?php
160
}
161
162
/**
163
 * Handle a bulk spam report
164
 */
165
add_action( 'admin_init', 'grunion_handle_bulk_spam' );
166
function grunion_handle_bulk_spam() {
167
	global $pagenow;
168
169
	if ( 'edit.php' != $pagenow
170
	|| ( empty( $_REQUEST['post_type'] ) || 'feedback' != $_REQUEST['post_type'] ) )
171
		return;
172
173
	// Slip in a success message
174
	if ( ! empty( $_REQUEST['message'] ) && 'marked-spam' == $_REQUEST['message'] )
175
		add_action( 'admin_notices', 'grunion_message_bulk_spam' );
176
177
	if ( ( empty( $_REQUEST['action'] ) || 'spam' != $_REQUEST['action'] ) && ( empty( $_REQUEST['action2'] ) || 'spam' != $_REQUEST['action2'] ) ) {
178
		return;
179
	}
180
181
	check_admin_referer('bulk-posts');
182
183
	if ( empty( $_REQUEST['post'] ) ) {
184
		wp_safe_redirect( wp_get_referer() );
185
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_handle_bulk_spam() 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...
186
	}
187
188
	$post_ids = array_map( 'intval', $_REQUEST['post'] );
189
190
	foreach( $post_ids as $post_id ) {
191
		if ( ! current_user_can( "edit_page", $post_id ) ) {
192
			wp_die( __( 'You are not allowed to manage this item.', 'jetpack' ) );
193
		}
194
195
		$post = array(
196
				'ID'           => $post_id,
197
				'post_status'  => 'spam',
198
			);
199
		$akismet_values = get_post_meta( $post_id, '_feedback_akismet_values', true );
200
		wp_update_post( $post );
201
202
		/**
203
		 * Fires after a comment has been marked by Akismet.
204
		 *
205
		 * Typically this means the comment is spam.
206
		 *
207
		 * @module contact-form
208
		 *
209
		 * @since 2.2.0
210
		 *
211
		 * @param string $comment_status Usually is 'spam', otherwise 'ham'.
212
		 * @param array $akismet_values From '_feedback_akismet_values' in comment meta
213
		 */
214
		do_action( 'contact_form_akismet', 'spam', $akismet_values );
215
	}
216
217
	$redirect_url = add_query_arg( 'message', 'marked-spam', wp_get_referer() );
218
	wp_safe_redirect( $redirect_url );
219
	exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_handle_bulk_spam() 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...
220
}
221
222
function grunion_message_bulk_spam() {
223
	echo '<div class="updated"><p>' . __( 'Feedback(s) marked as spam', 'jetpack' ) . '</p></div>';
224
}
225
226
// remove admin UI parts that we don't support in feedback management
227
add_action( 'admin_menu', 'grunion_admin_menu' );
228
function grunion_admin_menu() {
229
	global $menu, $submenu;
230
	unset( $submenu['edit.php?post_type=feedback'] );
231
}
232
233
add_filter( 'bulk_actions-edit-feedback', 'grunion_admin_bulk_actions' );
234
function grunion_admin_bulk_actions( $actions ) {
235
	global $current_screen;
236
	if ( 'edit-feedback' != $current_screen->id )
237
		return $actions;
238
239
	unset( $actions['edit'] );
240
	return $actions;
241
}
242
243
add_filter( 'views_edit-feedback', 'grunion_admin_view_tabs' );
244
function grunion_admin_view_tabs( $views ) {
245
	global $current_screen;
246
	if ( 'edit-feedback' != $current_screen->id )
247
		return $actions;
0 ignored issues
show
Bug introduced by
The variable $actions does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
248
249
	unset( $views['publish'] );
250
251
	preg_match( '|post_type=feedback\'( class="current")?\>(.*)\<span class=|', $views['all'], $match );
252
	if ( !empty( $match[2] ) )
253
		$views['all'] = str_replace( $match[2], __( 'Messages', 'jetpack' ) . ' ', $views['all'] );
254
255
	return $views;
256
}
257
258
add_filter( 'manage_feedback_posts_columns', 'grunion_post_type_columns_filter' );
259
function grunion_post_type_columns_filter( $cols ) {
260
	$cols = array(
261
		'cb'	=> '<input type="checkbox" />',
262
		'feedback_from'		=> __( 'From', 'jetpack' ),
263
		'feedback_message'		=> __( 'Message', 'jetpack' ),
264
		'feedback_date'			=> __( 'Date', 'jetpack' )
265
	);
266
267
	return $cols;
268
}
269
270
add_action( 'manage_posts_custom_column', 'grunion_manage_post_columns', 10, 2 );
271
function grunion_manage_post_columns( $col, $post_id ) {
272
	global $post;
273
274
	/**
275
	 * Only call parse_fields_from_content if we're dealing with a Grunion custom column.
276
	 */
277
	if ( ! in_array( $col, array( 'feedback_date', 'feedback_from', 'feedback_message' ) ) ) {
278
		return;
279
	}
280
281
	$content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $post_id );
282
283
	switch ( $col ) {
284
		case 'feedback_from':
285
			$author_name  = isset( $content_fields['_feedback_author'] ) ? $content_fields['_feedback_author'] : '';
286
			$author_email = isset( $content_fields['_feedback_author_email'] ) ? $content_fields['_feedback_author_email'] : '';
287
			$author_url   = isset( $content_fields['_feedback_author_url'] ) ? $content_fields['_feedback_author_url'] : '';
288
			$author_ip    = isset( $content_fields['_feedback_ip'] ) ? $content_fields['_feedback_ip'] : '';
289
			$form_url     = isset( $post->post_parent ) ? get_permalink( $post->post_parent ) : null;
290
291
			$author_name_line = '';
292
			if ( !empty( $author_name ) ) {
293
				if ( !empty( $author_email ) )
294
					$author_name_line = get_avatar( $author_email, 32 );
295
296
				$author_name_line .= sprintf( "<strong>%s</strong><br />", esc_html( $author_name ) );
297
			}
298
299
			$author_email_line = '';
300
			if ( !empty( $author_email ) ) {
301
				$author_email_line = sprintf( "<a href='%1\$s' target='_blank'>%2\$s</a><br />", esc_url( "mailto:" . $author_email ) , esc_html( $author_email ) );
302
			}
303
304
			$author_url_line = '';
305
			if ( !empty( $author_url ) ) {
306
				$author_url_line = sprintf( "<a href='%1\$s'>%1\$s</a><br />", esc_url( $author_url ) );
307
			}
308
309
			echo $author_name_line;
310
			echo $author_email_line;
311
			echo $author_url_line;
312
			echo "<a href='edit.php?post_type=feedback&s=" . urlencode( $author_ip );
313
			echo "&mode=detail'>" . esc_html( $author_ip ) . "</a><br />";
314
			if ( $form_url ) {
315
				echo '<a href="' . esc_url( $form_url ) . '">' . esc_html( $form_url ) . '</a>';
316
			}
317
			break;
318
319
		case 'feedback_message':
320
			$post_type_object = get_post_type_object( $post->post_type );
321
			if ( isset( $content_fields['_feedback_subject'] ) ) {
322
				echo '<strong>';
323
				echo esc_html( $content_fields['_feedback_subject'] );
324
				echo '</strong>';
325
				echo '<br />';
326
			}
327
			echo sanitize_text_field( get_the_content( '' ) );
328
			echo '<br />';
329
330
			$extra_fields = get_post_meta( $post_id, '_feedback_extra_fields', TRUE );
331
			if ( !empty( $extra_fields ) ) {
332
				echo '<br /><hr />';
333
				echo '<table cellspacing="0" cellpadding="0" style="">' . "\n";
334
				foreach ( (array) $extra_fields as $k => $v ) {
335
					// Remove prefix from exta fields
336
					echo "<tr><td align='right'><b>". esc_html( preg_replace( '#^\d+_#', '', $k ) ) ."</b></td><td>". sanitize_text_field( $v ) ."</td></tr>\n";
337
				}
338
				echo '</table>';
339
			}
340
341
			echo '<div class="row-actions">';
342
			if ( $post->post_status == 'trash' ) {
343
				echo '<span class="untrash" id="feedback-restore-' . $post_id;
344
				echo '"><a title="';
345
				echo esc_attr__( 'Restore this item from the Trash', 'jetpack' );
346
				echo '" href="' . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&amp;action=untrash', $post->ID ) ), 'untrash-' . $post->post_type . '_' . $post->ID );
347
				echo '">' . __( 'Restore', 'jetpack' ) . '</a></span> | ';
348
349
				echo "<span class='delete'> <a class='submitdelete' title='";
350
				echo esc_attr( __( 'Delete this item permanently', 'jetpack' ) );
351
				echo "' href='" . get_delete_post_link( $post->ID, '', true );
352
				echo "'>" . __( 'Delete Permanently', 'jetpack' ) . "</a></span>";
353
?>
354
355
<script>
356
jQuery(document).ready(function($) {
357
$('#feedback-restore-<?php echo $post_id; ?>').click(function(e) {
358
	e.preventDefault();
359
	$.post(ajaxurl, {
360
			action: 'grunion_ajax_spam',
361
			post_id: '<?php echo $post_id; ?>',
362
			make_it: 'publish',
363
			sub_menu: jQuery('.subsubsub .current').attr('href'),
364
			_ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
365
		},
366
		function(r) {
367
			$('#post-<?php echo $post_id; ?>')
368
				.css({backgroundColor: '#59C859'})
369
				.fadeOut(350, function() {
370
					$(this).remove();
371
					$('.subsubsub').html(r);
372
				});
373
		}
374
	);
375
});
376
});
377
</script>
378
379
<?php
380
			} elseif ( $post->post_status == 'publish' ) {
381
				echo '<span class="spam" id="feedback-spam-' . $post_id;
382
				echo '"><a title="';
383
				echo __( 'Mark this message as spam', 'jetpack' );
384
				echo '" href="' . wp_nonce_url( admin_url( 'admin-ajax.php?post_id=' . $post_id . '&amp;action=spam' ), 'spam-feedback_' . $post_id );
385
				echo '">Spam</a></span>';
386
				echo ' | ';
387
388
				echo '<span class="delete" id="feedback-trash-' . $post_id;
389
				echo '">';
390
				echo '<a class="submitdelete" title="' . esc_attr__( 'Trash', 'jetpack' );
391
				echo '" href="' . get_delete_post_link( $post_id );
392
				echo '">' . __( 'Trash', 'jetpack' ) . '</a></span>';
393
394
?>
395
396
<script>
397
jQuery(document).ready( function($) {
398
	$('#feedback-spam-<?php echo $post_id; ?>').click( function(e) {
399
		e.preventDefault();
400
		$.post( ajaxurl, {
401
				action: 'grunion_ajax_spam',
402
				post_id: '<?php echo $post_id; ?>',
403
				make_it: 'spam',
404
				sub_menu: jQuery('.subsubsub .current').attr('href'),
405
				_ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
406
			},
407
			function( r ) {
408
				$('#post-<?php echo $post_id; ?>')
409
					.css( {backgroundColor:'#FF7979'} )
410
					.fadeOut(350, function() {
411
						$(this).remove();
412
						$('.subsubsub').html(r);
413
				});
414
		});
415
	});
416
417
	$('#feedback-trash-<?php echo $post_id; ?>').click(function(e) {
418
		e.preventDefault();
419
		$.post(ajaxurl, {
420
				action: 'grunion_ajax_spam',
421
				post_id: '<?php echo $post_id; ?>',
422
				make_it: 'trash',
423
				sub_menu: jQuery('.subsubsub .current').attr('href'),
424
				_ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
425
			},
426
			function(r) {
427
				$('#post-<?php echo $post_id; ?>')
428
					.css({backgroundColor: '#FF7979'})
429
					.fadeOut(350, function() {
430
						$(this).remove();
431
						$('.subsubsub').html(r);
432
					});
433
			}
434
		);
435
	});
436
});
437
</script>
438
439
<?php
440
			} elseif ( $post->post_status == 'spam' ) {
441
				echo '<span class="unspam unapprove" id="feedback-ham-' . $post_id;
442
				echo '"><a title="';
443
				echo __( 'Mark this message as NOT spam', 'jetpack' );
444
				echo '" href="">Not Spam</a></span>';
445
				echo ' | ';
446
447
				echo "<span class='delete' id='feedback-trash-" . $post_id;
448
				echo "'> <a class='submitdelete' title='";
449
				echo esc_attr( __( 'Delete this item permanently', 'jetpack' ) );
450
				echo "' href='" . get_delete_post_link( $post->ID, '', true );
451
				echo "'>" . __( 'Delete Permanently', 'jetpack' ) . "</a></span>";
452
?>
453
454
<script>
455
jQuery(document).ready( function($) {
456
	$('#feedback-ham-<?php echo $post_id; ?>').click( function(e) {
457
		e.preventDefault();
458
		$.post( ajaxurl, {
459
				action: 'grunion_ajax_spam',
460
				post_id: '<?php echo $post_id; ?>',
461
				make_it: 'ham',
462
				sub_menu: jQuery('.subsubsub .current').attr('href'),
463
				_ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
464
			},
465
			function( r ) {
466
				$('#post-<?php echo $post_id; ?>')
467
					.css( {backgroundColor:'#59C859'} )
468
					.fadeOut(350, function() {
469
						$(this).remove();
470
						$('.subsubsub').html(r);
471
				});
472
			});
473
	});
474
});
475
</script>
476
477
<?php
478
			}
479
			break;
480
481
		case 'feedback_date':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
482
483
			$date_time_format = _x( '%1$s \a\t %2$s', '{$date_format} \a\t {$time_format}', 'jetpack' );
484
			$date_time_format = sprintf( $date_time_format, get_option( 'date_format' ), get_option( 'time_format' ) );
485
			$time = date_i18n( $date_time_format, get_the_time( 'U' ) );
486
487
			echo $time;
488
			break;
489
	}
490
}
491
492
function grunion_esc_attr( $attr ) {
493
	$out = esc_attr( $attr );
494
	// we also have to entity-encode square brackets so they don't interfere with the shortcode parser
495
	// FIXME: do this better - just stripping out square brackets for now since they mysteriously keep reappearing
496
	$out = str_replace( '[', '', $out );
497
	$out = str_replace( ']', '', $out );
498
	return $out;
499
}
500
501
function grunion_sort_objects( $a, $b ) {
502
	if ( isset($a['order']) && isset($b['order']) )
503
		return $a['order'] - $b['order'];
504
	return 0;
505
}
506
507
// take an array of field types from the form builder, and construct a shortcode form
508
// returns both the shortcode form, and HTML markup representing a preview of the form
509
function grunion_ajax_shortcode() {
510
	check_ajax_referer( 'grunion_shortcode' );
511
512
	if ( ! current_user_can( 'edit_posts' ) ) {
513
		die( '-1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_ajax_shortcode() 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...
514
	}
515
516
	$attributes = array();
517
518
	foreach ( array( 'subject', 'to' ) as $attribute ) {
519
		if ( isset( $_POST[$attribute] ) && strlen( $_POST[$attribute] ) ) {
520
			$attributes[$attribute] = stripslashes( $_POST[$attribute] );
521
		}
522
	}
523
524
	if ( is_array( $_POST['fields'] ) ) {
525
		$fields = stripslashes_deep( $_POST['fields'] );
526
		usort( $fields, 'grunion_sort_objects' );
527
528
		$field_shortcodes = array();
529
530
		foreach ( $fields as $field ) {
531
			$field_attributes = array();
532
533
			if ( isset( $field['required'] ) && 'true' === $field['required'] ) {
534
				$field_attributes['required'] = 'true';
535
			}
536
537
			foreach ( array( 'options', 'label', 'type' ) as $attribute ) {
538
				if ( isset( $field[$attribute] ) ) {
539
					$field_attributes[$attribute] = $field[$attribute];
540
				}
541
			}
542
543
			$field_shortcodes[] = new Grunion_Contact_Form_Field( $field_attributes );
544
		}
545
	}
546
547
	$grunion = new Grunion_Contact_Form( $attributes, $field_shortcodes );
0 ignored issues
show
Bug introduced by
The variable $field_shortcodes does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
548
549
	die( "\n$grunion\n" );
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_ajax_shortcode() 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...
550
}
551
552
// takes a post_id, extracts the contact-form shortcode from that post (if there is one), parses it,
553
// and constructs a json object representing its contents and attributes
554
function grunion_ajax_shortcode_to_json() {
555
	global $post, $grunion_form;
556
557
	check_ajax_referer( 'grunion_shortcode_to_json' );
558
559
	if ( ! empty( $_POST['post_id'] ) && ! current_user_can( 'edit_post', $_POST['post_id'] ) ) {
560
		die( '-1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_ajax_shortcode_to_json() 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...
561
	} elseif ( ! current_user_can( 'edit_posts' ) ) {
562
		die( '-1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_ajax_shortcode_to_json() 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...
563
	}
564
565
	if ( !isset( $_POST['content'] ) || !is_numeric( $_POST['post_id'] ) ) {
566
		die( '-1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_ajax_shortcode_to_json() 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...
567
	}
568
569
	$content = stripslashes( $_POST['content'] );
570
571
	// doesn't look like a post with a [contact-form] already.
572
	if ( false === has_shortcode( $content, 'contact-form' ) ) {
573
		die( '' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_ajax_shortcode_to_json() 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...
574
	}
575
576
	$post = get_post( $_POST['post_id'] );
577
578
	do_shortcode( $content );
579
580
	$grunion = Grunion_Contact_Form::$last;
0 ignored issues
show
Bug introduced by
The property last cannot be accessed from this context as it is declared private in class Grunion_Contact_Form.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
581
582
	$out = array(
583
		'to'      => '',
584
		'subject' => '',
585
		'fields'  => array(),
586
	);
587
588
	foreach ( $grunion->fields as $field ) {
589
		$out['fields'][$field->get_attribute( 'id' )] = $field->attributes;
590
	}
591
592
	$to = $grunion->get_attribute( 'to' );
0 ignored issues
show
Unused Code introduced by
$to is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
593
	$subject = $grunion->get_attribute( 'subject' );
0 ignored issues
show
Unused Code introduced by
$subject is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
594
	foreach ( array( 'to', 'subject' ) as $attribute ) {
595
		$value = $grunion->get_attribute( $attribute );
596
		if ( isset( $grunion->defaults[$attribute] ) && $value == $grunion->defaults[$attribute] ) {
597
			$value = '';
598
		}
599
		$out[$attribute] = $value;
600
	}
601
602
	die( json_encode( $out ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_ajax_shortcode_to_json() 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...
603
}
604
605
606
add_action( 'wp_ajax_grunion_shortcode', 'grunion_ajax_shortcode' );
607
add_action( 'wp_ajax_grunion_shortcode_to_json', 'grunion_ajax_shortcode_to_json' );
608
609
610
// process row-action spam/not spam clicks
611
add_action( 'wp_ajax_grunion_ajax_spam', 'grunion_ajax_spam' );
612
function grunion_ajax_spam() {
613
	global $wpdb;
614
615
	if ( empty( $_POST['make_it'] ) ) {
616
		return;
617
	}
618
619
	$post_id = (int) $_POST['post_id'];
620
	check_ajax_referer( 'grunion-post-status-' . $post_id );
621
	if ( ! current_user_can( "edit_page", $post_id ) ) {
622
		wp_die( __( 'You are not allowed to manage this item.', 'jetpack' ) );
623
	}
624
625
	require_once dirname( __FILE__ ) . '/grunion-contact-form.php';
626
627
	$current_menu = '';
628
	if ( isset( $_POST['sub_menu'] ) && preg_match( '|post_type=feedback|', $_POST['sub_menu'] ) ) {
629
		if ( preg_match( '|post_status=spam|', $_POST['sub_menu'] ) ) {
630
			$current_menu = 'spam';
631
		}
632
		elseif ( preg_match( '|post_status=trash|', $_POST['sub_menu'] ) ) {
633
			$current_menu = 'trash';
634
		}
635
		else {
636
			$current_menu = 'messages';
637
		}
638
639
	}
640
641
	$post = get_post( $post_id );
642
	$post_type_object = get_post_type_object( $post->post_type );
643
	$akismet_values   = get_post_meta( $post_id, '_feedback_akismet_values', TRUE );
644
	if ( $_POST['make_it'] == 'spam' ) {
645
		$post->post_status = 'spam';
646
		$status = wp_insert_post( $post );
0 ignored issues
show
Unused Code introduced by
$status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
647
		wp_transition_post_status( 'spam', 'publish', $post );
648
649
		/** This action is already documented in modules/contact-form/admin.php */
650
		do_action( 'contact_form_akismet', 'spam', $akismet_values );
651
	} elseif ( $_POST['make_it'] == 'ham' ) {
652
		$post->post_status = 'publish';
653
		$status = wp_insert_post( $post );
0 ignored issues
show
Unused Code introduced by
$status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
654
		wp_transition_post_status( 'publish', 'spam', $post );
655
656
		/** This action is already documented in modules/contact-form/admin.php */
657
		do_action( 'contact_form_akismet', 'ham', $akismet_values );
658
659
		$comment_author_email = $reply_to_addr = $message = $to = $headers = false;
0 ignored issues
show
Unused Code introduced by
$headers is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
660
		$blog_url = parse_url( site_url() );
661
662
		// resend the original email
663
		$email = get_post_meta( $post_id, '_feedback_email', TRUE );
664
		$content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $post_id );
665
666
		if ( ! empty( $email ) && !empty( $content_fields ) ) {
667
			if ( isset( $content_fields['_feedback_author_email'] ) ) {
668
				$comment_author_email = $content_fields['_feedback_author_email'];
669
			}
670
671
			if ( isset( $email['to'] ) ) {
672
				$to = $email['to'];
673
			}
674
675
			if ( isset( $email['message'] ) ) {
676
				$message = $email['message'];
677
			}
678
679
			if ( isset( $email['headers'] ) ) {
680
				$headers = $email['headers'];
681
			}
682
			else {
683
				$headers = 'From: "' . $content_fields['_feedback_author'] .'" <wordpress@' . $blog_url['host']  . ">\r\n";
684
685
				if ( ! empty( $comment_author_email ) ){
686
					$reply_to_addr = $comment_author_email;
687
				}
688
				elseif ( is_array( $to ) ) {
689
					$reply_to_addr = $to[0];
690
				}
691
692
				if ( $reply_to_addr ) {
693
					$headers .= 'Reply-To: "' . $content_fields['_feedback_author'] .'" <' . $reply_to_addr . ">\r\n";
694
				}
695
696
				$headers .= "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"";
697
			}
698
699
			/**
700
			 * Filters the subject of the email sent after a contact form submission.
701
			 *
702
			 * @module contact-form
703
			 *
704
			 * @since 3.0.0
705
			 *
706
			 * @param string $content_fields['_feedback_subject'] Feedback's subject line.
707
			 * @param array $content_fields['_feedback_all_fields'] Feedback's data from old fields.
708
			 */
709
			$subject = apply_filters( 'contact_form_subject', $content_fields['_feedback_subject'], $content_fields['_feedback_all_fields'] );
710
711
			Grunion_Contact_Form::wp_mail( $to, $subject, $message, $headers );
712
		}
713
	} elseif( $_POST['make_it'] == 'publish' ) {
714
		if ( ! current_user_can($post_type_object->cap->delete_post, $post_id) ) {
715
			wp_die( __( 'You are not allowed to move this item out of the Trash.', 'jetpack' ) );
716
		}
717
718
		if ( ! wp_untrash_post($post_id) ) {
719
			wp_die( __( 'Error in restoring from Trash.', 'jetpack' ) );
720
		}
721
722
	} elseif( $_POST['make_it'] == 'trash' ) {
723
		if ( ! current_user_can($post_type_object->cap->delete_post, $post_id) ) {
724
			wp_die( __( 'You are not allowed to move this item to the Trash.', 'jetpack' ) );
725
		}
726
727
		if ( ! wp_trash_post($post_id) ) {
728
			wp_die( __( 'Error in moving to Trash.', 'jetpack' ) );
729
		}
730
731
	}
732
733
	$sql = "
734
		SELECT post_status,
735
			COUNT( * ) AS post_count
736
		FROM `{$wpdb->posts}`
737
		WHERE post_type =  'feedback'
738
		GROUP BY post_status
739
	";
740
	$status_count = (array) $wpdb->get_results( $sql, ARRAY_A );
741
742
	$status = array();
743
	$status_html = '';
744
	foreach ( $status_count as $i => $row ) {
745
		$status[$row['post_status']] = $row['post_count'];
746
	}
747
748 View Code Duplication
	if ( isset( $status['publish'] ) ) {
749
		$status_html .= '<li><a href="edit.php?post_type=feedback"';
750
		if ( $current_menu == 'messages' ) {
751
			$status_html .= ' class="current"';
752
		}
753
754
		$status_html .= '>' . __( 'Messages', 'jetpack' ) . ' <span class="count">';
755
		$status_html .= '(' . number_format( $status['publish'] ) . ')';
756
		$status_html .= '</span></a> |</li>';
757
	}
758
759
	if ( isset( $status['trash'] ) ) {
760
		$status_html .= '<li><a href="edit.php?post_status=trash&amp;post_type=feedback"';
761
		if ( $current_menu == 'trash' )
762
			$status_html .= ' class="current"';
763
764
		$status_html .= '>' . __( 'Trash', 'jetpack' ) . ' <span class="count">';
765
		$status_html .= '(' . number_format( $status['trash'] ) . ')';
766
		$status_html .= '</span></a>';
767
		if ( isset( $status['spam'] ) )
768
			$status_html .= ' |';
769
		$status_html .= '</li>';
770
	}
771
772 View Code Duplication
	if ( isset( $status['spam'] ) ) {
773
		$status_html .= '<li><a href="edit.php?post_status=spam&amp;post_type=feedback"';
774
		if ( $current_menu == 'spam' )
775
			$status_html .= ' class="current"';
776
777
		$status_html .= '>' . __( 'Spam', 'jetpack' ) . ' <span class="count">';
778
		$status_html .= '(' . number_format( $status['spam'] ) . ')';
779
		$status_html .= '</span></a></li>';
780
	}
781
782
	echo $status_html;
783
	exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function grunion_ajax_spam() 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...
784
}
785
786
/**
787
 * Add the scripts that will add the "Check for Spam" button to the Feedbacks dashboard page.
788
 */
789
function grunion_enable_spam_recheck() {
790
	if ( ! defined( 'AKISMET_VERSION' ) ) {
791
		return;
792
	}
793
794
	$screen = get_current_screen();
795
796
	// Only add to feedback, only to non-spam view
797 View Code Duplication
	if ( 'edit-feedback' != $screen->id || ( ! empty( $_GET['post_status'] ) && 'spam' == $_GET['post_status'] ) ) {
798
		return;
799
	}
800
801
	// Add the scripts that handle the spam check event.
802
	wp_register_script(
803
		'grunion-admin',
804
		Jetpack::get_file_url_for_environment(
805
			'_inc/build/contact-form/js/grunion-admin.min.js',
806
			'modules/contact-form/js/grunion-admin.js'
807
		),
808
		array( 'jquery' )
809
	);
810
	wp_enqueue_script( 'grunion-admin' );
811
812
	wp_enqueue_style( 'grunion.css' );
813
814
	// Add the actual "Check for Spam" button.
815
	add_action( 'admin_head', 'grunion_check_for_spam_button' );
816
}
817
818
add_action( 'admin_enqueue_scripts', 'grunion_enable_spam_recheck' );
819
820
/**
821
 * Add the "Check for Spam" button to the Feedbacks dashboard page.
822
 */
823
function grunion_check_for_spam_button() {
824
	// Get HTML for the button
825
	$button_html = get_submit_button(
826
		__( 'Check for Spam', 'jetpack' ),
827
		'secondary',
828
		'jetpack-check-feedback-spam',
829
		false,
830
		array( 'class' => 'jetpack-check-feedback-spam' )
831
	);
832
	$button_html .= '<span class="jetpack-check-feedback-spam-spinner"></span>';
833
834
	// Add the button next to the filter button via js
835
	?>
836
	<script type="text/javascript">
837
		jQuery( function( $ ) {
838
			$( '#posts-filter #post-query-submit' ).after( '<?php echo $button_html; ?>' );
839
		} );
840
	</script>
841
	<?php
842
}
843
844
/**
845
 * Recheck all approved feedbacks for spam.
846
 */
847
function grunion_recheck_queue() {
848
	global $wpdb;
849
850
	$query = 'post_type=feedback&post_status=publish';
851
852
	if ( isset( $_POST['limit'], $_POST['offset'] ) ) {
853
		$query .= '&posts_per_page=' . intval( $_POST['limit'] ) . '&offset=' . intval( $_POST['offset'] );
854
	}
855
856
	$approved_feedbacks = get_posts( $query );
857
858
	foreach ( $approved_feedbacks as $feedback ) {
859
		$meta = get_post_meta( $feedback->ID, '_feedback_akismet_values', true );
860
861
		/**
862
		 * Filter whether the submitted feedback is considered as spam.
863
		 *
864
		 * @module contact-form
865
		 *
866
		 * @since 3.4.0
867
		 *
868
		 * @param bool false Is the submitted feedback spam? Default to false.
869
		 * @param array $meta Feedack values returned by the Akismet plugin.
870
		 */
871
		$is_spam = apply_filters( 'jetpack_contact_form_is_spam', false, $meta );
872
873
		if ( $is_spam ) {
874
			wp_update_post( array( 'ID' => $feedback->ID, 'post_status' => 'spam' ) );
875
			/** This action is already documented in modules/contact-form/admin.php */
876
			do_action( 'contact_form_akismet', 'spam', $akismet_values );
0 ignored issues
show
Bug introduced by
The variable $akismet_values does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
877
		}
878
	}
879
880
	wp_send_json( array(
881
		'processed' => count( $approved_feedbacks ),
882
	) );
883
}
884
885
add_action( 'wp_ajax_grunion_recheck_queue', 'grunion_recheck_queue' );
886