Completed
Push — fix/inline-docs-410 ( f96891...63b75c )
by
unknown
43:24 queued 33:40
created

admin.php ➔ grunion_admin_css()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 46
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
	$cols = array(
259
		'cb'	=> '<input type="checkbox" />',
260
		'feedback_from'		=> __( 'From', 'jetpack' ),
261
		'feedback_message'		=> __( 'Message', 'jetpack' ),
262
		'feedback_date'			=> __( 'Date', 'jetpack' )
263
	);
264
265
	return $cols;
266
}
267
268
add_action( 'manage_posts_custom_column', 'grunion_manage_post_columns', 10, 2 );
269
function grunion_manage_post_columns( $col, $post_id ) {
270
	global $post;
271
272
	/**
273
	 * Only call parse_fields_from_content if we're dealing with a Grunion custom column.
274
	 */
275
	if ( ! in_array( $col, array( 'feedback_date', 'feedback_from', 'feedback_message' ) ) ) {
276
		return;
277
	}
278
279
	$content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $post_id );
280
281
	switch ( $col ) {
282
		case 'feedback_from':
283
			$author_name  = isset( $content_fields['_feedback_author'] ) ? $content_fields['_feedback_author'] : '';
284
			$author_email = isset( $content_fields['_feedback_author_email'] ) ? $content_fields['_feedback_author_email'] : '';
285
			$author_url   = isset( $content_fields['_feedback_author_url'] ) ? $content_fields['_feedback_author_url'] : '';
286
			$author_ip    = isset( $content_fields['_feedback_ip'] ) ? $content_fields['_feedback_ip'] : '';
287
			$form_url     = isset( $post->post_parent ) ? get_permalink( $post->post_parent ) : null;
288
289
			$author_name_line = '';
290
			if ( !empty( $author_name ) ) {
291
				if ( !empty( $author_email ) )
292
					$author_name_line = get_avatar( $author_email, 32 );
293
294
				$author_name_line .= sprintf( "<strong>%s</strong><br />", esc_html( $author_name ) );
295
			}
296
297
			$author_email_line = '';
298
			if ( !empty( $author_email ) ) {
299
				$author_email_line = sprintf( "<a href='%1\$s' target='_blank'>%2\$s</a><br />", esc_url( "mailto:" . $author_email ) , esc_html( $author_email ) );
300
			}
301
302
			$author_url_line = '';
303
			if ( !empty( $author_url ) ) {
304
				$author_url_line = sprintf( "<a href='%1\$s'>%1\$s</a><br />", esc_url( $author_url ) );
305
			}
306
307
			echo $author_name_line;
308
			echo $author_email_line;
309
			echo $author_url_line;
310
			echo "<a href='edit.php?post_type=feedback&s=" . urlencode( $author_ip );
311
			echo "&mode=detail'>" . esc_html( $author_ip ) . "</a><br />";
312
			if ( $form_url ) {
313
				echo '<a href="' . esc_url( $form_url ) . '">' . esc_html( $form_url ) . '</a>';
314
			}
315
			break;
316
317
		case 'feedback_message':
318
			$post_type_object = get_post_type_object( $post->post_type );
319
			if ( isset( $content_fields['_feedback_subject'] ) ) {
320
				echo '<strong>';
321
				echo esc_html( $content_fields['_feedback_subject'] );
322
				echo '</strong>';
323
				echo '<br />';
324
			}
325
			echo sanitize_text_field( get_the_content( '' ) );
326
			echo '<br />';
327
328
			$extra_fields = get_post_meta( $post_id, '_feedback_extra_fields', TRUE );
329
			if ( !empty( $extra_fields ) ) {
330
				echo '<br /><hr />';
331
				echo '<table cellspacing="0" cellpadding="0" style="">' . "\n";
332
				foreach ( (array) $extra_fields as $k => $v ) {
333
					// Remove prefix from exta fields
334
					echo "<tr><td align='right'><b>". esc_html( preg_replace( '#^\d+_#', '', $k ) ) ."</b></td><td>". sanitize_text_field( $v ) ."</td></tr>\n";
335
				}
336
				echo '</table>';
337
			}
338
339
			echo '<div class="row-actions">';
340
			if ( $post->post_status == 'trash' ) {
341
				echo '<span class="untrash" id="feedback-restore-' . $post_id;
342
				echo '"><a title="';
343
				echo esc_attr__( 'Restore this item from the Trash', 'jetpack' );
344
				echo '" href="' . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&amp;action=untrash', $post->ID ) ), 'untrash-' . $post->post_type . '_' . $post->ID );
345
				echo '">' . __( 'Restore', 'jetpack' ) . '</a></span> | ';
346
347
				echo "<span class='delete'> <a class='submitdelete' title='";
348
				echo esc_attr( __( 'Delete this item permanently', 'jetpack' ) );
349
				echo "' href='" . get_delete_post_link( $post->ID, '', true );
350
				echo "'>" . __( 'Delete Permanently', 'jetpack' ) . "</a></span>";
351
?>
352
353
<script>
354
jQuery(document).ready(function($) {
355
$('#feedback-restore-<?php echo $post_id; ?>').click(function(e) {
356
	e.preventDefault();
357
	$.post(ajaxurl, {
358
			action: 'grunion_ajax_spam',
359
			post_id: '<?php echo $post_id; ?>',
360
			make_it: 'publish',
361
			sub_menu: jQuery('.subsubsub .current').attr('href'),
362
			_ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
363
		},
364
		function(r) {
365
			$('#post-<?php echo $post_id; ?>')
366
				.css({backgroundColor: '#59C859'})
367
				.fadeOut(350, function() {
368
					$(this).remove();
369
					$('.subsubsub').html(r);
370
				});
371
		}
372
	);
373
});
374
});
375
</script>
376
377
<?php
378
			} elseif ( $post->post_status == 'publish' ) {
379
				echo '<span class="spam" id="feedback-spam-' . $post_id;
380
				echo '"><a title="';
381
				echo __( 'Mark this message as spam', 'jetpack' );
382
				echo '" href="' . wp_nonce_url( admin_url( 'admin-ajax.php?post_id=' . $post_id . '&amp;action=spam' ), 'spam-feedback_' . $post_id );
383
				echo '">Spam</a></span>';
384
				echo ' | ';
385
386
				echo '<span class="delete" id="feedback-trash-' . $post_id;
387
				echo '">';
388
				echo '<a class="submitdelete" title="' . esc_attr__( 'Trash', 'jetpack' );
389
				echo '" href="' . get_delete_post_link( $post_id );
390
				echo '">' . __( 'Trash', 'jetpack' ) . '</a></span>';
391
392
?>
393
394
<script>
395
jQuery(document).ready( function($) {
396
	$('#feedback-spam-<?php echo $post_id; ?>').click( function(e) {
397
		e.preventDefault();
398
		$.post( ajaxurl, {
399
				action: 'grunion_ajax_spam',
400
				post_id: '<?php echo $post_id; ?>',
401
				make_it: 'spam',
402
				sub_menu: jQuery('.subsubsub .current').attr('href'),
403
				_ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
404
			},
405
			function( r ) {
406
				$('#post-<?php echo $post_id; ?>')
407
					.css( {backgroundColor:'#FF7979'} )
408
					.fadeOut(350, function() {
409
						$(this).remove();
410
						$('.subsubsub').html(r);
411
				});
412
		});
413
	});
414
415
	$('#feedback-trash-<?php echo $post_id; ?>').click(function(e) {
416
		e.preventDefault();
417
		$.post(ajaxurl, {
418
				action: 'grunion_ajax_spam',
419
				post_id: '<?php echo $post_id; ?>',
420
				make_it: 'trash',
421
				sub_menu: jQuery('.subsubsub .current').attr('href'),
422
				_ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
423
			},
424
			function(r) {
425
				$('#post-<?php echo $post_id; ?>')
426
					.css({backgroundColor: '#FF7979'})
427
					.fadeOut(350, function() {
428
						$(this).remove();
429
						$('.subsubsub').html(r);
430
					});
431
			}
432
		);
433
	});
434
});
435
</script>
436
437
<?php
438
			} elseif ( $post->post_status == 'spam' ) {
439
				echo '<span class="unspam unapprove" id="feedback-ham-' . $post_id;
440
				echo '"><a title="';
441
				echo __( 'Mark this message as NOT spam', 'jetpack' );
442
				echo '" href="">Not Spam</a></span>';
443
				echo ' | ';
444
445
				echo "<span class='delete' id='feedback-trash-" . $post_id;
446
				echo "'> <a class='submitdelete' title='";
447
				echo esc_attr( __( 'Delete this item permanently', 'jetpack' ) );
448
				echo "' href='" . get_delete_post_link( $post->ID, '', true );
449
				echo "'>" . __( 'Delete Permanently', 'jetpack' ) . "</a></span>";
450
?>
451
452
<script>
453
jQuery(document).ready( function($) {
454
	$('#feedback-ham-<?php echo $post_id; ?>').click( function(e) {
455
		e.preventDefault();
456
		$.post( ajaxurl, {
457
				action: 'grunion_ajax_spam',
458
				post_id: '<?php echo $post_id; ?>',
459
				make_it: 'ham',
460
				sub_menu: jQuery('.subsubsub .current').attr('href'),
461
				_ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
462
			},
463
			function( r ) {
464
				$('#post-<?php echo $post_id; ?>')
465
					.css( {backgroundColor:'#59C859'} )
466
					.fadeOut(350, function() {
467
						$(this).remove();
468
						$('.subsubsub').html(r);
469
				});
470
			});
471
	});
472
});
473
</script>
474
475
<?php
476
			}
477
			break;
478
479
		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...
480
481
			$date_time_format = _x( '%1$s \a\t %2$s', '{$date_format} \a\t {$time_format}', 'jetpack' );
482
			$date_time_format = sprintf( $date_time_format, get_option( 'date_format' ), get_option( 'time_format' ) );
483
			$time = date_i18n( $date_time_format, get_the_time( 'U' ) );
484
485
			echo $time;
486
			break;
487
	}
488
}
489
490
function grunion_esc_attr( $attr ) {
491
	$out = esc_attr( $attr );
492
	// we also have to entity-encode square brackets so they don't interfere with the shortcode parser
493
	// FIXME: do this better - just stripping out square brackets for now since they mysteriously keep reappearing
494
	$out = str_replace( '[', '', $out );
495
	$out = str_replace( ']', '', $out );
496
	return $out;
497
}
498
499
function grunion_sort_objects( $a, $b ) {
500
	if ( isset($a['order']) && isset($b['order']) )
501
		return $a['order'] - $b['order'];
502
	return 0;
503
}
504
505
// take an array of field types from the form builder, and construct a shortcode form
506
// returns both the shortcode form, and HTML markup representing a preview of the form
507
function grunion_ajax_shortcode() {
508
	check_ajax_referer( 'grunion_shortcode' );
509
510
	$attributes = array();
511
512
	foreach ( array( 'subject', 'to' ) as $attribute ) {
513
		if ( isset( $_POST[$attribute] ) && strlen( $_POST[$attribute] ) ) {
514
			$attributes[$attribute] = stripslashes( $_POST[$attribute] );
515
		}
516
	}
517
518
	if ( is_array( $_POST['fields'] ) ) {
519
		$fields = stripslashes_deep( $_POST['fields'] );
520
		usort( $fields, 'grunion_sort_objects' );
521
522
		$field_shortcodes = array();
523
524
		foreach ( $fields as $field ) {
525
			$field_attributes = array();
526
527
			if ( isset( $field['required'] ) && 'true' === $field['required'] ) {
528
				$field_attributes['required'] = 'true';
529
			}
530
531
			foreach ( array( 'options', 'label', 'type' ) as $attribute ) {
532
				if ( isset( $field[$attribute] ) ) {
533
					$field_attributes[$attribute] = $field[$attribute];
534
				}
535
			}
536
537
			$field_shortcodes[] = new Grunion_Contact_Form_Field( $field_attributes );
538
		}
539
	}
540
541
	$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...
542
543
	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...
544
}
545
546
// takes a post_id, extracts the contact-form shortcode from that post (if there is one), parses it,
547
// and constructs a json object representing its contents and attributes
548
function grunion_ajax_shortcode_to_json() {
549
	global $post, $grunion_form;
550
551
	check_ajax_referer( 'grunion_shortcode_to_json' );
552
553
	if ( !isset( $_POST['content'] ) || !is_numeric( $_POST['post_id'] ) ) {
554
		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...
555
	}
556
557
	$content = stripslashes( $_POST['content'] );
558
559
	// doesn't look like a post with a [contact-form] already.
560
	if ( false === has_shortcode( $content, 'contact-form' ) ) {
561
		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...
562
	}
563
564
	$post = get_post( $_POST['post_id'] );
565
566
	do_shortcode( $content );
567
568
	$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...
569
570
	$out = array(
571
		'to'      => '',
572
		'subject' => '',
573
		'fields'  => array(),
574
	);
575
576
	foreach ( $grunion->fields as $field ) {
577
		$out['fields'][$field->get_attribute( 'id' )] = $field->attributes;
578
	}
579
580
	$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...
581
	$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...
582
	foreach ( array( 'to', 'subject' ) as $attribute ) {
583
		$value = $grunion->get_attribute( $attribute );
584
		if ( isset( $grunion->defaults[$attribute] ) && $value == $grunion->defaults[$attribute] ) {
585
			$value = '';
586
		}
587
		$out[$attribute] = $value;
588
	}
589
590
	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...
591
}
592
593
594
add_action( 'wp_ajax_grunion_shortcode', 'grunion_ajax_shortcode' );
595
add_action( 'wp_ajax_grunion_shortcode_to_json', 'grunion_ajax_shortcode_to_json' );
596
597
598
// process row-action spam/not spam clicks
599
add_action( 'wp_ajax_grunion_ajax_spam', 'grunion_ajax_spam' );
600
function grunion_ajax_spam() {
601
	global $wpdb;
602
603
	if ( empty( $_POST['make_it'] ) ) {
604
		return;
605
	}
606
607
	$post_id = (int) $_POST['post_id'];
608
	check_ajax_referer( 'grunion-post-status-' . $post_id );
609
	if ( ! current_user_can( "edit_page", $post_id ) ) {
610
		wp_die( __( 'You are not allowed to manage this item.', 'jetpack' ) );
611
	}
612
613
	require_once dirname( __FILE__ ) . '/grunion-contact-form.php';
614
615
	$current_menu = '';
616
	if ( isset( $_POST['sub_menu'] ) && preg_match( '|post_type=feedback|', $_POST['sub_menu'] ) ) {
617
		if ( preg_match( '|post_status=spam|', $_POST['sub_menu'] ) ) {
618
			$current_menu = 'spam';
619
		}
620
		elseif ( preg_match( '|post_status=trash|', $_POST['sub_menu'] ) ) {
621
			$current_menu = 'trash';
622
		}
623
		else {
624
			$current_menu = 'messages';
625
		}
626
627
	}
628
629
	$post = get_post( $post_id );
630
	$post_type_object = get_post_type_object( $post->post_type );
631
	$akismet_values   = get_post_meta( $post_id, '_feedback_akismet_values', TRUE );
632
	if ( $_POST['make_it'] == 'spam' ) {
633
		$post->post_status = 'spam';
634
		$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...
635
		wp_transition_post_status( 'spam', 'publish', $post );
636
637
		/** This action is already documented in modules/contact-form/admin.php */
638
		do_action( 'contact_form_akismet', 'spam', $akismet_values );
639
	} elseif ( $_POST['make_it'] == 'ham' ) {
640
		$post->post_status = 'publish';
641
		$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...
642
		wp_transition_post_status( 'publish', 'spam', $post );
643
644
		/** This action is already documented in modules/contact-form/admin.php */
645
		do_action( 'contact_form_akismet', 'ham', $akismet_values );
646
647
		$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...
648
		$blog_url = parse_url( site_url() );
649
650
		// resend the original email
651
		$email = get_post_meta( $post_id, '_feedback_email', TRUE );
652
		$content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $post_id );
653
654
		if ( ! empty( $email ) && !empty( $content_fields ) ) {
655
			if ( isset( $content_fields['_feedback_author_email'] ) ) {
656
				$comment_author_email = $content_fields['_feedback_author_email'];
657
			}
658
659
			if ( isset( $email['to'] ) ) {
660
				$to = $email['to'];
661
			}
662
663
			if ( isset( $email['message'] ) ) {
664
				$message = $email['message'];
665
			}
666
667
			if ( isset( $email['headers'] ) ) {
668
				$headers = $email['headers'];
669
			}
670
			else {
671
				$headers = 'From: "' . $content_fields['_feedback_author'] .'" <wordpress@' . $blog_url['host']  . ">\r\n";
672
673
				if ( ! empty( $comment_author_email ) ){
674
					$reply_to_addr = $comment_author_email;
675
				}
676
				elseif ( is_array( $to ) ) {
677
					$reply_to_addr = $to[0];
678
				}
679
680
				if ( $reply_to_addr ) {
681
					$headers .= 'Reply-To: "' . $content_fields['_feedback_author'] .'" <' . $reply_to_addr . ">\r\n";
682
				}
683
684
				$headers .= "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"";
685
			}
686
687
			/**
688
			 * Filters the subject of the email sent after a contact form submission.
689
			 *
690
			 * @module contact-form
691
			 *
692
			 * @since 3.0.0
693
			 *
694
			 * @param string $content_fields['_feedback_subject'] Feedback's subject line.
695
			 * @param array $content_fields['_feedback_all_fields'] Feedback's data from old fields.
696
			 */
697
			$subject = apply_filters( 'contact_form_subject', $content_fields['_feedback_subject'], $content_fields['_feedback_all_fields'] );
698
699
			wp_mail( $to, $subject, $message, $headers );
700
		}
701
	} elseif( $_POST['make_it'] == 'publish' ) {
702
		if ( ! current_user_can($post_type_object->cap->delete_post, $post_id) ) {
703
			wp_die( __( 'You are not allowed to move this item out of the Trash.', 'jetpack' ) );
704
		}
705
706
		if ( ! wp_untrash_post($post_id) ) {
707
			wp_die( __( 'Error in restoring from Trash.', 'jetpack' ) );
708
		}
709
710
	} elseif( $_POST['make_it'] == 'trash' ) {
711
		if ( ! current_user_can($post_type_object->cap->delete_post, $post_id) ) {
712
			wp_die( __( 'You are not allowed to move this item to the Trash.', 'jetpack' ) );
713
		}
714
715
		if ( ! wp_trash_post($post_id) ) {
716
			wp_die( __( 'Error in moving to Trash.', 'jetpack' ) );
717
		}
718
719
	}
720
721
	$sql = "
722
		SELECT post_status,
723
			COUNT( * ) AS post_count
724
		FROM `{$wpdb->posts}`
725
		WHERE post_type =  'feedback'
726
		GROUP BY post_status
727
	";
728
	$status_count = (array) $wpdb->get_results( $sql, ARRAY_A );
729
730
	$status = array();
731
	$status_html = '';
732
	foreach ( $status_count as $i => $row ) {
733
		$status[$row['post_status']] = $row['post_count'];
734
	}
735
736 View Code Duplication
	if ( isset( $status['publish'] ) ) {
737
		$status_html .= '<li><a href="edit.php?post_type=feedback"';
738
		if ( $current_menu == 'messages' ) {
739
			$status_html .= ' class="current"';
740
		}
741
742
		$status_html .= '>' . __( 'Messages', 'jetpack' ) . ' <span class="count">';
743
		$status_html .= '(' . number_format( $status['publish'] ) . ')';
744
		$status_html .= '</span></a> |</li>';
745
	}
746
747
	if ( isset( $status['trash'] ) ) {
748
		$status_html .= '<li><a href="edit.php?post_status=trash&amp;post_type=feedback"';
749
		if ( $current_menu == 'trash' )
750
			$status_html .= ' class="current"';
751
752
		$status_html .= '>' . __( 'Trash', 'jetpack' ) . ' <span class="count">';
753
		$status_html .= '(' . number_format( $status['trash'] ) . ')';
754
		$status_html .= '</span></a>';
755
		if ( isset( $status['spam'] ) )
756
			$status_html .= ' |';
757
		$status_html .= '</li>';
758
	}
759
760 View Code Duplication
	if ( isset( $status['spam'] ) ) {
761
		$status_html .= '<li><a href="edit.php?post_status=spam&amp;post_type=feedback"';
762
		if ( $current_menu == 'spam' )
763
			$status_html .= ' class="current"';
764
765
		$status_html .= '>' . __( 'Spam', 'jetpack' ) . ' <span class="count">';
766
		$status_html .= '(' . number_format( $status['spam'] ) . ')';
767
		$status_html .= '</span></a></li>';
768
	}
769
770
	echo $status_html;
771
	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...
772
}
773
774
add_action( 'omnisearch_add_providers', 'grunion_omnisearch_add_providers' );
775
function grunion_omnisearch_add_providers() {
776
	// Feedback uses capability_type 'page'
777
	if ( current_user_can( 'edit_pages' ) ) {
778
		require_once( GRUNION_PLUGIN_DIR . '/grunion-omnisearch.php' );
779
		new Jetpack_Omnisearch_Grunion;
780
	}
781
}
782
783
/**
784
 * Add the scripts that will add the "Check for Spam" button to the Feedbacks dashboard page.
785
 */
786
function grunion_enable_spam_recheck() {
787
	if ( ! defined( 'AKISMET_VERSION' ) ) {
788
		return;
789
	}
790
791
	$screen = get_current_screen();
792
793
	// Only add to feedback, only to non-spam view
794 View Code Duplication
	if ( 'edit-feedback' != $screen->id || ( ! empty( $_GET['post_status'] ) && 'spam' == $_GET['post_status'] ) ) {
795
		return;
796
	}
797
798
	// Add the scripts that handle the spam check event.
799
	wp_register_script( 'grunion-admin', plugin_dir_url( __FILE__ ) . 'js/grunion-admin.js', array( 'jquery' ) );
800
	wp_enqueue_script( 'grunion-admin' );
801
802
	wp_enqueue_style( 'grunion.css' );
803
804
	// Add the actual "Check for Spam" button.
805
	add_action( 'admin_head', 'grunion_check_for_spam_button' );
806
}
807
808
add_action( 'admin_enqueue_scripts', 'grunion_enable_spam_recheck' );
809
810
/**
811
 * Add the "Check for Spam" button to the Feedbacks dashboard page.
812
 */
813
function grunion_check_for_spam_button() {
814
	// Get HTML for the button
815
	$button_html = get_submit_button(
816
		__( 'Check for Spam', 'jetpack' ),
817
		'secondary',
818
		'jetpack-check-feedback-spam',
819
		false,
820
		array( 'class' => 'jetpack-check-feedback-spam' )
821
	);
822
	$button_html .= '<span class="jetpack-check-feedback-spam-spinner"></span>';
823
824
	// Add the button next to the filter button via js
825
	?>
826
	<script type="text/javascript">
827
		jQuery( function( $ ) {
828
			$( '#posts-filter #post-query-submit' ).after( '<?php echo $button_html; ?>' );
829
		} );
830
	</script>
831
	<?php
832
}
833
834
/**
835
 * Recheck all approved feedbacks for spam.
836
 */
837
function grunion_recheck_queue() {
838
	global $wpdb;
839
840
	$query = 'post_type=feedback&post_status=publish';
841
842
	if ( isset( $_POST['limit'], $_POST['offset'] ) ) {
843
		$query .= '&posts_per_page=' . intval( $_POST['limit'] ) . '&offset=' . intval( $_POST['offset'] );
844
	}
845
846
	$approved_feedbacks = get_posts( $query );
847
848
	foreach ( $approved_feedbacks as $feedback ) {
849
		$meta = get_post_meta( $feedback->ID, '_feedback_akismet_values', true );
850
851
		/**
852
		 * Filter whether the submitted feedback is considered as spam.
853
		 *
854
		 * @module contact-form
855
		 *
856
		 * @since 3.4.0
857
		 *
858
		 * @param bool false Is the submitted feedback spam? Default to false.
859
		 * @param array $meta Feedack values returned by the Akismet plugin.
860
		 */
861
		$is_spam = apply_filters( 'jetpack_contact_form_is_spam', false, $meta );
862
863
		if ( $is_spam ) {
864
			wp_update_post( array( 'ID' => $feedback->ID, 'post_status' => 'spam' ) );
865
			/** This action is already documented in modules/contact-form/admin.php */
866
			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...
867
		}
868
	}
869
870
	wp_send_json( array(
871
		'processed' => count( $approved_feedbacks ),
872
	) );
873
}
874
875
add_action( 'wp_ajax_grunion_recheck_queue', 'grunion_recheck_queue' );
876