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