[ Index ]

MailPress 7.2

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


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1