/** * Returns text with {variables} replaced. * * @param string $string * @param array $additional_replacements * @param array Array of list ID's (needed if {subscriber_count} is set * * @return string $text The text with {variables} replaced. * replaced. */ public static function replace_variables($string, $additional_replacements = array(), $list_ids = array()) { // replace general vars $replacements = array('{ip}' => self::get_client_ip(), '{current_url}' => mc4wp_get_current_url(), '{current_path}' => !empty($_SERVER['REQUEST_URI']) ? esc_html($_SERVER['REQUEST_URI']) : '', '{date}' => date('m/d/Y'), '{time}' => date('H:i:s'), '{language}' => defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : get_locale(), '{email}' => self::get_known_email(), '{user_email}' => '', '{user_firstname}' => '', '{user_lastname}' => '', '{user_name}' => '', '{user_id}' => ''); // setup replacements for logged-in users if (is_user_logged_in() && ($user = wp_get_current_user()) && $user instanceof WP_User) { // logged in user, replace vars by user vars $replacements['{user_email}'] = $user->user_email; $replacements['{user_firstname}'] = $user->first_name; $replacements['{user_lastname}'] = $user->last_name; $replacements['{user_name}'] = $user->display_name; $replacements['{user_id}'] = $user->ID; } // merge with additional replacements $replacements = array_merge($replacements, $additional_replacements); // subscriber count? only fetch these if the tag is actually used if (stristr($string, '{subscriber_count}') !== false) { $mailchimp = new MC4WP_MailChimp(); $subscriber_count = $mailchimp->get_subscriber_count($list_ids); $replacements['{subscriber_count}'] = $subscriber_count; } // perform the replacement $string = str_ireplace(array_keys($replacements), array_values($replacements), $string); // replace dynamic variables if (stristr($string, '{data_') !== false) { $string = preg_replace_callback('/\\{data_([\\w-.]+)( default=\\"([^"]*)\\"){0,1}\\}/', array('MC4WP_Tools', 'replace_request_data_variables'), $string); } return $string; }
/** * Returns text with {variables} replaced. * * @param string $text * @param array $list_ids Array of list id's * @return string $text with {variables} replaced. */ function mc4wp_replace_variables($text, $list_ids = array()) { // get current WPML language or general site language $language = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : get_locale(); // replace general vars $needles = array('{ip}', '{current_url}', '{date}', '{time}', '{language}'); $replacements = array($_SERVER['REMOTE_ADDR'], mc4wp_get_current_url(), date('m/d/Y'), date('H:i:s'), $language); $text = str_ireplace($needles, $replacements, $text); // subscriber count? only fetch these if the tag is actually used if (stristr($text, '{subscriber_count}') !== false) { $mailchimp = new MC4WP_MailChimp(); $subscriber_count = $mailchimp->get_subscriber_count($list_ids); $text = str_ireplace('{subscriber_count}', $subscriber_count, $text); } // replace {email} tag if (isset($_GET['mc4wp_email'])) { $email = esc_attr($_GET['mc4wp_email']); } elseif (isset($_COOKIE['mc4wp_email'])) { $email = esc_attr($_COOKIE['mc4wp_email']); } else { $email = ''; } $text = str_ireplace('{email}', $email, $text); // replace user variables $needles = array('{user_email}', '{user_firstname}', '{user_lastname}', '{user_name}', '{user_id}'); if (is_user_logged_in() && ($user = wp_get_current_user()) && $user instanceof WP_User) { // logged in user, replace vars by user vars $replacements = array($user->user_email, $user->user_firstname, $user->user_lastname, $user->display_name, $user->ID); $text = str_ireplace($needles, $replacements, $text); } else { // no logged in user, replace vars with empty string $text = str_ireplace($needles, '', $text); } return $text; }
/** * Returns text with {variables} replaced. * * @param string $text * @param array $list_ids Array of list id's * @return string $text with {variables} replaced. */ function mc4wp_replace_variables($text, $list_ids = array()) { $needles = array('{ip}', '{current_url}', '{date}', '{time}'); $replacements = array($_SERVER['REMOTE_ADDR'], mc4wp_get_current_url(), date("m/d/Y"), date("H:i:s")); $text = str_ireplace($needles, $replacements, $text); // subscriber count? only fetch these if the tag is actually used if (stristr($text, '{subscriber_count}') != false) { $subscriber_count = mc4wp_get_subscriber_count($list_ids); $text = str_ireplace('{subscriber_count}', $subscriber_count, $text); } $needles = array('{user_email}', '{user_firstname}', '{user_lastname}', '{user_name}', '{user_id}'); if (is_user_logged_in() && ($user = wp_get_current_user()) && $user instanceof WP_User) { // logged in user, replace vars by user vars $user = wp_get_current_user(); $replacements = array($user->user_email, $user->user_firstname, $user->user_lastname, $user->display_name, $user->ID); $text = str_replace($needles, $replacements, $text); } else { // no logged in user, remove vars $text = str_replace($needles, '', $text); } return $text; }
/** * Returns the MailChimp for WP form mark-up * * @param array $atts * @param string $content * * @return string */ public function form($atts, $content = null) { $opts = mc4wp_get_options('form'); if (!function_exists('mc4wp_replace_variables')) { include_once MC4WP_LITE_PLUGIN_DIR . 'includes/functions/template.php'; } // allow developers to add css classes $css_classes = $this->get_css_classes(); $form_action = apply_filters('mc4wp_form_action', mc4wp_get_current_url()); $content = "\n<!-- Form by MailChimp for WordPress plugin v" . MC4WP_LITE_VERSION . " - http://dannyvankooten.com/mailchimp-for-wordpress/ -->\n"; $content .= '<form method="post" action="' . $form_action . '" id="mc4wp-form-' . $this->form_instance_number . '" class="' . $css_classes . '">'; // maybe hide the form if (!($this->success && $opts['hide_after_success'])) { $form_markup = __($opts['markup']); // replace special values $form_markup = str_ireplace(array('%N%', '{n}'), $this->form_instance_number, $form_markup); $form_markup = mc4wp_replace_variables($form_markup, 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(); $form_markup = str_ireplace('[captcha]', $captcha_fields, $form_markup); } // allow plugins to add form fieldsq do_action('mc4wp_before_form_fields', 0); // allow plugins to alter form content $content .= apply_filters('mc4wp_form_content', $form_markup); // allow plugins to add form fields do_action('mc4wp_after_form_fields', 0); // hidden fields $content .= '<textarea name="_mc4wp_required_but_not_really" style="display: none !important;"></textarea>'; $content .= '<input type="hidden" name="_mc4wp_form_submit" value="1" />'; $content .= '<input type="hidden" name="_mc4wp_form_instance" value="' . $this->form_instance_number . '" />'; $content .= '<input type="hidden" name="_mc4wp_form_nonce" value="' . wp_create_nonce('_mc4wp_form_nonce') . '" />'; } if ($this->form_instance_number === $this->submitted_form_instance) { if ($this->success) { $content .= '<div class="mc4wp-alert mc4wp-success">' . __($opts['text_success']) . '</div>'; } elseif ($this->error !== '') { $api = mc4wp_get_api(); $e = $this->error; $error_type = $e === 'already_subscribed' ? 'notice' : 'error'; $error_message = isset($opts['text_' . $e]) ? $opts['text_' . $e] : $opts['text_error']; // allow developers to customize error message $error_message = apply_filters('mc4wp_form_error_message', $error_message, $e); $content .= '<div class="mc4wp-alert mc4wp-' . $error_type . '">' . __($error_message, 'mailchimp-for-wp') . '</div>'; // show the eror returned by MailChimp? if ($api->has_error() && current_user_can('manage_options')) { $content .= '<div class="mc4wp-alert mc4wp-error"><strong>Admin notice:</strong> ' . $api->get_error_message() . '</div>'; } } // endif } if (current_user_can('manage_options') && empty($opts['lists'])) { $content .= '<div class="mc4wp-alert mc4wp-error"><strong>Admin notice:</strong> you have not selected a MailChimp list for this sign-up form to subscribe to yet. <a href="' . admin_url('admin.php?page=mc4wp-lite-form-settings') . '">Edit your form settings</a> and select at least 1 list.</div>'; } $content .= "</form>"; $content .= "\n<!-- / MailChimp for WP Plugin -->\n"; // 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'); } return $content; }
/** * Returns the MailChimp for WP form mark-up * * @param array $atts * @param string $content * * @return string */ public function form( $atts = array(), $content = '' ) { $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 ); // enqueue scripts (in footer) if form was submitted if( $was_submitted ) { 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' => stripslashes_deep( $_POST ) ) ); } if ( ! function_exists( 'mc4wp_replace_variables' ) ) { include_once MC4WP_LITE_PLUGIN_DIR . 'includes/functions/template.php'; } /** * @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() ); /** * @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' ); // Start building content string $content = "\n<!-- Form by MailChimp for WordPress plugin v". MC4WP_LITE_VERSION ." - https://dannyvankooten.com/mailchimp-for-wordpress/ -->\n"; $content .= '<form method="post" action="'. $form_action .'" id="mc4wp-form-'.$this->form_instance_number.'" class="'. $this->get_css_classes() .'">'; // show message if form was submitted and message position is before if( $was_submitted && $message_position === 'before' ) { $content .= $this->get_form_message_html(); } // do not add form fields if form was submitted and hide_after_success is enabled if( ! ( $opts['hide_after_success'] && $was_submitted && $this->form_request->is_successful() ) ) { // add form fields from settings $form_markup = __( $opts['markup'] ); // replace special values $form_markup = str_ireplace( array( '%N%', '{n}' ), $this->form_instance_number, $form_markup ); $form_markup = mc4wp_replace_variables( $form_markup, 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(); $form_markup = str_ireplace( '[captcha]', $captcha_fields, $form_markup ); } // allow plugins to add form fieldsq do_action( 'mc4wp_before_form_fields', 0 ); /** * @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. */ $content .= apply_filters( 'mc4wp_form_content', $form_markup ); // allow plugins to add form fields do_action( 'mc4wp_after_form_fields', 0 ); // hidden fields $content .= '<textarea name="_mc4wp_required_but_not_really" style="display: none !important;"></textarea>'; $content .= '<input type="hidden" name="_mc4wp_form_submit" value="1" />'; $content .= '<input type="hidden" name="_mc4wp_form_instance" value="'. $this->form_instance_number .'" />'; $content .= '<input type="hidden" name="_mc4wp_form_nonce" value="'. wp_create_nonce( '_mc4wp_form_nonce' ) .'" />'; } // show message if form was submitted and message position is after if( $was_submitted && $message_position === 'after' ) { $content .= $this->get_form_message_html(); } $content .= "</form>"; $content .= "\n<!-- / MailChimp for WP Plugin -->\n"; // 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' ); } return $content; }
/** * 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; }