/modules/contact-form/admin.php

  1. <?php 
  2.  
  3. function grunion_menu_alter() { 
  4. if( is_rtl() ) { 
  5. wp_enqueue_style( 'grunion-menu-alter', plugins_url( 'css/rtl/menu-alter-rtl.css', __FILE__ ) ); 
  6. } else { 
  7. wp_enqueue_style( 'grunion-menu-alter', plugins_url( 'css/menu-alter.css', __FILE__ ) ); 
  8.  
  9. add_action( 'admin_enqueue_scripts', 'grunion_menu_alter' ); 
  10.  
  11. /** 
  12. * Add a contact form button to the post composition screen 
  13. */ 
  14. add_action( 'media_buttons', 'grunion_media_button', 999 ); 
  15. function grunion_media_button( ) { 
  16. global $post_ID, $temp_ID, $pagenow; 
  17.  
  18. if ( 'press-this.php' === $pagenow ) { 
  19. return; 
  20.  
  21. $iframe_post_id = (int) (0 == $post_ID ? $temp_ID : $post_ID); 
  22. $title = __( 'Add Contact Form', 'jetpack' ); 
  23. $plugin_url = esc_url( GRUNION_PLUGIN_URL ); 
  24. $site_url = esc_url( admin_url( "/admin-ajax.php?post_id={$iframe_post_id}&action=grunion_form_builder&TB_iframe=true&width=768" ) ); 
  25. ?> 
  26.  
  27. <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"> 
  28. <span class="jetpack-contact-form-icon"></span> <?php echo esc_html( $title ); ?> 
  29. </a> 
  30.  
  31. <?php 
  32.  
  33. add_action( 'wp_ajax_grunion_form_builder', 'grunion_display_form_view' ); 
  34.  
  35. function grunion_display_form_view() { 
  36. require_once GRUNION_PLUGIN_DIR . 'grunion-form-view.php'; 
  37. exit; 
  38.  
  39. // feedback specific css items 
  40. add_action( 'admin_print_styles', 'grunion_admin_css' ); 
  41. function grunion_admin_css() { 
  42. global $current_screen; 
  43. if ( is_null( $current_screen ) ) { 
  44. return; 
  45. if ( ! in_array( $current_screen->id, array( 'edit-feedback', 'jetpack_page_omnisearch', 'dashboard_page_omnisearch' ) ) ) { 
  46. return; 
  47.  
  48. wp_enqueue_script( 'wp-lists' ); 
  49. ?> 
  50.  
  51. <style type='text/css'> 
  52. .add-new-h2, .view-switch, body.no-js .tablenav select[name^=action], body.no-js #doaction, body.no-js #doaction2 { 
  53. display: none 
  54.  
  55. .column-feedback_from img { 
  56. float:left; 
  57. margin-right:10px; 
  58. margin-top:3px; 
  59.  
  60. .widefat .column-feedback_from { 
  61. width: 17%; 
  62. .widefat .column-feedback_date { 
  63. width: 17%; 
  64.  
  65. .spam a { 
  66. color: #BC0B0B; 
  67.  
  68. .untrash a { 
  69. color: #D98500; 
  70.  
  71. .unspam a { 
  72. color: #D98500; 
  73.  
  74. #icon-edit.icon32-posts-feedback, #icon-post.icon32-posts-feedback { background: url("<?php echo GRUNION_PLUGIN_URL; ?>images/grunion-menu-big.png") no-repeat !important; } 
  75. @media only screen and (min--moz-device-pixel-ratio: 1.5), only screen and (-o-min-device-pixel-ratio: 3/2), only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-device-pixel-ratio: 1.5) { 
  76. #icon-edit.icon32-posts-feedback, #icon-post.icon32-posts-feedback { background: url("<?php echo GRUNION_PLUGIN_URL; ?>images/grunion-menu-big-2x.png") no-repeat !important; background-size: 30px 31px !important; } 
  77.  
  78. #icon-edit.icon32-posts-feedback { background-position: 2px 2px !important; } 
  79.  
  80. </style> 
  81.  
  82. <?php 
  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. if ( 'edit-feedback' != $screen->id ) { 
  100. return; 
  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. <script type="text/javascript"> 
  118. jQuery(document).ready(function($) { 
  119. $('#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>' ); 
  120. }) 
  121. </script> 
  122. <?php 
  123.  
  124. /** 
  125. * Hack an 'Empty Spam' button to spam view 
  126. * 
  127. * Leverages core's delete_all functionality 
  128. */ 
  129. add_action( 'admin_head', 'grunion_add_empty_spam_button' ); 
  130. function grunion_add_empty_spam_button() { 
  131. $screen = get_current_screen(); 
  132.  
  133. if ( is_null( $screen ) ) { 
  134. return; 
  135.  
  136. // Only add to feedback, only to spam view 
  137. if ( 'edit-feedback' != $screen->id 
  138. || empty( $_GET['post_status'] ) 
  139. || 'spam' !== $_GET['post_status'] ) { 
  140. return; 
  141.  
  142. // Get HTML for the button 
  143. $button_html = wp_nonce_field( 'bulk-destroy', '_destroy_nonce', true, false ); 
  144. $button_html .= get_submit_button( __( 'Empty Spam', 'jetpack' ), 'apply', 'delete_all', false ); 
  145.  
  146. // Add the button next to the filter button via js 
  147. ?> 
  148. <script type="text/javascript"> 
  149. jQuery(document).ready(function($) { 
  150. $('#posts-filter #post-query-submit').after('<?php echo $button_html; ?>' ); 
  151. }) 
  152. </script> 
  153. <?php 
  154.  
  155. /** 
  156. * Handle a bulk spam report 
  157. */ 
  158. add_action( 'admin_init', 'grunion_handle_bulk_spam' ); 
  159. function grunion_handle_bulk_spam() { 
  160. global $pagenow; 
  161.  
  162. if ( 'edit.php' != $pagenow 
  163. || ( empty( $_REQUEST['post_type'] ) || 'feedback' != $_REQUEST['post_type'] ) ) 
  164. return; 
  165.  
  166. // Slip in a success message 
  167. if ( ! empty( $_REQUEST['message'] ) && 'marked-spam' == $_REQUEST['message'] ) 
  168. add_action( 'admin_notices', 'grunion_message_bulk_spam' ); 
  169.  
  170. if ( ( empty( $_REQUEST['action'] ) || 'spam' != $_REQUEST['action'] ) && ( empty( $_REQUEST['action2'] ) || 'spam' != $_REQUEST['action2'] ) ) { 
  171. return; 
  172.  
  173. check_admin_referer('bulk-posts'); 
  174.  
  175. if ( empty( $_REQUEST['post'] ) ) { 
  176. wp_safe_redirect( wp_get_referer() ); 
  177. exit; 
  178.  
  179. $post_ids = array_map( 'intval', $_REQUEST['post'] ); 
  180.  
  181. foreach( $post_ids as $post_id ) { 
  182. if ( ! current_user_can( "edit_page", $post_id ) ) { 
  183. wp_die( __( 'You are not allowed to manage this item.', 'jetpack' ) ); 
  184.  
  185. $post = array( 
  186. 'ID' => $post_id,  
  187. 'post_status' => 'spam',  
  188. ); 
  189. $akismet_values = get_post_meta( $post_id, '_feedback_akismet_values', true ); 
  190. wp_update_post( $post ); 
  191.  
  192. /** 
  193. * Fires after a comment has been marked by Akismet. 
  194. * 
  195. * Typically this means the comment is spam. 
  196. * 
  197. * @module contact-form 
  198. * 
  199. * @since 2.2.0 
  200. * 
  201. * @param string $comment_status Usually is 'spam', otherwise 'ham'. 
  202. * @param array $akismet_values From '_feedback_akismet_values' in comment meta 
  203. */ 
  204. do_action( 'contact_form_akismet', 'spam', $akismet_values ); 
  205.  
  206. $redirect_url = add_query_arg( 'message', 'marked-spam', wp_get_referer() ); 
  207. wp_safe_redirect( $redirect_url ); 
  208. exit; 
  209.  
  210. function grunion_message_bulk_spam() { 
  211. echo '<div class="updated"><p>' . __( 'Feedback(s) marked as spam', 'jetpack' ) . '</p></div>'; 
  212.  
  213. // remove admin UI parts that we don't support in feedback management 
  214. add_action( 'admin_menu', 'grunion_admin_menu' ); 
  215. function grunion_admin_menu() { 
  216. global $menu, $submenu; 
  217. unset( $submenu['edit.php?post_type=feedback'] ); 
  218.  
  219. add_filter( 'bulk_actions-edit-feedback', 'grunion_admin_bulk_actions' ); 
  220. function grunion_admin_bulk_actions( $actions ) { 
  221. global $current_screen; 
  222. if ( 'edit-feedback' != $current_screen->id ) 
  223. return $actions; 
  224.  
  225. unset( $actions['edit'] ); 
  226. return $actions; 
  227.  
  228. add_filter( 'views_edit-feedback', 'grunion_admin_view_tabs' ); 
  229. function grunion_admin_view_tabs( $views ) { 
  230. global $current_screen; 
  231. if ( 'edit-feedback' != $current_screen->id ) 
  232. return $actions; 
  233.  
  234. unset( $views['publish'] ); 
  235.  
  236. preg_match( '|post_type=feedback\'( class="current")?\>(.*)\<span class=|', $views['all'], $match ); 
  237. if ( !empty( $match[2] ) ) 
  238. $views['all'] = str_replace( $match[2], __( 'Messages', 'jetpack' ) . ' ', $views['all'] ); 
  239.  
  240. return $views; 
  241.  
  242. add_filter( 'manage_feedback_posts_columns', 'grunion_post_type_columns_filter' ); 
  243. function grunion_post_type_columns_filter( $cols ) { 
  244. $cols = array( 
  245. 'cb' => '<input type="checkbox" />',  
  246. 'feedback_from' => __( 'From', 'jetpack' ),  
  247. 'feedback_message' => __( 'Message', 'jetpack' ),  
  248. 'feedback_date' => __( 'Date', 'jetpack' ) 
  249. ); 
  250.  
  251. return $cols; 
  252.  
  253. add_action( 'manage_posts_custom_column', 'grunion_manage_post_columns', 10, 2 ); 
  254. function grunion_manage_post_columns( $col, $post_id ) { 
  255. global $post; 
  256.  
  257. /** 
  258. * Only call parse_fields_from_content if we're dealing with a Grunion custom column. 
  259. */ 
  260. if ( ! in_array( $col, array( 'feedback_date', 'feedback_from', 'feedback_message' ) ) ) { 
  261. return; 
  262.  
  263. $content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $post_id ); 
  264.  
  265. switch ( $col ) { 
  266. case 'feedback_from': 
  267. $author_name = $content_fields['_feedback_author']; 
  268. $author_email = $content_fields['_feedback_author_email']; 
  269. $author_url = $content_fields['_feedback_author_url']; 
  270. $author_ip = $content_fields['_feedback_ip']; 
  271. $form_url = isset( $post->post_parent ) ? get_permalink( $post->post_parent ) : null; 
  272.  
  273. $author_name_line = ''; 
  274. if ( !empty( $author_name ) ) { 
  275. if ( !empty( $author_email ) ) 
  276. $author_name_line = get_avatar( $author_email, 32 ); 
  277.  
  278. $author_name_line .= sprintf( "<strong>%s</strong><br />", esc_html( $author_name ) ); 
  279.  
  280. $author_email_line = ''; 
  281. if ( !empty( $author_email ) ) { 
  282. $author_email_line = sprintf( "<a href='%1\$s'>%2\$s</a><br />", esc_url( "mailto:" . $author_email ) , esc_html( $author_email ) ); 
  283.  
  284. $author_url_line = ''; 
  285. if ( !empty( $author_url ) ) { 
  286. $author_url_line = sprintf( "<a href='%1\$s'>%1\$s</a><br />", esc_url( $author_url ) ); 
  287.  
  288. echo $author_name_line; 
  289. echo $author_email_line; 
  290. echo $author_url_line; 
  291. echo "<a href='edit.php?post_type=feedback&s=" . urlencode( $author_ip ); 
  292. echo "&mode=detail'>" . esc_html( $author_ip ) . "</a><br />"; 
  293. if ( $form_url ) { 
  294. echo '<a href="' . esc_url( $form_url ) . '">' . esc_html( $form_url ) . '</a>'; 
  295. break; 
  296.  
  297. case 'feedback_message': 
  298. $post_type_object = get_post_type_object( $post->post_type ); 
  299. echo '<strong>'; 
  300. echo esc_html( $content_fields['_feedback_subject'] ); 
  301. echo '</strong><br />'; 
  302. echo sanitize_text_field( get_the_content( '' ) ); 
  303. echo '<br />'; 
  304.  
  305. $extra_fields = get_post_meta( $post_id, '_feedback_extra_fields', TRUE ); 
  306. if ( !empty( $extra_fields ) ) { 
  307. echo '<br /><hr />'; 
  308. echo '<table cellspacing="0" cellpadding="0" style="">' . "\n"; 
  309. foreach ( (array) $extra_fields as $k => $v ) { 
  310. // Remove prefix from exta fields 
  311. echo "<tr><td align='right'><b>". esc_html( preg_replace( '#^\d+_#', '', $k ) ) ."</b></td><td>". sanitize_text_field( $v ) ."</td></tr>\n"; 
  312. echo '</table>'; 
  313.  
  314. echo '<div class="row-actions">'; 
  315. if ( $post->post_status == 'trash' ) { 
  316. echo '<span class="untrash" id="feedback-restore-' . $post_id; 
  317. echo '"><a title="'; 
  318. echo esc_attr__( 'Restore this item from the Trash', 'jetpack' ); 
  319. echo '" href="' . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=untrash', $post->ID ) ), 'untrash-' . $post->post_type . '_' . $post->ID ); 
  320. echo '">' . __( 'Restore', 'jetpack' ) . '</a></span> | '; 
  321.  
  322. echo "<span class='delete'> <a class='submitdelete' title='"; 
  323. echo esc_attr( __( 'Delete this item permanently', 'jetpack' ) ); 
  324. echo "' href='" . get_delete_post_link( $post->ID, '', true ); 
  325. echo "'>" . __( 'Delete Permanently', 'jetpack' ) . "</a></span>"; 
  326. ?> 
  327.  
  328. <script> 
  329. jQuery(document).ready(function($) { 
  330. $('#feedback-restore-<?php echo $post_id; ?>').click(function(e) { 
  331. e.preventDefault(); 
  332. $.post(ajaxurl, { 
  333. action: 'grunion_ajax_spam',  
  334. post_id: '<?php echo $post_id; ?>',  
  335. make_it: 'publish',  
  336. sub_menu: jQuery('.subsubsub .current').attr('href'),  
  337. _ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>' 
  338. },  
  339. function(r) { 
  340. $('#post-<?php echo $post_id; ?>') 
  341. .css({backgroundColor: '#59C859'}) 
  342. .fadeOut(350, function() { 
  343. $(this).remove(); 
  344. $('.subsubsub').html(r); 
  345. }); 
  346. ); 
  347. }); 
  348. }); 
  349. </script> 
  350.  
  351. <?php 
  352. } elseif ( $post->post_status == 'publish' ) { 
  353. echo '<span class="spam" id="feedback-spam-' . $post_id; 
  354. echo '"><a title="'; 
  355. echo __( 'Mark this message as spam', 'jetpack' ); 
  356. echo '" href="' . wp_nonce_url( admin_url( 'admin-ajax.php?post_id=' . $post_id . '&action=spam' ), 'spam-feedback_' . $post_id ); 
  357. echo '">Spam</a></span>'; 
  358. echo ' | '; 
  359.  
  360. echo '<span class="delete" id="feedback-trash-' . $post_id; 
  361. echo '">'; 
  362. echo '<a class="submitdelete" title="' . esc_attr__( 'Trash', 'jetpack' ); 
  363. echo '" href="' . get_delete_post_link( $post_id ); 
  364. echo '">' . __( 'Trash', 'jetpack' ) . '</a></span>'; 
  365.  
  366. ?> 
  367.  
  368. <script> 
  369. jQuery(document).ready( function($) { 
  370. $('#feedback-spam-<?php echo $post_id; ?>').click( function(e) { 
  371. e.preventDefault(); 
  372. $.post( ajaxurl, { 
  373. action: 'grunion_ajax_spam',  
  374. post_id: '<?php echo $post_id; ?>',  
  375. make_it: 'spam',  
  376. sub_menu: jQuery('.subsubsub .current').attr('href'),  
  377. _ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>' 
  378. },  
  379. function( r ) { 
  380. $('#post-<?php echo $post_id; ?>') 
  381. .css( {backgroundColor:'#FF7979'} ) 
  382. .fadeOut(350, function() { 
  383. $(this).remove(); 
  384. $('.subsubsub').html(r); 
  385. }); 
  386. }); 
  387. }); 
  388.  
  389. $('#feedback-trash-<?php echo $post_id; ?>').click(function(e) { 
  390. e.preventDefault(); 
  391. $.post(ajaxurl, { 
  392. action: 'grunion_ajax_spam',  
  393. post_id: '<?php echo $post_id; ?>',  
  394. make_it: 'trash',  
  395. sub_menu: jQuery('.subsubsub .current').attr('href'),  
  396. _ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>' 
  397. },  
  398. function(r) { 
  399. $('#post-<?php echo $post_id; ?>') 
  400. .css({backgroundColor: '#FF7979'}) 
  401. .fadeOut(350, function() { 
  402. $(this).remove(); 
  403. $('.subsubsub').html(r); 
  404. }); 
  405. ); 
  406. }); 
  407. }); 
  408. </script> 
  409.  
  410. <?php 
  411. } elseif ( $post->post_status == 'spam' ) { 
  412. echo '<span class="unspam unapprove" id="feedback-ham-' . $post_id; 
  413. echo '"><a title="'; 
  414. echo __( 'Mark this message as NOT spam', 'jetpack' ); 
  415. echo '" href="">Not Spam</a></span>'; 
  416. echo ' | '; 
  417.  
  418. echo "<span class='delete' id='feedback-trash-" . $post_id; 
  419. echo "'> <a class='submitdelete' title='"; 
  420. echo esc_attr( __( 'Delete this item permanently', 'jetpack' ) ); 
  421. echo "' href='" . get_delete_post_link( $post->ID, '', true ); 
  422. echo "'>" . __( 'Delete Permanently', 'jetpack' ) . "</a></span>"; 
  423. ?> 
  424.  
  425. <script> 
  426. jQuery(document).ready( function($) { 
  427. $('#feedback-ham-<?php echo $post_id; ?>').click( function(e) { 
  428. e.preventDefault(); 
  429. $.post( ajaxurl, { 
  430. action: 'grunion_ajax_spam',  
  431. post_id: '<?php echo $post_id; ?>',  
  432. make_it: 'ham',  
  433. sub_menu: jQuery('.subsubsub .current').attr('href'),  
  434. _ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>' 
  435. },  
  436. function( r ) { 
  437. $('#post-<?php echo $post_id; ?>') 
  438. .css( {backgroundColor:'#59C859'} ) 
  439. .fadeOut(350, function() { 
  440. $(this).remove(); 
  441. $('.subsubsub').html(r); 
  442. }); 
  443. }); 
  444. }); 
  445. }); 
  446. </script> 
  447.  
  448. <?php 
  449. break; 
  450.  
  451. case 'feedback_date': 
  452.  
  453. $date_time_format = _x( '%1$s \a\t %2$s', '{$date_format} \a\t {$time_format}', 'jetpack' ); 
  454. $date_time_format = sprintf( $date_time_format, get_option( 'date_format' ), get_option( 'time_format' ) ); 
  455. $time = date_i18n( $date_time_format, get_the_time( 'U' ) ); 
  456.  
  457. echo $time; 
  458. break; 
  459.  
  460. function grunion_esc_attr( $attr ) { 
  461. $out = esc_attr( $attr ); 
  462. // we also have to entity-encode square brackets so they don't interfere with the shortcode parser 
  463. // FIXME: do this better - just stripping out square brackets for now since they mysteriously keep reappearing 
  464. $out = str_replace( '[', '', $out ); 
  465. $out = str_replace( ']', '', $out ); 
  466. return $out; 
  467.  
  468. function grunion_sort_objects( $a, $b ) { 
  469. if ( isset($a['order']) && isset($b['order']) ) 
  470. return $a['order'] - $b['order']; 
  471. return 0; 
  472.  
  473. // take an array of field types from the form builder, and construct a shortcode form 
  474. // returns both the shortcode form, and HTML markup representing a preview of the form 
  475. function grunion_ajax_shortcode() { 
  476. check_ajax_referer( 'grunion_shortcode' ); 
  477.  
  478. $attributes = array(); 
  479.  
  480. foreach ( array( 'subject', 'to' ) as $attribute ) { 
  481. if ( isset( $_POST[$attribute] ) && strlen( $_POST[$attribute] ) ) { 
  482. $attributes[$attribute] = stripslashes( $_POST[$attribute] ); 
  483.  
  484. if ( is_array( $_POST['fields'] ) ) { 
  485. $fields = stripslashes_deep( $_POST['fields'] ); 
  486. usort( $fields, 'grunion_sort_objects' ); 
  487.  
  488. $field_shortcodes = array(); 
  489.  
  490. foreach ( $fields as $field ) { 
  491. $field_attributes = array(); 
  492.  
  493. if ( isset( $field['required'] ) && 'true' === $field['required'] ) { 
  494. $field_attributes['required'] = 'true'; 
  495.  
  496. foreach ( array( 'options', 'label', 'type' ) as $attribute ) { 
  497. if ( isset( $field[$attribute] ) ) { 
  498. $field_attributes[$attribute] = $field[$attribute]; 
  499.  
  500. $field_shortcodes[] = new Grunion_Contact_Form_Field( $field_attributes ); 
  501.  
  502. $grunion = new Grunion_Contact_Form( $attributes, $field_shortcodes ); 
  503.  
  504. die( "\n$grunion\n" ); 
  505.  
  506. // takes a post_id, extracts the contact-form shortcode from that post (if there is one), parses it,  
  507. // and constructs a json object representing its contents and attributes 
  508. function grunion_ajax_shortcode_to_json() { 
  509. global $post, $grunion_form; 
  510.  
  511. check_ajax_referer( 'grunion_shortcode_to_json' ); 
  512.  
  513. if ( !isset( $_POST['content'] ) || !is_numeric( $_POST['post_id'] ) ) { 
  514. die( '-1' ); 
  515.  
  516. $content = stripslashes( $_POST['content'] ); 
  517.  
  518. // doesn't look like a post with a [contact-form] already. 
  519. if ( false === has_shortcode( $content, 'contact-form' ) ) { 
  520. die( '' ); 
  521.  
  522. $post = get_post( $_POST['post_id'] ); 
  523.  
  524. do_shortcode( $content ); 
  525.  
  526. $grunion = Grunion_Contact_Form::$last; 
  527.  
  528. $out = array( 
  529. 'to' => '',  
  530. 'subject' => '',  
  531. 'fields' => array(),  
  532. ); 
  533.  
  534. foreach ( $grunion->fields as $field ) { 
  535. $out['fields'][$field->get_attribute( 'id' )] = $field->attributes; 
  536.  
  537. $to = $grunion->get_attribute( 'to' ); 
  538. $subject = $grunion->get_attribute( 'subject' ); 
  539. foreach ( array( 'to', 'subject' ) as $attribute ) { 
  540. $value = $grunion->get_attribute( $attribute ); 
  541. if ( isset( $grunion->defaults[$attribute] ) && $value == $grunion->defaults[$attribute] ) { 
  542. $value = ''; 
  543. $out[$attribute] = $value; 
  544.  
  545. die( json_encode( $out ) ); 
  546.  
  547.  
  548. add_action( 'wp_ajax_grunion_shortcode', 'grunion_ajax_shortcode' ); 
  549. add_action( 'wp_ajax_grunion_shortcode_to_json', 'grunion_ajax_shortcode_to_json' ); 
  550.  
  551.  
  552. // process row-action spam/not spam clicks 
  553. add_action( 'wp_ajax_grunion_ajax_spam', 'grunion_ajax_spam' ); 
  554. function grunion_ajax_spam() { 
  555. global $wpdb; 
  556.  
  557. if ( empty( $_POST['make_it'] ) ) { 
  558. return; 
  559.  
  560. $post_id = (int) $_POST['post_id']; 
  561. check_ajax_referer( 'grunion-post-status-' . $post_id ); 
  562. if ( ! current_user_can( "edit_page", $post_id ) ) { 
  563. wp_die( __( 'You are not allowed to manage this item.', 'jetpack' ) ); 
  564.  
  565. require_once dirname( __FILE__ ) . '/grunion-contact-form.php'; 
  566.  
  567. $current_menu = ''; 
  568. if ( isset( $_POST['sub_menu'] ) && preg_match( '|post_type=feedback|', $_POST['sub_menu'] ) ) { 
  569. if ( preg_match( '|post_status=spam|', $_POST['sub_menu'] ) ) { 
  570. $current_menu = 'spam'; 
  571. elseif ( preg_match( '|post_status=trash|', $_POST['sub_menu'] ) ) { 
  572. $current_menu = 'trash'; 
  573. else { 
  574. $current_menu = 'messages'; 
  575.  
  576.  
  577. $post = get_post( $post_id ); 
  578. $post_type_object = get_post_type_object( $post->post_type ); 
  579. $akismet_values = get_post_meta( $post_id, '_feedback_akismet_values', TRUE ); 
  580. if ( $_POST['make_it'] == 'spam' ) { 
  581. $post->post_status = 'spam'; 
  582. $status = wp_insert_post( $post ); 
  583. wp_transition_post_status( 'spam', 'publish', $post ); 
  584.  
  585. /** This action is already documented in modules/contact-form/admin.php */ 
  586. do_action( 'contact_form_akismet', 'spam', $akismet_values ); 
  587. } elseif ( $_POST['make_it'] == 'ham' ) { 
  588. $post->post_status = 'publish'; 
  589. $status = wp_insert_post( $post ); 
  590. wp_transition_post_status( 'publish', 'spam', $post ); 
  591.  
  592. /** This action is already documented in modules/contact-form/admin.php */ 
  593. do_action( 'contact_form_akismet', 'ham', $akismet_values ); 
  594.  
  595. $comment_author_email = $reply_to_addr = $message = $to = $headers = false; 
  596. $blog_url = parse_url( site_url() ); 
  597.  
  598. // resend the original email 
  599. $email = get_post_meta( $post_id, '_feedback_email', TRUE ); 
  600. $content_fields = Grunion_Contact_Form_Plugin::parse_fields_from_content( $post_id ); 
  601.  
  602. if ( ! empty( $email ) && !empty( $content_fields ) ) { 
  603. if ( isset( $content_fields['_feedback_author_email'] ) ) { 
  604. $comment_author_email = $content_fields['_feedback_author_email']; 
  605.  
  606. if ( isset( $email['to'] ) ) { 
  607. $to = $email['to']; 
  608.  
  609. if ( isset( $email['message'] ) ) { 
  610. $message = $email['message']; 
  611.  
  612. if ( isset( $email['headers'] ) ) { 
  613. $headers = $email['headers']; 
  614. else { 
  615. $headers = 'From: "' . $content_fields['_feedback_author'] .'" <wordpress@' . $blog_url['host'] . ">\r\n"; 
  616.  
  617. if ( ! empty( $comment_author_email ) ) { 
  618. $reply_to_addr = $comment_author_email; 
  619. elseif ( is_array( $to ) ) { 
  620. $reply_to_addr = $to[0]; 
  621.  
  622. if ( $reply_to_addr ) { 
  623. $headers .= 'Reply-To: "' . $content_fields['_feedback_author'] .'" <' . $reply_to_addr . ">\r\n"; 
  624.  
  625. $headers .= "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\""; 
  626.  
  627. /** 
  628. * Filters the subject of the email sent after a contact form submission. 
  629. * 
  630. * @module contact-form 
  631. * 
  632. * @since 3.0.0 
  633. * 
  634. * @param string $content_fields['_feedback_subject'] Feedback's subject line. 
  635. * @param array $content_fields['_feedback_all_fields'] Feedback's data from old fields. 
  636. */ 
  637. $subject = apply_filters( 'contact_form_subject', $content_fields['_feedback_subject'], $content_fields['_feedback_all_fields'] ); 
  638.  
  639. wp_mail( $to, $subject, $message, $headers ); 
  640. } elseif( $_POST['make_it'] == 'publish' ) { 
  641. if ( ! current_user_can($post_type_object->cap->delete_post, $post_id) ) { 
  642. wp_die( __( 'You are not allowed to move this item out of the Trash.', 'jetpack' ) ); 
  643.  
  644. if ( ! wp_untrash_post($post_id) ) { 
  645. wp_die( __( 'Error in restoring from Trash.', 'jetpack' ) ); 
  646.  
  647. } elseif( $_POST['make_it'] == 'trash' ) { 
  648. if ( ! current_user_can($post_type_object->cap->delete_post, $post_id) ) { 
  649. wp_die( __( 'You are not allowed to move this item to the Trash.', 'jetpack' ) ); 
  650.  
  651. if ( ! wp_trash_post($post_id) ) { 
  652. wp_die( __( 'Error in moving to Trash.', 'jetpack' ) ); 
  653.  
  654.  
  655. $sql = " 
  656. SELECT post_status,  
  657. COUNT( * ) AS post_count 
  658. FROM `{$wpdb->posts}` 
  659. WHERE post_type = 'feedback' 
  660. GROUP BY post_status 
  661. "; 
  662. $status_count = (array) $wpdb->get_results( $sql, ARRAY_A ); 
  663.  
  664. $status = array(); 
  665. $status_html = ''; 
  666. foreach ( $status_count as $i => $row ) { 
  667. $status[$row['post_status']] = $row['post_count']; 
  668.  
  669. if ( isset( $status['publish'] ) ) { 
  670. $status_html .= '<li><a href="edit.php?post_type=feedback"'; 
  671. if ( $current_menu == 'messages' ) { 
  672. $status_html .= ' class="current"'; 
  673.  
  674. $status_html .= '>' . __( 'Messages', 'jetpack' ) . ' <span class="count">'; 
  675. $status_html .= '(' . number_format( $status['publish'] ) . ')'; 
  676. $status_html .= '</span></a> |</li>'; 
  677.  
  678. if ( isset( $status['trash'] ) ) { 
  679. $status_html .= '<li><a href="edit.php?post_status=trash&post_type=feedback"'; 
  680. if ( $current_menu == 'trash' ) 
  681. $status_html .= ' class="current"'; 
  682.  
  683. $status_html .= '>' . __( 'Trash', 'jetpack' ) . ' <span class="count">'; 
  684. $status_html .= '(' . number_format( $status['trash'] ) . ')'; 
  685. $status_html .= '</span></a>'; 
  686. if ( isset( $status['spam'] ) ) 
  687. $status_html .= ' |'; 
  688. $status_html .= '</li>'; 
  689.  
  690. if ( isset( $status['spam'] ) ) { 
  691. $status_html .= '<li><a href="edit.php?post_status=spam&post_type=feedback"'; 
  692. if ( $current_menu == 'spam' ) 
  693. $status_html .= ' class="current"'; 
  694.  
  695. $status_html .= '>' . __( 'Spam', 'jetpack' ) . ' <span class="count">'; 
  696. $status_html .= '(' . number_format( $status['spam'] ) . ')'; 
  697. $status_html .= '</span></a></li>'; 
  698.  
  699. echo $status_html; 
  700. exit; 
  701.  
  702. add_action( 'omnisearch_add_providers', 'grunion_omnisearch_add_providers' ); 
  703. function grunion_omnisearch_add_providers() { 
  704. // Feedback uses capability_type 'page' 
  705. if ( current_user_can( 'edit_pages' ) ) { 
  706. require_once( GRUNION_PLUGIN_DIR . '/grunion-omnisearch.php' ); 
  707. new Jetpack_Omnisearch_Grunion; 
  708.  
  709. /** 
  710. * Add the scripts that will add the "Check for Spam" button to the Feedbacks dashboard page. 
  711. */ 
  712. function grunion_enable_spam_recheck() { 
  713. if ( ! defined( 'AKISMET_VERSION' ) ) { 
  714. return; 
  715.  
  716. $screen = get_current_screen(); 
  717.  
  718. // Only add to feedback, only to non-spam view 
  719. if ( 'edit-feedback' != $screen->id || ( ! empty( $_GET['post_status'] ) && 'spam' == $_GET['post_status'] ) ) { 
  720. return; 
  721.  
  722. // Add the scripts that handle the spam check event. 
  723. wp_register_script( 'grunion-admin', plugin_dir_url( __FILE__ ) . 'js/grunion-admin.js', array( 'jquery' ) ); 
  724. wp_enqueue_script( 'grunion-admin' ); 
  725.  
  726. wp_enqueue_style( 'grunion.css' ); 
  727.  
  728. // Add the actual "Check for Spam" button. 
  729. add_action( 'admin_head', 'grunion_check_for_spam_button' ); 
  730.  
  731. add_action( 'admin_enqueue_scripts', 'grunion_enable_spam_recheck' ); 
  732.  
  733. /** 
  734. * Add the "Check for Spam" button to the Feedbacks dashboard page. 
  735. */ 
  736. function grunion_check_for_spam_button() { 
  737. // Get HTML for the button 
  738. $button_html = get_submit_button( 
  739. __( 'Check for Spam', 'jetpack' ),  
  740. 'secondary',  
  741. 'jetpack-check-feedback-spam',  
  742. false,  
  743. array( 'class' => 'jetpack-check-feedback-spam' ) 
  744. ); 
  745. $button_html .= '<span class="jetpack-check-feedback-spam-spinner"></span>'; 
  746.  
  747. // Add the button next to the filter button via js 
  748. ?> 
  749. <script type="text/javascript"> 
  750. jQuery( function( $ ) { 
  751. $( '#posts-filter #post-query-submit' ).after( '<?php echo $button_html; ?>' ); 
  752. } ); 
  753. </script> 
  754. <?php 
  755.  
  756. /** 
  757. * Recheck all approved feedbacks for spam. 
  758. */ 
  759. function grunion_recheck_queue() { 
  760. global $wpdb; 
  761.  
  762. $query = 'post_type=feedback&post_status=publish'; 
  763.  
  764. if ( isset( $_POST['limit'], $_POST['offset'] ) ) { 
  765. $query .= '&posts_per_page=' . intval( $_POST['limit'] ) . '&offset=' . intval( $_POST['offset'] ); 
  766.  
  767. $approved_feedbacks = get_posts( $query ); 
  768.  
  769. foreach ( $approved_feedbacks as $feedback ) { 
  770. $meta = get_post_meta( $feedback->ID, '_feedback_akismet_values', true ); 
  771.  
  772. /** 
  773. * Filter whether the submitted feedback is considered as spam. 
  774. * 
  775. * @module contact-form 
  776. * 
  777. * @since 3.4.0 
  778. * 
  779. * @param bool false Is the submitted feedback spam? Default to false. 
  780. * @param array $meta Feedack values returned by the Akismet plugin. 
  781. */ 
  782. $is_spam = apply_filters( 'jetpack_contact_form_is_spam', false, $meta ); 
  783.  
  784. if ( $is_spam ) { 
  785. wp_update_post( array( 'ID' => $feedback->ID, 'post_status' => 'spam' ) ); 
  786. /** This action is already documented in modules/contact-form/admin.php */ 
  787. do_action( 'contact_form_akismet', 'spam', $akismet_values ); 
  788.  
  789. wp_send_json( array( 
  790. 'processed' => count( $approved_feedbacks ),  
  791. ) ); 
  792.  
  793. add_action( 'wp_ajax_grunion_recheck_queue', 'grunion_recheck_queue' ); 
.