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