views
12 years ago
class-admin.php
12 years ago
class-api.php
12 years ago
class-checkbox.php
12 years ago
class-form.php
12 years ago
class-plugin.php
12 years ago
class-widget.php
12 years ago
functions.php
12 years ago
index.php
12 years ago
template-functions.php
12 years ago
class-form.php
376 lines
| 1 | <?php |
| 2 | |
| 3 | class MC4WP_Lite_Form { |
| 4 | private static $instance = null; |
| 5 | private $form_instance_number = 1; |
| 6 | private $error = null; |
| 7 | private $success = false; |
| 8 | private $submitted_form_instance = 0; |
| 9 | |
| 10 | public static function init() { |
| 11 | if(self::$instance) { |
| 12 | throw new Exception("Already initialized"); |
| 13 | } else { |
| 14 | self::$instance = new self; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | public static function instance() { |
| 19 | return self::$instance; |
| 20 | } |
| 21 | |
| 22 | private function __construct() { |
| 23 | $opts = mc4wp_get_options('form'); |
| 24 | |
| 25 | if($opts['css']) { |
| 26 | add_filter('mc4wp_stylesheets', array($this, 'add_stylesheets')); |
| 27 | } |
| 28 | |
| 29 | add_shortcode( 'mc4wp_form', array( $this, 'output_form' ) ); |
| 30 | add_shortcode( 'mc4wp-form', array( $this, 'output_form' ) ); |
| 31 | |
| 32 | // enable shortcodes in text widgets |
| 33 | add_filter( 'widget_text', 'shortcode_unautop' ); |
| 34 | add_filter( 'widget_text', 'do_shortcode', 11 ); |
| 35 | |
| 36 | if ( isset( $_POST['mc4wp_form_submit'] ) ) { |
| 37 | $this->ensure_backwards_compatibility(); |
| 38 | add_action( 'init', array( $this, 'submit' ) ); |
| 39 | add_action( 'wp_footer', array( $this, 'print_scroll_js' ), 99); |
| 40 | } |
| 41 | |
| 42 | add_action( 'wp_enqueue_scripts', array($this, 'register_scripts' ) ); |
| 43 | } |
| 44 | |
| 45 | public function register_scripts() |
| 46 | { |
| 47 | wp_register_script( 'mc4wp-placeholders', plugins_url('mailchimp-for-wp/assets/js/placeholders.min.js'), array(), MC4WP_LITE_VERSION, true ); |
| 48 | } |
| 49 | |
| 50 | public function add_stylesheets($stylesheets) { |
| 51 | $opts = mc4wp_get_options('form'); |
| 52 | |
| 53 | $stylesheets['form'] = 1; |
| 54 | |
| 55 | // theme? |
| 56 | if($opts['css'] != 1 && $opts['css'] != 'default') { |
| 57 | $stylesheets['form-theme'] = $opts['css']; |
| 58 | } |
| 59 | |
| 60 | return $stylesheets; |
| 61 | } |
| 62 | |
| 63 | public function output_form( $atts, $content = null ) { |
| 64 | $opts = mc4wp_get_options('form'); |
| 65 | |
| 66 | if ( !function_exists( 'mc4wp_replace_variables' ) ) { |
| 67 | include_once MC4WP_LITE_PLUGIN_DIR . 'includes/template-functions.php'; |
| 68 | } |
| 69 | |
| 70 | // add some useful css classes |
| 71 | $css_classes = ' '; |
| 72 | if ( $this->error ) $css_classes .= 'mc4wp-form-error '; |
| 73 | if ( $this->success ) $css_classes .= 'mc4wp-form-success '; |
| 74 | |
| 75 | $content = "\n<!-- Form by MailChimp for WordPress plugin v". MC4WP_LITE_VERSION ." - http://dannyvankooten.com/mailchimp-for-wordpress/ -->\n"; |
| 76 | $content .= '<form method="post" action="'. mc4wp_get_current_url() .'" id="mc4wp-form-'.$this->form_instance_number.'" class="mc4wp-form form'.$css_classes.'">'; |
| 77 | |
| 78 | // maybe hide the form |
| 79 | if ( !( $this->success && $opts['hide_after_success'] ) ) { |
| 80 | $form_markup = __( $opts['markup'] ); |
| 81 | |
| 82 | // replace special values |
| 83 | $form_markup = str_replace( array( '%N%', '{n}' ), $this->form_instance_number, $form_markup ); |
| 84 | $form_markup = mc4wp_replace_variables( $form_markup, array_values( $opts['lists'] ) ); |
| 85 | $form_markup = apply_filters('mc4wp_form_content', $form_markup); |
| 86 | $content .= $form_markup; |
| 87 | |
| 88 | // hidden fields |
| 89 | $content .= '<textarea name="mc4wp_required_but_not_really" style="display: none;"></textarea>'; |
| 90 | $content .= '<input type="hidden" name="mc4wp_form_submit" value="1" />'; |
| 91 | $content .= '<input type="hidden" name="mc4wp_form_instance" value="'. $this->form_instance_number .'" />'; |
| 92 | $content .= '<input type="hidden" name="mc4wp_form_nonce" value="'. wp_create_nonce( '_mc4wp_form_nonce' ) .'" />'; |
| 93 | } |
| 94 | |
| 95 | if ( $this->form_instance_number == $this->submitted_form_instance ) { |
| 96 | |
| 97 | if ( $this->success ) { |
| 98 | $content .= '<div class="mc4wp-alert mc4wp-success">' . __( $opts['text_success'] ) . '</div>'; |
| 99 | } elseif ( $this->error ) { |
| 100 | |
| 101 | $api = mc4wp_get_api(); |
| 102 | $e = $this->error; |
| 103 | |
| 104 | if ( $e == 'already_subscribed' ) { |
| 105 | $text = ( empty( $opts['text_already_subscribed'] ) ) ? $api->get_error_message() : $opts['text_already_subscribed']; |
| 106 | $content .= '<div class="mc4wp-alert mc4wp-notice">'. __( $text ) .'</div>'; |
| 107 | } elseif ( isset( $opts['text_' . $e] ) && !empty( $opts['text_'. $e] ) ) { |
| 108 | $content .= '<div class="mc4wp-alert mc4wp-error">' . __( $opts['text_' . $e] ) . '</div>'; |
| 109 | } |
| 110 | |
| 111 | if ( current_user_can( 'manage_options' ) ) { |
| 112 | |
| 113 | if ( $api->has_error() ) { |
| 114 | $content .= '<div class="mc4wp-alert mc4wp-error"><strong>Admin notice:</strong> '. $api->get_error_message() . '</div>'; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | // endif |
| 120 | } |
| 121 | |
| 122 | if ( current_user_can( 'manage_options' ) && empty( $opts['lists'] ) ) { |
| 123 | $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>'; |
| 124 | } |
| 125 | |
| 126 | $content .= "</form>"; |
| 127 | $content .= "\n<!-- / MailChimp for WP Plugin -->\n"; |
| 128 | |
| 129 | // increase form instance number in case there is more than one form on a page |
| 130 | $this->form_instance_number++; |
| 131 | |
| 132 | // make sure scripts are enqueued later |
| 133 | global $is_IE; |
| 134 | if(isset($is_IE) && $is_IE) { |
| 135 | wp_enqueue_script('mc4wp-placeholders'); |
| 136 | } |
| 137 | |
| 138 | return $content; |
| 139 | } |
| 140 | |
| 141 | public function submit() { |
| 142 | $opts = mc4wp_get_options('form'); |
| 143 | $this->submitted_form_instance = (int) $_POST['mc4wp_form_instance']; |
| 144 | |
| 145 | if ( !isset( $_POST['mc4wp_form_nonce'] ) || !wp_verify_nonce( $_POST['mc4wp_form_nonce'], '_mc4wp_form_nonce' ) ) { |
| 146 | $this->error = 'invalid_nonce'; |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | if ( !isset( $_POST['EMAIL'] ) || !is_email( $_POST['EMAIL'] ) ) { |
| 151 | // no (valid) e-mail address has been given |
| 152 | $this->error = 'invalid_email'; |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | if ( isset( $_POST['mc4wp_required_but_not_really'] ) && !empty( $_POST['mc4wp_required_but_not_really'] ) ) { |
| 157 | // spam bot filled the honeypot field |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | $email = $_POST['EMAIL']; |
| 162 | |
| 163 | // setup merge vars |
| 164 | $merge_vars = array(); |
| 165 | |
| 166 | foreach ( $_POST as $name => $value ) { |
| 167 | |
| 168 | // only add uppercases fields to merge variables array |
| 169 | if ( !empty( $name ) && $name == 'EMAIL' || $name !== strtoupper( $name ) ) { continue; } |
| 170 | |
| 171 | if ( $name === 'GROUPINGS' ) { |
| 172 | |
| 173 | $groupings = $value; |
| 174 | |
| 175 | // malformed |
| 176 | if ( !is_array( $groupings ) ) { continue; } |
| 177 | |
| 178 | // setup groupings array |
| 179 | $merge_vars['GROUPINGS'] = array(); |
| 180 | |
| 181 | foreach ( $groupings as $grouping_id_or_name => $groups ) { |
| 182 | |
| 183 | $grouping = array(); |
| 184 | |
| 185 | if ( is_numeric( $grouping_id_or_name ) ) { |
| 186 | $grouping['id'] = $grouping_id_or_name; |
| 187 | } else { |
| 188 | $grouping['name'] = $grouping_id_or_name; |
| 189 | } |
| 190 | |
| 191 | if ( !is_array( $groups ) ) { |
| 192 | $grouping['groups'] = explode( ',', $groups ); |
| 193 | } else { |
| 194 | $grouping['groups'] = $groups; |
| 195 | } |
| 196 | |
| 197 | // add grouping to array |
| 198 | $merge_vars['GROUPINGS'][] = $grouping; |
| 199 | } |
| 200 | |
| 201 | if ( empty( $merge_vars['GROUPINGS'] ) ) { unset( $merge_vars['GROUPINGS'] ); } |
| 202 | |
| 203 | } else { |
| 204 | $merge_vars[$name] = $value; |
| 205 | } |
| 206 | |
| 207 | } |
| 208 | |
| 209 | $result = $this->subscribe( $email, $merge_vars ); |
| 210 | |
| 211 | if ( $result === true ) { |
| 212 | $this->success = true; |
| 213 | |
| 214 | // check if we want to redirect the visitor |
| 215 | if ( !empty( $opts['redirect'] ) ) { |
| 216 | wp_redirect( $opts['redirect'] ); |
| 217 | exit; |
| 218 | } |
| 219 | |
| 220 | return true; |
| 221 | } else { |
| 222 | |
| 223 | $this->success = false; |
| 224 | $this->error = $result; |
| 225 | |
| 226 | return false; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | Ensure backwards compatibility so sign-up forms that contain old form mark-up rules don't break |
| 232 | - Uppercase $_POST variables that should be sent to MailChimp |
| 233 | - Format GROUPINGS in one of the following formats. |
| 234 | $_POST[GROUPINGS][$group_id] = "Group 1, Group 2" |
| 235 | $_POST[GROUPINGS][$group_name] = array("Group 1", "Group 2") |
| 236 | */ |
| 237 | public function ensure_backwards_compatibility() { |
| 238 | |
| 239 | // upercase $_POST variables |
| 240 | foreach ( $_POST as $name => $value ) { |
| 241 | |
| 242 | // skip mc4wp internal vars |
| 243 | if ($name == strtoupper($name) || in_array( $name, array( 'mc4wp_form_instance', 'mc4wp_form_nonce', 'mc4wp_required_but_not_really', 'mc4wp_form_submit' ) ) ) { |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | $ucname = strtoupper( $name ); |
| 248 | $_POST[$ucname] = $value; |
| 249 | unset( $_POST[$name] ); |
| 250 | } |
| 251 | |
| 252 | // detect old style GROUPINGS, then fix it. |
| 253 | if ( isset( $_POST['GROUPINGS'] ) && is_array( $_POST['GROUPINGS'] ) && isset( $_POST['GROUPINGS'][0] ) ) { |
| 254 | |
| 255 | $old_groupings = $_POST['GROUPINGS']; |
| 256 | unset( $_POST['GROUPINGS'] ); |
| 257 | $new_groupings = array(); |
| 258 | |
| 259 | foreach ( $old_groupings as $grouping ) { |
| 260 | |
| 261 | if(!isset($grouping['groups'])) { continue; } |
| 262 | |
| 263 | if ( isset( $grouping['id'] ) ) { |
| 264 | $key = $grouping['id']; |
| 265 | } else if(isset( $grouping['name'] ) ) { |
| 266 | $key = $grouping['name']; |
| 267 | } else { |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | $new_groupings[$key] = $grouping['groups']; |
| 272 | |
| 273 | } |
| 274 | |
| 275 | // re-fill $_POST array with new groupings |
| 276 | if ( !empty( $new_groupings ) ) { $_POST['GROUPINGS'] = $new_groupings; } |
| 277 | |
| 278 | } |
| 279 | |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | public function subscribe( $email, array $merge_vars = array() ) { |
| 284 | $api = mc4wp_get_api(); |
| 285 | $opts = mc4wp_get_options('form'); |
| 286 | |
| 287 | $lists = $opts['lists']; |
| 288 | |
| 289 | if ( empty( $lists ) ) { |
| 290 | return 'no_lists_selected'; |
| 291 | } |
| 292 | |
| 293 | // guess FNAME and LNAME |
| 294 | if ( isset( $merge_vars['NAME'] ) && !isset( $merge_vars['FNAME'] ) && !isset( $merge_vars['LNAME'] ) ) { |
| 295 | |
| 296 | $strpos = strpos( $merge_vars['NAME'], ' ' ); |
| 297 | |
| 298 | if ( $strpos ) { |
| 299 | $merge_vars['FNAME'] = trim( substr( $merge_vars['NAME'], 0, $strpos ) ); |
| 300 | $merge_vars['LNAME'] = trim( substr( $merge_vars['NAME'], $strpos ) ); |
| 301 | } else { |
| 302 | $merge_vars['FNAME'] = $merge_vars['NAME']; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | do_action('mc4wp_before_subscribe', $email, $merge_vars, 0); |
| 307 | |
| 308 | $result = false; |
| 309 | $email_type = apply_filters('mc4wp_email_type', 'html'); |
| 310 | $lists = apply_filters('mc4wp_lists', $lists, $merge_vars); |
| 311 | |
| 312 | foreach ( $lists as $list_id ) { |
| 313 | |
| 314 | $list_merge_vars = apply_filters('mc4wp_merge_vars', $merge_vars, 0, $list_id); |
| 315 | $result = $api->subscribe( $list_id, $email, $list_merge_vars, $email_type, $opts['double_optin'] ); |
| 316 | |
| 317 | if($result === true) { |
| 318 | $from_url = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : ''; |
| 319 | do_action('mc4wp_subscribe_form', $email, $list_id, 0, $merge_vars, $from_url); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | do_action('mc4wp_after_subscribe', $email, $merge_vars, 0, $result); |
| 324 | |
| 325 | if ( $result === true ) { |
| 326 | $this->success = true; |
| 327 | } else { |
| 328 | $this->success = false; |
| 329 | $this->error = $result; |
| 330 | } |
| 331 | |
| 332 | // flawed |
| 333 | // this will only return the result of the last list a subscribe attempt has been sent to |
| 334 | return $result; |
| 335 | } |
| 336 | |
| 337 | public function print_scroll_js() { |
| 338 | $form_id = $_POST['mc4wp_form_instance']; |
| 339 | ?><script type="text/javascript">(function(){var e=document.getElementById("mc4wp-form-<?php echo esc_js($form_id); ?>");if(!e){return}var t=0;var n=e;var r=window.innerHeight;if(n.offsetParent){do{t+=n.offsetTop}while(n=n.offsetParent)}else{t=e.offsetTop}if(r>e.clientHeight){t=t-(r-e.clientHeight)/2}if(window.jQuery!==undefined){var i=500+t;jQuery("html, body").animate({scrollTop:t},i)}else{window.scrollTo(0,t)}})()</script><?php |
| 340 | /*?><script> |
| 341 | (function() { |
| 342 | var element = document.getElementById('mc4wp-form-<?php echo esc_js($form_id); ?>'); |
| 343 | |
| 344 | if(!element) { |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | var scrollToHeight = 0; |
| 349 | var obj = element; |
| 350 | var windowHeight = window.innerHeight; |
| 351 | |
| 352 | if (obj.offsetParent) { |
| 353 | do { |
| 354 | scrollToHeight += obj.offsetTop; |
| 355 | } while (obj = obj.offsetParent); |
| 356 | } else { |
| 357 | scrollToHeight = element.offsetTop; |
| 358 | } |
| 359 | |
| 360 | if(windowHeight > element.clientHeight) { |
| 361 | scrollToHeight = scrollToHeight - ((windowHeight - element.clientHeight) / 2); |
| 362 | } |
| 363 | |
| 364 | if(window.jQuery !== undefined) { |
| 365 | var animationTime = (500 + scrollToHeight); |
| 366 | jQuery('html, body').animate({ scrollTop: scrollToHeight }, animationTime); |
| 367 | } else { |
| 368 | window.scrollTo(0, scrollToHeight); |
| 369 | } |
| 370 | })(); |
| 371 | </script> |
| 372 | <?php*/ |
| 373 | } |
| 374 | |
| 375 | } |
| 376 |