Bez popisu

admin.php 30KB

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