| 1 |
<?php |
| 2 |
namespace FlexMLS\Shortcodes; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) or die( 'This plugin requires WordPress' ); |
| 5 |
|
| 6 |
class LeadGeneration { |
| 7 |
|
| 8 |
public static function tinymce_form(){ |
| 9 |
$widget = new \FlexMLS\Widgets\LeadGeneration(); |
| 10 |
|
| 11 |
$response = array( |
| 12 |
'title' => 'Lead Generation / Contact Form', |
| 13 |
'body' => $widget->get_form( array( |
| 14 |
'is_tinymce' => 1 |
| 15 |
) ) |
| 16 |
); |
| 17 |
exit( json_encode( $response ) ); |
| 18 |
} |
| 19 |
|
| 20 |
public static function shortcode( $atts, $content = null ){ |
| 21 |
$atts = shortcode_atts( array( |
| 22 |
'title' => 'Lead Generation', |
| 23 |
'blurb' => '', |
| 24 |
'success' => 'Thank you for your request', |
| 25 |
'buttontext' => 'Submit', |
| 26 |
'use_captcha' => 0, |
| 27 |
'is_widget' => 0, |
| 28 |
'before_title' => '<h3>', |
| 29 |
'after_title' => '</h3>', |
| 30 |
'widget_id' => '' |
| 31 |
), $atts, 'lead_generation' ); |
| 32 |
|
| 33 |
global $wp; |
| 34 |
$fmc_settings = get_option( 'fmc_settings' ); |
| 35 |
if( empty( $atts[ 'title' ] ) && 1 == $fmc_settings[ 'default_titles' ] ){ |
| 36 |
$atts[ 'title' ] = 'Lead Generation'; |
| 37 |
} |
| 38 |
|
| 39 |
$random_string = uniqid( 'j' ); |
| 40 |
|
| 41 |
$html = ''; |
| 42 |
if( !empty( $atts[ 'title' ] ) ){ |
| 43 |
$html .= $atts[ 'before_title' ] . apply_filters( 'widget_title', $atts[ 'title' ] ) . $atts[ 'after_title' ]; |
| 44 |
} |
| 45 |
if( !empty( $atts[ 'blurb' ] ) ){ |
| 46 |
$html .= wpautop( $atts[ 'blurb' ] ); |
| 47 |
} |
| 48 |
$label_id_prefix = !empty( $atts[ 'widget_id' ] ) ? $atts[ 'widget_id' ] . '-' : ''; |
| 49 |
$html .= '<form id="' . $label_id_prefix . $random_string . '">'; |
| 50 |
$html .= ' <div class="flexmls_connect__form_row"> |
| 51 |
<label for="' . $label_id_prefix . 'name" class="flexmls_connect__form_label">Your Name</label> |
| 52 |
<input class="flexmls_connect__form_input" type="text" name="name" id="' . $label_id_prefix . 'name" placeholder="Your Name" required> |
| 53 |
</div>'; |
| 54 |
$html .= ' <div class="flexmls_connect__form_row"> |
| 55 |
<label for="' . $label_id_prefix . 'email" class="flexmls_connect__form_label">Email Address</label> |
| 56 |
<input class="flexmls_connect__form_input" type="email" name="email" id="' . $label_id_prefix . 'email" placeholder="Email Address" required> |
| 57 |
</div>'; |
| 58 |
|
| 59 |
$prefs = new \SparkAPI\Preferences(); |
| 60 |
$preferences = $prefs->get_preferences(); |
| 61 |
|
| 62 |
if( in_array( 'address', $preferences[ 'RequiredFields' ] ) ){ |
| 63 |
$html .= ' <div class="flexmls_connect__form_row"> |
| 64 |
<label for="' . $label_id_prefix . 'address" class="flexmls_connect__form_label">Home Address</label> |
| 65 |
<input class="flexmls_connect__form_input" type="text" name="address" id="' . $label_id_prefix . 'address" placeholder="Home Address" required> |
| 66 |
</div> |
| 67 |
<div class="flexmls_connect__form_row"> |
| 68 |
<label for="' . $label_id_prefix . 'city" class="flexmls_connect__form_label">City</label> |
| 69 |
<input class="flexmls_connect__form_input" type="text" name="city" id="' . $label_id_prefix . 'city" placeholder="City" required> |
| 70 |
</div> |
| 71 |
<div class="flexmls_connect__form_row"> |
| 72 |
<label for="' . $label_id_prefix . 'state" class="flexmls_connect__form_label">State</label> |
| 73 |
<input class="flexmls_connect__form_input" type="text" name="state" id="' . $label_id_prefix . 'state" placeholder="State" required> |
| 74 |
</div> |
| 75 |
<div class="flexmls_connect__form_row"> |
| 76 |
<label for="' . $label_id_prefix . 'zip" class="flexmls_connect__form_label">Zip Code</label> |
| 77 |
<input class="flexmls_connect__form_input" type="text" name="zip" id="' . $label_id_prefix . 'zip" placeholder="ZIP Code" required> |
| 78 |
</div>'; |
| 79 |
} |
| 80 |
if( in_array( 'phone', $preferences[ 'RequiredFields' ] ) ){ |
| 81 |
$html .= ' <div class="flexmls_connect__form_row"> |
| 82 |
<label for="' . $label_id_prefix . 'phone" class="flexmls_connect__form_label">Phone Number</label> |
| 83 |
<input class="flexmls_connect__form_input" type="tel" name="phone" id="' . $label_id_prefix . 'phone" placeholder="Phone Number" required> |
| 84 |
</div>'; |
| 85 |
} |
| 86 |
$html .= ' <div class="flexmls_connect__form_row"> |
| 87 |
<label for="' . $label_id_prefix . 'message" class="flexmls_connect__form_label">Your Message</label> |
| 88 |
<textarea class="flexmls_connect__form_textarea" name="message_body" id="' . $label_id_prefix . 'message" rows="5" placeholder="Your Message"></textarea> |
| 89 |
</div>'; |
| 90 |
$html .= ' <div class="flexmls_connect__form_row flexmls_connect__form_row_color"> |
| 91 |
<label for="' . $random_string . '">' . $random_string . '</label> |
| 92 |
<input type="text" name="color" id="' . $random_string . '" tabindex="-1"> |
| 93 |
</div>'; |
| 94 |
$html .= ' <input type="hidden" name="source" value="' . ( $wp->did_permalink ? home_url( add_query_arg( array(), $wp->request ) ) : home_url( '?' . $wp->query_string ) ) . '">'; |
| 95 |
$html .= ' <input type="hidden" name="success" value="' . esc_attr( $atts[ 'success' ] ) . '">'; |
| 96 |
$html .= ' <div class="flexmls_connect__form_footer"> |
| 97 |
<span class="flexmls_loading_svg" style="display: none;">' . \FlexMLS\Admin\Utilities::get_loading_svg() . '</span> |
| 98 |
<button class="flexmls_connect__form_submit flexmls_leadgen_button" type="button" data-form="#' . $label_id_prefix . $random_string . '">' . $atts[ 'buttontext' ] . '</button> |
| 99 |
</div>'; |
| 100 |
$html .= '</form>'; |
| 101 |
|
| 102 |
return $html; |
| 103 |
} |
| 104 |
|
| 105 |
public static function submit_lead(){ |
| 106 |
$result = array( |
| 107 |
'message' => '', |
| 108 |
'success' => 1 |
| 109 |
); |
| 110 |
if( !empty( $_POST[ 'honeypot' ] ) ){ |
| 111 |
// This is spam. Send a success message and call it quits. |
| 112 |
exit( json_encode( array( 'success' => 1 ) ) ); |
| 113 |
} |
| 114 |
$data = array( |
| 115 |
'DisplayName' => sanitize_text_field( $_POST[ 'name' ] ), |
| 116 |
'PrimaryEmail' => is_email( $_POST[ 'email' ] ), |
| 117 |
'SourceURL' => filter_var( $_POST[ 'source' ], FILTER_VALIDATE_URL ) |
| 118 |
); |
| 119 |
if( array_key_exists( 'address', $_POST ) ){ |
| 120 |
$data[ 'HomeStreetAddress' ] = sanitize_text_field( $_POST[ 'address' ] ); |
| 121 |
$data[ 'HomeLocality' ] = sanitize_text_field( $_POST[ 'city' ] ); |
| 122 |
$data[ 'HomeRegion' ] = sanitize_text_field( $_POST[ 'state' ] ); |
| 123 |
$data[ 'HomePostalCode' ] = sanitize_text_field( $_POST[ 'zip' ] ); |
| 124 |
} |
| 125 |
if( array_key_exists( 'phone', $_POST ) ){ |
| 126 |
$data[ 'PrimaryPhoneNumber' ] = sanitize_text_field( $_POST[ 'phone' ] ); |
| 127 |
} |
| 128 |
|
| 129 |
foreach( $data as $d ){ |
| 130 |
if( empty( $d ) || !$d ){ |
| 131 |
$result[ 'message' ] = 'All fields are required'; |
| 132 |
$result[ 'success' ] = 0; |
| 133 |
exit( json_encode( $result ) ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
$Contacts = new \SparkAPI\Contacts(); |
| 138 |
$contact = $Contacts->add_contact( $data ); |
| 139 |
|
| 140 |
$message_body = strip_tags( $_POST[ 'message_body' ] ); |
| 141 |
|
| 142 |
$subject = $data[ 'DisplayName' ] . ' would like you to contact them.'; |
| 143 |
|
| 144 |
$body = 'Message: ' . stripslashes( $message_body ) . PHP_EOL . PHP_EOL; |
| 145 |
$body .= 'Email: ' . $data[ 'PrimaryEmail' ] . PHP_EOL; |
| 146 |
if( array_key_exists( 'PrimaryPhoneNumber', $data ) ){ |
| 147 |
$body .= 'Phone: ' . $data[ 'PrimaryPhoneNumber' ] . PHP_EOL; |
| 148 |
} |
| 149 |
$body .= PHP_EOL; |
| 150 |
$body .= '(This message was generated by your WordPress FlexMLS(r) Contact Me Form on ' . $data[ 'SourceURL' ] . ')'; |
| 151 |
|
| 152 |
$message_me = $Contacts->message_me( $subject, $body, $data[ 'PrimaryEmail' ] ); |
| 153 |
exit( json_encode( $result ) ); |
| 154 |
} |
| 155 |
} |