/** * Get the text for the label element * * @return string */ public function get_label_text() { $opts = $this->get_options(); // Get general label text $label = $opts['label']; // Override label text if a specific text for this integration is set if (isset($opts['text_' . $this->type . '_label']) && !empty($opts['text_' . $this->type . '_label'])) { // custom label text was set $label = $opts['text_' . $this->type . '_label']; } // replace label variables $label = mc4wp_replace_variables($label, $opts['lists']); return $label; }
/** * @param mixed $hook Array or string * @return string */ public function get_checkbox($args = array()) { $opts = mc4wp_get_options('checkbox'); $checked = $opts['precheck'] ? "checked" : ''; // set label text if (isset($args['labels'][0])) { // cf 7 shortcode $label = $args['labels'][0]; } else { if (isset($opts['text_' . $this->type . '_label']) && !empty($opts['text_' . $this->type . '_label'])) { // custom label text was set $label = __($opts['text_' . $this->type . '_label'], 'mailchimp-for-wp'); } else { // default label text $label = __($opts['label'], 'mailchimp-for-wp'); } } // replace label variables $label = mc4wp_replace_variables($label, $opts['lists']); // CF7 checkbox? if (is_array($args) && isset($args['type'])) { // check for default:0 or default:1 to set the checked attribute if (in_array('default:1', $args['options'])) { $checked = 'checked'; } else { if (in_array('default:0', $args['options'])) { $checked = ''; } } } $content = "<!-- MailChimp for WP v" . MC4WP_LITE_VERSION . " - https://mc4wp.com/ -->"; do_action('mc4wp_before_checkbox'); // checkbox $content .= '<p id="mc4wp-checkbox">'; $content .= '<label>'; $content .= '<input type="checkbox" name="' . $this->checkbox_name . '" value="1" ' . $checked . ' /> '; $content .= $label; $content .= '</label>'; $content .= '</p>'; // honeypot $content .= '<textarea name="_mc4wp_required_but_not_really" style="display: none !important;"></textarea>'; do_action('mc4wp_after_checkbox'); return $content; }
/** * @param mixed $hook Array or string * @return string */ public function get_checkbox($hook = '') { $args = $hook; $opts = mc4wp_get_options('checkbox'); $checked = $opts['precheck'] ? "checked" : ''; // set label text if ($hook && is_string($hook) && isset($opts['text_' . $hook . '_label']) && !empty($opts['text_' . $hook . '_label'])) { // custom label text was set $label = __($opts['text_' . $hook . '_label']); } elseif ($args && is_array($args) && isset($args['labels'][0])) { // cf 7 shortcode $label = $args['labels'][0]; } else { // default label text $label = __($opts['label']); } // replace label variables $label = mc4wp_replace_variables($label, $opts['lists']); // CF7 checkbox? if (is_array($args) && isset($args['type'])) { // check for default:0 or default:1 to set the checked attribute if (in_array('default:1', $args['options'])) { $checked = 'checked'; } else { if (in_array('default:0', $args['options'])) { $checked = ''; } } } // add starting debug marker $content = "\n<!-- MailChimp for WordPress v" . MC4WP_LITE_VERSION . " - http://wordpress.org/plugins/mailchimp-for-wp/ -->\n"; do_action('mc4wp_before_checkbox'); $content .= '<p id="mc4wp-checkbox">'; $content .= '<label>'; $content .= '<input type="checkbox" name="' . esc_attr($this->checkbox_name_value) . '" value="1" ' . $checked . ' /> '; $content .= $label; $content .= '</label>'; $content .= '</p>'; $content .= '<textarea type="text" name="_mc4wp_required_but_not_really" style="display: none !important;"></textarea>'; do_action('mc4wp_after_checkbox'); // add ending debug marker $content .= "\n<!-- / MailChimp for WordPress -->\n"; return $content; }
/** * 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; }
/** * @param $form * @param $opts * * @return string */ private function get_visible_form_fields($form, $opts) { // replace special values $visible_fields = __($form->post_content, 'mailchimp-for-wp'); $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, $form->ID); return (string) $visible_fields; }
public function get_checkbox($hook = '') { $opts = mc4wp_get_options('checkbox'); $checked = $opts['precheck'] ? "checked" : ''; if ($hook && is_string($hook) && isset($opts['text_' . $hook . '_label']) && !empty($opts['text_' . $hook . '_label'])) { // custom label text was set $label = __($opts['text_' . $hook . '_label']); } elseif ($hook && is_array($hook) && isset($hook['labels'][0])) { // cf 7 shortcode $label = $hook['labels'][0]; } else { // default label text $label = __($opts['label']); } // replace label variables $label = mc4wp_replace_variables($label, $opts['lists']); $content = "\n<!-- MailChimp for WP Pro v" . MC4WP_VERSION_NUMBER . " -->\n"; $content .= '<p id="mc4wp-checkbox">'; $content .= '<label>'; $content .= '<input type="checkbox" name="mc4wp-do-subscribe" value="1" ' . $checked . ' /> '; $content .= $label; $content .= '</label>'; $content .= '</p>'; 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; }