PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.2
Jetpack – WP Security, Backup, Speed, & Growth v11.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / contact-form / admin.php

admin.php in Jetpack – WP Security, Backup, Speed, & Growth 11.2, at modules/contact-form/admin.php

1,080 lines 33.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Contact form elements in the admin area. Used with Classic Editor.
4 *
5 * @package automattic/jetpack
6 */
7
8 use Automattic\Jetpack\Assets;
9
10 /**
11 * Add a contact form button to the post composition screen
12 */
13 add_action( 'media_buttons', 'grunion_media_button', 999 );
14 /**
15 * Build contact form button.
16 *
17 * @return void
18 */
19 function grunion_media_button() {
20 global $post_ID, $temp_ID, $pagenow;// phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
21
22 if ( 'press-this.php' === $pagenow ) {
23 return;
24 }
25
26 $iframe_post_id = (int) ( 0 === $post_ID ? $temp_ID : $post_ID );// phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
27 $title = __( 'Add Contact Form', 'jetpack' );
28 $site_url = esc_url( admin_url( "/admin-ajax.php?post_id={$iframe_post_id}&action=grunion_form_builder&TB_iframe=true&width=768" ) );
29 ?>
30
31 <a id="insert-jetpack-contact-form" class="button thickbox" title="<?php echo esc_attr( $title ); ?>" data-editor="content" href="<?php echo esc_attr( $site_url ); ?>&id=add_form">
32 <span class="jetpack-contact-form-icon"></span> <?php echo esc_html( $title ); ?>
33 </a>
34
35 <?php
36 }
37
38 add_action( 'wp_ajax_grunion_form_builder', 'grunion_display_form_view' );
39 /**
40 * Display edit form view.
41 *
42 * @return void
43 */
44 function grunion_display_form_view() {
45 if ( current_user_can( 'edit_posts' ) ) {
46 require_once GRUNION_PLUGIN_DIR . 'grunion-form-view.php';
47 }
48 exit;
49 }
50
51 // feedback specific css items
52 add_action( 'admin_print_styles', 'grunion_admin_css' );
53 /**
54 * Enqueue styles.
55 *
56 * @return void
57 */
58 function grunion_admin_css() {
59 global $current_screen;
60 if ( $current_screen === null ) {
61 return;
62 }
63 if ( 'edit-feedback' !== $current_screen->id ) {
64 return;
65 }
66
67 wp_enqueue_script( 'wp-lists' );
68 ?>
69
70 <style type='text/css'>
71 .add-new-h2, .view-switch, body.no-js .tablenav select[name^=action], body.no-js #doaction, body.no-js #doaction2 {
72 display: none
73 }
74
75 .column-feedback_from img {
76 float:left;
77 margin-right:10px;
78 margin-top:3px;
79 }
80
81 .widefat .column-feedback_from {
82 width: 17%;
83 }
84 .widefat .column-feedback_date {
85 width: 17%;
86 }
87
88 .spam a {
89 color: #BC0B0B;
90 }
91
92 .untrash a {
93 color: #D98500;
94 }
95
96 .unspam a {
97 color: #D98500;
98 }
99
100 </style>
101
102 <?php
103 }
104
105 add_action( 'admin_head', 'grunion_add_bulk_edit_option' );
106 /**
107 * Hack a 'Bulk Spam' option for bulk edit in other than spam view
108 * Hack a 'Bulk Delete' option for bulk edit in spam view
109 *
110 * There isn't a better way to do this until
111 * https://core.trac.wordpress.org/changeset/17297 is resolved
112 */
113 function grunion_add_bulk_edit_option() {
114
115 $screen = get_current_screen();
116
117 if ( $screen === null ) {
118 return;
119 }
120
121 if ( 'edit-feedback' !== $screen->id ) {
122 return;
123 }
124
125 // When viewing spam we want to be able to be able to bulk delete
126 // When viewing anything we want to be able to bulk move to spam
127 if ( isset( $_GET['post_status'] ) && 'spam' === $_GET['post_status'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no changes to the site, we're only rendering the option to choose bulk delete/spam.
128 // Create Delete Permanently bulk item
129 $option_val = 'delete';
130 $option_txt = __( 'Delete Permanently', 'jetpack' );
131 $pseudo_selector = 'last-child';
132
133 } else {
134 // Create Mark Spam bulk item
135 $option_val = 'spam';
136 $option_txt = __( 'Mark as Spam', 'jetpack' );
137 $pseudo_selector = 'first-child';
138 }
139
140 ?>
141 <script type="text/javascript">
142 jQuery(document).ready(function($) {
143 $('#posts-filter .actions select').filter('[name=action], [name=action2]').find('option:<?php echo esc_attr( $pseudo_selector ); ?>').after('<option value="<?php echo esc_attr( $option_val ); ?>"><?php echo esc_attr( $option_txt ); ?></option>' );
144 })
145 </script>
146 <?php
147 }
148
149 add_action( 'admin_init', 'grunion_handle_bulk_spam' );
150 /**
151 * Handle a bulk spam report
152 */
153 function grunion_handle_bulk_spam() {
154 global $pagenow;
155
156 if ( 'edit.php' !== $pagenow
157 || ( empty( $_REQUEST['post_type'] ) || 'feedback' !== $_REQUEST['post_type'] ) ) {
158 return;
159 }
160
161 // Slip in a success message
162 if ( ! empty( $_REQUEST['message'] ) && 'marked-spam' === $_REQUEST['message'] ) {
163 add_action( 'admin_notices', 'grunion_message_bulk_spam' );
164 }
165
166 if ( ( empty( $_REQUEST['action'] ) || 'spam' !== $_REQUEST['action'] ) && ( empty( $_REQUEST['action2'] ) || 'spam' !== $_REQUEST['action2'] ) ) {
167 return;
168 }
169
170 check_admin_referer( 'bulk-posts' );
171
172 if ( empty( $_REQUEST['post'] ) ) {
173 wp_safe_redirect( wp_get_referer() );
174 exit;
175 }
176
177 $post_ids = array_map( 'intval', $_REQUEST['post'] );
178
179 foreach ( $post_ids as $post_id ) {
180 if ( ! current_user_can( 'edit_page', $post_id ) ) {
181 wp_die( esc_html__( 'You are not allowed to manage this item.', 'jetpack' ) );
182 }
183
184 $post = array(
185 'ID' => $post_id,
186 'post_status' => 'spam',
187 );
188 $akismet_values = get_post_meta( $post_id, '_feedback_akismet_values', true );
189 wp_update_post( $post );
190
191 /**
192 * Fires after a comment has been marked by Akismet.
193 *
194 * Typically this means the comment is spam.
195 *
196 * @module contact-form
197 *
198 * @since 2.2.0
199 *
200 * @param string $comment_status Usually is 'spam', otherwise 'ham'.
201 * @param array $akismet_values From '_feedback_akismet_values' in comment meta
202 */
203 do_action( 'contact_form_akismet', 'spam', $akismet_values );
204 }
205
206 $redirect_url = add_query_arg( 'message', 'marked-spam', wp_get_referer() );
207 wp_safe_redirect( $redirect_url );
208 exit;
209 }
210 /**
211 * Display spam message.
212 *
213 * @return void
214 */
215 function grunion_message_bulk_spam() {
216 echo '<div class="updated"><p>' . esc_html__( 'Feedback(s) marked as spam', 'jetpack' ) . '</p></div>';
217 }
218
219 add_filter( 'bulk_actions-edit-feedback', 'grunion_admin_bulk_actions' );
220 /**
221 * Unset edit option when bulk editing.
222 *
223 * @param array $actions List of actions available.
224 * @return array $actions
225 */
226 function grunion_admin_bulk_actions( $actions ) {
227 global $current_screen;
228 if ( 'edit-feedback' !== $current_screen->id ) {
229 return $actions;
230 }
231
232 unset( $actions['edit'] );
233 return $actions;
234 }
235
236 add_filter( 'views_edit-feedback', 'grunion_admin_view_tabs' );
237 /**
238 * Unset publish button when editing feedback.
239 *
240 * @param array $views List of post views.
241 * @return array $views
242 */
243 function grunion_admin_view_tabs( $views ) {
244 global $current_screen;
245 if ( 'edit-feedback' !== $current_screen->id ) {
246 return $views;
247 }
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
256 return $views;
257 }
258
259 add_filter( 'manage_feedback_posts_columns', 'grunion_post_type_columns_filter' );
260 /**
261 * Build Feedback admin page columns.
262 *
263 * @param array $cols List of available columns.
264 * @return array $cols
265 */
266 function grunion_post_type_columns_filter( $cols ) {
267 $cols = array(
268 'cb' => '<input type="checkbox" />',
269 'feedback_from' => __( 'From', 'jetpack' ),
270 'feedback_message' => __( 'Message', 'jetpack' ),
271 'feedback_date' => __( 'Date', 'jetpack' ),
272 );
273
274 return $cols;
275 }
276
277 add_action( 'manage_posts_custom_column', 'grunion_manage_post_columns', 10, 2 );
278 /**
279 * Parse message content and display in appropriate columns.
280 *
281 * @param array $col List of columns available on admin page.
282 * @param int $post_id The current post ID.
283 * @return void
284 */
285 function grunion_manage_post_columns( $col, $post_id ) {
286 global $post;
287
288 /**
289 * Only call parse_fields_from_content if we're dealing with a Grunion custom column.
290 */
291 if ( ! in_array( $col, array( 'feedback_date', 'feedback_from', 'feedback_message' ), true ) ) {
292 return;
293 }
294
295 $content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $post_id );
296
297 switch ( $col ) {
298 case 'feedback_from':
299 $author_name = isset( $content_fields['_feedback_author'] ) ? $content_fields['_feedback_author'] : '';
300 $author_email = isset( $content_fields['_feedback_author_email'] ) ? $content_fields['_feedback_author_email'] : '';
301 $author_url = isset( $content_fields['_feedback_author_url'] ) ? $content_fields['_feedback_author_url'] : '';
302 $author_ip = isset( $content_fields['_feedback_ip'] ) ? $content_fields['_feedback_ip'] : '';
303 $form_url = isset( $post->post_parent ) ? get_permalink( $post->post_parent ) : null;
304
305 $author_name_line = '';
306 if ( ! empty( $author_name ) ) {
307 if ( ! empty( $author_email ) ) {
308 $author_name_line = get_avatar( $author_email, 32 );
309 }
310
311 $author_name_line .= sprintf( '<strong>%s</strong><br />', esc_html( $author_name ) );
312 }
313
314 $author_email_line = '';
315 if ( ! empty( $author_email ) ) {
316 $author_email_line = sprintf( "<a href='%1\$s' target='_blank'>%2\$s</a><br />", esc_url( 'mailto:' . $author_email ), esc_html( $author_email ) );
317 }
318
319 $author_url_line = '';
320 if ( ! empty( $author_url ) ) {
321 $author_url_line = sprintf( "<a href='%1\$s'>%1\$s</a><br />", esc_url( $author_url ) );
322 }
323
324 echo $author_name_line; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, Output escaped above.
325 echo $author_email_line; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, Output escaped above.
326 echo $author_url_line; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, Output escaped above.
327 echo "<a href='edit.php?post_type=feedback&s=" . rawurlencode( $author_ip );
328 echo "&mode=detail'>" . esc_html( $author_ip ) . '</a><br />';
329 if ( $form_url ) {
330 echo '<a href="' . esc_url( $form_url ) . '">' . esc_html( $form_url ) . '</a>';
331 }
332 break;
333
334 case 'feedback_message':
335 $post_type_object = get_post_type_object( $post->post_type );
336 if ( isset( $content_fields['_feedback_subject'] ) ) {
337 echo '<strong>';
338 echo esc_html( $content_fields['_feedback_subject'] );
339 echo '</strong>';
340 echo '<br />';
341 }
342 echo esc_html( get_the_content( '' ) );
343 echo '<br />';
344
345 $extra_fields = get_post_meta( $post_id, '_feedback_extra_fields', true );
346 if ( ! empty( $extra_fields ) ) {
347 echo '<br /><hr />';
348 echo '<table cellspacing="0" cellpadding="0" style="">' . "\n";
349 foreach ( (array) $extra_fields as $k => $v ) {
350 // Remove prefix from exta fields
351 echo "<tr><td align='right'><b>" . esc_html( preg_replace( '#^\d+_#', '', $k ) ) . '</b></td><td>' . esc_html( $v ) . "</td></tr>\n";
352 }
353 echo '</table>';
354 }
355
356 echo '<div class="row-actions">';
357 if ( $post->post_status === 'trash' ) {
358 echo '<span class="untrash" id="feedback-restore-' . (int) $post_id;
359 echo '"><a title="';
360 echo esc_attr__( 'Restore this item from the Trash', 'jetpack' );
361 echo '" href="' . esc_url( wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=untrash', rawurlencode( $post->ID ) ) ) ), 'untrash-' . $post->post_type . '_' . $post->ID );
362 echo '">' . esc_html__( 'Restore', 'jetpack' ) . '</a></span> | ';
363
364 echo "<span class='delete'> <a class='submitdelete' title='";
365 echo esc_attr( __( 'Delete this item permanently', 'jetpack' ) );
366 echo "' href='" . get_delete_post_link( $post->ID, '', true );
367 echo "'>" . esc_html__( 'Delete Permanently', 'jetpack' ) . '</a></span>';
368 ?>
369
370 <script>
371 jQuery(document).ready(function($) {
372 $('#feedback-restore-<?php echo (int) $post_id; ?>').click(function(e) {
373 e.preventDefault()
374 $.post(ajaxurl, {
375 action: 'grunion_ajax_spam',
376 post_id: '<?php echo (int) $post_id; ?>',
377 make_it: 'publish',
378 sub_menu: jQuery('.subsubsub .current').attr('href'),
379 _ajax_nonce: <?php echo wp_json_encode( wp_create_nonce( 'grunion-post-status-' . $post_id ) ); ?>
380 },
381 function(r) {
382 $('#post-<?php echo (int) $post_id; ?>')
383 .css({backgroundColor: '#59C859'})
384 .fadeOut(350, function() {
385 $(this).remove();
386 $('.subsubsub').html(r);
387 });
388 }
389 );
390 });
391 });
392 </script>
393
394 <?php
395 } elseif ( $post->post_status === 'publish' ) {
396 echo '<span class="spam" id="feedback-spam-' . esc_attr( $post_id );
397 echo '"><a title="';
398 echo esc_html__( 'Mark this message as spam', 'jetpack' );
399 echo '" href="' . esc_url( wp_nonce_url( admin_url( 'admin-ajax.php?post_id=' . rawurlencode( $post_id ) . '&action=spam' ) ), 'spam-feedback_' . $post_id );
400 echo '">Spam</a></span>';
401 echo ' | ';
402
403 echo '<span class="delete" id="feedback-trash-' . esc_attr( $post_id );
404 echo '">';
405 echo '<a class="submitdelete" title="' . esc_attr__( 'Trash', 'jetpack' );
406 echo '" href="' . get_delete_post_link( $post_id );
407 echo '">' . esc_html__( 'Trash', 'jetpack' ) . '</a></span>';
408
409 ?>
410
411 <script>
412 jQuery(document).ready( function($) {
413 $('#feedback-spam-<?php echo (int) $post_id; ?>').click( function(e) {
414 e.preventDefault();
415 $.post( ajaxurl, {
416 action: 'grunion_ajax_spam',
417 post_id: '<?php echo (int) $post_id; ?>',
418 make_it: 'spam',
419 sub_menu: jQuery('.subsubsub .current').attr('href'),
420 _ajax_nonce: <?php echo wp_json_encode( wp_create_nonce( 'grunion-post-status-' . $post_id ) ); ?>
421 },
422 function( r ) {
423 $('#post-<?php echo (int) $post_id; ?>')
424 .css( {backgroundColor:'#FF7979'} )
425 .fadeOut(350, function() {
426 $(this).remove();
427 $('.subsubsub').html(r);
428 });
429 });
430 });
431
432 $('#feedback-trash-<?php echo (int) $post_id; ?>').click(function(e) {
433 e.preventDefault();
434 $.post(ajaxurl, {
435 action: 'grunion_ajax_spam',
436 post_id: '<?php echo (int) $post_id; ?>',
437 make_it: 'trash',
438 sub_menu: jQuery('.subsubsub .current').attr('href'),
439 _ajax_nonce: <?php echo wp_json_encode( wp_create_nonce( 'grunion-post-status-' . $post_id ) ); ?>
440 },
441 function(r) {
442 $('#post-<?php echo (int) $post_id; ?>')
443 .css({backgroundColor: '#FF7979'})
444 .fadeOut(350, function() {
445 $(this).remove();
446 $('.subsubsub').html(r);
447 });
448 }
449 );
450 });
451 });
452 </script>
453
454 <?php
455 } elseif ( $post->post_status === 'spam' ) {
456 echo '<span class="unspam unapprove" id="feedback-ham-' . esc_attr( $post_id );
457 echo '"><a title="';
458 echo esc_html__( 'Mark this message as NOT spam', 'jetpack' );
459 echo '" href="">Not Spam</a></span>';
460 echo ' | ';
461
462 echo "<span class='delete' id='feedback-trash-" . esc_attr( $post_id );
463 echo "'> <a class='submitdelete' title='";
464 echo esc_attr( __( 'Delete this item permanently', 'jetpack' ) );
465 echo "' href='" . get_delete_post_link( $post->ID, '', true );
466 echo "'>" . esc_html__( 'Delete Permanently', 'jetpack' ) . '</a></span>';
467 ?>
468
469 <script>
470 jQuery(document).ready( function($) {
471 $('#feedback-ham-<?php echo (int) $post_id; ?>').click( function(e) {
472 e.preventDefault();
473 $.post( ajaxurl, {
474 action: 'grunion_ajax_spam',
475 post_id: '<?php echo (int) $post_id; ?>',
476 make_it: 'ham',
477 sub_menu: jQuery('.subsubsub .current').attr('href'),
478 _ajax_nonce: <?php echo wp_json_encode( wp_create_nonce( 'grunion-post-status-' . $post_id ) ); ?>
479 },
480 function( r ) {
481 $('#post-<?php echo (int) $post_id; ?>')
482 .css( {backgroundColor:'#59C859'} )
483 .fadeOut(350, function() {
484 $(this).remove();
485 $('.subsubsub').html(r);
486 });
487 });
488 });
489 });
490 </script>
491
492 <?php
493 }
494 break;
495
496 case 'feedback_date':
497 // translators: the time the feedback was sent.
498 $date_time_format = _x( '%1$s \a\t %2$s', '{$date_format} \a\t {$time_format}', 'jetpack' );
499 $date_time_format = sprintf( $date_time_format, get_option( 'date_format' ), get_option( 'time_format' ) );
500 $time = date_i18n( $date_time_format, get_the_time( 'U' ) );
501
502 echo esc_html( $time );
503 break;
504 }
505 }
506
507 /**
508 * Escape grunion attributes.
509 *
510 * @param string $attr - the attribute we're escaping.
511 *
512 * @return string
513 */
514 function grunion_esc_attr( $attr ) {
515 $out = esc_attr( $attr );
516 // we also have to entity-encode square brackets so they don't interfere with the shortcode parser
517 // FIXME: do this better - just stripping out square brackets for now since they mysteriously keep reappearing
518 $out = str_replace( '[', '', $out );
519 $out = str_replace( ']', '', $out );
520 return $out;
521 }
522
523 /**
524 * Sort grunion items.
525 *
526 * @param array $a - the first item we're sorting.
527 * @param array $b - the second item we're sorting.
528 *
529 * @return string
530 */
531 function grunion_sort_objects( $a, $b ) {
532 if ( isset( $a['order'] ) && isset( $b['order'] ) ) {
533 return $a['order'] - $b['order'];
534 }
535 return 0;
536 }
537
538 /**
539 * Take an array of field types from the form builder, and construct a shortcode form.
540 * returns both the shortcode form, and HTML markup representing a preview of the form
541 */
542 function grunion_ajax_shortcode() {
543 check_ajax_referer( 'grunion_shortcode' );
544
545 if ( ! current_user_can( 'edit_posts' ) ) {
546 die( '-1' );
547 }
548
549 $attributes = array();
550
551 foreach ( array( 'subject', 'to' ) as $attribute ) {
552 if ( isset( $_POST[ $attribute ] ) && is_scalar( $_POST[ $attribute ] ) && (string) $_POST[ $attribute ] !== '' ) {
553 $attributes[ $attribute ] = sanitize_text_field( wp_unslash( $_POST[ $attribute ] ) );
554 }
555 }
556
557 if ( isset( $_POST['fields'] ) && is_array( $_POST['fields'] ) ) {
558 $fields = sanitize_text_field( stripslashes_deep( $_POST['fields'] ) );
559 usort( $fields, 'grunion_sort_objects' );
560
561 $field_shortcodes = array();
562
563 foreach ( $fields as $field ) {
564 $field_attributes = array();
565
566 if ( isset( $field['required'] ) && 'true' === $field['required'] ) {
567 $field_attributes['required'] = 'true';
568 }
569
570 foreach ( array( 'options', 'label', 'type' ) as $attribute ) {
571 if ( isset( $field[ $attribute ] ) ) {
572 $field_attributes[ $attribute ] = $field[ $attribute ];
573 }
574 }
575
576 $field_shortcodes[] = new Grunion_Contact_Form_Field( $field_attributes );
577 }
578 }
579
580 $grunion = new Grunion_Contact_Form( $attributes, $field_shortcodes );
581
582 die( "\n$grunion\n" ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
583 }
584
585 /**
586 * Takes a post_id, extracts the contact-form shortcode from that post (if there is one), parses it,
587 * and constructs a json object representing its contents and attributes.
588 */
589 function grunion_ajax_shortcode_to_json() {
590 global $post;
591
592 check_ajax_referer( 'grunion_shortcode_to_json' );
593
594 if ( ! empty( $_POST['post_id'] ) && ! current_user_can( 'edit_post', (int) $_POST['post_id'] ) ) {
595 die( '-1' );
596 } elseif ( ! current_user_can( 'edit_posts' ) ) {
597 die( '-1' );
598 }
599
600 if ( ! isset( $_POST['content'] ) || ! is_numeric( $_POST['post_id'] ) ) {
601 die( '-1' );
602 }
603
604 $content = sanitize_text_field( wp_unslash( $_POST['content'] ) );
605
606 // doesn't look like a post with a [contact-form] already.
607 if ( false === has_shortcode( $content, 'contact-form' ) ) {
608 die( '' );
609 }
610
611 $post = get_post( (int) $_POST['post_id'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
612
613 do_shortcode( $content );
614
615 $grunion = Grunion_Contact_Form::$last;
616
617 $out = array(
618 'to' => '',
619 'subject' => '',
620 'fields' => array(),
621 );
622
623 foreach ( $grunion->fields as $field ) {
624 $out['fields'][ $field->get_attribute( 'id' ) ] = $field->attributes;
625 }
626
627 foreach ( array( 'to', 'subject' ) as $attribute ) {
628 $value = $grunion->get_attribute( $attribute );
629 if ( isset( $grunion->defaults[ $attribute ] ) && $value === $grunion->defaults[ $attribute ] ) {
630 $value = '';
631 }
632 $out[ $attribute ] = $value;
633 }
634
635 die( wp_json_encode( $out ) );
636 }
637
638 add_action( 'wp_ajax_grunion_shortcode', 'grunion_ajax_shortcode' );
639 add_action( 'wp_ajax_grunion_shortcode_to_json', 'grunion_ajax_shortcode_to_json' );
640
641 // process row-action spam/not spam clicks
642 add_action( 'wp_ajax_grunion_ajax_spam', 'grunion_ajax_spam' );
643
644 /**
645 * Handle marking feedback as spam.
646 */
647 function grunion_ajax_spam() {
648 global $wpdb;
649
650 if ( empty( $_POST['make_it'] ) ) {
651 return;
652 }
653
654 $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
655 check_ajax_referer( 'grunion-post-status-' . $post_id );
656 if ( ! current_user_can( 'edit_page', $post_id ) ) {
657 wp_die( esc_html__( 'You are not allowed to manage this item.', 'jetpack' ) );
658 }
659
660 require_once __DIR__ . '/grunion-contact-form.php';
661
662 $current_menu = '';
663 if ( isset( $_POST['sub_menu'] ) && preg_match( '|post_type=feedback|', sanitize_text_field( wp_unslash( $_POST['sub_menu'] ) ) ) ) {
664 if ( preg_match( '|post_status=spam|', sanitize_text_field( wp_unslash( $_POST['sub_menu'] ) ) ) ) {
665 $current_menu = 'spam';
666 } elseif ( preg_match( '|post_status=trash|', sanitize_text_field( wp_unslash( $_POST['sub_menu'] ) ) ) ) {
667 $current_menu = 'trash';
668 } else {
669 $current_menu = 'messages';
670 }
671 }
672
673 $post = get_post( $post_id );
674 $post_type_object = get_post_type_object( $post->post_type );
675 $akismet_values = get_post_meta( $post_id, '_feedback_akismet_values', true );
676 if ( $_POST['make_it'] === 'spam' ) {
677 $post->post_status = 'spam';
678 $status = wp_insert_post( $post );
679
680 /** This action is already documented in modules/contact-form/admin.php */
681 do_action( 'contact_form_akismet', 'spam', $akismet_values );
682 } elseif ( $_POST['make_it'] === 'ham' ) {
683 $post->post_status = 'publish';
684 $status = wp_insert_post( $post );
685
686 /** This action is already documented in modules/contact-form/admin.php */
687 do_action( 'contact_form_akismet', 'ham', $akismet_values );
688
689 $comment_author_email = false;
690 $reply_to_addr = false;
691 $message = false;
692 $to = false;
693 $headers = false;
694 $blog_url = wp_parse_url( site_url() );
695
696 // resend the original email
697 $email = get_post_meta( $post_id, '_feedback_email', true );
698 $content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $post_id );
699
700 if ( ! empty( $email ) && ! empty( $content_fields ) ) {
701 if ( isset( $content_fields['_feedback_author_email'] ) ) {
702 $comment_author_email = $content_fields['_feedback_author_email'];
703 }
704
705 if ( isset( $email['to'] ) ) {
706 $to = $email['to'];
707 }
708
709 if ( isset( $email['message'] ) ) {
710 $message = $email['message'];
711 }
712
713 if ( isset( $email['headers'] ) ) {
714 $headers = $email['headers'];
715 } else {
716 $headers = 'From: "' . $content_fields['_feedback_author'] . '" <wordpress@' . $blog_url['host'] . ">\r\n";
717
718 if ( ! empty( $comment_author_email ) ) {
719 $reply_to_addr = $comment_author_email;
720 } elseif ( is_array( $to ) ) {
721 $reply_to_addr = $to[0];
722 }
723
724 if ( $reply_to_addr ) {
725 $headers .= 'Reply-To: "' . $content_fields['_feedback_author'] . '" <' . $reply_to_addr . ">\r\n";
726 }
727
728 $headers .= 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . '"';
729 }
730
731 /**
732 * Filters the subject of the email sent after a contact form submission.
733 *
734 * @module contact-form
735 *
736 * @since 3.0.0
737 *
738 * @param string $content_fields['_feedback_subject'] Feedback's subject line.
739 * @param array $content_fields['_feedback_all_fields'] Feedback's data from old fields.
740 */
741 $subject = apply_filters( 'contact_form_subject', $content_fields['_feedback_subject'], $content_fields['_feedback_all_fields'] );
742
743 Grunion_Contact_Form::wp_mail( $to, $subject, $message, $headers );
744 }
745 } elseif ( $_POST['make_it'] === 'publish' ) {
746 if ( ! current_user_can( $post_type_object->cap->delete_post, $post_id ) ) {
747 wp_die( esc_html__( 'You are not allowed to move this item out of the Trash.', 'jetpack' ) );
748 }
749
750 if ( ! wp_untrash_post( $post_id ) ) {
751 wp_die( esc_html__( 'Error in restoring from Trash.', 'jetpack' ) );
752 }
753 } elseif ( $_POST['make_it'] === 'trash' ) {
754 if ( ! current_user_can( $post_type_object->cap->delete_post, $post_id ) ) {
755 wp_die( esc_html__( 'You are not allowed to move this item to the Trash.', 'jetpack' ) );
756 }
757
758 if ( ! wp_trash_post( $post_id ) ) {
759 wp_die( esc_html__( 'Error in moving to Trash.', 'jetpack' ) );
760 }
761 }
762
763 $sql = "
764 SELECT post_status,
765 COUNT( * ) AS post_count
766 FROM `{$wpdb->posts}`
767 WHERE post_type = 'feedback'
768 GROUP BY post_status
769 ";
770 $status_count = (array) $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
771
772 $status = array();
773 $status_html = '';
774 foreach ( $status_count as $row ) {
775 $status[ $row['post_status'] ] = $row['post_count'];
776 }
777
778 if ( isset( $status['publish'] ) ) {
779 $status_html .= '<li><a href="edit.php?post_type=feedback"';
780 if ( $current_menu === 'messages' ) {
781 $status_html .= ' class="current"';
782 }
783
784 $status_html .= '>' . __( 'Messages', 'jetpack' ) . ' <span class="count">';
785 $status_html .= '(' . number_format( $status['publish'] ) . ')';
786 $status_html .= '</span></a> |</li>';
787 }
788
789 if ( isset( $status['trash'] ) ) {
790 $status_html .= '<li><a href="edit.php?post_status=trash&amp;post_type=feedback"';
791 if ( $current_menu === 'trash' ) {
792 $status_html .= ' class="current"';
793 }
794
795 $status_html .= '>' . __( 'Trash', 'jetpack' ) . ' <span class="count">';
796 $status_html .= '(' . number_format( $status['trash'] ) . ')';
797 $status_html .= '</span></a>';
798 if ( isset( $status['spam'] ) ) {
799 $status_html .= ' |';
800 }
801 $status_html .= '</li>';
802 }
803
804 if ( isset( $status['spam'] ) ) {
805 $status_html .= '<li><a href="edit.php?post_status=spam&amp;post_type=feedback"';
806 if ( $current_menu === 'spam' ) {
807 $status_html .= ' class="current"';
808 }
809
810 $status_html .= '>' . __( 'Spam', 'jetpack' ) . ' <span class="count">';
811 $status_html .= '(' . number_format( $status['spam'] ) . ')';
812 $status_html .= '</span></a></li>';
813 }
814
815 echo $status_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we're building the html to echo.
816 exit;
817 }
818
819 /**
820 * Add the scripts that will add the "Check for Spam" button to the Feedbacks dashboard page.
821 */
822 function grunion_enable_spam_recheck() {
823 if ( ! defined( 'AKISMET_VERSION' ) ) {
824 return;
825 }
826
827 $screen = get_current_screen();
828
829 // Only add to feedback, only to non-spam view
830 if ( 'edit-feedback' !== $screen->id || ( ! empty( $_GET['post_status'] ) && 'spam' === $_GET['post_status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not making site changes with this check.
831 return;
832 }
833
834 // Add the actual "Check for Spam" button.
835 add_action( 'admin_head', 'grunion_check_for_spam_button' );
836 }
837
838 add_action( 'admin_enqueue_scripts', 'grunion_enable_spam_recheck' );
839
840 /**
841 * Add the JS and CSS necessary for the Feedback admin page to function.
842 */
843 function grunion_add_admin_scripts() {
844 $screen = get_current_screen();
845
846 if ( 'edit-feedback' !== $screen->id ) {
847 return;
848 }
849
850 // Add the scripts that handle the spam check event.
851 wp_register_script(
852 'grunion-admin',
853 Assets::get_file_url_for_environment(
854 '_inc/build/contact-form/js/grunion-admin.min.js',
855 'modules/contact-form/js/grunion-admin.js'
856 ),
857 array( 'jquery' ),
858 JETPACK__VERSION,
859 true
860 );
861
862 wp_enqueue_script( 'grunion-admin' );
863
864 wp_enqueue_style( 'grunion.css' );
865
866 // Only add to feedback, only to spam view.
867 if ( empty( $_GET['post_status'] ) || 'spam' !== $_GET['post_status'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not making site changes with this check
868 return;
869 }
870
871 $feedbacks_count = wp_count_posts( 'feedback' );
872 $nonce = wp_create_nonce( 'jetpack_delete_spam_feedbacks' );
873 $success_url = remove_query_arg( array( 'jetpack_empty_feedback_spam_error', 'post_status' ) ); // Go to the "All Feedback" page.
874 $failure_url = add_query_arg( 'jetpack_empty_feedback_spam_error', '1' ); // Refresh the current page and show an error.
875 $spam_count = $feedbacks_count->spam;
876
877 $button_parameters = array(
878 /* translators: The placeholder is for showing how much of the process has completed, as a percent. e.g., "Emptying Spam (40%)" */
879 'progress_label' => __( 'Emptying Spam (%1$s%)', 'jetpack' ),
880 'success_url' => $success_url,
881 'failure_url' => $failure_url,
882 'spam_count' => $spam_count,
883 'nonce' => $nonce,
884 'label' => __( 'Empty Spam', 'jetpack' ),
885 );
886
887 wp_localize_script( 'grunion-admin', 'jetpack_empty_spam_button_parameters', $button_parameters );
888 }
889
890 add_action( 'admin_enqueue_scripts', 'grunion_add_admin_scripts' );
891
892 /**
893 * Add the "Check for Spam" button to the Feedbacks dashboard page.
894 */
895 function grunion_check_for_spam_button() {
896 // Nonce name.
897 $nonce_name = 'jetpack_check_feedback_spam_' . (string) get_current_blog_id();
898 // Get HTML for the button.
899 $button_html = get_submit_button(
900 __( 'Check for Spam', 'jetpack' ),
901 'secondary',
902 'jetpack-check-feedback-spam',
903 false,
904 array(
905 'data-failure-url' => add_query_arg( 'jetpack_check_feedback_spam_error', '1' ), // Refresh the current page and show an error.
906 'data-nonce-name' => $nonce_name,
907 )
908 );
909 $button_html .= '<span class="jetpack-check-feedback-spam-spinner"></span>';
910 $button_html .= wp_nonce_field( 'grunion_recheck_queue', $nonce_name, false, false );
911
912 // Add the button next to the filter button via js.
913 ?>
914 <script type="text/javascript">
915 jQuery( function( $ ) {
916 $( '#posts-filter #post-query-submit' ).after( <?php echo wp_json_encode( $button_html ); ?> );
917 } );
918 </script>
919 <?php
920 }
921
922 /**
923 * Recheck all approved feedbacks for spam.
924 */
925 function grunion_recheck_queue() {
926 $blog_id = get_current_blog_id();
927
928 if (
929 empty( $_POST[ 'jetpack_check_feedback_spam_' . (string) $blog_id ] )
930 || ! wp_verify_nonce( sanitize_key( $_POST[ 'jetpack_check_feedback_spam_' . (string) $blog_id ] ), 'grunion_recheck_queue' )
931 ) {
932 wp_send_json_error(
933 __( 'You aren’t authorized to do that.', 'jetpack' ),
934 403
935 );
936
937 return;
938 }
939
940 if ( ! current_user_can( 'delete_others_posts' ) ) {
941 wp_send_json_error(
942 __( 'You don’t have permission to do that.', 'jetpack' ),
943 403
944 );
945
946 return;
947 }
948
949 $query = 'post_type=feedback&post_status=publish';
950
951 if ( isset( $_POST['limit'], $_POST['offset'] ) ) {
952 $query .= '&posts_per_page=' . (int) $_POST['limit'] . '&offset=' . (int) $_POST['offset'];
953 }
954
955 $approved_feedbacks = get_posts( $query );
956
957 foreach ( $approved_feedbacks as $feedback ) {
958 $meta = get_post_meta( $feedback->ID, '_feedback_akismet_values', true );
959
960 if ( ! $meta ) {
961 // _feedback_akismet_values is eventually deleted when it's no longer
962 // within a reasonable time period to check the feedback for spam, so
963 // if it's gone, don't attempt a spam recheck.
964 continue;
965 }
966
967 $meta['recheck_reason'] = 'recheck_queue';
968
969 /**
970 * Filter whether the submitted feedback is considered as spam.
971 *
972 * @module contact-form
973 *
974 * @since 3.4.0
975 *
976 * @param bool false Is the submitted feedback spam? Default to false.
977 * @param array $meta Feedack values returned by the Akismet plugin.
978 */
979 $is_spam = apply_filters( 'jetpack_contact_form_is_spam', false, $meta );
980
981 if ( $is_spam ) {
982 wp_update_post(
983 array(
984 'ID' => $feedback->ID,
985 'post_status' => 'spam',
986 )
987 );
988 /** This action is already documented in modules/contact-form/admin.php */
989 do_action( 'contact_form_akismet', 'spam', $meta );
990 }
991 }
992
993 wp_send_json(
994 array(
995 'processed' => count( $approved_feedbacks ),
996 )
997 );
998 }
999
1000 add_action( 'wp_ajax_grunion_recheck_queue', 'grunion_recheck_queue' );
1001
1002 /**
1003 * Delete a number of spam feedbacks via an AJAX request.
1004 */
1005 function grunion_delete_spam_feedbacks() {
1006 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'jetpack_delete_spam_feedbacks' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- core doesn't sanitize nonce checks either.
1007 wp_send_json_error(
1008 __( 'You aren’t authorized to do that.', 'jetpack' ),
1009 403
1010 );
1011
1012 return;
1013 }
1014
1015 if ( ! current_user_can( 'delete_others_posts' ) ) {
1016 wp_send_json_error(
1017 __( 'You don’t have permission to do that.', 'jetpack' ),
1018 403
1019 );
1020
1021 return;
1022 }
1023
1024 $deleted_feedbacks = 0;
1025
1026 $delete_limit = 25;
1027 /**
1028 * Filter the amount of Spam feedback one can delete at once.
1029 *
1030 * @module contact-form
1031 *
1032 * @since 8.7.0
1033 *
1034 * @param int $delete_limit Number of spam to process at once. Default to 25.
1035 */
1036 $delete_limit = apply_filters( 'jetpack_delete_spam_feedbacks_limit', $delete_limit );
1037 $delete_limit = (int) $delete_limit;
1038 $delete_limit = max( 1, min( 100, $delete_limit ) ); // Allow a range of 1-100 for the delete limit.
1039
1040 $query_args = array(
1041 'post_type' => 'feedback',
1042 'post_status' => 'spam',
1043 'posts_per_page' => $delete_limit,
1044 );
1045
1046 $query = new WP_Query( $query_args );
1047 $spam_feedbacks = $query->get_posts();
1048
1049 foreach ( $spam_feedbacks as $feedback ) {
1050 wp_delete_post( $feedback->ID, true );
1051
1052 $deleted_feedbacks++;
1053 }
1054
1055 wp_send_json(
1056 array(
1057 'success' => true,
1058 'data' => array(
1059 'counts' => array(
1060 'deleted' => $deleted_feedbacks,
1061 'limit' => $delete_limit,
1062 ),
1063 ),
1064 )
1065 );
1066 }
1067 add_action( 'wp_ajax_jetpack_delete_spam_feedbacks', 'grunion_delete_spam_feedbacks' );
1068
1069 /**
1070 * Show an admin notice if the "Empty Spam" or "Check Spam" process was unable to complete, probably due to a permissions error.
1071 */
1072 function grunion_feedback_admin_notice() {
1073 if ( isset( $_GET['jetpack_empty_feedback_spam_error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1074 echo '<div class="notice notice-error"><p>' . esc_html( __( 'An error occurred while trying to empty the Feedback spam folder.', 'jetpack' ) ) . '</p></div>';
1075 } elseif ( isset( $_GET['jetpack_check_feedback_spam_error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1076 echo '<div class="notice notice-error"><p>' . esc_html( __( 'An error occurred while trying to check for spam among the feedback you received.', 'jetpack' ) ) . '</p></div>';
1077 }
1078 }
1079 add_action( 'admin_notices', 'grunion_feedback_admin_notice' );
1080