Skip to content

Instantly share code, notes, and snippets.

@eduard-ungureanu
Last active November 10, 2023 14:35
Show Gist options
  • Save eduard-ungureanu/b15aabad355f3bdb9a15 to your computer and use it in GitHub Desktop.
Save eduard-ungureanu/b15aabad355f3bdb9a15 to your computer and use it in GitHub Desktop.
Custom Contact Form Module
<?php
/*Change the thank you message */
class WPC_ET_Builder_Module_Contact_Form extends ET_Builder_Module {
function init() {
$this->name = __( 'Contact Form', 'et_builder' );
$this->slug = 'et_pb_contact_form';
$this->whitelisted_fields = array(
'captcha',
'email',
'title',
'admin_label',
'module_id',
'module_class',
'form_background_color',
'input_border_radius',
);
$this->fields_defaults = array(
'captcha' => array( 'on' ),
);
$this->main_css_element = '%%order_class%%.et_pb_contact_form_container';
$this->advanced_options = array(
'fonts' => array(
'title' => array(
'label' => __( 'Title', 'et_builder' ),
'css' => array(
'main' => "{$this->main_css_element} h1",
),
),
'form_field' => array(
'label' => __( 'Form Field', 'et_builder' ),
'css' => array(
'main' => "{$this->main_css_element} .input",
),
),
),
'border' => array(
'css' => array(
'main' => "{$this->main_css_element} .input",
),
'settings' => array(
'color' => 'alpha',
),
),
'button' => array(
'button' => array(
'label' => __( 'Button', 'et_builder' ),
),
),
);
$this->custom_css_options = array(
'contact_title' => array(
'label' => __( 'Contact Title', 'et_builder' ),
'selector' => '.et_pb_contact_main_title',
),
'contact_button' => array(
'label' => __( 'Contact Button', 'et_builder' ),
'selector' => '.et_pb_contact_submit',
),
);
}
function get_fields() {
$fields = array(
'captcha' => array(
'label' => __( 'Display Captcha', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'on' => __( 'Yes', 'et_builder' ),
'off' => __( 'No', 'et_builder' ),
),
'description' => __( 'Turn the captcha on or off using this option.', 'et_builder' ),
),
'email' => array(
'label' => __( 'Email', 'et_builder' ),
'type' => 'text',
'option_category' => 'basic_option',
'description' => __( 'Input the email address where messages should be sent.', 'et_builder' ),
),
'title' => array(
'label' => __( 'Title', 'et_builder' ),
'type' => 'text',
'option_category' => 'basic_option',
'description' => __( 'Define a title for your contact form.', 'et_builder' ),
),
'admin_label' => array(
'label' => __( 'Admin Label', 'et_builder' ),
'type' => 'text',
'description' => __( 'This will change the label of the module in the builder for easy identification.', 'et_builder' ),
),
'module_id' => array(
'label' => __( 'CSS ID', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'description' => __( 'Enter an optional CSS ID to be used for this module. An ID can be used to create custom CSS styling, or to create links to particular sections of your page.', 'et_builder' ),
),
'module_class' => array(
'label' => __( 'CSS Class', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'description' => __( 'Enter optional CSS classes to be used for this module. A CSS class can be used to create custom CSS styling. You can add multiple classes, separated with a space.', 'et_builder' ),
),
'form_background_color' => array(
'label' => __( 'Form Background Color', 'et_builder' ),
'type' => 'color-alpha',
'custom_color' => true,
'tab_slug' => 'advanced',
),
'input_border_radius' => array(
'label' => __( 'Input Border Radius', 'et_builder' ),
'type' => 'range',
'option_category' => 'layout',
'tab_slug' => 'advanced',
),
);
return $fields;
}
function shortcode_callback( $atts, $content = null, $function_name ) {
$module_id = $this->shortcode_atts['module_id'];
$module_class = $this->shortcode_atts['module_class'];
$captcha = $this->shortcode_atts['captcha'];
$email = $this->shortcode_atts['email'];
$title = $this->shortcode_atts['title'];
$form_background_color = $this->shortcode_atts['form_background_color'];
$input_border_radius = $this->shortcode_atts['input_border_radius'];
$button_custom = $this->shortcode_atts['custom_button'];
$custom_icon = $this->shortcode_atts['button_icon'];
$module_class = ET_Builder_Element::add_module_order_class( $module_class, $function_name );
if ( '' !== $form_background_color ) {
ET_Builder_Element::set_style( $function_name, array(
'selector' => '%%order_class%% .input',
'declaration' => sprintf(
'background-color: %1$s;',
esc_html( $form_background_color )
),
) );
}
if ( ! in_array( $input_border_radius, array( '', '0' ) ) ) {
ET_Builder_Element::set_style( $function_name, array(
'selector' => '%%order_class%% .input',
'declaration' => sprintf(
'-moz-border-radius: %1$s; -webkit-border-radius: %1$s; border-radius: %1$s;',
esc_html( et_builder_process_range_value( $input_border_radius ) )
),
) );
}
$et_error_message = '';
$et_contact_error = false;
$contact_email = isset( $_POST['et_pb_contact_email'] ) ? sanitize_email( $_POST['et_pb_contact_email'] ) : '';
if ( isset( $_POST['et_pb_contactform_submit'] ) ) {
if ( 'on' === $captcha && ( ! isset( $_POST['et_pb_contact_captcha'] ) || empty( $_POST['et_pb_contact_captcha'] ) ) ) {
$et_error_message .= sprintf( '<p>%1$s</p>', esc_html__( 'Make sure you entered the captcha.', 'et_builder' ) );
$et_contact_error = true;
} else if ( 'on' === $captcha && ( $_POST['et_pb_contact_captcha'] <> ( $_SESSION['et_pb_first_digit'] + $_SESSION['et_pb_second_digit'] ) ) ) {
$et_error_message .= sprintf( '<p>%1$s</p>', esc_html__( 'You entered the wrong number in captcha.', 'et_builder' ) );
unset( $_SESSION['et_pb_first_digit'] );
unset( $_SESSION['et_pb_second_digit'] );
$et_contact_error = true;
} else if ( empty( $_POST['et_pb_contact_name'] ) || empty( $contact_email ) || empty( $_POST['et_pb_contact_message'] ) ) {
$et_error_message .= sprintf( '<p>%1$s</p>', esc_html__( 'Make sure you fill all fields.', 'et_builder' ) );
$et_contact_error = true;
}
if ( ! is_email( $contact_email ) ) {
$et_error_message .= sprintf( '<p>%1$s</p>', esc_html__( 'Invalid Email.', 'et_builder' ) );
$et_contact_error = true;
}
} else {
$et_contact_error = true;
if ( isset( $_SESSION['et_pb_first_digit'] ) )
unset( $_SESSION['et_pb_first_digit'] );
if ( isset( $_SESSION['et_pb_second_digit'] ) )
unset( $_SESSION['et_pb_second_digit'] );
}
if ( ! isset( $_SESSION['et_pb_first_digit'] ) )
$_SESSION['et_pb_first_digit'] = $et_pb_first_digit = rand(1, 15);
else
$et_pb_first_digit = $_SESSION['et_pb_first_digit'];
if ( ! isset( $_SESSION['et_pb_second_digit'] ) )
$_SESSION['et_pb_second_digit'] = $et_pb_second_digit = rand(1, 15);
else
$et_pb_second_digit = $_SESSION['et_pb_second_digit'];
if ( ! $et_contact_error && isset( $_POST['_wpnonce-et-pb-contact-form-submitted'] ) && wp_verify_nonce( $_POST['_wpnonce-et-pb-contact-form-submitted'], 'et-pb-contact-form-submit' ) ) {
$et_email_to = '' !== $email
? $email
: get_site_option( 'admin_email' );
$et_site_name = get_option( 'blogname' );
$contact_name = stripslashes( sanitize_text_field( $_POST['et_pb_contact_name'] ) );
$headers[] = "From: \"{$contact_name}\" <{$contact_email}>";
$headers[] = "Reply-To: <{$contact_email}>";
wp_mail( apply_filters( 'et_contact_page_email_to', $et_email_to ),
sprintf( __( 'New Message From %1$s%2$s', 'et_builder' ),
sanitize_text_field( $et_site_name ),
( '' !== $title ? sprintf( _x( ' - %s', 'contact form title separator', 'et_builder' ), sanitize_text_field( $title ) ) : '' )
), stripslashes( wp_strip_all_tags( $_POST['et_pb_contact_message'] ) ), apply_filters( 'et_contact_page_headers', $headers, $contact_name, $contact_email ) );
$et_error_message = sprintf( '<p>%1$s</p>', esc_html__( 'Thanks for contacting us', 'et_builder' ) );
}
$form = '';
$name_label = __( 'Name', 'et_builder' );
$email_label = __( 'Email Address', 'et_builder' );
$message_label = __( 'Message', 'et_builder' );
$et_pb_contact_form_num = $this->shortcode_callback_num();
$et_pb_captcha = sprintf( '
<div class="et_pb_contact_right">
<p class="clearfix">
<span class="et_pb_contact_captcha_question">%1$s</span> = <input type="text" size="2" class="input et_pb_contact_captcha" value="" name="et_pb_contact_captcha">
</p>
</div> <!-- .et_pb_contact_right -->',
sprintf( '%1$s + %2$s', esc_html( $et_pb_first_digit ), esc_html( $et_pb_second_digit ) )
);
if ( $et_contact_error )
$form = sprintf( '
<div class="et_pb_contact">
<div class="et-pb-contact-message">%11$s</div>
<form class="et_pb_contact_form clearfix" method="post" action="%1$s">
<div class="et_pb_contact_left">
<p class="clearfix">
<label for="et_pb_contact_name_%14$s" class="et_pb_contact_form_label">%2$s</label>
<input type="text" id="et_pb_contact_name_%14$s" class="input et_pb_contact_name" value="%3$s" name="et_pb_contact_name">
</p>
<p class="clearfix">
<label for="et_pb_contact_email_%14$s" class="et_pb_contact_form_label">%4$s</label>
<input type="text" id="et_pb_contact_email_%14$s" class="input et_pb_contact_email" value="%5$s" name="et_pb_contact_email">
</p>
</div> <!-- .et_pb_contact_left -->
<div class="clear"></div>
<p class="clearfix">
<label for="et_pb_contact_message_%14$s" class="et_pb_contact_form_label">%7$s</label>
<textarea name="et_pb_contact_message" id="et_pb_contact_message_%14$s" class="et_pb_contact_message input">%8$s</textarea>
</p>
<input type="hidden" value="et_contact_proccess" name="et_pb_contactform_submit">
<button type="submit" class="et_pb_contact_submit et_pb_button%13$s"%12$s>%9$s</button>
%6$s
%10$s
</form>
</div> <!-- .et_pb_contact -->',
esc_url( get_permalink( get_the_ID() ) ),
$name_label,
( isset( $_POST['et_pb_contact_name'] ) ? esc_attr( $_POST['et_pb_contact_name'] ) : $name_label ),
$email_label,
( isset( $_POST['et_pb_contact_email'] ) ? esc_attr( $_POST['et_pb_contact_email'] ) : $email_label ),
( 'on' === $captcha ? $et_pb_captcha : '' ),
$message_label,
( isset( $_POST['et_pb_contact_message'] ) ? esc_attr( $_POST['et_pb_contact_message'] ) : $message_label ),
__( 'Submit', 'et_builder' ),
wp_nonce_field( 'et-pb-contact-form-submit', '_wpnonce-et-pb-contact-form-submitted', true, false ),
$et_error_message,
'' !== $custom_icon && 'on' === $button_custom ? sprintf(
' data-icon="%1$s"',
esc_attr( et_pb_process_font_icon( $custom_icon ) )
) : '',
'' !== $custom_icon && 'on' === $button_custom ? ' et_pb_custom_button_icon' : '',
esc_attr( $et_pb_contact_form_num )
);
$output = sprintf( '
<div id="%4$s" class="et_pb_module et_pb_contact_form_container clearfix%5$s">
%1$s
%2$s
%3$s
</div> <!-- .et_pb_contact_form_container -->
',
( '' !== $title ? sprintf( '<h1 class="et_pb_contact_main_title">%1$s</h1>', esc_html( $title ) ) : '' ),
( '' !== $et_error_message ? sprintf( '<div class="et-pb-contact-message">%1$s</div>', $et_error_message ) : '' ),
$form,
( '' !== $module_id
? esc_attr( $module_id )
: esc_attr( 'et_pb_contact_form_' . $et_pb_contact_form_num )
),
( '' !== $module_class ? sprintf( ' %1$s', esc_attr( $module_class ) ) : '' )
);
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment