admin.php 26 KB

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