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