/** * @param iMC4WP_Request $request * @return MC4WP_Form|null */ public static function get(iMC4WP_Request $request = null) { // has instance been created already? if (self::$instance) { $form = self::$instance; } else { // create a new instance $form = new MC4WP_Form($request); self::$instance = $form; } // attach request to form if ($request && !$form->has_request($request)) { $form->attach_request($request); } return $form; }
/** * @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; }
/** * @since 2.3.8 * @param int $form_id * @return string */ function mc4wp_form_get_response_html($form_id = 0) { $form = MC4WP_Form::get($form_id); // return empty string if form doesn't exist or isn't submitted if (!$form instanceof MC4WP_Form || !$form->is_submitted()) { return ''; } return $form->request->get_response_html(); }
/** * @param int|WP_post $post * @param iMC4WP_Request $request * @return MC4WP_Form|null */ public static function get($post, iMC4WP_Request $request = null) { if (!is_object($post)) { $post = get_post($post); } // create new instance if ($post instanceof WP_Post && $post->post_type === 'mc4wp-form') { // has instance been created already? if (isset(self::$instances[$post->ID])) { $form = self::$instances[$post->ID]; } else { // create a new instance $form = new MC4WP_Form($post, $request); self::$instances[$post->ID] = $form; } // attach request to form if ($request && !$form->has($request)) { $form->attach($request); } return $form; } return null; }
/** * Returns the HTML for success or error messages * * @return string */ public function get_response_html() { // get all form messages $messages = $this->form->get_messages(); // retrieve correct message $message = isset($messages[$this->message_type]) ? $messages[$this->message_type] : $messages['error']; // replace variables in message text $message['text'] = MC4WP_Tools::replace_variables($message['text'], array(), array_values($this->get_lists())); $html = '<div class="mc4wp-alert mc4wp-' . esc_attr($message['type']) . '">' . $message['text'] . '</div>'; // show additional MailChimp API errors to administrators if (!$this->success && current_user_can('manage_options')) { if ('' !== $this->mailchimp_error) { $html .= '<div class="mc4wp-alert mc4wp-error"><strong>Admin notice:</strong> ' . $this->mailchimp_error . '</div>'; } } return $html; }
/** * Returns the HTML for success or error messages * * @return string */ public function get_response_html() { // get all form messages $messages = $this->form->get_messages(); // retrieve correct message $message = isset($messages[$this->status]) ? $messages[$this->status] : $messages['error']; // replace variables in message text $message['text'] = MC4WP_Tools::replace_variables($message['text'], array(), array_values($this->get_lists())); $html = '<div class="mc4wp-alert mc4wp-' . esc_attr($message['type']) . '">' . $message['text'] . '</div>'; // show additional MailChimp API errors to administrators if (!$this->success && current_user_can('manage_options')) { if (!empty($this->responses[0]->error)) { $html .= '<div class="mc4wp-alert mc4wp-error">'; $html .= $this->responses[0]->error; $html .= '<br /><small>' . __('This message is only visible to logged-in administrators.', 'mailchimp-for-wp') . '</small>'; $html .= '</div>'; } } return $html; }
/** * @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; }
/** * Returns the form response * * @return string */ public function get_form_response() { return $this->form->get_response_html(); }
/** * Outputs a form with the given ID * * @param array $attributes * @param string $content * @return string */ public function output_form($attributes = array(), $content = '') { global $is_IE; // increase count of outputted forms $this->outputted_forms_count++; $attributes = shortcode_atts(array('id' => 0, 'element_id' => 'mc4wp-form-' . $this->outputted_forms_count), $attributes, 'mc4wp_form'); // try to get default form ID if it wasn't specified in the shortcode atts if (!$attributes['id']) { // try to get default form id $attributes['id'] = absint(get_option('mc4wp_default_form_id', 0)); if (empty($attributes['id'])) { // return error message or empty form 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>'; } else { return ''; } } } // Get the form with the specified ID $form = MC4WP_Form::get($attributes['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 ''; } // make sure to print date fallback later on if form contains a date field if ($form->contains_field_type('date')) { $this->print_date_fallback = true; } // does this form have AJAX enabled? if ($form->settings['ajax']) { $this->load_ajax_scripts(); } // was form submited? if ($form->is_submitted($attributes['element_id'])) { // enqueue scripts (in footer) if form was submited $animate_scroll = apply_filters('mc4wp_form_animate_scroll', true); wp_enqueue_script('mc4wp-form-request'); wp_localize_script('mc4wp-form-request', 'mc4wpFormRequestData', array('success' => $form->request->success ? 1 : 0, 'formElementId' => $form->request->form_element_id, 'data' => $form->request->user_data, 'animate_scroll' => $animate_scroll)); } // make sure scripts are enqueued later 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'), 99); // output form return $form->output($attributes['element_id'], $attributes, false); }
/** * @hooked `mc4wp_form_subscribed` * @param MC4WP_Form $form */ public function on_form_success(MC4WP_Form $form) { $data = $form->get_data(); $this->save($data); }
/** * Returns the MailChimp for WP form mark-up * * @param array $attributes * @param string $content * * @return string */ public function output_form($attributes = array(), $content = '') { global $is_IE; // increase count of outputted forms $this->outputted_forms_count++; $attributes = shortcode_atts(array('id' => 0, 'element_id' => 'mc4wp-form-' . $this->outputted_forms_count), $attributes, 'mc4wp_form'); // create or retrieve form instance $form = MC4WP_Form::get(); // make sure to print date fallback later on if form contains a date field if ($form->contains_field_type('date')) { $this->print_date_fallback = true; } // was form submited? if ($form->is_submitted($attributes['element_id'])) { // enqueue scripts (in footer) if form was submited wp_enqueue_script('mc4wp-form-request'); wp_localize_script('mc4wp-form-request', 'mc4wpFormRequestData', array('success' => $form->request->success ? 1 : 0, 'formElementId' => $form->request->form_element_id, 'data' => $form->request->data)); } // Print small JS snippet later on in the footer. add_action('wp_footer', array($this, 'print_js')); // Print CSS to hide honeypot (should be printed in `wp_head` by now) $html = ''; // add inline css if it was not printed yet $html .= $this->print_css(false); // output form $html .= $form->output($attributes['element_id'], $attributes, false); return $html; }
/** * @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; } } }
/** * Returns the MailChimp for WP form mark-up * * @param array $attributes * @param string $content * * @return string */ public function output_form($attributes = array(), $content = '') { // increase count of outputted forms $this->outputted_forms_count++; // parse shortcode attributes (sets up defaults too) $attributes = $this->parse_shortcode_attributes($attributes); // create or retrieve form instance $form = MC4WP_Form::get($attributes['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 ''; } // tell asset manager to print assets for this form $this->assets->print_form_assets($form); // add inline css to form output if it was not printed yet $content .= $this->assets->print_css(false); // output form $content .= $form->output($attributes['element_id'], $attributes, false); return $content; }
/** * Returns the number of subscribers on the selected lists (for the form context) * * @return int */ public function get_subscriber_count() { $mailchimp = new MC4WP_MailChimp(); return $mailchimp->get_subscriber_count($this->form->get_lists()); }
/** * We only need the stylesheet of this form for this preview page. * * @param $stylesheets * * @return string */ public function set_stylesheet($stylesheets) { return array($this->form->get_stylesheet()); }
/** * Outputs the optional form settings metabox * @param WP_Post $post */ public function show_optional_form_settings_metabox($post) { $form = MC4WP_Form::get($post); $individual_form_settings = $form->load_settings(false); $inherited_settings = mc4wp_get_options('form'); include MC4WP_PLUGIN_DIR . 'includes/views/metaboxes/optional-form-settings.php'; }
/** * @param MC4WP_Form $form */ public function print_form_assets(MC4WP_Form $form) { // make sure to print date fallback later on if form contains a date field if ($form->contains_field_type('date')) { $this->print_date_fallback = true; } // if form was submitted, print scripts (only once) if ($form->is_submitted() && !wp_script_is('mc4wp-form-request', 'enqueued')) { // enqueue scripts (in footer) if form was submited wp_enqueue_script('mc4wp-form-request'); wp_localize_script('mc4wp-form-request', 'mc4wpFormRequestData', array('success' => $form->request->success ? 1 : 0, 'formElementId' => $form->request->config['form_element_id'], 'data' => $form->request->data)); } // make sure scripts are enqueued later global $is_IE; if (isset($is_IE) && $is_IE) { wp_enqueue_script('mc4wp-placeholders'); } // print snippet of JS $this->print_js(); }
/** * @param $item * * @return string */ public function column_lists($item) { $form = MC4WP_Form::get($item->ID); $content = ''; if (!empty($form->settings['lists'])) { foreach ($form->settings['lists'] as $list_id) { $content .= $this->mailchimp->get_list_name($list_id) . '<br />'; } } else { return '<a style="color: red; text-decoration: underline;" href="' . get_edit_post_link($item->ID) . '">' . __('No MailChimp list(s) selected yet.', 'mailchimp-for-wp') . '</a>'; } return $content; }
/** * Returns the MailChimp for WP form mark-up * * @param array $attributes * @param string $content * * @return string */ public function output_form($attributes = array(), $content = '') { global $is_IE; // increase count of outputted forms $this->outputted_forms_count++; $attributes = shortcode_atts(array('id' => 0, 'element_id' => 'mc4wp-form-' . $this->outputted_forms_count), $attributes, 'mc4wp_form'); // create or retrieve form instance $form = MC4WP_Form::get(); // make sure to print date fallback later on if form contains a date field if ($form->contains_field_type('date')) { $this->print_date_fallback = true; } // was form submited? if ($form->is_submitted($attributes['element_id'])) { // enqueue scripts (in footer) if form was submitted $animate_scroll = apply_filters('mc4wp_form_animate_scroll', true); wp_enqueue_script('mc4wp-form-request'); wp_localize_script('mc4wp-form-request', 'mc4wpFormRequestData', array('success' => $form->request->success ? 1 : 0, 'formElementId' => $form->request->form_element_id, 'data' => $form->request->user_data, 'animate_scroll' => $animate_scroll)); } // Print small JS snippet later on in the footer. add_action('wp_footer', array($this, 'print_js'), 99); // make sure scripts are enqueued later if (isset($is_IE) && $is_IE) { wp_enqueue_script('mc4wp-placeholders'); } // output form return $form->output($attributes['element_id'], $attributes, false); }
/** * Returns a Form instance * * @access public * * @param int|WP_Post $form_id. * * @return MC4WP_Form */ function mc4wp_get_form($form_id = 0) { return MC4WP_Form::get_instance($form_id); }