/** * @param array|null $config Use this to override the configuration for this form element * @return string */ public function generate_html(array $config = null) { if ($config) { $this->config = $config; } // Start building content string $opening_html = '<!-- MailChimp for WordPress v' . MC4WP_VERSION . ' - https://wordpress.org/plugins/mailchimp-for-wp/ -->'; $opening_html .= '<form ' . $this->get_form_element_attributes() . '>'; $before_fields = $this->get_html_before_fields(); $fields = ''; $after_fields = $this->get_html_after_fields(); $closing_html = '</form><!-- / MailChimp for WordPress Plugin -->'; if (!$this->is_submitted || !$this->form->settings['hide_after_success'] || $this->form->has_errors()) { // add HTML for fields + wrapper element. $fields = '<div class="mc4wp-form-fields">' . $this->get_visible_fields() . $this->get_hidden_fields() . '</div>'; } // concatenate everything $output = $opening_html . $before_fields . $fields . $after_fields . $closing_html; return $output; }
/** * @param MC4WP_Form $form */ public function respond(MC4WP_Form $form) { $success = !$form->has_errors(); if ($success) { /** * Fires right after a form is submitted with errors. * * @since 3.0 * * @param MC4WP_Form $form The submitted form instance. */ do_action('mc4wp_form_error', $form); // fire a dedicated event for each error foreach ($form->errors as $error) { /** * Fires right after a form was submitted with errors. * * The dynamic portion of the hook, `$error`, refers to the error that occured. * * Default errors give us the following possible hooks: * * - mc4wp_form_error_error General errors * - mc4wp_form_error_spam * - mc4wp_form_error_invalid_email Invalid email address * - mc4wp_form_error_already_subscribed Email is already on selected list(s) * - mc4wp_form_error_required_field_missing One or more required fields are missing * - mc4wp_form_error_no_lists_selected No MailChimp lists were selected * * @since 3.0 * * @param MC4WP_Form $form The form instance of the submitted form. */ do_action('mc4wp_form_error_' . $error, $form); } } else { /** * Fires right after a form is submitted without any errors (success). * * @since 3.0 * * @param MC4WP_Form $form Instance of the submitted form */ do_action('mc4wp_form_success', $form); } /** * Fires right before responding to the form request. * * @since 3.0 * * @param MC4WP_Form $form Instance of the submitted form. */ do_action('mc4wp_form_respond', $form); // do stuff on success (non-AJAX) if ($success && (!defined('DOING_AJAX') || !DOING_AJAX)) { // do we want to redirect? $redirect_url = $form->get_redirect_url(); if (!empty($redirect_url)) { wp_redirect($form->get_redirect_url()); exit; } } }
/** * @param MC4WP_Form $form */ public function respond_to_request(MC4WP_Form $form) { // do nothing if we're not doing AJAX if (!defined('DOING_AJAX') || !DOING_AJAX) { return; } // clear output, some plugins might have thrown errors by now. if (ob_get_level() > 0) { ob_end_clean(); } // Format response using Google JSON Style Guide: https://google.github.io/styleguide/jsoncstyleguide.xml $response = array(); // error if ($form->has_errors()) { $response['error'] = array('type' => $form->errors[0], 'message' => $form->get_response_html(), 'errors' => $form->errors); wp_send_json($response); exit; } // success $response['data'] = array('event' => $form->get_action() . 'd', 'message' => $form->get_response_html(), 'hide_fields' => (bool) $form->settings['hide_after_success']); if ($form->get_redirect_url()) { $response['data']['redirect_to'] = $form->get_redirect_url(); } wp_send_json($response); exit; }