/** * @param MC4WP_Form_Request $request * * @return bool */ public function process(MC4WP_Form_Request $request) { $valid = $request->validate(); $success = false; if ($valid) { // prepare request data $ready = $request->prepare(); // if request is ready, send an API call to MailChimp if ($ready) { $success = $request->process(); } } $request->respond(); return $success; }
/** * Returns the HTML for success or error messages * * @return string */ private function get_form_message_html( $form_id = 0 ) { // don't show message if form wasn't submitted if( ! is_object( $this->form_request ) ) { return ''; } // get all form messages $messages = $this->get_form_messages( $form_id ); // retrieve correct message $type = ( $this->form_request->is_successful() ) ? 'success' : $this->form_request->get_error_code(); $message = ( isset( $messages[ $type ] ) ) ? $messages[ $type ] : $messages['error']; /** * @filter mc4wp_form_error_message * @deprecated 2.0.5 * @use mc4wp_form_messages * * Used to alter the error message, don't use. Use `mc4wp_form_messages` instead. */ $message['text'] = apply_filters('mc4wp_form_error_message', $message['text'], $this->form_request->get_error_code() ); $html = '<div class="mc4wp-alert mc4wp-'. $message['type'].'">' . $message['text'] . '</div>'; // show additional MailChimp API errors to administrators if( false === $this->form_request->is_successful() && current_user_can( 'manage_options' ) ) { // show MailChimp error message (if any) to administrators $api = mc4wp_get_api(); if( $api->has_error() ) { $html .= '<div class="mc4wp-alert mc4wp-error"><strong>Admin notice:</strong> '. $api->get_error_message() . '</div>'; } } return $html; }
/** * Outputs a form with the given ID * * @param array $atts * @param string $content * @return string */ public function output_form($atts = array(), $content = '') { // include the necessary functions file if (!function_exists('mc4wp_replace_variables')) { include_once MC4WP_PLUGIN_DIR . 'includes/functions/template.php'; } // try to get default form ID if it wasn't specified in the shortcode atts if (false === isset($atts['id'])) { // try to get default form id $atts['id'] = get_option('mc4wp_default_form_id', false); if (false === $atts['id']) { if (current_user_can('manage_options')) { return '<p>' . sprintf(__('<strong>Error:</strong> Please specify a form ID. Example: %s.', 'mailchimp-for-wp'), '<code>[mc4wp_form id="321"]</code>') . '</p>'; } return ''; } } // Get the form with the specified ID $form = $this->get_form($atts['id']); // did we find a valid form with this ID? if (!$form) { if (current_user_can('manage_options')) { return '<p>' . __('<strong>Error:</strong> Sign-up form not found. Please check if you used the correct form ID.', 'mailchimp-for-wp') . '</p>'; } return ''; } // was this form submitted? $was_submitted = is_object($this->form_request) && $this->form_request->get_form_instance_number() === $this->form_instance_number; $opts = mc4wp_get_form_settings($form->ID, true); // add some useful css classes $css_classes = $this->get_form_css_classes($form->ID); // Start building content string $opening_html = '<!-- MailChimp for WordPress Pro v' . MC4WP_VERSION . ' - https://mc4wp.com/ -->'; $opening_html .= '<div id="mc4wp-form-' . $this->form_instance_number . '" class="' . $css_classes . '">'; // Generate before & after fields HTML $before_form = apply_filters('mc4wp_form_before_form', ''); $after_form = apply_filters('mc4wp_form_after_form', ''); $form_opening_html = ''; $form_closing_html = ''; $visible_fields = ''; $hidden_fields = ''; $before_fields = apply_filters('mc4wp_form_before_fields', ''); $after_fields = apply_filters('mc4wp_form_after_fields', ''); // only generate form & fields HTML if necessary if (!$was_submitted || !$opts['hide_after_success'] || !$this->form_request->is_successful()) { $form_opening_html = '<form method="post">'; $visible_fields = $this->get_visible_form_fields($form, $opts); // make sure to print date fallback later on if form contains a date field if ($this->form_contains_field_type($visible_fields, 'date')) { $this->print_date_fallback = true; } $hidden_fields = $this->get_hidden_form_fields($form); $form_closing_html = '</form>'; } // empty string for response $response_html = ''; // does this form have AJAX enabled? if ($opts['ajax']) { // load ajax scripts (in footer) $this->load_ajax_scripts(); // set placeholder div for ajax response $response_html .= '<div class="mc4wp-response"></div>'; // Add AJAX loader span to output $hidden_fields .= '<span class="mc4wp-ajax-loader" style="display: none;"></span>'; } // was form submited? if ($was_submitted) { // enqueue scripts (in footer) if form was submited wp_enqueue_script('mc4wp-form-request'); wp_localize_script('mc4wp-form-request', 'mc4wpFormRequestData', array('success' => $this->form_request->is_successful() ? 1 : 0, 'formId' => $this->form_request->get_form_instance_number(), 'data' => $this->form_request->get_data())); // set response html $response_html .= $this->form_request->get_response_html(); } // add form response to content, if no {response} tag present if ('' !== $response_html && (stristr($visible_fields, '{response}') === false || $opts['hide_after_success'])) { /** * @filter mc4wp_form_message_position * @expects string before|after * * Can be used to change the position of the form success & error messages. * Valid options are 'before' or 'after' */ $message_position = apply_filters('mc4wp_form_message_position', 'after'); switch ($message_position) { case 'before': $before_form = $before_form . $response_html; break; case 'after': $after_form = $response_html . $after_form; break; } // reset response html, we only need it once $response_html = ''; } // Always replace {response} tag, either with empty string or actual response $visible_fields = str_ireplace('{response}', $response_html, $visible_fields); $closing_html = '</div><!-- / MailChimp for WP Pro Plugin -->'; // increase form instance number in case there is more than one form on a page $this->form_instance_number++; // make sure scripts are enqueued later global $is_IE; if (isset($is_IE) && $is_IE) { wp_enqueue_script('mc4wp-placeholders'); } // Print small JS snippet later on in the footer. add_action('wp_footer', array($this, 'print_js')); ob_start(); // echo HTML parts of form echo $opening_html; echo $before_form; echo $form_opening_html; echo $before_fields; echo $visible_fields; echo $hidden_fields; echo $after_fields; echo $form_closing_html; echo $after_form; echo $closing_html; // print css to hide honeypot, if not printed already $this->print_css(); $output = ob_get_contents(); ob_end_clean(); return $output; }
/** * Returns the MailChimp for WP form mark-up * * @param array $atts * @param string $content * * @return string */ public function form($atts = array(), $content = '') { // make sure template functions are loaded if (!function_exists('mc4wp_replace_variables')) { include_once MC4WP_LITE_PLUGIN_DIR . 'includes/functions/template.php'; } // Get form options $opts = mc4wp_get_options('form'); // was this form submitted? $was_submitted = is_object($this->form_request) && $this->form_request->get_form_instance_number() === $this->form_instance_number; /** * @filter mc4wp_form_action * @expects string * * Sets the `action` attribute of the form element. Defaults to the current URL. */ $form_action = apply_filters('mc4wp_form_action', mc4wp_get_current_url()); // Generate opening HTML $opening_html = "<!-- Form by MailChimp for WordPress plugin v" . MC4WP_LITE_VERSION . " - https://mc4wp.com/ -->"; $opening_html .= '<form method="post" action="' . $form_action . '" id="mc4wp-form-' . $this->form_instance_number . '" class="' . $this->get_css_classes() . '">'; // Generate before & after fields HTML $before_fields = apply_filters('mc4wp_form_before_fields', ''); $after_fields = apply_filters('mc4wp_form_after_fields', ''); // Process fields, if not submitted or not successfull or hide_after_success disabled if (!$was_submitted || !$opts['hide_after_success'] || !$this->form_request->is_successful()) { // add form fields from settings $visible_fields = __($opts['markup'], 'mailchimp-for-wp'); // replace special values $visible_fields = str_ireplace(array('%N%', '{n}'), $this->form_instance_number, $visible_fields); $visible_fields = mc4wp_replace_variables($visible_fields, array_values($opts['lists'])); // insert captcha if (function_exists('cptch_display_captcha_custom')) { $captcha_fields = '<input type="hidden" name="_mc4wp_has_captcha" value="1" /><input type="hidden" name="cntctfrm_contact_action" value="true" />' . cptch_display_captcha_custom(); $visible_fields = str_ireplace(array('{captcha}', '[captcha]'), $captcha_fields, $visible_fields); } /** * @filter mc4wp_form_content * @param int $form_id The ID of the form that is being shown * @expects string * * Can be used to customize the content of the form mark-up, eg adding additional fields. */ $visible_fields = apply_filters('mc4wp_form_content', $visible_fields); // hidden fields $hidden_fields = '<textarea name="_mc4wp_required_but_not_really" style="display: none !important;"></textarea>'; $hidden_fields .= '<input type="hidden" name="_mc4wp_form_submit" value="1" />'; $hidden_fields .= '<input type="hidden" name="_mc4wp_form_instance" value="' . $this->form_instance_number . '" />'; $hidden_fields .= '<input type="hidden" name="_mc4wp_form_nonce" value="' . wp_create_nonce('_mc4wp_form_nonce') . '" />'; } else { $visible_fields = ''; $hidden_fields = ''; } // empty string for response $response_html = ''; if ($was_submitted) { // Enqueue script (only after submit) wp_enqueue_script('mc4wp-form-request'); wp_localize_script('mc4wp-form-request', 'mc4wpFormRequestData', array('success' => $this->form_request->is_successful() ? 1 : 0, 'submittedFormId' => $this->form_request->get_form_instance_number(), 'postData' => $this->form_request->get_data())); // get actual response html $response_html = $this->form_request->get_response_html(); // add form response after or before fields if no {response} tag if (stristr($visible_fields, '{response}') === false || $opts['hide_after_success']) { /** * @filter mc4wp_form_message_position * @expects string before|after * * Can be used to change the position of the form success & error messages. * Valid options are 'before' or 'after' */ $message_position = apply_filters('mc4wp_form_message_position', 'after'); switch ($message_position) { case 'before': $before_fields = $before_fields . $response_html; break; case 'after': $after_fields = $response_html . $after_fields; break; } } } // Always replace {response} tag, either with empty string or actual response $visible_fields = str_ireplace('{response}', $response_html, $visible_fields); // Generate closing HTML $closing_html = "</form>"; $closing_html .= "<!-- / MailChimp for WP Plugin -->"; // increase form instance number in case there is more than one form on a page $this->form_instance_number++; // make sure scripts are enqueued later global $is_IE; if (isset($is_IE) && $is_IE) { wp_enqueue_script('mc4wp-placeholders'); } // Print small JS snippet later on in the footer. add_action('wp_footer', array($this, 'print_js')); // concatenate and return the HTML parts return $opening_html . $before_fields . $visible_fields . $hidden_fields . $after_fields . $closing_html; }
protected function get_response_html($element_id) { $html = $this->is_submitted($element_id) ? $this->request->get_response_html() : ''; return apply_filters('mc4wp_form_response_html', $html, $this); }
/** * Returns the MailChimp for WP form mark-up * * @param array $atts * @param string $content * * @return string */ public function output_form($atts = array(), $content = '') { // make sure template functions are loaded if (!function_exists('mc4wp_replace_variables')) { include_once MC4WP_LITE_PLUGIN_DIR . 'includes/functions/template.php'; } // was this form submitted? $was_submitted = is_object($this->form_request) && $this->form_request->get_form_instance_number() === $this->form_instance_number; // Generate opening HTML $opening_html = '<!-- Form by MailChimp for WordPress plugin v' . MC4WP_LITE_VERSION . ' - https://mc4wp.com/ -->'; $opening_html .= '<div id="mc4wp-form-' . $this->form_instance_number . '" class="' . $this->get_css_classes() . '">'; // Generate before & after fields HTML $before_form = apply_filters('mc4wp_form_before_form', ''); $after_form = apply_filters('mc4wp_form_after_form', ''); $form_opening_html = ''; $form_closing_html = ''; $visible_fields = ''; $hidden_fields = ''; $before_fields = apply_filters('mc4wp_form_before_fields', ''); $after_fields = apply_filters('mc4wp_form_after_fields', ''); // Process fields, if not submitted or not successfull or hide_after_success disabled if (!$was_submitted || !$this->options['hide_after_success'] || !$this->form_request->is_successful()) { $form_opening_html = '<form method="post">'; $visible_fields = $this->get_visible_form_fields(); // make sure to print date fallback later on if form contains a date field if ($this->form_contains_field_type($visible_fields, 'date')) { $this->print_date_fallback = true; } $hidden_fields = $this->get_hidden_form_fields($atts); $form_closing_html = '</form>'; } // empty string for response $response_html = ''; if ($was_submitted) { // Enqueue script (only after submit) wp_enqueue_script('mc4wp-form-request'); wp_localize_script('mc4wp-form-request', 'mc4wpFormRequestData', array('success' => $this->form_request->is_successful() ? 1 : 0, 'formId' => $this->form_request->get_form_instance_number(), 'data' => $this->form_request->get_data())); // get actual response html $response_html .= $this->form_request->get_response_html(); // add form response after or before fields if no {response} tag if (stristr($visible_fields, '{response}') === false || $this->options['hide_after_success']) { /** * @filter mc4wp_form_message_position * @expects string before|after * * Can be used to change the position of the form success & error messages. * Valid options are 'before' or 'after' */ $message_position = apply_filters('mc4wp_form_message_position', 'after'); switch ($message_position) { case 'before': $before_form = $before_form . $response_html; break; case 'after': $after_form = $response_html . $after_form; break; } } } // Always replace {response} tag, either with empty string or actual response $visible_fields = str_ireplace('{response}', $response_html, $visible_fields); // Generate closing HTML $closing_html = '</div><!-- / MailChimp for WP Plugin -->'; // increase form instance number in case there is more than one form on a page $this->form_instance_number++; // make sure scripts are enqueued later global $is_IE; if (isset($is_IE) && $is_IE) { wp_enqueue_script('mc4wp-placeholders'); } // Print small JS snippet later on in the footer. add_action('wp_footer', array($this, 'print_js')); ob_start(); // echo HTML parts of form echo $opening_html; echo $before_form; echo $form_opening_html; echo $before_fields; echo $visible_fields; echo $hidden_fields; echo $after_fields; echo $form_closing_html; echo $after_form; echo $closing_html; // print css to hide honeypot, if not printed already $this->print_css(); $output = ob_get_contents(); ob_end_clean(); return $output; }