PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/sharpmachine/wakeupmedia.com
PHP | 1382 lines | 901 code | 211 blank | 270 comment | 134 complexity | 7f13924b6e5425e698695e11f16b16a7 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Grunion Contact Form
  4. Description: Add a contact form to any post, page or text widget. Emails will be sent to the post's author by default, or any email address you choose. As seen on WordPress.com.
  5. Plugin URI: http://automattic.com/#
  6. AUthor: Automattic, Inc.
  7. Author URI: http://automattic.com/
  8. Version: 2.4
  9. License: GPLv2 or later
  10. */
  11. define( 'GRUNION_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
  12. define( 'GRUNION_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
  13. if ( is_admin() )
  14. require_once GRUNION_PLUGIN_DIR . '/admin.php';
  15. /**
  16. * Sets up various actions, filters, post types, post statuses, shortcodes.
  17. */
  18. class Grunion_Contact_Form_Plugin {
  19. /**
  20. * @var string The Widget ID of the widget currently being processed. Used to build the unique contact-form ID for forms embedded in widgets.
  21. */
  22. var $current_widget_id;
  23. static function init() {
  24. static $instance = false;
  25. if ( !$instance ) {
  26. $instance = new Grunion_Contact_Form_Plugin;
  27. }
  28. return $instance;
  29. }
  30. /**
  31. * Strips HTML tags from input. Output is NOT HTML safe.
  32. *
  33. * @param string $string
  34. * @return string
  35. */
  36. static function strip_tags( $string ) {
  37. $string = wp_kses( $string, array() );
  38. return str_replace( '&amp;', '&', $string ); // undo damage done by wp_kses_normalize_entities()
  39. }
  40. function __construct() {
  41. $this->add_shortcode();
  42. // While generating the output of a text widget with a contact-form shortcode, we need to know its widget ID.
  43. add_action( 'dynamic_sidebar', array( $this, 'track_current_widget' ) );
  44. // Add a "widget" shortcode attribute to all contact-form shortcodes embedded in widgets
  45. add_filter( 'widget_text', array( $this, 'widget_atts' ), 0 );
  46. // If Text Widgets don't get shortcode processed, hack ours into place.
  47. if ( !has_filter( 'widget_text', 'do_shortcode' ) )
  48. add_filter( 'widget_text', array( $this, 'widget_shortcode_hack' ), 5 );
  49. // Akismet to the rescue
  50. if ( function_exists( 'akismet_http_post' ) ) {
  51. add_filter( 'contact_form_is_spam', array( $this, 'is_spam_akismet' ), 10 );
  52. add_action( 'contact_form_akismet', array( $this, 'akismet_submit' ), 10, 2 );
  53. }
  54. add_action( 'loop_start', array( 'Grunion_Contact_Form', '_style_on' ) );
  55. // custom post type we'll use to keep copies of the feedback items
  56. register_post_type( 'feedback', array(
  57. 'labels' => array(
  58. 'name' => __( 'Feedbacks', 'jetpack' ),
  59. 'singular_name' => __( 'Feedback', 'jetpack' ),
  60. 'search_items' => __( 'Search Feedback', 'jetpack' ),
  61. 'not_found' => __( 'No feedback found', 'jetpack' ),
  62. 'not_found_in_trash' => __( 'No feedback found', 'jetpack' )
  63. ),
  64. 'menu_icon' => GRUNION_PLUGIN_URL . '/images/grunion-menu.png',
  65. 'show_ui' => TRUE,
  66. 'show_in_admin_bar' => FALSE,
  67. 'public' => FALSE,
  68. 'rewrite' => FALSE,
  69. 'query_var' => FALSE,
  70. 'capability_type' => 'page'
  71. ) );
  72. // Add "spam" as a post status
  73. register_post_status( 'spam', array(
  74. 'label' => 'Spam',
  75. 'public' => FALSE,
  76. 'exclude_from_search' => TRUE,
  77. 'show_in_admin_all_list' => FALSE,
  78. 'label_count' => _n_noop( 'Spam <span class="count">(%s)</span>', 'Spam <span class="count">(%s)</span>', 'jetpack' ),
  79. 'protected' => TRUE,
  80. '_builtin' => FALSE
  81. ) );
  82. // POST handler
  83. if (
  84. 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] )
  85. &&
  86. isset( $_POST['action'] ) && 'grunion-contact-form' == $_POST['action']
  87. &&
  88. isset( $_POST['contact-form-id'] )
  89. ) {
  90. add_action( 'template_redirect', array( $this, 'process_form_submission' ) );
  91. }
  92. /* Can be dequeued by placing the following in wp-content/themes/yourtheme/functions.php
  93. *
  94. * function remove_grunion_style() {
  95. * wp_deregister_style('grunion.css');
  96. * }
  97. * add_action('wp_print_styles', 'remove_grunion_style');
  98. */
  99. wp_register_style( 'grunion.css', GRUNION_PLUGIN_URL . 'css/grunion.css', array(), JETPACK__VERSION );
  100. }
  101. /**
  102. * Handles all contact-form POST submissions
  103. *
  104. * Conditionally attached to `template_redirect`
  105. */
  106. function process_form_submission() {
  107. $id = stripslashes( $_POST['contact-form-id'] );
  108. check_admin_referer( "contact-form_{$id}" );
  109. $is_widget = 0 === strpos( $id, 'widget-' );
  110. $form = false;
  111. if ( $is_widget ) {
  112. // It's a form embedded in a text widget
  113. $this->current_widget_id = substr( $id, 7 ); // remove "widget-"
  114. // Is the widget active?
  115. $sidebar = is_active_widget( false, $this->current_widget_id, 'text' );
  116. // This is lame - no core API for getting a widget by ID
  117. $widget = isset( $GLOBALS['wp_registered_widgets'][$this->current_widget_id] ) ? $GLOBALS['wp_registered_widgets'][$this->current_widget_id] : false;
  118. if ( $sidebar && $widget && isset( $widget['callback'] ) ) {
  119. // This is lamer - no API for outputting a given widget by ID
  120. ob_start();
  121. // Process the widget to populate Grunion_Contact_Form::$last
  122. call_user_func( $widget['callback'], array(), $widget['params'][0] );
  123. ob_end_clean();
  124. }
  125. } else {
  126. // It's a form embedded in a post
  127. $post = get_post( $id );
  128. // Process the content to populate Grunion_Contact_Form::$last
  129. apply_filters( 'the_content', $post->post_content );
  130. }
  131. $form = Grunion_Contact_Form::$last;
  132. if ( !$form || ( is_wp_error( $form->errors ) && $form->errors->get_error_codes() ) ) {
  133. return;
  134. }
  135. // Process the form
  136. $form->process_submission();
  137. }
  138. /**
  139. * Ensure the post author is always zero for contact-form feedbacks
  140. * Attached to `wp_insert_post_data`
  141. *
  142. * @see Grunion_Contact_Form::process_submission()
  143. *
  144. * @param array $data the data to insert
  145. * @param array $postarr the data sent to wp_insert_post()
  146. * @return array The filtered $data to insert
  147. */
  148. function insert_feedback_filter( $data, $postarr ) {
  149. if ( $data['post_type'] == 'feedback' && $postarr['post_type'] == 'feedback' ) {
  150. $data['post_author'] = 0;
  151. }
  152. return $data;
  153. }
  154. /*
  155. * Adds our contact-form shortcode
  156. * The "child" contact-field shortcode is added as needed by the contact-form shortcode handler
  157. */
  158. function add_shortcode() {
  159. add_shortcode( 'contact-form', array( 'Grunion_Contact_Form', 'parse' ) );
  160. }
  161. /**
  162. * Tracks the widget currently being processed.
  163. * Attached to `dynamic_sidebar`
  164. *
  165. * @see $current_widget_id
  166. *
  167. * @param array $widget The widget data
  168. */
  169. function track_current_widget( $widget ) {
  170. $this->current_widget_id = $widget['id'];
  171. }
  172. /**
  173. * Adds a "widget" attribute to every contact-form embedded in a text widget.
  174. * Used to tell the difference between post-embedded contact-forms and widget-embedded contact-forms
  175. * Attached to `widget_text`
  176. *
  177. * @param string $text The widget text
  178. * @return string The filtered widget text
  179. */
  180. function widget_atts( $text ) {
  181. Grunion_Contact_Form::style( true );
  182. return preg_replace( '/\[contact-form([^a-zA-Z_-])/', '[contact-form widget="' . $this->current_widget_id . '"\\1', $text );
  183. }
  184. /**
  185. * For sites where text widgets are not processed for shortcodes, we add this hack to process just our shortcode
  186. * Attached to `widget_text`
  187. *
  188. * @param string $text The widget text
  189. * @return string The contact-form filtered widget text
  190. */
  191. function widget_shortcode_hack( $text ) {
  192. if ( !preg_match( '/\[contact-form([^a-zA-Z_-])/', $text ) ) {
  193. return $text;
  194. }
  195. $old = $GLOBALS['shortcode_tags'];
  196. remove_all_shortcodes();
  197. $this->add_shortcode();
  198. $text = do_shortcode( $text );
  199. $GLOBALS['shortcode_tags'] = $old;
  200. return $text;
  201. }
  202. /**
  203. * Populate an array with all values necessary to submit a NEW contact-form feedback to Akismet.
  204. * Note that this includes the current user_ip etc, so this should only be called when accepting a new item via $_POST
  205. *
  206. * @param array $form Contact form feedback array
  207. * @return array feedback array with additional data ready for submission to Akismet
  208. */
  209. function prepare_for_akismet( $form ) {
  210. $form['comment_type'] = 'contact_form';
  211. $form['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
  212. $form['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  213. $form['referrer'] = $_SERVER['HTTP_REFERER'];
  214. $form['blog'] = get_option( 'home' );
  215. $ignore = array( 'HTTP_COOKIE' );
  216. foreach ( $_SERVER as $k => $value )
  217. if ( !in_array( $k, $ignore ) && is_string( $value ) )
  218. $form["$k"] = $value;
  219. return $form;
  220. }
  221. /**
  222. * Submit contact-form data to Akismet to check for spam.
  223. * If you're accepting a new item via $_POST, run it Grunion_Contact_Form_Plugin::prepare_for_akismet() first
  224. * Attached to `contact_form_is_spam`
  225. *
  226. * @param array $form
  227. * @return bool|WP_Error TRUE => spam, FALSE => not spam, WP_Error => stop processing entirely
  228. */
  229. function is_spam_akismet( $form ) {
  230. global $akismet_api_host, $akismet_api_port;
  231. if ( !function_exists( 'akismet_http_post' ) )
  232. return false;
  233. $query_string = http_build_query( $form );
  234. $response = akismet_http_post( $query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
  235. $result = false;
  236. if ( 'true' == trim( $response[1] ) ) // 'true' is spam
  237. $result = true;
  238. return apply_filters( 'contact_form_is_spam_akismet', $result, $form );
  239. }
  240. /**
  241. * Submit a feedback as either spam or ham
  242. *
  243. * @param string $as Either 'spam' or 'ham'.
  244. * @param array $form the contact-form data
  245. */
  246. function akismet_submit( $as, $form ) {
  247. global $akismet_api_host, $akismet_api_port;
  248. if ( !in_array( $as, array( 'ham', 'spam' ) ) )
  249. return false;
  250. $query_string = http_build_query( $form );
  251. $response = akismet_http_post( $query_string, $akismet_api_host, "/1.1/submit-{$as}", $akismet_api_port );
  252. return trim( $response[1] );
  253. }
  254. }
  255. /**
  256. * Generic shortcode class.
  257. * Does nothing other than store structured data and output the shortcode as a string
  258. *
  259. * Not very general - specific to Grunion.
  260. */
  261. class Crunion_Contact_Form_Shortcode {
  262. /**
  263. * @var string the name of the shortcode: [$shortcode_name /]
  264. */
  265. var $shortcode_name;
  266. /**
  267. * @var array key => value pairs for the shortcode's attributes: [$shortcode_name key="value" ... /]
  268. */
  269. var $attributes;
  270. /**
  271. * @var array key => value pair for attribute defaults
  272. */
  273. var $defaults = array();
  274. /**
  275. * @var null|string Null for selfclosing shortcodes. Hhe inner content of otherwise: [$shortcode_name]$content[/$shortcode_name]
  276. */
  277. var $content;
  278. /**
  279. * @var array Associative array of inner "child" shortcodes equivalent to the $content: [$shortcode_name][child 1/][child 2/][/$shortcode_name]
  280. */
  281. var $fields;
  282. /**
  283. * @var null|string The HTML of the parsed inner "child" shortcodes". Null for selfclosing shortcodes.
  284. */
  285. var $body;
  286. /**
  287. * @param array $attributes An associative array of shortcode attributes. @see shortcode_atts()
  288. * @param null|string $content Null for selfclosing shortcodes. The inner content otherwise.
  289. */
  290. function __construct( $attributes, $content = null ) {
  291. $this->attributes = $this->unesc_attr( $attributes );
  292. if ( is_array( $content ) ) {
  293. $string_content = '';
  294. foreach ( $content as $field ) {
  295. $string_content .= (string) $field;
  296. }
  297. $this->content = $string_content;
  298. } else {
  299. $this->content = $content;
  300. }
  301. $this->parse_content( $content );
  302. }
  303. /**
  304. * Processes the shortcode's inner content for "child" shortcodes
  305. *
  306. * @param string $content The shortcode's inner content: [shortcode]$content[/shortcode]
  307. */
  308. function parse_content( $content ) {
  309. if ( is_null( $content ) ) {
  310. $this->body = null;
  311. }
  312. $this->body = do_shortcode( $content );
  313. }
  314. /**
  315. * Returns the value of the requested attribute.
  316. *
  317. * @param string $key The attribute to retrieve
  318. * @return mixed
  319. */
  320. function get_attribute( $key ) {
  321. return isset( $this->attributes[$key] ) ? $this->attributes[$key] : null;
  322. }
  323. function esc_attr( $value ) {
  324. if ( is_array( $value ) ) {
  325. return array_map( array( $this, 'esc_attr' ), $value );
  326. }
  327. $value = Grunion_Contact_Form_Plugin::strip_tags( $value );
  328. $value = _wp_specialchars( $value, ENT_QUOTES, false, true );
  329. // Shortcode attributes can't contain "]"
  330. $value = str_replace( ']', '', $value );
  331. $value = str_replace( ',', '&#x002c;', $value ); // store commas encoded
  332. $value = strtr( $value, array( '%' => '%25', '&' => '%26' ) );
  333. // shortcode_parse_atts() does stripcslashes()
  334. $value = addslashes( $value );
  335. return $value;
  336. }
  337. function unesc_attr( $value ) {
  338. if ( is_array( $value ) ) {
  339. return array_map( array( $this, 'unesc_attr' ), $value );
  340. }
  341. // For back-compat with old Grunion encoding
  342. // Also, unencode commas
  343. $value = strtr( $value, array( '%26' => '&', '%25' => '%' ) );
  344. $value = preg_replace( array( '/&#x0*22;/i', '/&#x0*27;/i', '/&#x0*26;/i', '/&#x0*2c;/i' ), array( '"', "'", '&', ',' ), $value );
  345. $value = htmlspecialchars_decode( $value, ENT_QUOTES );
  346. $value = Grunion_Contact_Form_Plugin::strip_tags( $value );
  347. return $value;
  348. }
  349. /**
  350. * Generates the shortcode
  351. */
  352. function __toString() {
  353. $r = "[{$this->shortcode_name} ";
  354. foreach ( $this->attributes as $key => $value ) {
  355. if ( !$value ) {
  356. continue;
  357. }
  358. if ( isset( $this->defaults[$key] ) && $this->defaults[$key] == $value ) {
  359. continue;
  360. }
  361. if ( 'id' == $key ) {
  362. continue;
  363. }
  364. $value = $this->esc_attr( $value );
  365. if ( is_array( $value ) ) {
  366. $value = join( ',', $value );
  367. }
  368. if ( false === strpos( $value, "'" ) ) {
  369. $value = "'$value'";
  370. } elseif ( false === strpos( $value, '"' ) ) {
  371. $value = '"' . $value . '"';
  372. } else {
  373. // Shortcodes can't contain both '"' and "'". Strip one.
  374. $value = str_replace( "'", '', $value );
  375. $value = "'$value'";
  376. }
  377. $r .= "{$key}={$value} ";
  378. }
  379. $r = rtrim( $r );
  380. if ( $this->fields ) {
  381. $r .= ']';
  382. foreach ( $this->fields as $field ) {
  383. $r .= (string) $field;
  384. }
  385. $r .= "[/{$this->shortcode_name}]";
  386. } else {
  387. $r .= '/]';
  388. }
  389. return $r;
  390. }
  391. }
  392. /**
  393. * Class for the contact-form shortcode.
  394. * Parses shortcode to output the contact form as HTML
  395. * Sends email and stores the contact form response (a.k.a. "feedback")
  396. */
  397. class Grunion_Contact_Form extends Crunion_Contact_Form_Shortcode {
  398. var $shortcode_name = 'contact-form';
  399. /**
  400. * @var WP_Error stores form submission errors
  401. */
  402. var $errors;
  403. /**
  404. * @var Grunion_Contact_Form The most recent (inclusive) contact-form shortcode processed
  405. */
  406. static $last;
  407. /**
  408. * @var bool Whether to print the grunion.css style when processing the contact-form shortcode
  409. */
  410. static $style = false;
  411. function __construct( $attributes, $content = null ) {
  412. global $wp_query;
  413. // Set up the default subject and recipient for this form
  414. $default_to = get_option( 'admin_email' );
  415. $default_subject = "[" . get_option( 'blogname' ) . "]";
  416. if ( !empty( $attributes['widget'] ) && $attributes['widget'] ) {
  417. $attributes['id'] = 'widget-' . $attributes['widget'];
  418. $default_subject = sprintf( _x( '%1$s Sidebar', '%1$s = blog name', 'jetpack' ), $default_subject );
  419. } else {
  420. $attributes['id'] = get_the_ID();
  421. $post = get_post( $attributes['id'] );
  422. if ( $post ) {
  423. $default_subject = sprintf( _x( '%1$s %2$s', '%1$s = blog name, %2$s = post title' , 'jetpack'), $default_subject, Grunion_Contact_Form_Plugin::strip_tags( $post->post_title ) );
  424. $post_author = get_userdata( $post->post_author );
  425. $default_to = $post_author->user_email;
  426. }
  427. }
  428. $this->defaults = array(
  429. 'to' => $default_to,
  430. 'subject' => $default_subject,
  431. 'show_subject' => 'no', // only used in back-compat mode
  432. 'widget' => 0, // Not exposed to the user. Works with Grunion_Contact_Form_Plugin::widget_atts()
  433. 'id' => null, // Not exposed to the user. Set above.
  434. );
  435. $attributes = shortcode_atts( $this->defaults, $attributes );
  436. // We only add the contact-field shortcode temporarily while processing the contact-form shortcode
  437. add_shortcode( 'contact-field', array( $this, 'parse_contact_field' ) );
  438. parent::__construct( $attributes, $content );
  439. // There were no fields in the contact form. The form was probably just [contact-form /]. Build a default form.
  440. if ( empty( $this->fields ) ) {
  441. // same as the original Grunion v1 form
  442. $default_form = '
  443. [contact-field label="' . __( 'Name', 'jetpack' ) . '" type="name" required="true" /]
  444. [contact-field label="' . __( 'Email', 'jetpack' ) . '" type="email" required="true" /]
  445. [contact-field label="' . __( 'Website', 'jetpack' ) . '" type="url" /]';
  446. if ( 'yes' == strtolower( $this->get_attribute( 'show_subject' ) ) ) {
  447. $default_form .= '
  448. [contact-field label="' . __( 'Subject', 'jetpack' ) . '" type="subject" /]';
  449. }
  450. $default_form .= '
  451. [contact-field label="' . __( 'Message', 'jetpack' ) . '" type="textarea" /]';
  452. $this->parse_content( $default_form );
  453. }
  454. // $this->body and $this->fields have been setup. We no longer need the contact-field shortcode.
  455. remove_shortcode( 'contact-field' );
  456. }
  457. /**
  458. * Toggle for printing the grunion.css stylesheet
  459. *
  460. * @param bool $style
  461. */
  462. static function style( $style ) {
  463. $previous_style = self::$style;
  464. self::$style = (bool) $style;
  465. return $previous_style;
  466. }
  467. /**
  468. * Turn on printing of grunion.css stylesheet
  469. * @see ::style()
  470. * @internal
  471. * @param bool $style
  472. */
  473. static function _style_on() {
  474. return self::style( true );
  475. }
  476. /**
  477. * The contact-form shortcode processor
  478. *
  479. * @param array $attributes Key => Value pairs as parsed by shortcode_parse_atts()
  480. * @param string|null $content The shortcode's inner content: [contact-form]$content[/contact-form]
  481. * @return string HTML for the concat form.
  482. */
  483. static function parse( $attributes, $content ) {
  484. // Create a new Grunion_Contact_Form object (this class)
  485. $form = new Grunion_Contact_Form( $attributes, $content );
  486. $id = $form->get_attribute( 'id' );
  487. if ( !$id ) { // something terrible has happened
  488. return '[contact-form]';
  489. }
  490. if ( apply_filters( 'jetpack_bail_on_shortcode', false, 'contact-form' ) || is_feed() ) {
  491. return '[contact-form]';
  492. }
  493. // Only allow one contact form per post/widget
  494. if ( self::$last && $id == self::$last->get_attribute( 'id' ) ) {
  495. // We're processing the same post
  496. if ( self::$last->attributes != $form->attributes || self::$last->content != $form->content ) {
  497. // And we're processing a different shortcode;
  498. return '';
  499. } // else, we're processing the same shortcode - probably a separate run of do_shortcode() - let it through
  500. } else {
  501. self::$last = $form;
  502. }
  503. // Output the grunion.css stylesheet if self::$style allows it
  504. if ( self::$style && ( empty( $_REQUEST['action'] ) || $_REQUEST['action'] != 'grunion_shortcode_to_json' ) ) {
  505. ob_start();
  506. wp_print_styles( 'grunion.css' ); // wp_print_styles() will only ever print grunion.css once, regaurdless of how many times it is called.
  507. $r = ob_get_clean();
  508. } else {
  509. $r = '';
  510. }
  511. $r .= "<div id='contact-form-$id'>\n";
  512. if ( is_wp_error( $form->errors ) && $form->errors->get_error_codes() ) {
  513. // There are errors. Display them
  514. $r .= "<div class='form-error'>\n<h3>" . __( 'Error!', 'jetpack' ) . "</h3>\n<ul class='form-errors'>\n";
  515. foreach ( $form->errors->get_error_messages() as $message )
  516. $r .= "\t<li class='form-error-message'>" . esc_html( $message ) . "</li>\n";
  517. $r .= "</ul>\n</div>\n\n";
  518. }
  519. if ( isset( $_GET['contact-form-id'] ) && $_GET['contact-form-id'] == self::$last->get_attribute( 'id' ) && isset( $_GET['contact-form-sent'] ) ) {
  520. // The contact form was submitted. Show the success message/results
  521. $feedback_id = (int) $_GET['contact-form-sent'];
  522. $back_url = remove_query_arg( array( 'contact-form-id', 'contact-form-sent', '_wpnonce' ) );
  523. $r_success_message =
  524. "<h3>" . __( 'Message Sent', 'jetpack' ) .
  525. ' (<a href="' . esc_url( $back_url ) . '">' . esc_html__( 'go back', 'jetpack' ) . '</a>)' .
  526. "</h3>\n\n";
  527. // Don't show the feedback details unless the nonce matches
  528. if ( $feedback_id && wp_verify_nonce( stripslashes( $_GET['_wpnonce'] ), "contact-form-sent-{$feedback_id}" ) ) {
  529. $feedback = get_post( $feedback_id );
  530. $field_ids = $form->get_field_ids();
  531. // Maps field_ids to post_meta keys
  532. $field_value_map = array(
  533. 'name' => 'author',
  534. 'email' => 'author_email',
  535. 'url' => 'author_url',
  536. 'subject' => 'subject',
  537. 'textarea' => false, // not a post_meta key. This is stored in post_content
  538. );
  539. $contact_form_message = "<blockquote>\n";
  540. // "Standard" field whitelist
  541. foreach ( $field_value_map as $type => $meta_key ) {
  542. if ( isset( $field_ids[$type] ) ) {
  543. $field = $form->fields[$field_ids[$type]];
  544. if ( $meta_key ) {
  545. $value = get_post_meta( $feedback_id, "_feedback_{$meta_key}", true );
  546. } else {
  547. // The feedback content is stored as the first "half" of post_content
  548. $value = $feedback->post_content;
  549. list( $value ) = explode( '<!--more-->', $value );
  550. $value = trim( $value );
  551. }
  552. $contact_form_message .= sprintf(
  553. _x( '%1$s: %2$s', '%1$s = form field label, %2$s = form field value', 'jetpack' ),
  554. wp_kses( $field->get_attribute( 'label' ), array() ),
  555. wp_kses( $value, array() )
  556. ) . '<br />';
  557. }
  558. }
  559. // "Non-standard" fields
  560. if ( $field_ids['extra'] ) {
  561. // array indexed by field label (not field id)
  562. $extra_fields = get_post_meta( $feedback_id, '_feedback_extra_fields', true );
  563. foreach ( $field_ids['extra'] as $field_id ) {
  564. $field = $form->fields[$field_id];
  565. $label = $field->get_attribute( 'label' );
  566. $contact_form_message .= sprintf(
  567. _x( '%1$s: %2$s', '%1$s = form field label, %2$s = form field value', 'jetpack' ),
  568. wp_kses( $label, array() ),
  569. wp_kses( $extra_fields[$label], array() )
  570. ) . '<br />';
  571. }
  572. }
  573. $contact_form_message .= "</blockquote><br /><br />";
  574. $r_success_message .= wp_kses( $contact_form_message, array( 'br' => array(), 'blockquote' => array() ) );
  575. }
  576. $r .= apply_filters( 'grunion_contact_form_success_message', $r_success_message );
  577. } else {
  578. // Nothing special - show the normal contact form
  579. if ( $form->get_attribute( 'widget' ) ) {
  580. // Submit form to the current URL
  581. $url = remove_query_arg( array( 'contact-form-id', 'contact-form-sent', 'action', '_wpnonce' ) );
  582. } else {
  583. // Submit form to the post permalink
  584. $url = get_permalink();
  585. }
  586. // May eventually want to send this to admin-post.php...
  587. $url = apply_filters( 'grunion_contact_form_form_action', "{$url}#contact-form-{$id}", $GLOBALS['post'], $id );
  588. $r .= "<form action='" . esc_url( $url ) . "' method='post' class='contact-form commentsblock'>\n";
  589. $r .= $form->body;
  590. $r .= "\t<p class='contact-submit'>\n";
  591. $r .= "\t\t<input type='submit' value='" . esc_attr__( 'Submit &#187;', 'jetpack' ) . "' class='pushbutton-wide'/>\n";
  592. $r .= "\t\t" . wp_nonce_field( 'contact-form_' . $id, '_wpnonce', true, false ) . "\n"; // nonce and referer
  593. $r .= "\t\t<input type='hidden' name='contact-form-id' value='$id' />\n";
  594. $r .= "\t\t<input type='hidden' name='action' value='grunion-contact-form' />\n";
  595. $r .= "\t</p>\n";
  596. $r .= "</form>\n";
  597. }
  598. $r .= "</div>";
  599. return $r;
  600. }
  601. /**
  602. * The contact-field shortcode processor
  603. * We use an object method here instead of a static Grunion_Contact_Form_Field class method to parse contact-field shortcodes so that we can tie them to the contact-form object.
  604. *
  605. * @param array $attributes Key => Value pairs as parsed by shortcode_parse_atts()
  606. * @param string|null $content The shortcode's inner content: [contact-field]$content[/contact-field]
  607. * @return HTML for the contact form field
  608. */
  609. function parse_contact_field( $attributes, $content ) {
  610. $field = new Grunion_Contact_Form_Field( $attributes, $content, $this );
  611. $field_id = $field->get_attribute( 'id' );
  612. if ( $field_id ) {
  613. $this->fields[$field_id] = $field;
  614. } else {
  615. $this->fields[] = $field;
  616. }
  617. if (
  618. isset( $_POST['action'] ) && 'grunion-contact-form' === $_POST['action']
  619. &&
  620. isset( $_POST['contact-form-id'] ) && $this->get_attribute( 'id' ) == $_POST['contact-form-id']
  621. ) {
  622. // If we're processing a POST submission for this contact form, validate the field value so we can show errors as necessary.
  623. $field->validate();
  624. }
  625. // Output HTML
  626. return $field->render();
  627. }
  628. /**
  629. * Loops through $this->fields to generate a (structured) list of field IDs
  630. * @return array
  631. */
  632. function get_field_ids() {
  633. $field_ids = array(
  634. 'all' => array(), // array of all field_ids
  635. 'extra' => array(), // array of all non-whitelisted field IDs
  636. // Whitelisted "standard" field IDs:
  637. // 'email' => field_id,
  638. // 'name' => field_id,
  639. // 'url' => field_id,
  640. // 'subject' => field_id,
  641. // 'textarea' => field_id,
  642. );
  643. foreach ( $this->fields as $id => $field ) {
  644. $field_ids['all'][] = $id;
  645. $type = $field->get_attribute( 'type' );
  646. if ( isset( $field_ids[$type] ) ) {
  647. // This type of field is already present in our whitelist of "standard" fields for this form
  648. // Put it in extra
  649. $field_ids['extra'][] = $id;
  650. continue;
  651. }
  652. switch ( $type ) {
  653. case 'email' :
  654. case 'name' :
  655. case 'url' :
  656. case 'subject' :
  657. case 'textarea' :
  658. $field_ids[$type] = $id;
  659. break;
  660. default :
  661. // Put everything else in extra
  662. $field_ids['extra'][] = $id;
  663. }
  664. }
  665. return $field_ids;
  666. }
  667. /**
  668. * Process the contact form's POST submission
  669. * Stores feedback. Sends email.
  670. */
  671. function process_submission() {
  672. global $post;
  673. $plugin = Grunion_Contact_Form_Plugin::init();
  674. $id = $this->get_attribute( 'id' );
  675. $to = $this->get_attribute( 'to' );
  676. $widget = $this->get_attribute( 'widget' );
  677. $contact_form_subject = $this->get_attribute( 'subject' );
  678. $to = str_replace( ' ', '', $to );
  679. $emails = explode( ',', $to );
  680. $valid_emails = array();
  681. foreach ( (array) $emails as $email ) {
  682. if ( !is_email( $email ) ) {
  683. continue;
  684. }
  685. if ( function_exists( 'is_email_address_unsafe' ) && is_email_address_unsafe( $email ) ) {
  686. continue;
  687. }
  688. $valid_emails[] = $email;
  689. }
  690. // No one to send it to :(
  691. if ( !$valid_emails ) {
  692. return;
  693. }
  694. $to = $valid_emails;
  695. // Make sure we're processing the form we think we're processing... probably a redundant check.
  696. if ( $widget ) {
  697. if ( 'widget-' . $widget != $_POST['contact-form-id'] ) {
  698. return;
  699. }
  700. } else {
  701. if ( $post->ID != $_POST['contact-form-id'] ) {
  702. return;
  703. }
  704. }
  705. $field_ids = $this->get_field_ids();
  706. // Initialize all these "standard" fields to null
  707. $comment_author_email = $comment_author_email_label = // v
  708. $comment_author = $comment_author_label = // v
  709. $comment_author_url = $comment_author_url_label = // v
  710. $comment_content = $comment_content_label = null;
  711. // For each of the "standard" fields, grab their field label and value.
  712. if ( isset( $field_ids['name'] ) ) {
  713. $field = $this->fields[$field_ids['name']];
  714. $comment_author = Grunion_Contact_Form_Plugin::strip_tags( stripslashes( apply_filters( 'pre_comment_author_name', addslashes( $field->value ) ) ) );
  715. $comment_author_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  716. }
  717. if ( isset( $field_ids['email'] ) ) {
  718. $field = $this->fields[$field_ids['email']];
  719. $comment_author_email = Grunion_Contact_Form_Plugin::strip_tags( stripslashes( apply_filters( 'pre_comment_author_email', addslashes( $field->value ) ) ) );
  720. $comment_author_email_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  721. }
  722. if ( isset( $field_ids['url'] ) ) {
  723. $field = $this->fields[$field_ids['url']];
  724. $comment_author_url = Grunion_Contact_Form_Plugin::strip_tags( stripslashes( apply_filters( 'pre_comment_author_url', addslashes( $field->value ) ) ) );
  725. if ( 'http://' == $comment_author_url ) {
  726. $comment_author_url = '';
  727. }
  728. $comment_author_url_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  729. }
  730. if ( isset( $field_ids['textarea'] ) ) {
  731. $field = $this->fields[$field_ids['textarea']];
  732. $comment_content = trim( Grunion_Contact_Form_Plugin::strip_tags( $field->value ) );
  733. $comment_content_label = Grunion_Contact_Form_Plugin::strip_tags( $field->get_attribute( 'label' ) );
  734. }
  735. if ( isset( $field_ids['subject'] ) ) {
  736. $field = $this->fields[$field_ids['subject']];
  737. if ( $field->value ) {
  738. $contact_form_subject = Grunion_Contact_Form_Plugin::strip_tags( $field->value );
  739. }
  740. }
  741. $all_values = $extra_values = array();
  742. // For all fields, grab label and value
  743. foreach ( $field_ids['all'] as $field_id ) {
  744. $field = $this->fields[$field_id];
  745. $label = $field->get_attribute( 'label' );
  746. $value = $field->value;
  747. $all_values[$label] = $value;
  748. }
  749. // For the "non-standard" fields, grab label and value
  750. foreach ( $field_ids['extra'] as $field_id ) {
  751. $field = $this->fields[$field_id];
  752. $label = $field->get_attribute( 'label' );
  753. $value = $field->value;
  754. $extra_values[$label] = $value;
  755. }
  756. $contact_form_subject = trim( $contact_form_subject );
  757. $comment_author_IP = Grunion_Contact_Form_Plugin::strip_tags( $_SERVER['REMOTE_ADDR'] );
  758. $vars = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'contact_form_subject', 'comment_author_IP' );
  759. foreach ( $vars as $var )
  760. $$var = str_replace( array( "\n", "\r" ), '', $$var );
  761. $vars[] = 'comment_content';
  762. $spam = '';
  763. $akismet_values = $plugin->prepare_for_akismet( compact( $vars ) );
  764. // Is it spam?
  765. $is_spam = apply_filters( 'contact_form_is_spam', $akismet_values );
  766. if ( is_wp_error( $is_spam ) ) // WP_Error to abort
  767. return; // abort
  768. else if ( $is_spam === TRUE ) // TRUE to flag a spam
  769. $spam = '***SPAM*** ';
  770. if ( !$comment_author )
  771. $comment_author = $comment_author_email;
  772. $to = (array) apply_filters( 'contact_form_to', $to );
  773. foreach ( $to as $to_key => $to_value ) {
  774. $to[$to_key] = Grunion_Contact_Form_Plugin::strip_tags( $to_value );
  775. }
  776. $blog_url = parse_url( site_url() );
  777. $from_email_addr = 'wordpress@' . $blog_url['host'];
  778. $reply_to_addr = $to[0];
  779. if ( ! empty( $comment_author_email ) ) {
  780. $reply_to_addr = $comment_author_email;
  781. }
  782. $headers = 'From: ' . $comment_author .' <' . $from_email_addr . ">\r\n" .
  783. 'Reply-To: ' . $comment_author . ' <' . $reply_to_addr . ">\r\n" .
  784. "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"";
  785. $subject = apply_filters( 'contact_form_subject', $contact_form_subject );
  786. $time = date_i18n( __( 'l F j, Y \a\t g:i a', 'jetpack' ), current_time( 'timestamp' ) );
  787. $extra_content = '';
  788. foreach ( $extra_values as $label => $value ) {
  789. $extra_content .= $label . ': ' . trim( $value ) . "\n";
  790. }
  791. $message = "$comment_author_label: $comment_author\n";
  792. if ( !empty( $comment_author_email ) ) {
  793. $message .= "$comment_author_email_label: $comment_author_email\n";
  794. }
  795. if ( !empty( $comment_author_url ) ) {
  796. $message .= "$comment_author_url_label: $comment_author_url\n";
  797. }
  798. if ( !empty( $comment_content_label ) ) {
  799. $message .= "$comment_content_label: $comment_content\n";
  800. }
  801. $message .= $extra_content . "\n";
  802. $message .= __( 'Time:', 'jetpack' ) . ' ' . $time . "\n";
  803. $message .= __( 'IP Address:', 'jetpack' ) . ' ' . $comment_author_IP . "\n";
  804. $message .= __( 'Contact Form URL:', 'jetpack' ) . ' ' . get_permalink( $post->ID ) . "\n";
  805. if ( is_user_logged_in() ) {
  806. $message .= "\n";
  807. $message .= sprintf(
  808. __( 'Sent by a verified %s user.', 'jetpack' ),
  809. isset( $GLOBALS['current_site']->site_name ) && $GLOBALS['current_site']->site_name ? $GLOBALS['current_site']->site_name : '"' . get_option( 'blogname' ) . '"'
  810. );
  811. } else {
  812. $message .= __( 'Sent by an unverified visitor to your site.', 'jetpack' );
  813. }
  814. $message = apply_filters( 'contact_form_message', $message );
  815. $message = Grunion_Contact_Form_Plugin::strip_tags( $message );
  816. // keep a copy of the feedback as a custom post type
  817. $feedback_mysql_time = current_time( 'mysql' );
  818. $feedback_title = "{$comment_author} - {$feedback_mysql_time}";
  819. $feedback_status = 'publish';
  820. if ( $is_spam === TRUE )
  821. $feedback_status = 'spam';
  822. foreach ( (array) $akismet_values as $av_key => $av_value ) {
  823. $akismet_values[$av_key] = Grunion_Contact_Form_Plugin::strip_tags( $av_value );
  824. }
  825. foreach ( (array) $all_values as $all_key => $all_value ) {
  826. $all_values[$all_key] = Grunion_Contact_Form_Plugin::strip_tags( $all_value );
  827. }
  828. foreach ( (array) $extra_values as $ev_key => $ev_value ) {
  829. $extra_values[$ev_key] = Grunion_Contact_Form_Plugin::strip_tags( $ev_value );
  830. }
  831. /* We need to make sure that the post author is always zero for contact
  832. * form submissions. This prevents export/import from trying to create
  833. * new users based on form submissions from people who were logged in
  834. * at the time.
  835. *
  836. * Unfortunately wp_insert_post() tries very hard to make sure the post
  837. * author gets the currently logged in user id. That is how we ended up
  838. * with this work around. */
  839. add_filter( 'wp_insert_post_data', array( $plugin, 'insert_feedback_filter' ), 10, 2 );
  840. $post_id = wp_insert_post( array(
  841. 'post_date' => addslashes( $feedback_mysql_time ),
  842. 'post_type' => 'feedback',
  843. 'post_status' => addslashes( $feedback_status ),
  844. 'post_parent' => (int) $post->ID,
  845. 'post_title' => addslashes( wp_kses( $feedback_title, array() ) ),
  846. 'post_content' => addslashes( wp_kses( $comment_content . "\n<!--more-->\n" . "AUTHOR: {$comment_author}\nAUTHOR EMAIL: {$comment_author_email}\nAUTHOR URL: {$comment_author_url}\nSUBJECT: {$contact_form_subject}\nIP: {$comment_author_IP}\n" . print_r( $all_values, TRUE ), array() ) ), // so that search will pick up this data
  847. 'post_name' => md5( $feedback_title ),
  848. ) );
  849. // once insert has finished we don't need this filter any more
  850. remove_filter( 'wp_insert_post_data', array( $plugin, 'insert_feedback_filter' ), 10, 2 );
  851. update_post_meta( $post_id, '_feedback_author', addslashes( $comment_author ) );
  852. update_post_meta( $post_id, '_feedback_author_email', addslashes( $comment_author_email ) );
  853. update_post_meta( $post_id, '_feedback_author_url', addslashes( $comment_author_url ) );
  854. update_post_meta( $post_id, '_feedback_subject', addslashes( $contact_form_subject ) );
  855. update_post_meta( $post_id, '_feedback_ip', addslashes( $comment_author_IP ) );
  856. update_post_meta( $post_id, '_feedback_contact_form_url', addslashes( get_permalink( $post->ID ) ) );
  857. update_post_meta( $post_id, '_feedback_all_fields', $this->addslashes_deep( $all_values ) );
  858. update_post_meta( $post_id, '_feedback_extra_fields', $this->addslashes_deep( $extra_values ) );
  859. update_post_meta( $post_id, '_feedback_akismet_values', $this->addslashes_deep( $akismet_values ) );
  860. update_post_meta( $post_id, '_feedback_email', $this->addslashes_deep( array( 'to' => $to, 'subject' => $subject, 'message' => $message, 'headers' => $headers ) ) );
  861. do_action( 'grunion_pre_message_sent', $post_id, $all_values, $extra_values );
  862. // schedule deletes of old spam feedbacks
  863. if ( !wp_next_scheduled( 'grunion_scheduled_delete' ) ) {
  864. wp_schedule_event( time() + 250, 'daily', 'grunion_scheduled_delete' );
  865. }
  866. if ( $is_spam !== TRUE )
  867. wp_mail( $to, "{$spam}{$subject}", $message, $headers );
  868. elseif ( apply_filters( 'grunion_still_email_spam', FALSE ) == TRUE ) // don't send spam by default. Filterable.
  869. wp_mail( $to, "{$spam}{$subject}", $message, $headers );
  870. $redirect = wp_get_referer();
  871. if ( !$redirect ) { // wp_get_referer() returns false if the referer is the same as the current page
  872. $redirect = $_SERVER['REQUEST_URI'];
  873. }
  874. $redirect = add_query_arg( urlencode_deep( array(
  875. 'contact-form-id' => $id,
  876. 'contact-form-sent' => $post_id,
  877. '_wpnonce' => wp_create_nonce( "contact-form-sent-{$post_id}" ), // wp_nonce_url HTMLencodes :(
  878. ) ), $redirect );
  879. $redirect = apply_filters( 'grunion_contact_form_redirect_url', $redirect, $id, $post_id );
  880. wp_safe_redirect( $redirect );
  881. exit;
  882. }
  883. function addslashes_deep( $value ) {
  884. if ( is_array( $value ) ) {
  885. return array_map( array( $this, 'addslashes_deep' ), $value );
  886. } elseif ( is_object( $value ) ) {
  887. $vars = get_object_vars( $value );
  888. foreach ( $vars as $key => $data ) {
  889. $value->{$key} = $this->addslashes_deep( $data );
  890. }
  891. return $value;
  892. }
  893. return addslashes( $value );
  894. }
  895. }
  896. /**
  897. * Class for the contact-field shortcode.
  898. * Parses shortcode to output the contact form field as HTML.
  899. * Validates input.
  900. */
  901. class Grunion_Contact_Form_Field extends Crunion_Contact_Form_Shortcode {
  902. var $shortcode_name = 'contact-field';
  903. /**
  904. * @var Grunion_Contact_Form parent form
  905. */
  906. var $form;
  907. /**
  908. * @var string default or POSTed value
  909. */
  910. var $value;
  911. /**
  912. * @var bool Is the input invalid?
  913. */
  914. var $error = false;
  915. /**
  916. * @param array $attributes An associative array of shortcode attributes. @see shortcode_atts()
  917. * @param null|string $content Null for selfclosing shortcodes. The inner content otherwise.
  918. * @param Grunion_Contact_Form $form The parent form
  919. */
  920. function __construct( $attributes, $content = null, $form = null ) {
  921. $attributes = shortcode_atts( array(
  922. 'label' => null,
  923. 'type' => 'text',
  924. 'required' => false,
  925. 'options' => array(),
  926. 'id' => null,
  927. 'default' => null,
  928. ), $attributes );
  929. // special default for subject field
  930. if ( 'subject' == $attributes['type'] && is_null( $attributes['default'] ) && !is_null( $form ) ) {
  931. $attributes['default'] = $form->get_attribute( 'subject' );
  932. }
  933. // allow required=1 or required=true
  934. if ( '1' == $attributes['required'] || 'true' == strtolower( $attributes['required'] ) )
  935. $attributes['required'] = true;
  936. else
  937. $attributes['required'] = false;
  938. // parse out comma-separated options list (for selects and radios)
  939. if ( !empty( $attributes['options'] ) && is_string( $attributes['options'] ) ) {
  940. $attributes['options'] = array_map( 'trim', explode( ',', $attributes['options'] ) );
  941. }
  942. if ( $form ) {
  943. // make a unique field ID based on the label, with an incrementing number if needed to avoid clashes
  944. $form_id = $form->get_attribute( 'id' );
  945. $id = isset( $attributes['id'] ) ? $attributes['id'] : false;
  946. $unescaped_label = $this->unesc_attr( $attributes['label'] );
  947. $unescaped_label = str_replace( '%', '-', $unescaped_label ); // jQuery doesn't like % in IDs?
  948. if ( empty( $id ) ) {
  949. $id = sanitize_title_with_dashes( $form_id . '-' . $unescaped_label );
  950. $i = 0;
  951. $max_tries = 12;
  952. while ( isset( $form->fields[$id] ) ) {
  953. $i++;
  954. $id = sanitize_title_with_dashes( $form_id . '-' . $unescaped_label . '-' . $i );
  955. if ( $i > $max_tries ) {
  956. break;
  957. }
  958. }
  959. }
  960. $attributes['id'] = $id;
  961. }
  962. parent::__construct( $attributes, $content );
  963. // Store parent form
  964. $this->form = $form;
  965. }
  966. /**
  967. * This field's input is invalid. Flag as invalid and add an error to the parent form
  968. *
  969. * @param string $message The error message to display on the form.
  970. */
  971. function add_error( $message ) {
  972. $this->is_error = true;
  973. if ( !is_wp_error( $this->form->errors ) ) {
  974. $this->form->errors = new WP_Error;
  975. }
  976. $this->form->errors->add( $this->get_attribute( 'id' ), $message );
  977. }
  978. /**
  979. * Is the field input invalid?
  980. *
  981. * @see $error
  982. *
  983. * @return bool
  984. */
  985. function is_error() {
  986. return $this->error;
  987. }
  988. /**
  989. * Validates the form input
  990. */
  991. function validate() {
  992. // If it's not required, there's nothing to validate
  993. if ( !$this->get_attribute( 'required' ) ) {
  994. return;
  995. }
  996. $field_id = $this->get_attribute( 'id' );
  997. $field_type = $this->get_attribute( 'type' );
  998. $field_label = $this->get_attribute( 'label' );
  999. $field_value = isset( $_POST[$field_id] ) ? stripslashes( $_POST[$field_id] ) : '';
  1000. switch ( $field_type ) {
  1001. case 'email' :
  1002. // Make sure the email address is valid
  1003. if ( !is_email( $field_value ) ) {
  1004. $this->add_error( sprintf( __( '%s requires a valid email address', 'jetpack' ), $field_label ) );
  1005. }
  1006. break;
  1007. default :
  1008. // Just check for presence of any text
  1009. if ( !strlen( trim( $field_value ) ) ) {
  1010. $this->add_error( sprintf( __( '%s is required', 'jetpack' ), $field_label ) );
  1011. }
  1012. }
  1013. }
  1014. /**
  1015. * Outputs the HTML for this form field
  1016. *
  1017. * @return string HTML
  1018. */
  1019. function render() {
  1020. global $current_user, $user_identity;
  1021. $r = '';
  1022. $field_id = $this->get_attribute( 'id' );
  1023. $field_type = $this->get_attribute( 'type' );
  1024. $field_label = $this->get_attribute( 'label' );
  1025. $field_required = $this->get_attribute( 'required' );
  1026. if ( isset( $_POST[$field_id] ) ) {
  1027. $this->value = stripslashes( (string) $_POST[$field_id] );
  1028. } elseif ( is_user_logged_in() ) {
  1029. // Special defaults for logged-in users
  1030. switch ( $this->get_attribute( 'type' ) ) {
  1031. case 'email';
  1032. $this->value = $current_user->data->user_email;
  1033. break;
  1034. case 'name' :
  1035. $this->value = $user_identity;
  1036. break;
  1037. case 'url' :
  1038. $this->value = $current_user->data->user_url;
  1039. break;
  1040. default :
  1041. $this->value = $this->get_attribute( 'default' );
  1042. }
  1043. } else {
  1044. $this->value = $this->get_attribute( 'default' );
  1045. }
  1046. $field_value = Grunion_Contact_Form_Plugin::strip_tags( $this->value );
  1047. $field_label = Grunion_Contact_Form_Plugin::strip_tags( $field_label );
  1048. switch ( $field_type ) {
  1049. case 'email' :
  1050. $r .= "\n<div>\n";
  1051. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label email" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n";
  1052. $r .= "\t\t<input type='email' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' class='email' />\n";
  1053. $r .= "\t</div>\n";
  1054. break;
  1055. case 'textarea' :
  1056. $r .= "\n<div>\n";
  1057. $r .= "\t\t<label for='contact-form-comment-" . esc_attr( $field_id ) . "' class='grunion-field-label textarea" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n";
  1058. $r .= "\t\t<textarea name='" . esc_attr( $field_id ) . "' id='contact-form-comment-" . esc_attr( $field_id ) . "' rows='20'>" . esc_textarea( $field_value ) . "</textarea>\n";
  1059. $r .= "\t</div>\n";
  1060. break;
  1061. case 'radio' :
  1062. $r .= "\t<div><label class='grunion-field-label" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n";
  1063. foreach ( $this->get_attribute( 'options' ) as $option ) {
  1064. $option = Grunion_Contact_Form_Plugin::strip_tags( $option );
  1065. $r .= "\t\t<label class='grunion-radio-label radio" . ( $this->is_error() ? ' form-error' : '' ) . "'>";
  1066. $r .= "<input type='radio' name='" . esc_attr( $field_id ) . "' value='" . esc_attr( $option ) . "' class='radio' " . checked( $option, $field_value, false ) . " /> ";
  1067. $r .= esc_html( $option ) . "</label>\n";
  1068. $r .= "\t\t<div class='clear-form'></div>\n";
  1069. }
  1070. $r .= "\t\t</div>\n";
  1071. break;
  1072. case 'checkbox' :
  1073. $r .= "\t<div>\n";
  1074. $r .= "\t\t<label class='grunion-field-label checkbox" . ( $this->is_error() ? ' form-error' : '' ) . "'>\n";
  1075. $r .= "\t\t<input type='checkbox' name='" . esc_attr( $field_id ) . "' value='" . esc_attr__( 'Yes', 'jetpack' ) . "' class='checkbox' " . checked( (bool) $field_value, true, false ) . " /> \n";
  1076. $r .= "\t\t" . esc_html( $field_label ) . ( $field_required ? '<span>'. __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n";
  1077. $r .= "\t\t<div class='clear-form'></div>\n";
  1078. $r .= "\t</div>\n";
  1079. break;
  1080. case 'select' :
  1081. $r .= "\n<div>\n";
  1082. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label select" . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>'. __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n";
  1083. $r .= "\t<select name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' class='select' />\n";
  1084. foreach ( $this->get_attribute( 'options' ) as $option ) {
  1085. $option = Grunion_Contact_Form_Plugin::strip_tags( $option );
  1086. $r .= "\t\t<option" . selected( $option, $field_value, false ) . ">" . esc_html( $option ) . "</option>\n";
  1087. }
  1088. $r .= "\t</select>\n";
  1089. $r .= "\t</div>\n";
  1090. break;
  1091. default : // text field
  1092. // note that any unknown types will produce a text input, so we can use arbitrary type names to handle
  1093. // input fields like name, email, url that require special validation or handling at POST
  1094. $r .= "\n<div>\n";
  1095. $r .= "\t\t<label for='" . esc_attr( $field_id ) . "' class='grunion-field-label " . esc_attr( $field_type ) . ( $this->is_error() ? ' form-error' : '' ) . "'>" . esc_html( $field_label ) . ( $field_required ? '<span>' . __( "(required)", 'jetpack' ) . '</span>' : '' ) . "</label>\n";
  1096. $r .= "\t\t<input type='text' name='" . esc_attr( $field_id ) . "' id='" . esc_attr( $field_id ) . "' value='" . esc_attr( $field_value ) . "' class='" . esc_attr( $field_type ) . "'/>\n";
  1097. $r .= "\t</div>\n";
  1098. }
  1099. return $r;
  1100. }
  1101. }
  1102. add_action( 'init', array( 'Grunion_Contact_Form_Plugin', 'init' ) );
  1103. add_action( 'grunion_scheduled_delete', 'grunion_delete_old_spam' );
  1104. /**
  1105. * Deletes old spam feedbacks to keep the posts table size under control
  1106. */
  1107. function grunion_delete_old_spam() {
  1108. global $wpdb;
  1109. $grunion_delete_limit = 100;
  1110. $now_gmt = current_time( 'mysql', 1 );
  1111. $sql = $wpdb->prepare( "
  1112. SELECT `ID`
  1113. FROM $wpdb->posts
  1114. WHERE DATE_SUB( %s, INTERVAL 15 DAY ) > `post_date_gmt`
  1115. AND `post_type` = 'feedback'
  1116. AND `post_status` = 'spam'
  1117. LIMIT %d
  1118. ", $now_gmt, $grunion_delete_limit );
  1119. $post_ids = $wpdb->get_col( $sql );
  1120. foreach ( (array) $post_ids as $post_id ) {
  1121. # force a full delete, skip the trash
  1122. wp_delete_post( $post_id, TRUE );
  1123. }
  1124. # Arbitrary check points for running OPTIMIZE
  1125. # nothing special about 5000 or 11
  1126. # just trying to periodically recover deleted rows
  1127. $random_num = mt_rand( 1, 5000 );
  1128. if ( apply_filters( 'grunion_optimize_table', ( $random_number == 11 ) ) ) {
  1129. $wpdb->query( "OPTIMIZE TABLE $wpdb->posts" );
  1130. }
  1131. # if we hit the max then schedule another run
  1132. if ( count( $post_ids ) >= $grunion_delete_limit ) {
  1133. wp_schedule_single_event( time() + 700, 'grunion_scheduled_delete' );
  1134. }
  1135. }