Advertisement
Guest User

Untitled

a guest
Aug 1st, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 28.79 KB | None | 0 0
  1. <?php
  2. define( 'GRUNION_PLUGIN_DIR', TEMPLATEPATH . '/functions/contact-form/');
  3. define( 'GRUNION_PLUGIN_URL', get_template_directory_uri() . '/functions/contact-form/');
  4.  
  5. if ( is_admin() )
  6.     require_once GRUNION_PLUGIN_DIR . '/admin.php';
  7.  
  8. // take the content of a contact-form shortcode and parse it into a list of field types
  9. function contact_form_parse( $content ) {
  10.     // first parse all the contact-field shortcodes into an array
  11.     global $contact_form_fields, $grunion_form;
  12.     $contact_form_fields = array();
  13.  
  14.     if ( empty( $_REQUEST['action'] ) || $_REQUEST['action'] != 'grunion_shortcode_to_json' ) {
  15.             wp_print_styles( 'grunion.css' );
  16.     }
  17.    
  18.     $out = do_shortcode( $content );
  19.    
  20.     if ( empty($contact_form_fields) || !is_array($contact_form_fields) ) {
  21.         // default form: same as the original Grunion form
  22.         $default_form = '
  23.         [contact-field label="'.__('Name').'" type="name" required="true" /]
  24.         [contact-field label="'.__('Email').'" type="email" required="true" /]
  25.         [contact-field label="'.__('Website').'" type="url" /]';
  26.         if ( 'yes' == strtolower($grunion_form->show_subject) )
  27.             $default_form .= '
  28.             [contact-field label="'.__('Subject').'" type="subject" /]';
  29.         $default_form .= '
  30.         [contact-field label="'.__('Message').'" type="textarea" /]';
  31.  
  32.         $out = do_shortcode( $default_form );
  33.     }
  34.  
  35.     return $out;
  36. }
  37.  
  38. function contact_form_render_field( $field ) {
  39.     if (get_option_tree('contact_required')){
  40.     $contact_required = get_option_tree('contact_required');
  41.     } else {
  42.     $contact_required = '(required)';}
  43.    
  44.     global $contact_form_last_id, $contact_form_errors, $contact_form_fields, $current_user, $user_identity;
  45.    
  46.     $r = '';
  47.    
  48.     $field_id = $field['id'];
  49.     if ( isset($_POST[ $field_id ]) ) {
  50.         $field_value = stripslashes( $_POST[ $field_id ] );
  51.     } elseif ( is_user_logged_in() ) {
  52.         // Special defaults for logged-in users
  53.         if ( $field['type'] == 'email' )
  54.             $field_value = $current_user->data->user_email;
  55.         elseif ( $field['type'] == 'name' )
  56.             $field_value = $user_identity;
  57.         elseif ( $field['type'] == 'url' )
  58.             $field_value = $current_user->data->user_url;
  59.         else
  60.             $field_value = $field['default'];
  61.     } else {
  62.         $field_value = $field['default'];
  63.     }
  64.    
  65.     $field_value = wp_kses($field_value, array());
  66.  
  67.     $field['label'] = html_entity_decode( $field['label'] );
  68.     $field['label'] = wp_kses( $field['label'], array() );
  69.  
  70.     if ( $field['type'] == 'email' ) {
  71.         $r .= "\n<div>\n";
  72.         $r .= "\t\t<label for='".esc_attr($field_id)."' class='grunion-field-label ".esc_attr($field['type']) . ( contact_form_is_error($field_id) ? ' form-error' : '' ) . "'>" . htmlspecialchars( $field['label'] ) . ( $field['required'] ? '<span>'. __($contact_required) . '</span>' : '' ) . "</label>\n";
  73.         $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";
  74.         $r .= "\t</div>\n";
  75.     } elseif ( $field['type'] == 'textarea' ) {
  76.         $r .= "\n<div>\n";
  77.         $r .= "\t\t<label for='".esc_attr($field_id)."' class='".esc_attr($field['type']) . ( contact_form_is_error($field_id) ? ' form-error' : '' ) . "'>" . htmlspecialchars( $field['label'] ) . ( $field['required'] ? '<span>'. __($contact_required) . '</span>' : '' ) . "</label>\n";
  78.         $r .= "\t\t<textarea name='".esc_attr($field_id)."' id='contact-form-comment-".esc_attr($field_id)."' rows='20'>".htmlspecialchars($field_value)."</textarea>\n";
  79.         $r .= "\t</div>\n";
  80.     } elseif ( $field['type'] == 'radio' ) {
  81.         $r .= "\t<div><label class='". ( contact_form_is_error($field_id) ? ' form-error' : '' ) . "'>" . htmlspecialchars( $field['label'] ) . ( $field['required'] ? '<span>'. __($contact_required) . '</span>' : '' ) . "</label>\n";
  82.         foreach ( $field['options'] as $option ) {
  83.             $r .= "\t\t<input type='radio' name='".esc_attr($field_id)."' value='".esc_attr($option)."' class='".esc_attr($field['type'])."' ".( $option == $field_value ? "checked='checked' " : "")." />\n";
  84.             $r .= "\t\t<label class='".esc_attr($field['type']) . ( contact_form_is_error($field_id) ? ' form-error' : '' ) . "'>". htmlspecialchars( $option ) . "</label>\n";
  85.             $r .= "\t\t<div class='clear-form'></div>\n";
  86.         }
  87.         $r .= "\t\t</div>\n";
  88.     } elseif ( $field['type'] == 'checkbox' ) {
  89.         $r .= "\t<div>\n";
  90.         $r .= "\t\t<input type='checkbox' name='".esc_attr($field_id)."' value='".__('Yes')."' class='".esc_attr($field['type'])."' ".( $field_value ? "checked='checked' " : "")." />\n";
  91.         $r .= "\t\t<label class='".esc_attr($field['type']) . ( contact_form_is_error($field_id) ? ' form-error' : '' ) . "'>\n";
  92.         $r .= "\t\t". htmlspecialchars( $field['label'] ) . ( $field['required'] ? '<span>'. __($contact_required) . '</span>' : '' ) . "</label>\n";
  93.         $r .= "\t\t<div class='clear-form'></div>\n";
  94.         $r .= "\t</div>\n";
  95.     } elseif ( $field['type'] == 'select' ) {
  96.         $r .= "\n<div>\n";
  97.         $r .= "\t\t<label for='".esc_attr($field_id)."' class='".esc_attr($field['type']) . ( contact_form_is_error($field_id) ? ' form-error' : '' ) . "'>" . htmlspecialchars( $field['label'] ) . ( $field['required'] ? '<span>'. __($contact_required) . '</span>' : '' ) . "</label>\n";
  98.         $r .= "\t<select name='".esc_attr($field_id)."' id='".esc_attr($field_id)."' value='".esc_attr($field_value)."' class='".esc_attr($field['type'])."'/>\n";
  99.         foreach ( $field['options'] as $option ) {
  100.             $option = html_entity_decode( $option );
  101.             $option = wp_kses( $option, array() );
  102.             $r .= "\t\t<option".( $option == $field_value ? " selected='selected'" : "").">". esc_html( $option ) ."</option>\n";
  103.         }
  104.         $r .= "\t</select>\n";
  105.         $r .= "\t</div>\n";
  106.     } else {
  107.         // default: text field
  108.         // note that any unknown types will produce a text input, so we can use arbitrary type names to handle
  109.         // input fields like name, email, url that require special validation or handling at POST
  110.         $r .= "\n<div>\n";
  111.         $r .= "\t\t<label for='".esc_attr($field_id)."' class='".esc_attr($field['type']) . ( contact_form_is_error($field_id) ? ' form-error' : '' ) . "'>" . htmlspecialchars( $field['label'] ) . ( $field['required'] ? '<span>'. __($contact_required) . '</span>' : '' ) . "</label>\n";
  112.         $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";
  113.         $r .= "\t</div>\n";
  114.     }
  115.    
  116.     return $r;
  117. }
  118.  
  119. function contact_form_validate_field( $field ) {
  120.     global $contact_form_last_id, $contact_form_errors, $contact_form_values;
  121.  
  122.     $field_id = $field['id'];
  123.     $field_value = isset($_POST[ $field_id ]) ? stripslashes($_POST[ $field_id ]) : '';
  124.  
  125.     # pay special attention to required email fields
  126.     if ( $field['required'] && $field['type'] == 'email' ) {
  127.         if ( !is_email( $field_value ) ) {
  128.             if ( !is_wp_error( $contact_form_errors ) ) {
  129.                 $contact_form_errors = new WP_Error();
  130.             }
  131.  
  132.             $contact_form_errors->add( $field_id, sprintf( __( '%s requires a valid email address' ), $field['label'] ) );
  133.         }
  134.     } elseif ( $field['required'] && !trim($field_value) ) {
  135.         if ( !is_wp_error($contact_form_errors) ) {
  136.             $contact_form_errors = new WP_Error();
  137.         }
  138.         $contact_is_required = get_option_tree('contact_is_required');
  139.         if($contact_is_required){
  140.         $contact_form_errors->add( $field_id, sprintf( __('%s '.$contact_is_required), $field['label'] ) );
  141.         }else{
  142.         $contact_form_errors->add( $field_id, sprintf( __('%s is required'), $field['label'] ) );}
  143.     }
  144.    
  145.     $contact_form_values[ $field_id ] = $field_value;
  146. }
  147.  
  148. function contact_form_is_error( $field_id ) {
  149.     global $contact_form_errors;
  150.    
  151.     return ( is_wp_error( $contact_form_errors ) && $contact_form_errors->get_error_message( $field_id ) );
  152. }
  153.  
  154. // generic shortcode that handles all of the major input types
  155. // this parses the field attributes into an array that is used by other functions for rendering, validation etc
  156. function contact_form_field( $atts, $content, $tag ) {
  157.     global $contact_form_fields, $contact_form_last_id, $grunion_form;
  158.    
  159.     $field = shortcode_atts( array(
  160.         'label' => null,
  161.         'type' => 'text',
  162.         'required' => false,
  163.         'options' => array(),
  164.         'id' => null,
  165.         'default' => null,
  166.     ), $atts);
  167.    
  168.     // special default for subject field
  169.     if ( $field['type'] == 'subject' && is_null($field['default']) )
  170.         $field['default'] = $grunion_form->subject;
  171.    
  172.     // allow required=1 or required=true
  173.     if ( $field['required'] == '1' || strtolower($field['required']) == 'true' )
  174.         $field['required'] = true;
  175.     else
  176.         $field['required'] = false;
  177.        
  178.     // parse out comma-separated options list
  179.     if ( !empty($field['options']) && is_string($field['options']) )
  180.         $field['options'] = array_map('trim', explode(',', $field['options']));
  181.  
  182.     // make a unique field ID based on the label, with an incrementing number if needed to avoid clashes
  183.     $id = $field['id'];
  184.     if ( empty($id) ) {
  185.         $id = sanitize_title_with_dashes( $contact_form_last_id . '-' . $field['label'] );
  186.         $i = 0;
  187.         while ( isset( $contact_form_fields[ $id ] ) ) {
  188.             $i++;
  189.             $id = sanitize_title_with_dashes( $contact_form_last_id . '-' . $field['label'] . '-' . $i );
  190.         }
  191.         $field['id'] = $id;
  192.     }
  193.    
  194.     $contact_form_fields[ $id ] = $field;
  195.    
  196.     if ( $_POST )
  197.         contact_form_validate_field( $field );
  198.    
  199.     return contact_form_render_field( $field );
  200. }
  201.  
  202. add_shortcode('contact-field', 'contact_form_field');
  203.  
  204.  
  205. function contact_form_shortcode( $atts, $content ) {
  206.     global $post;
  207.  
  208.     $default_to = get_option( 'admin_email' );
  209.     $default_subject = "[" . get_option( 'blogname' ) . "]";
  210.  
  211.     if ( !empty( $atts['widget'] ) && $atts['widget'] ) {
  212.         $default_subject .=  " Sidebar";
  213.     } elseif ( $post->ID ) {
  214.         $default_subject .= " ". wp_kses( $post->post_title, array() );
  215.         $post_author = get_userdata( $post->post_author );
  216.         $default_to = $post_author->user_email;
  217.     }
  218.  
  219.     extract( shortcode_atts( array(
  220.         'to' => $default_to,
  221.         'subject' => $default_subject,
  222.         'show_subject' => 'no', // only used in back-compat mode
  223.         'widget' => 0 //This is not exposed to the user. Works with contact_form_widget_atts
  224.     ), $atts ) );
  225.  
  226.      $widget = esc_attr( $widget );
  227.  
  228.     if ( ( function_exists( 'faux_faux' ) && faux_faux() ) || is_feed() )
  229.         return '[contact-form]';
  230.  
  231.     global $wp_query, $grunion_form, $contact_form_errors, $contact_form_values, $user_identity, $contact_form_last_id, $contact_form_message;
  232.    
  233.     // used to store attributes, configuration etc for access by contact-field shortcodes
  234.     $grunion_form = new stdClass();
  235.     $grunion_form->to = $to;
  236.     $grunion_form->subject = $subject;
  237.     $grunion_form->show_subject = $show_subject;
  238.  
  239.     if ( $widget )
  240.         $id = 'widget-' . $widget;
  241.     elseif ( is_singular() )
  242.         $id = $wp_query->get_queried_object_id();
  243.     else
  244.         $id = $GLOBALS['post']->ID;
  245.     if ( !$id ) // something terrible has happened
  246.         return '[contact-form]';
  247.  
  248.     if ( $id == $contact_form_last_id )
  249.         return;
  250.     else
  251.         $contact_form_last_id = $id;
  252.  
  253.     ob_start();
  254.         wp_nonce_field( 'contact-form_' . $id );
  255.         $nonce = ob_get_contents();
  256.     ob_end_clean();
  257.  
  258.  
  259.     $body = contact_form_parse( $content );
  260.  
  261.     $r = "<div id='contact-form-$id'>\n";
  262.    
  263.     $errors = array();
  264.     if ( is_wp_error( $contact_form_errors ) && $errors = (array) $contact_form_errors->get_error_codes() ) {
  265.         $r .= "<div class='form-error'>\n<ul class='form-errors'>\n";
  266.         foreach ( $contact_form_errors->get_error_messages() as $message )
  267.             $r .= "\t<li class='form-error-message' style='color: red;'>$message</li>\n";
  268.         $r .= "</ul>\n<br />\n</div>\n\n";
  269.     }
  270.    
  271.     $contact_submit = get_option_tree ('contact_submit', '');
  272.     $r .= "<form action='#contact-form-$id' method='post' class='contact-form commentsblock'>\n";
  273.     $r .= $body;
  274.     $r .= "\t<p class='contact-submit'>\n";
  275.     if($contact_submit){
  276.     $r .= "\t\t<input type='submit' value='" . __( $contact_submit) . "' class='ka-form-submit'/>\n";}
  277.     else{
  278.     $r .= "\t\t<input type='submit' value='" . __( "Submit" ) . "' class='ka-form-submit'/>\n";}    $r .= "\t\t$nonce\n";
  279.     $r .= "\t\t<input type='hidden' name='contact-form-id' value='$id' />\n";
  280.     $r .= "\t</p>\n";
  281.     $r .= "</form>\n</div>";
  282.    
  283.     // form wasn't submitted, just a GET
  284.     if ( empty($_POST) )
  285.         return $r;
  286.  
  287.  
  288.     if ( is_wp_error($contact_form_errors) )
  289.         return $r;
  290.  
  291.    
  292.     $emails = str_replace( ' ', '', $to );
  293.     $emails = explode( ',', $emails );
  294.     foreach ( (array) $emails as $email ) {
  295.         if ( is_email( $email ) && ( !function_exists( 'is_email_address_unsafe' ) || !is_email_address_unsafe( $email ) ) )
  296.             $valid_emails[] = $email;
  297.     }
  298.  
  299.     $to = ( $valid_emails ) ? $valid_emails : $default_to;
  300.  
  301.     $message_sent = contact_form_send_message( $to, $subject, $widget );
  302.  
  303.     if ( is_array( $contact_form_values ) )
  304.         extract( $contact_form_values );
  305.  
  306.     if ( !isset( $comment_content ) )
  307.         $comment_content = '';
  308.     else
  309.         $comment_content = wp_kses( $comment_content, array() );
  310.  
  311.  
  312.     $r = "<div id='contact-form-$id'>\n";
  313.  
  314.     $errors = array();
  315.     if ( is_wp_error( $contact_form_errors ) && $errors = (array) $contact_form_errors->get_error_codes() ) :
  316.         $r .= "<div class='form-error'>\n<p>\n";
  317.         foreach ( $contact_form_errors->get_error_messages() as $message )
  318.             $r .= "\t$message<br />\n";
  319.         $r .= "</p>\n</div>\n\n";
  320.     else :
  321.         $contact_successmsg = get_option_tree('contact_successmsg');
  322.                
  323.         if($contact_successmsg){
  324.         $r .= "<h3>" . $contact_successmsg . "</h3>\n\n";}
  325.         else{
  326.         $r .= "<h3>" . __( 'Message Sent' ) . "</h3>\n\n";}
  327.  
  328.         $r .= wp_kses($contact_form_message, array('br' => array())) . "</div>";
  329.        
  330.         // Reset for multiple contact forms. Hacky
  331.         $contact_form_values['comment_content'] = '';
  332.  
  333.         return $r;
  334.     endif;
  335.  
  336.     return $r;
  337. }
  338. add_shortcode( 'contact-form', 'contact_form_shortcode' );
  339.  
  340. function contact_form_send_message( $to, $subject, $widget ) {
  341.     global $post;
  342.    
  343.     if ( !isset( $_POST['contact-form-id'] ) )
  344.         return;
  345.        
  346.     if ( ( $widget && 'widget-' . $widget != $_POST['contact-form-id'] ) || ( !$widget && $post->ID != $_POST['contact-form-id'] ) )
  347.         return;
  348.  
  349.     if ( $widget )
  350.         check_admin_referer( 'contact-form_widget-' . $widget );
  351.     else
  352.         check_admin_referer( 'contact-form_' . $post->ID );
  353.  
  354.     global $contact_form_values, $contact_form_errors, $current_user, $user_identity;
  355.     global $contact_form_fields, $contact_form_message;
  356.    
  357.     // compact the fields and values into an array of Label => Value pairs
  358.     // also find values for comment_author_email and other significant fields
  359.     $all_values = $extra_values = array();
  360.    
  361.     foreach ( $contact_form_fields as $id => $field ) {
  362.         if ( $field['type'] == 'email' && !isset( $comment_author_email ) ) {
  363.             $comment_author_email = $contact_form_values[ $id ];
  364.             $comment_author_email_label = $field['label'];
  365.         } elseif  ( $field['type'] == 'name' && !isset( $comment_author ) ) {
  366.             $comment_author = $contact_form_values[ $id ];
  367.             $comment_author_label = $field['label'];
  368.         } elseif ( $field['type'] == 'url' && !isset( $comment_author_url ) ) {
  369.             $comment_author_url = $contact_form_values[ $id ];
  370.             $comment_author_url_label = $field['label'];
  371.         } elseif ( $field['type'] == 'textarea' && !isset( $comment_content ) ) {
  372.             $comment_content = $contact_form_values[ $id ];
  373.             $comment_content_label = $field['label'];
  374.         } else {
  375.             $extra_values[ $field['label'] ] = $contact_form_values[ $id ];
  376.         }
  377.        
  378.         $all_values[ $field['label'] ] = $contact_form_values[ $id ];
  379.     }
  380.  
  381. /*
  382.     $contact_form_values = array();
  383.     $contact_form_errors = new WP_Error();
  384.  
  385.     list($comment_author, $comment_author_email, $comment_author_url) = is_user_logged_in() ?
  386.         add_magic_quotes( array( $user_identity, $current_user->data->user_email, $current_user->data->user_url ) ) :
  387.         array( $_POST['comment_author'], $_POST['comment_author_email'], $_POST['comment_author_url'] );
  388. */
  389.  
  390.     $comment_author = stripslashes( apply_filters( 'pre_comment_author_name', $comment_author ) );
  391.  
  392.     $comment_author_email = stripslashes( apply_filters( 'pre_comment_author_email', $comment_author_email ) );
  393.  
  394.     $comment_author_url = stripslashes( apply_filters( 'pre_comment_author_url', $comment_author_url ) );
  395.     if ( 'http://' == $comment_author_url )
  396.         $comment_author_url = '';
  397.  
  398.     $comment_content = stripslashes( $comment_content );
  399.     $comment_content = trim( wp_kses( $comment_content, array() ) );
  400.  
  401.     if ( empty( $contact_form_subject ) )
  402.         $contact_form_subject = $subject;
  403.     else
  404.         $contact_form_subject = trim( wp_kses( $contact_form_subject, array() ) );
  405.        
  406.     $comment_author_IP = $_SERVER['REMOTE_ADDR'];
  407.  
  408.     $vars = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'contact_form_subject', 'comment_author_IP' );
  409.     foreach ( $vars as $var )
  410.         $$var = str_replace( array("\n", "\r" ), '', $$var ); // I don't know if it's possible to inject this
  411.     $vars[] = 'comment_content';
  412.  
  413.     $contact_form_values = compact( $vars );
  414.  
  415.     $spam = '';
  416.     $akismet_values = contact_form_prepare_for_akismet( $contact_form_values );
  417.     $is_spam = apply_filters( 'contact_form_is_spam', $akismet_values );
  418.     if ( is_wp_error( $is_spam ) )
  419.         return; // abort
  420.     else if ( $is_spam === TRUE )
  421.         $spam = '***SPAM*** ';
  422.  
  423.     if ( !$comment_author )
  424.         $comment_author = $comment_author_email;
  425.        
  426.     $headers = 'From: ' . wp_kses( $comment_author, array() ) .
  427.         ' <' . wp_kses( $comment_author_email, array() ) . ">\r\n" .
  428.         'Reply-To: ' . wp_kses( $comment_author_email, array() ) . "\r\n" .
  429.         "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"";
  430.     $subject = apply_filters( 'contact_form_subject', $spam . $contact_form_subject );
  431.     $subject = wp_kses( $subject, array() );
  432.  
  433.     $time = date_i18n( __('l F j, Y \a\t g:i a'), current_time( 'timestamp' ) );
  434.    
  435.     $extra_content = '';
  436.    
  437.     foreach ( $extra_values as $label => $value ) {
  438.         $extra_content .= $label . ': ' . trim($value) . "\n";
  439.         $extra_content_br .= wp_kses( $label, array() ) . ': ' . wp_kses( trim($value), array() ) . "<br />";
  440.     }
  441.  
  442.     $message = $comment_author_label . ": " . $comment_author . "
  443. " . $comment_author_email_label . ": " . $comment_author_email . "
  444. " . $comment_author_url_label . ": " . $comment_author_url . "
  445. " . $comment_content_label . ": " . $comment_content . "
  446. $extra_content
  447.  
  448. " . __( "Time:" ) . " " . $time . "
  449. " . __( "IP Address:" ) . " " . $comment_author_IP . "
  450. " . __( "Contact Form URL:" ) . " " . get_permalink( $post->ID ) . "
  451.  
  452. ";
  453.  
  454.     // Construct message that is returned to user
  455.     $contact_form_message = "<blockquote>";
  456.     if (isset($comment_author_label))
  457.         $contact_form_message .= wp_kses( $comment_author_label, array() ) . ": " . wp_kses( $comment_author, array() ) . "<br />";
  458.     if (isset($comment_author_email_label))
  459.         $contact_form_message .= wp_kses( $comment_author_email_label, array() ) . ": " . wp_kses( $comment_author_email, array() ) . "<br />";
  460.     if (isset($comment_author_url_label))
  461.         $contact_form_message .= wp_kses( $comment_author_url_label, array() ) . ": " . wp_kses( $comment_author_url, array() ) . "<br />";
  462.     if (isset($comment_content_label))
  463.         $contact_form_message .= wp_kses( $comment_content_label, array() ) . ": " . wp_kses( $comment_content, array() ) . "<br />";
  464.     if (isset($extra_content_br))
  465.         $contact_form_message .= $extra_content_br;
  466.     $contact_form_message .= "</blockquote><br /><br />";
  467.  
  468.     if ( is_user_logged_in() ) {
  469.         $message .= sprintf(
  470.             __( "\nSent by a verified %s user." ),
  471.             isset( $GLOBALS['current_site']->site_name ) && $GLOBALS['current_site']->site_name ? $GLOBALS['current_site']->site_name : '"' . get_option( 'blogname' ) . '"'
  472.         );
  473.     } else {
  474.         $message .= __( "Sent by an unverified visitor to your site." );
  475.     }
  476.  
  477.     $message = apply_filters( 'contact_form_message', $message );
  478.     $message = wp_kses( $message, array() );
  479.  
  480.     $to = apply_filters( 'contact_form_to', $to );
  481.  
  482.     foreach ( (array) $to as $to_key => $to_value ) {
  483.         $to[$to_key] = wp_kses( $to_value, array() );
  484.     }
  485.  
  486.     // keep a copy of the feedback as a custom post type
  487.     $feedback_mysql_time = current_time( 'mysql' );
  488.     $feedback_title = "{$comment_author} - {$feedback_mysql_time}";
  489.     $feedback_status = 'publish';
  490.     if ( $is_spam === TRUE )
  491.         $feedback_status = 'spam';
  492.  
  493.     foreach ( (array) $akismet_values as $av_key => $av_value ) {
  494.         $akismet_values[$av_key] = wp_kses( $av_value, array() );
  495.     }
  496.  
  497.     foreach ( (array) $all_values as $all_key => $all_value ) {
  498.         $all_values[$all_key] = wp_kses( $all_value, array() );
  499.     }
  500.  
  501.     foreach ( (array) $extra_values as $ev_key => $ev_value ) {
  502.         $ev_values[$ev_key] = wp_kses( $ev_value, array() );
  503.     }
  504.  
  505.     # We need to make sure that the post author is always zero for contact
  506.     # form submissions.  This prevents export/import from trying to create
  507.     # new users based on form submissions from people who were logged in
  508.     # at the time.
  509.     #
  510.     # Unfortunately wp_insert_post() tries very hard to make sure the post
  511.     # author gets the currently logged in user id.  That is how we ended up
  512.     # with this work around.
  513.     global $do_grunion_insert;
  514.     $do_grunion_insert = TRUE;
  515.     add_filter( 'wp_insert_post_data', 'grunion_insert_filter', 10, 2 );
  516.  
  517.     $post_id = wp_insert_post( array(
  518.         'post_date'     => $feedback_mysql_time,
  519.         'post_type'     => 'feedback',
  520.         'post_status'   => $feedback_status,
  521.         'post_parent'   => $post->ID,
  522.         'post_title'    => wp_kses( $feedback_title, array() ),
  523.         'post_content'  => 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
  524.         'post_name'     => md5( $feedback_title )
  525.     ) );
  526.  
  527.     # once insert has finished we don't need this filter any more
  528.     remove_filter( 'wp_insert_post_data', 'grunion_insert_filter' );
  529.     $do_grunion_insert = FALSE;
  530.  
  531.     update_post_meta( $post_id, '_feedback_author', wp_kses( $comment_author, array() ) );
  532.     update_post_meta( $post_id, '_feedback_author_email', wp_kses( $comment_author_email, array() ) );
  533.     update_post_meta( $post_id, '_feedback_author_url', wp_kses( $comment_author_url, array() ) );
  534.     update_post_meta( $post_id, '_feedback_subject', wp_kses( $contact_form_subject, array() ) );
  535.     update_post_meta( $post_id, '_feedback_ip', wp_kses( $comment_author_IP, array() ) );
  536.     update_post_meta( $post_id, '_feedback_contact_form_url', wp_kses( get_permalink( $post->ID ), array() ) );
  537.     update_post_meta( $post_id, '_feedback_all_fields', $all_values );
  538.     update_post_meta( $post_id, '_feedback_extra_fields', $extra_values );
  539.     update_post_meta( $post_id, '_feedback_akismet_values', $akismet_values );
  540.     update_post_meta( $post_id, '_feedback_email', array( 'to' => $to, 'subject' => $subject, 'message' => $message, 'headers' => $headers ) );
  541.  
  542.     do_action( 'grunion_pre_message_sent', $post_id, $all_values, $extra_values );
  543.  
  544.     if ( $is_spam !== TRUE )
  545.         return wp_mail( $to, $subject, $message, $headers );
  546.     elseif ( apply_filters( 'grunion_still_email_spam', FALSE ) == TRUE )
  547.         return wp_mail( $to, $subject, $message, $headers );
  548.  
  549.     return true;
  550. }
  551.  
  552. // populate an array with all values necessary to submit a NEW comment to Akismet
  553. // note that this includes the current user_ip etc, so this should only be called when accepting a new item via $_POST
  554. function contact_form_prepare_for_akismet( $form ) {
  555.  
  556.     $form['comment_type'] = 'contact_form';
  557.     $form['user_ip']      = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
  558.     $form['user_agent']   = $_SERVER['HTTP_USER_AGENT'];
  559.     $form['referrer']     = $_SERVER['HTTP_REFERER'];
  560.     $form['blog']         = get_option( 'home' );
  561.  
  562.     $ignore = array( 'HTTP_COOKIE' );
  563.  
  564.     foreach ( $_SERVER as $k => $value )
  565.         if ( !in_array( $k, $ignore ) && is_string( $value ) )
  566.             $form["$k"] = $value;
  567.            
  568.     return $form;
  569. }
  570.  
  571. // submit an array to Akismet. If you're accepting a new item via $_POST, run it through contact_form_prepare_for_akismet() first
  572. function contact_form_is_spam_akismet( $form ) {
  573.     if ( !function_exists( 'akismet_http_post' ) )
  574.         return false;
  575.        
  576.     global $akismet_api_host, $akismet_api_port;
  577.  
  578.     $query_string = '';
  579.     foreach ( array_keys( $form ) as $k )
  580.         $query_string .= $k . '=' . urlencode( $form[$k] ) . '&';
  581.  
  582.     $response = akismet_http_post( $query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
  583.     $result = false;
  584.     if ( 'true' == trim( $response[1] ) ) // 'true' is spam
  585.         $result = true;
  586.     return apply_filters( 'contact_form_is_spam_akismet', $result, $form );
  587. }
  588.  
  589. // submit a comment as either spam or ham
  590. // $as should be a string (either 'spam' or 'ham'), $form should be the comment array
  591. function contact_form_akismet_submit( $as, $form ) {
  592.     global $akismet_api_host, $akismet_api_port;
  593.    
  594.     if ( !in_array( $as, array( 'ham', 'spam' ) ) )
  595.         return false;
  596.  
  597.     $query_string = '';
  598.     foreach ( array_keys( $form ) as $k )
  599.         $query_string .= $k . '=' . urlencode( $form[$k] ) . '&';
  600.  
  601.     $response = akismet_http_post( $query_string, $akismet_api_host, '/1.1/submit-'.$as, $akismet_api_port );
  602.     return trim( $response[1] );
  603. }
  604.  
  605. function contact_form_widget_atts( $text ) {
  606.     static $widget = 0;
  607.    
  608.     $widget++;
  609.  
  610.     return str_replace( '[contact-form', '[contact-form widget="' . $widget . '"', $text );
  611. }
  612. add_filter( 'widget_text', 'contact_form_widget_atts', 0 );
  613.  
  614. function contact_form_widget_shortcode_hack( $text ) {
  615.     $old = $GLOBALS['shortcode_tags'];
  616.     //remove_all_shortcodes();
  617.     add_shortcode( 'contact-form', 'contact_form_shortcode' );
  618.     $text = do_shortcode( $text );
  619.     $GLOBALS['shortcode_tags'] = $old;
  620.     return $text;
  621. }
  622.  
  623. function contact_form_init() {
  624.     if ( function_exists( 'akismet_http_post' ) ) {
  625.         add_filter( 'contact_form_is_spam', 'contact_form_is_spam_akismet', 10 );
  626.         add_action( 'contact_form_akismet', 'contact_form_akismet_submit', 10, 2 );
  627.     }
  628.     if ( !has_filter( 'widget_text', 'do_shortcode' ) )
  629.         add_filter( 'widget_text', 'contact_form_widget_shortcode_hack', 5 );
  630.  
  631.     // custom post type we'll use to keep copies of the feedback items
  632.     register_post_type( 'feedback', array(
  633.         'labels'    => array(
  634.             'name'          => __( 'Messages' ),
  635.             'singular_name' => __( 'Message' ),
  636.             'search_items'  => __( 'Search Messages' ),
  637.             'not_found'     => __( 'No messages found' ),
  638.             'not_found_in_trash'    => __( 'No message found' )
  639.         ),
  640.         'menu_icon'     => GRUNION_PLUGIN_URL . '/images/grunion-menu.png',
  641.         'show_ui'       => TRUE,
  642.         'public'        => FALSE,
  643.         'rewrite'       => FALSE,
  644.         'query_var'     => FALSE,
  645.         'capability_type'   => 'page'
  646.     ) );
  647.  
  648.     register_post_status( 'spam', array(
  649.         'label'         => 'Spam',
  650.         'public'        => FALSE,
  651.         'exclude_from_search'   => TRUE,
  652.         'show_in_admin_all_list'=> FALSE,
  653.         'label_count' => _n_noop( 'Spam <span class="count">(%s)</span>', 'Spam <span class="count">(%s)</span>' ),
  654.         'protected'     => TRUE,
  655.         '_builtin'      => FALSE
  656.     ) );
  657.    
  658.     /* Can be dequeued by placing the following in wp-content/themes/yourtheme/functions.php
  659.      *
  660.      *  function remove_grunion_style() {
  661.      *      wp_deregister_style('grunion.css');
  662.      *  }
  663.      *  add_action('wp_print_styles', 'remove_grunion_style');
  664.      */
  665.    
  666.     wp_register_style('grunion.css', GRUNION_PLUGIN_URL . 'css/grunion.css');
  667. }
  668. add_action( 'init', 'contact_form_init' );
  669.  
  670. /**
  671.  * Add a contact form button to the post composition screen
  672.  */
  673. add_action( 'media_buttons', 'grunion_media_button', 999 );
  674. function grunion_media_button( ) {
  675.     global $post_ID, $temp_ID;
  676.     $iframe_post_id = (int) (0 == $post_ID ? $temp_ID : $post_ID);
  677.     $title = esc_attr( __( 'Add a custom form' ) );
  678.     $plugin_url = esc_url( GRUNION_PLUGIN_URL );
  679.     $site_url = admin_url( "/admin-ajax.php?post_id=$iframe_post_id&amp;grunion=form-builder&amp;action=grunion_form_builder&amp;TB_iframe=true&amp;width=768" );
  680.  
  681.     echo '<a href="' . $site_url . '&id=add_form" class="thickbox" title="' . $title . '"><img src="' . $plugin_url . '/images/grunion-form.png" alt="' . $title . '" width="13" height="12" /></a>';
  682. }
  683.  
  684.  
  685. if ( !empty( $_GET['grunion'] ) && $_GET['grunion'] == 'form-builder' ) {
  686.     add_action( 'parse_request', 'parse_wp_request' );
  687.     add_action( 'wp_ajax_grunion_form_builder', 'parse_wp_request' );
  688. }
  689.  
  690. function parse_wp_request( $wp ) {
  691.     display_form_view( );
  692.     exit;
  693. }
  694.  
  695. function display_form_view( ) {
  696.     require_once GRUNION_PLUGIN_DIR . 'grunion-form-view.php';
  697. }
  698.  
  699. function menu_alter() {
  700.     echo '
  701.     <style>
  702.     #menu-posts-feedback .wp-menu-image img { display: none; }
  703.     #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 6px 7px !important; }
  704.     #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 6px 7px !important; }
  705.     </style>';
  706. }
  707.  
  708. add_action('admin_head', 'menu_alter');
  709.  
  710. function grunion_insert_filter( $data, $postarr ) {
  711.     global $do_grunion_insert;
  712.  
  713.     if ( $do_grunion_insert === TRUE ) {
  714.         if ( $data['post_type'] == 'feedback' ) {
  715.             if ( $postarr['post_type'] == 'feedback' ) {
  716.                 $data['post_author'] = 0;
  717.             }
  718.         }
  719.     }
  720.  
  721.     return $data;
  722. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement