PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/contact-form/admin.php

https://github.com/sharpmachine/wakeupmedia.com
PHP | 560 lines | 444 code | 99 blank | 17 comment | 57 complexity | 389f44c8aaf23109f50623062192d702 MD5 | raw file
  1. <?php
  2. function menu_alter() {
  3. echo '
  4. <style>
  5. #menu-posts-feedback .wp-menu-image img { display: none; }
  6. #adminmenu .menu-icon-feedback:hover div.wp-menu-image, #adminmenu .menu-icon-feedback.wp-has-current-submenu div.wp-menu-image, #adminmenu .menu-icon-feedback.current div.wp-menu-image { background: url("' .GRUNION_PLUGIN_URL . 'images/grunion-menu-hover.png") no-repeat 7px 7px !important; }
  7. #adminmenu .menu-icon-feedback div.wp-menu-image, #adminmenu .menu-icon-feedback div.wp-menu-image, #adminmenu .menu-icon-feedback div.wp-menu-image { background: url("' . GRUNION_PLUGIN_URL . 'images/grunion-menu.png") no-repeat 7px 7px !important; }
  8. .grunion-menu-button { background: url("' . GRUNION_PLUGIN_URL . 'images/grunion-form.png") no-repeat; width: 13px; height: 12px; display: inline-block; ) }
  9. @media only screen and (-moz-min-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) {
  10. #adminmenu .menu-icon-feedback:hover div.wp-menu-image, #adminmenu .menu-icon-feedback.wp-has-current-submenu div.wp-menu-image, #adminmenu .menu-icon-feedback.current div.wp-menu-image { background: url("' .GRUNION_PLUGIN_URL . 'images/grunion-menu-hover-2x.png") no-repeat 7px 7px !important; background-size: 15px 16px !important; }
  11. #adminmenu .menu-icon-feedback div.wp-menu-image, #adminmenu .menu-icon-feedback div.wp-menu-image, #adminmenu .menu-icon-feedback div.wp-menu-image { background: url("' . GRUNION_PLUGIN_URL . 'images/grunion-menu-2x.png") no-repeat 7px 7px !important; background-size: 15px 16px !important; }
  12. .grunion-menu-button { background-image: url("' . GRUNION_PLUGIN_URL . 'images/grunion-form-2x.png"); background-size: 13px 12px !important; vertical-align: bottom; }
  13. }
  14. </style>';
  15. }
  16. add_action('admin_head', 'menu_alter');
  17. /**
  18. * Add a contact form button to the post composition screen
  19. */
  20. add_action( 'media_buttons', 'grunion_media_button', 999 );
  21. function grunion_media_button( ) {
  22. global $post_ID, $temp_ID;
  23. $iframe_post_id = (int) (0 == $post_ID ? $temp_ID : $post_ID);
  24. $title = esc_attr( __( 'Add a custom form', 'jetpack' ) );
  25. $plugin_url = esc_url( GRUNION_PLUGIN_URL );
  26. $site_url = esc_url( admin_url( "/admin-ajax.php?post_id={$iframe_post_id}&action=grunion_form_builder&TB_iframe=true&width=768" ) );
  27. echo '<a href="' . $site_url . '&id=add_form" class="thickbox" title="' . $title . '"><div class="grunion-menu-button" alt="' . $title . '"></div></a>';
  28. }
  29. add_action( 'wp_ajax_grunion_form_builder', 'display_form_view' );
  30. function display_form_view() {
  31. require_once GRUNION_PLUGIN_DIR . 'grunion-form-view.php';
  32. exit;
  33. }
  34. // feedback specific css items
  35. add_action( 'admin_print_styles', 'grunion_admin_css' );
  36. function grunion_admin_css() {
  37. global $current_screen;
  38. if ( 'edit-feedback' != $current_screen->id )
  39. return;
  40. wp_enqueue_script( 'wp-lists' );
  41. ?>
  42. <style type='text/css'>
  43. .add-new-h2, .view-switch, body.no-js .tablenav select[name^=action], body.no-js #doaction, body.no-js #doaction2 {
  44. display: none
  45. }
  46. .column-feedback_from img {
  47. float:left;
  48. margin-right:10px;
  49. margin-top:3px;
  50. }
  51. .widefat .column-feedback_from {
  52. width: 17%;
  53. }
  54. .widefat .column-feedback_date {
  55. width: 17%;
  56. }
  57. .spam a {
  58. color: #BC0B0B;
  59. }
  60. .untrash a {
  61. color: #D98500;
  62. }
  63. .unspam a {
  64. color: #D98500;
  65. }
  66. #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; }
  67. @media only screen and (-moz-min-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) {
  68. #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; }
  69. }
  70. #icon-edit.icon32-posts-feedback { background-position: 2px 2px !important; }
  71. </style>
  72. <?php
  73. }
  74. // remove admin UI parts that we don't support in feedback management
  75. add_action( 'admin_menu', 'grunion_admin_menu' );
  76. function grunion_admin_menu() {
  77. global $menu, $submenu;
  78. unset( $submenu['edit.php?post_type=feedback'] );
  79. }
  80. add_filter( 'bulk_actions-edit-feedback', 'grunion_admin_bulk_actions' );
  81. function grunion_admin_bulk_actions( $actions ) {
  82. global $current_screen;
  83. if ( 'edit-feedback' != $current_screen->id )
  84. return $actions;
  85. unset( $actions['edit'] );
  86. return $actions;
  87. }
  88. add_filter( 'views_edit-feedback', 'grunion_admin_view_tabs' );
  89. function grunion_admin_view_tabs( $views ) {
  90. global $current_screen;
  91. if ( 'edit-feedback' != $current_screen->id )
  92. return $actions;
  93. unset( $views['publish'] );
  94. preg_match( '|post_type=feedback\'( class="current")?\>(.*)\<span class=|', $views['all'], $match );
  95. if ( !empty( $match[2] ) )
  96. $views['all'] = str_replace( $match[2], 'Messages ', $views['all'] );
  97. return $views;
  98. }
  99. add_filter( 'manage_feedback_posts_columns', 'grunion_post_type_columns_filter' );
  100. function grunion_post_type_columns_filter( $cols ) {
  101. $cols = array(
  102. 'cb' => '<input type="checkbox" />',
  103. 'feedback_from' => __( 'From', 'jetpack' ),
  104. 'feedback_message' => __( 'Message', 'jetpack' ),
  105. 'feedback_date' => __( 'Date', 'jetpack' )
  106. );
  107. return $cols;
  108. }
  109. add_action( 'manage_posts_custom_column', 'grunion_manage_post_columns', 10, 2 );
  110. function grunion_manage_post_columns( $col, $post_id ) {
  111. global $post;
  112. switch ( $col ) {
  113. case 'feedback_from':
  114. $author_name = get_post_meta( $post_id, '_feedback_author', TRUE );
  115. $author_email = get_post_meta( $post_id, '_feedback_author_email', TRUE );
  116. $author_url = get_post_meta( $post_id, '_feedback_author_url', TRUE );
  117. $author_ip = get_post_meta( $post_id, '_feedback_ip', TRUE );
  118. $form_url = get_post_meta( $post_id, '_feedback_contact_form_url', TRUE );
  119. $author_name_line = '';
  120. if ( !empty( $author_name ) ) {
  121. if ( !empty( $author_email ) )
  122. $author_name_line = get_avatar( $author_email, 32 );
  123. $author_name_line .= "<strong>{$author_name}</strong><br />";
  124. }
  125. $author_email_line = '';
  126. if ( !empty( $author_email ) ) {
  127. $author_email_line = "<a href='mailto:{$author_email}'>";
  128. $author_email_line .= "{$author_email}</a><br />";
  129. }
  130. $author_url_line = '';
  131. if ( !empty( $author_url ) ) {
  132. $author_url_line = "<a href='{$author_url}'>";
  133. $author_url_line .= "{$author_url}</a><br />";
  134. }
  135. echo $author_name_line;
  136. echo $author_email_line;
  137. echo $author_url_line;
  138. echo "<a href='edit.php?post_type=feedback&s={$author_ip}";
  139. echo "&mode=detail'>{$author_ip}</a><br />";
  140. echo "<a href='{$form_url}'>{$form_url}</a>";
  141. break;
  142. case 'feedback_message':
  143. $post = get_post( $post_id );
  144. $post_type_object = get_post_type_object( $post->post_type );
  145. echo '<strong>';
  146. echo esc_html( get_post_meta( $post_id, '_feedback_subject', TRUE ) );
  147. echo '</strong><br />';
  148. echo sanitize_text_field( get_the_content( '' ) );
  149. echo '<br />';
  150. $extra_fields = get_post_meta( $post_id, '_feedback_extra_fields', TRUE );
  151. if ( !empty( $extra_fields ) ) {
  152. echo '<br /><hr />';
  153. echo '<table cellspacing="0" cellpadding="0" style="">' . "\n";
  154. foreach ( (array) $extra_fields as $k => $v ) {
  155. echo "<tr><td align='right'><b>". esc_html( $k ) ."</b></td><td>". sanitize_text_field( $v ) ."</td></tr>\n";
  156. }
  157. echo '</table>';
  158. }
  159. echo '<div class="row-actions">';
  160. if ( $post->post_status == 'trash' ) {
  161. echo '<span class="untrash" id="feedback-restore-' . $post_id;
  162. echo '"><a title="';
  163. echo esc_attr__( 'Restore this item from the Trash', 'jetpack' );
  164. echo '" href="' . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&amp;action=untrash', $post->ID ) ), 'untrash-' . $post->post_type . '_' . $post->ID );
  165. echo '">' . __( 'Restore', 'jetpack' ) . '</a></span> | ';
  166. echo "<span class='delete'> <a class='submitdelete' title='";
  167. echo esc_attr( __( 'Delete this item permanently', 'jetpack' ) );
  168. echo "' href='" . get_delete_post_link( $post->ID, '', true );
  169. echo "'>" . __( 'Delete Permanently', 'jetpack' ) . "</a></span>";
  170. ?>
  171. <script>
  172. jQuery(document).ready(function($) {
  173. $('#feedback-restore-<?php echo $post_id; ?>').click(function(e) {
  174. e.preventDefault();
  175. $.post(ajaxurl, {
  176. action: 'grunion_ajax_spam',
  177. post_id: '<?php echo $post_id; ?>',
  178. make_it: 'publish',
  179. sub_menu: jQuery('.subsubsub .current').attr('href'),
  180. _ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
  181. },
  182. function(r) {
  183. $('#post-<?php echo $post_id; ?>')
  184. .css({backgroundColor: '#59C859'})
  185. .fadeOut(350, function() {
  186. $(this).remove();
  187. $('.subsubsub').html(r);
  188. });
  189. }
  190. );
  191. });
  192. });
  193. </script>
  194. <?php
  195. } elseif ( $post->post_status == 'publish' ) {
  196. echo '<span class="spam" id="feedback-spam-' . $post_id;
  197. echo '"><a title="';
  198. echo __( 'Mark this message as spam', 'jetpack' );
  199. echo '" href="' . wp_nonce_url( admin_url( 'admin-ajax.php?post_id=' . $post_id . '&amp;action=spam' ), 'spam-feedback_' . $post_id );
  200. echo '">Spam</a></span>';
  201. echo ' | ';
  202. echo '<span class="delete" id="feedback-trash-' . $post_id;
  203. echo '">';
  204. echo '<a class="submitdelete" title="' . esc_attr__( 'Trash', 'jetpack' );
  205. echo '" href="' . get_delete_post_link( $post_id );
  206. echo '">' . __( 'Trash', 'jetpack' ) . '</a></span>';
  207. ?>
  208. <script>
  209. jQuery(document).ready( function($) {
  210. $('#feedback-spam-<?php echo $post_id; ?>').click( function(e) {
  211. e.preventDefault();
  212. $.post( ajaxurl, {
  213. action: 'grunion_ajax_spam',
  214. post_id: '<?php echo $post_id; ?>',
  215. make_it: 'spam',
  216. sub_menu: jQuery('.subsubsub .current').attr('href'),
  217. _ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
  218. },
  219. function( r ) {
  220. $('#post-<?php echo $post_id; ?>')
  221. .css( {backgroundColor:'#FF7979'} )
  222. .fadeOut(350, function() {
  223. $(this).remove();
  224. $('.subsubsub').html(r);
  225. });
  226. });
  227. });
  228. $('#feedback-trash-<?php echo $post_id; ?>').click(function(e) {
  229. e.preventDefault();
  230. $.post(ajaxurl, {
  231. action: 'grunion_ajax_spam',
  232. post_id: '<?php echo $post_id; ?>',
  233. make_it: 'trash',
  234. sub_menu: jQuery('.subsubsub .current').attr('href'),
  235. _ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
  236. },
  237. function(r) {
  238. $('#post-<?php echo $post_id; ?>')
  239. .css({backgroundColor: '#FF7979'})
  240. .fadeOut(350, function() {
  241. $(this).remove();
  242. $('.subsubsub').html(r);
  243. });
  244. }
  245. );
  246. });
  247. });
  248. </script>
  249. <?php
  250. } elseif ( $post->post_status == 'spam' ) {
  251. echo '<span class="unspam unapprove" id="feedback-ham-' . $post_id;
  252. echo '"><a title="';
  253. echo __( 'Mark this message as NOT spam', 'jetpack' );
  254. echo '" href="">Not Spam</a></span>';
  255. echo ' | ';
  256. echo "<span class='delete' id='feedback-trash-" . $post_id;
  257. echo "'> <a class='submitdelete' title='";
  258. echo esc_attr( __( 'Delete this item permanently', 'jetpack' ) );
  259. echo "' href='" . get_delete_post_link( $post->ID, '', true );
  260. echo "'>" . __( 'Delete Permanently', 'jetpack' ) . "</a></span>";
  261. ?>
  262. <script>
  263. jQuery(document).ready( function($) {
  264. $('#feedback-ham-<?php echo $post_id; ?>').click( function(e) {
  265. e.preventDefault();
  266. $.post( ajaxurl, {
  267. action: 'grunion_ajax_spam',
  268. post_id: '<?php echo $post_id; ?>',
  269. make_it: 'ham',
  270. sub_menu: jQuery('.subsubsub .current').attr('href'),
  271. _ajax_nonce: '<?php echo wp_create_nonce( 'grunion-post-status-' . $post_id ); ?>'
  272. },
  273. function( r ) {
  274. $('#post-<?php echo $post_id; ?>')
  275. .css( {backgroundColor:'#59C859'} )
  276. .fadeOut(350, function() {
  277. $(this).remove();
  278. $('.subsubsub').html(r);
  279. });
  280. });
  281. });
  282. });
  283. </script>
  284. <?php
  285. }
  286. break;
  287. case 'feedback_date':
  288. echo get_the_date( __( 'Y-m-d @ g:i:s A', 'jetpack' ) );
  289. break;
  290. }
  291. }
  292. function grunion_esc_attr( $attr ) {
  293. $out = esc_attr( $attr );
  294. // we also have to entity-encode square brackets so they don't interfere with the shortcode parser
  295. // FIXME: do this better - just stripping out square brackets for now since they mysteriously keep reappearing
  296. $out = str_replace( '[', '', $out );
  297. $out = str_replace( ']', '', $out );
  298. return $out;
  299. }
  300. function grunion_sort_objects( $a, $b ) {
  301. if ( isset($a['order']) && isset($b['order']) )
  302. return $a['order'] - $b['order'];
  303. return 0;
  304. }
  305. // take an array of field types from the form builder, and construct a shortcode form
  306. // returns both the shortcode form, and HTML markup representing a preview of the form
  307. function grunion_ajax_shortcode() {
  308. check_ajax_referer( 'grunion_shortcode' );
  309. $attributes = array();
  310. foreach ( array( 'subject', 'to' ) as $attribute ) {
  311. if ( isset( $_POST[$attribute] ) && strlen( $_POST[$attribute] ) ) {
  312. $attributes[$attribute] = stripslashes( $_POST[$attribute] );
  313. }
  314. }
  315. if ( is_array( $_POST['fields'] ) ) {
  316. $fields = stripslashes_deep( $_POST['fields'] );
  317. usort( $fields, 'grunion_sort_objects' );
  318. $field_shortcodes = array();
  319. foreach ( $fields as $field ) {
  320. $field_attributes = array();
  321. if ( isset( $field['required'] ) && 'true' === $field['required'] ) {
  322. $field_attributes['required'] = 'true';
  323. }
  324. foreach ( array( 'options', 'label', 'type' ) as $attribute ) {
  325. if ( isset( $field[$attribute] ) ) {
  326. $field_attributes[$attribute] = $field[$attribute];
  327. }
  328. }
  329. $field_shortcodes[] = new Grunion_Contact_Form_Field( $field_attributes );
  330. }
  331. }
  332. $grunion = new Grunion_Contact_Form( $attributes, $field_shortcodes );
  333. die( "\n$grunion\n" );
  334. }
  335. // takes a post_id, extracts the contact-form shortcode from that post (if there is one), parses it,
  336. // and constructs a json object representing its contents and attributes
  337. function grunion_ajax_shortcode_to_json() {
  338. global $post, $grunion_form;
  339. check_ajax_referer( 'grunion_shortcode_to_json' );
  340. if ( !isset( $_POST['content'] ) || !is_numeric( $_POST['post_id'] ) ) {
  341. die( '-1' );
  342. }
  343. $content = stripslashes( $_POST['content'] );
  344. // doesn't look like a post with a [contact-form] already.
  345. if ( false === strpos( $content, '[contact-form' ) ) {
  346. die( '' );
  347. }
  348. $post = get_post( $_POST['post_id'] );
  349. do_shortcode( $content );
  350. $grunion = Grunion_Contact_Form::$last;
  351. $out = array(
  352. 'to' => '',
  353. 'subject' => '',
  354. 'fields' => array(),
  355. );
  356. foreach ( $grunion->fields as $field ) {
  357. $out['fields'][$field->get_attribute( 'id' )] = $field->attributes;
  358. }
  359. $to = $grunion->get_attribute( 'to' );
  360. $subject = $grunion->get_attribute( 'subject' );
  361. foreach ( array( 'to', 'subject' ) as $attribute ) {
  362. $value = $grunion->get_attribute( $attribute );
  363. if ( isset( $grunion->defaults[$attribute] ) && $value == $grunion->defaults[$attribute] ) {
  364. $value = '';
  365. }
  366. $out[$attribute] = $value;
  367. }
  368. die( json_encode( $out ) );
  369. }
  370. add_action( 'wp_ajax_grunion_shortcode', 'grunion_ajax_shortcode' );
  371. add_action( 'wp_ajax_grunion_shortcode_to_json', 'grunion_ajax_shortcode_to_json' );
  372. // process row-action spam/not spam clicks
  373. add_action( 'wp_ajax_grunion_ajax_spam', 'grunion_ajax_spam' );
  374. function grunion_ajax_spam() {
  375. global $wpdb;
  376. if ( empty( $_POST['make_it'] ) )
  377. return;
  378. $post_id = (int) $_POST['post_id'];
  379. check_ajax_referer( 'grunion-post-status-' . $post_id );
  380. if ( !current_user_can("edit_page", $post_id) )
  381. wp_die( __( 'You are not allowed to manage this item.', 'jetpack' ) );
  382. require_once dirname( __FILE__ ) . '/grunion-contact-form.php';
  383. $current_menu = '';
  384. if ( preg_match( '|post_type=feedback|', $_POST['sub_menu'] ) ) {
  385. if ( preg_match( '|post_status=spam|', $_POST['sub_menu'] ) )
  386. $current_menu = 'spam';
  387. else if ( preg_match( '|post_status=trash|', $_POST['sub_menu'] ) )
  388. $current_menu = 'trash';
  389. else
  390. $current_menu = 'messages';
  391. }
  392. $post = get_post( $post_id );
  393. $post_type_object = get_post_type_object( $post->post_type );
  394. $akismet_values = get_post_meta( $post_id, '_feedback_akismet_values', TRUE );
  395. if ( $_POST['make_it'] == 'spam' ) {
  396. $post->post_status = 'spam';
  397. $status = wp_insert_post( $post );
  398. wp_transition_post_status( 'spam', 'publish', $post );
  399. do_action( 'contact_form_akismet', 'spam', $akismet_values );
  400. } elseif ( $_POST['make_it'] == 'ham' ) {
  401. $post->post_status = 'publish';
  402. $status = wp_insert_post( $post );
  403. wp_transition_post_status( 'publish', 'spam', $post );
  404. do_action( 'contact_form_akismet', 'spam', $akismet_values );
  405. // resend the original email
  406. $email = get_post_meta( $post_id, '_feedback_email', TRUE );
  407. wp_mail( $email['to'], $email['subject'], $email['message'], $email['headers'] );
  408. } elseif( $_POST['make_it'] == 'publish' ) {
  409. if ( !current_user_can($post_type_object->cap->delete_post, $post_id) )
  410. wp_die( __( 'You are not allowed to move this item out of the Trash.', 'jetpack' ) );
  411. if ( ! wp_untrash_post($post_id) )
  412. wp_die( __( 'Error in restoring from Trash.', 'jetpack' ) );
  413. } elseif( $_POST['make_it'] == 'trash' ) {
  414. if ( !current_user_can($post_type_object->cap->delete_post, $post_id) )
  415. wp_die( __( 'You are not allowed to move this item to the Trash.', 'jetpack' ) );
  416. if ( ! wp_trash_post($post_id) )
  417. wp_die( __( 'Error in moving to Trash.', 'jetpack' ) );
  418. }
  419. $sql = "
  420. SELECT post_status,
  421. COUNT( * ) AS post_count
  422. FROM `{$wpdb->posts}`
  423. WHERE post_type = 'feedback'
  424. GROUP BY post_status
  425. ";
  426. $status_count = (array) $wpdb->get_results( $sql, ARRAY_A );
  427. $status = array();
  428. $status_html = '';
  429. foreach ( $status_count as $i => $row ) {
  430. $status[$row['post_status']] = $row['post_count'];
  431. }
  432. if ( isset( $status['publish'] ) ) {
  433. $status_html .= '<li><a href="edit.php?post_type=feedback"';
  434. if ( $current_menu == 'messages' )
  435. $status_html .= ' class="current"';
  436. $status_html .= '>' . __( 'Messages', 'jetpack' ) . ' <span class="count">';
  437. $status_html .= '(' . number_format( $status['publish'] ) . ')';
  438. $status_html .= '</span></a> |</li>';
  439. }
  440. if ( isset( $status['trash'] ) ) {
  441. $status_html .= '<li><a href="edit.php?post_status=trash&amp;post_type=feedback"';
  442. if ( $current_menu == 'trash' )
  443. $status_html .= ' class="current"';
  444. $status_html .= '>' . __( 'Trash', 'jetpack' ) . ' <span class="count">';
  445. $status_html .= '(' . number_format( $status['trash'] ) . ')';
  446. $status_html .= '</span></a>';
  447. if ( isset( $status['spam'] ) )
  448. $status_html .= ' |';
  449. $status_html .= '</li>';
  450. }
  451. if ( isset( $status['spam'] ) ) {
  452. $status_html .= '<li><a href="edit.php?post_status=spam&amp;post_type=feedback"';
  453. if ( $current_menu == 'spam' )
  454. $status_html .= ' class="current"';
  455. $status_html .= '>' . __( 'Spam', 'jetpack' ) . ' <span class="count">';
  456. $status_html .= '(' . number_format( $status['spam'] ) . ')';
  457. $status_html .= '</span></a></li>';
  458. }
  459. echo $status_html;
  460. exit;
  461. }