[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Widget.class.php (source)

   1  <?php
   2  class MP_Widget extends WP_Widget
   3  {
   4  	function __construct()
   5      {
   6          $widget_ops  = array('classname' => 'widget_mailpress', 'description' => __( 'the mailpress subscription form', MP_TXTDOM));
   7          $control_ops = array('width' => 400, 'height' => 300);
   8          parent::__construct('mailpress', 'MailPress', $widget_ops, $control_ops);
   9      }
  10  
  11  	function widget($args, $instance) 
  12      {
  13          extract($args);
  14          $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
  15  
  16          echo $before_widget;
  17          if ( !empty( $title ) ) { echo $before_title . stripslashes($title) . $after_title; }
  18  
  19          $instance['widget_id'] = $args['widget_id'];
  20          self::widget_form($instance); 
  21  
  22          echo $after_widget;
  23      }
  24  
  25  	function update($new_instance, $old_instance) 
  26      {
  27          $instance             = $old_instance;
  28          $instance['title']     = strip_tags($new_instance['title']);
  29          $instance['txtbutton']     = strip_tags($new_instance['txtbutton']);
  30          $instance['txtsubmgt']     = strip_tags($new_instance['txtsubmgt']);
  31          if (isset($new_instance['css'])) $instance['css'] = true;
  32          else unset($instance['css']);
  33             if (isset($new_instance['jq']))  $instance['jq']  = true;
  34          else unset($instance['jq']);
  35          if (isset($new_instance['js']))  $instance['js']  = true;
  36          else unset($instance['js']);
  37          if (isset($new_instance['urlsubmgt'])) $instance['urlsubmgt'] = true;
  38          else {unset($instance['urlsubmgt']); $instance['txtsubmgt'] = '';}
  39  
  40          $instance = apply_filters('MP_Widget_save_instance',$instance);
  41  
  42          return $instance;
  43      }
  44  
  45  	function form($instance) 
  46      {
  47          $instance     = wp_parse_args( (array) $instance, self::form_defaults() );
  48           $title     = (isset($instance['title'])) ? strip_tags($instance['title']) : '';
  49          $txtbutton    = strip_tags($instance['txtbutton']);
  50          $txtsubmgt    = strip_tags($instance['txtsubmgt']);
  51  ?>
  52  <p>
  53      <label for='<?php echo $this->get_field_id('title'); ?>'>
  54          <?php _e('Title:'); ?> 
  55          <input type='text' id='<?php echo $this->get_field_id('title'); ?>' name='<?php echo $this->get_field_name('title'); ?>' value="<?php echo esc_attr($title); ?>" class='widefat' />
  56      </label>
  57      <br /><br />
  58      <label for='<?php echo $this->get_field_id('txtbutton'); ?>'>
  59          <?php _e('Button:'); ?> 
  60          <input type='text' id='<?php echo $this->get_field_id('txtbutton'); ?>' name='<?php echo $this->get_field_name('txtbutton'); ?>' value="<?php echo esc_attr($txtbutton); ?>" class='widefat' />
  61      </label>
  62      <br /><br />
  63      <label for='<?php echo $this->get_field_id('urlsubmgt'); ?>'>
  64          <input type='checkbox' id='<?php echo $this->get_field_id('urlsubmgt'); ?>' name='<?php echo $this->get_field_name('urlsubmgt'); ?>'<?php if ($instance['urlsubmgt']) checked(true); ?> onchange="jQuery('#<?php echo $this->get_field_id('txtsubmgt') ; ?>').toggle();" />
  65          <?php _e("\"Manage your subscription\" link ?", MP_TXTDOM); ?>
  66      </label>
  67      <label for='<?php echo $this->get_field_id('txtsubmgt'); ?>'>
  68          <input type='text' id='<?php echo $this->get_field_id('txtsubmgt'); ?>' name='<?php echo $this->get_field_name('txtsubmgt'); ?>' value="<?php echo esc_attr($txtsubmgt); ?>" class='widefat<?php if (!$instance['urlsubmgt']) echo ' hide-if-js'; ?>' />
  69      </label>
  70      <br />
  71      <div style='background-color:#f1f1f1;border:solid 1px #ddd;color:#999;padding:3px;'>
  72          <small>
  73              <?php _e('Preloaded :', MP_TXTDOM); ?>
  74              <label for='<?php echo $this->get_field_id('css'); ?>'>
  75                  <input type='checkbox' id='<?php echo $this->get_field_id('css'); ?>' name='<?php echo $this->get_field_name('css'); ?>'<?php if ($instance['css']) checked(true); ?> /> <?php _e('css', MP_TXTDOM); ?> 
  76              </label>
  77              <label for='<?php echo $this->get_field_id('jq'); ?>'>
  78                  <input type='checkbox' id='<?php echo $this->get_field_id('jq'); ?>' name='<?php echo $this->get_field_name('jq'); ?>'<?php if ($instance['jq']) checked(true); ?> /> <?php _e('jQuery', MP_TXTDOM); ?> 
  79              </label>
  80              <label for='<?php echo $this->get_field_id('js'); ?>'>    
  81                  <input type='checkbox' id='<?php echo $this->get_field_id('js'); ?>' name='<?php echo $this->get_field_name('js'); ?>'<?php if ($instance['js']) checked(true); ?> /> <?php _e('javascript', MP_TXTDOM); ?>
  82          </label>
  83          </small>
  84      </div>
  85  </p>
  86  <?php
  87      }
  88  
  89  ////  Defaults  ////
  90  
  91  	public static function form_defaults($options = array()) 
  92      {
  93          $defaults = array(    'css'            => false,
  94                          'jq'            => false,
  95                          'js'            => false,
  96                          'urlsubmgt'     => false, 
  97                          'txtbutton'     => __('Subscribe', MP_TXTDOM), 
  98                          'txtsubmgt'     => __('Manage your subscription', MP_TXTDOM), 
  99                          'txtloading'    => __('Loading...', MP_TXTDOM), 
 100  
 101                          'txtfield'         => __('Your email', MP_TXTDOM), 
 102                          'txtfieldname'     => __('Your name', MP_TXTDOM), 
 103                          'txtwait'        => __('Waiting for ...', MP_TXTDOM), 
 104                          'txtwaitconf'     => __('Waiting for your confirmation', MP_TXTDOM), 
 105                          'txtallready'     => __('You have already subscribed', MP_TXTDOM), 
 106                          'txtvalidemail'     => __('Enter a valid email !', MP_TXTDOM), 
 107                          'txterrconf'     => __('ERROR. resend confirmation email failed', MP_TXTDOM), 
 108                          'txtdberror'     => __('ERROR in the database : subscriber not inserted', MP_TXTDOM)
 109                      );
 110  
 111          $defaults = apply_filters('MailPress_form_defaults', $defaults);
 112          $options  = wp_parse_args( $options, $defaults );
 113          $options  = apply_filters('MailPress_form_options', $options);
 114          return $options;
 115      }
 116  
 117  ////  Form  ////
 118  
 119  	public static function widget_form($options = array()) 
 120      {
 121          static $wp_head = false;
 122  
 123          $email = $name = $message = $widget_title = '';
 124  
 125          $options  = self::form_defaults($options);
 126  
 127          $id = $options['widget_id'];
 128  
 129          if (isset($_POST['MailPress_submit']) && ($_POST['id'] == '_MP_' . $id))
 130              list($message, $email, $name) = self::insert(false);
 131          else
 132          {
 133              $user = wp_get_current_user();
 134              switch (true)
 135              {
 136                  case ( $user->ID ) :
 137  // user connected
 138                      $email = $user->user_email;
 139                      $name  = $user->display_name;
 140                  break;
 141                  default :
 142  // user as already a cookie !
 143                      $email = (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) ? $_COOKIE['comment_author_email_' . COOKIEHASH] : '';
 144                      $name  = (isset($_COOKIE['comment_author_'       . COOKIEHASH])) ? $_COOKIE['comment_author_'       . COOKIEHASH] : '';
 145                  break;
 146              }
 147              $email = apply_filters('MailPress_form_email', $email);
 148              $name  = apply_filters('MailPress_form_name',  $name);
 149  
 150              if ( MP_User::is_user($email) ) $email = $name = ''; 
 151          }
 152          if ('' == $email) $email = $options['txtfield'];
 153          if ('' == $name)  $name  = $options['txtfieldname'];
 154  ?>
 155  
 156  <!-- start of code generated by MailPress (<?php echo MP_Version; ?>) -->
 157  <?php
 158          if (!$wp_head)
 159          {
 160              $wp_head = true;
 161              if (!$options['css']) 
 162              {
 163                  $root = MP_CONTENT_DIR . 'advanced/subscription-form';
 164                  $root = apply_filters('MailPress_advanced_subscription-form_root', $root);
 165                  echo "<style  type='text/css'>\n"; include "$root/style.css"; echo "\n</style>\n"; 
 166              }
 167              if (!$options['jq'])
 168              {
 169                  if (defined('MP_wp_enqueue_script') && MP_wp_enqueue_script)
 170                  {
 171                      wp_enqueue_script('jquery');
 172                  }
 173                  else
 174                  {
 175                      echo "<script type='text/javascript' src='" . site_url() . "/wp-includes/js/jquery/jquery.js'></script>\n";
 176                  }
 177              }
 178              if (!$options['js'])
 179              {
 180                  $js = '/' . MP_PATH . 'mp-includes/js/mp_form.js';
 181                  if (defined('MP_wp_enqueue_script') && MP_wp_enqueue_script)
 182                  {
 183                      wp_register_script( 'mailpress_widget',    $js);
 184                      wp_localize_script( 'mailpress_widget',     'MP_Widget', array(
 185                          'url' => MP_Action_url
 186                      ));
 187                      wp_enqueue_script('mailpress_widget');
 188                  }
 189                  else
 190                  {
 191                      echo "<script type='text/javascript'>\n/* <![CDATA[ */\nvar MP_Widget = {\n\turl: '" . MP_Action_url . "'\n};\n/* ]]> */\n</script>\n";
 192                      echo "<script type='text/javascript' src='" . site_url() . $js . "'></script>\n";
 193                  }
 194              }
 195          }
 196  
 197          $imgloading = apply_filters('MailPress_form_imgloading', site_url() . '/' . MP_PATH . 'mp-includes/images/loading.gif');
 198  ?>
 199  <div class='MailPress' id='_MP_<?php echo $id; ?>'>
 200      <div class='mp-container'>
 201          <div class='mp-message'></div>
 202          <div class='mp-loading'><img src='<?php echo $imgloading; ?>' alt='<?php  echo $options['txtloading']; ?>' title='<?php  echo $options['txtloading']; ?>' /><?php  echo $options['txtloading']; ?></div>
 203          <div class='mp-formdiv'>
 204  <?php if ('' != $message) echo $message . "\n"; ?>
 205              <form class='mp-form' method='post' action=''>
 206                  <input type='hidden' name='action'     value='add_user_fo' />
 207                  <input type='hidden' name='id'     value='<?php echo '_MP_' . $id; ?>' />
 208                  <input type='text'   name='_MP_email'      value="<?php echo $email; ?>" class='MailPressFormEmail' size='25' onfocus="if(this.value=='<?php echo esc_js($options['txtfield']); ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo esc_js($email); ?>';" /><br />
 209                  <input type='text'   name='_MP_name'      value="<?php echo $name;  ?>" class='MailPressFormName'  size='25' onfocus="if(this.value=='<?php echo esc_js($options['txtfieldname']); ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo esc_js($name); ?>';" /><br />
 210  <?php do_action('MailPress_form', $email, $options); ?>
 211                  <input class='MailPressFormSubmit mp_submit' type='submit' name='MailPress_submit' value="<?php echo esc_attr($options['txtbutton']); ?>" />
 212              </form>
 213          </div>
 214      </div>
 215  <?php 
 216  $url = ($options['urlsubmgt']) ? esc_url(MP_WP_User::get_unsubscribe_url()) : false;
 217  if ($url) :
 218  ?>
 219      <div id='mp-urlsubmgt'><a href='<?php echo $url; ?>'><?php echo (!empty($options['txtsubmgt'])) ? esc_attr($options['txtsubmgt']) : __('Manage your subscription', MP_TXTDOM); ?></a></div>
 220  <?php
 221  endif;
 222  ?>
 223  <?php do_action('MailPress_form_div_misc', $email, $options); ?>
 224  </div>
 225  <!-- end of code generated by MailPress (<?php echo MP_Version; ?>) -->
 226  <?php
 227      }
 228  
 229  	public static function insert($ajax = true)
 230      {
 231          $bots_useragent = array('googlebot', 'google', 'msnbot', 'ia_archiver', 'lycos', 'jeeves', 'scooter', 'fast-webcrawler', 'slurp@inktomi', 'turnitinbot', 'technorati', 'yahoo', 'findexa', 'findlinks', 'gaisbo', 'zyborg', 'surveybot', 'bloglines', 'blogsearch', 'ubsub', 'syndic8', 'userland', 'gigabot', 'become.com');
 232          $useragent = $_SERVER['HTTP_USER_AGENT'];
 233          foreach ($bots_useragent as $bot) if (stristr($useragent, $bot) !== false) return false;                // goodbye bot !
 234  
 235          $message = $email = $name = '';
 236          $options = self::form_defaults();
 237  
 238          $email = ( isset($_POST['_MP_email']) ) ? $_POST['_MP_email'] : '';                                    //has the user entered an email 
 239          $name  = ( isset($_POST['_MP_name']) )  ? $_POST['_MP_name']  : '';    
 240  
 241          if ( '' == $email || $options['txtfield'] == $email ) 
 242          {                                                                        // check for bot
 243              $message = "<span class='error'>" . $options['txtwait'] . "</span>";
 244              $email = $options['txtfield'];
 245          }
 246          else
 247          {
 248              $name = ( '' == $name || $options['txtfieldname'] == $name ) ? '' : $name;
 249              $add = MP_User::add($email, $name);
 250              $shortcode_message = apply_filters('MailPress_form_submit', '', $email);
 251              $message = ($add['result']) ? "<span class='success'>" . $add['message'] . $shortcode_message . "</span><br />" : "<span class='error'>" . $add['message']  . $shortcode_message . "</span><br />";
 252              $email   = ($add['result']) ? $email : $options['txtfield'];
 253              $name    = ($add['result']) ? $name  : $options['txtfieldname'];
 254              if ($add['result']) 
 255                  if ($ajax)     do_action('MailPress_form_user_added_ajax', $email, $name, $options);
 256                  else         do_action('MailPress_form_user_added', $email, $name, $options);
 257          }
 258  
 259          $user = wp_get_current_user();
 260          if ( !$user->ID ) 
 261          {
 262              $comment_cookie_lifetime = apply_filters('comment_cookie_lifetime', 30000000);
 263              setcookie('comment_author_'       . COOKIEHASH, $name,  time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
 264              setcookie('comment_author_email_' . COOKIEHASH, $email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
 265          }
 266  
 267          return array($message, $email, $name);
 268      }
 269  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1