[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  class MP_User
   3  {
   4      const object_type = 'user';
   5  
   6      const status_deleted = 'deleted';
   7  
   8  	public static function get( $user, $output = OBJECT ) 
   9      {
  10          switch ( true )
  11          {
  12              case ( empty( $user ) ) :
  13                  if ( isset( $GLOBALS['mp_user'] ) )     $_user = & $GLOBALS['mp_user'];
  14                  else                        return null;
  15              break;
  16              case ( is_object( $user ) ) :
  17                  wp_cache_add( $user->id, $user, 'mp_user' );
  18                  $_user = $user;
  19              break;
  20              default :
  21                  if ( isset( $GLOBALS['mp_user'] ) && ( $GLOBALS['mp_user']->id == $user ) ) 
  22                  {
  23                      $_user = & $GLOBALS['mp_user'];
  24                  } 
  25                  elseif ( ! $_user = wp_cache_get( $user, 'mp_user' ) ) 
  26                  {
  27                         global $wpdb;
  28                      $_user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->mp_users WHERE id = %d LIMIT 1", $user ) );
  29                      if ( $_user ) wp_cache_add( $_user->id, $_user, 'mp_user' );
  30                  }
  31              break;
  32          }
  33  
  34          if ( $output == OBJECT ) {
  35              return $_user;
  36          } elseif ( $output == ARRAY_A ) {
  37              return get_object_vars( $_user );
  38          } elseif ( $output == ARRAY_N ) {
  39              return array_values( get_object_vars( $_user ) );
  40          } else {
  41              return $_user;
  42          }
  43      }
  44  
  45  	public static function get_var( $var, $key_col, $key, $format = '%s' ) 
  46      {
  47          global $wpdb;
  48          return $wpdb->get_var( $wpdb->prepare( "SELECT $var FROM $wpdb->mp_users WHERE $key_col = $format LIMIT 1;", $key ) );
  49      }
  50  
  51  	public static function get_status( $id ) 
  52      {
  53          $result = self::get_var( 'status', 'id', $id );
  54          return ( $result == NULL ) ? self::status_deleted : $result;
  55      }
  56  
  57  	public static function update_status( $id, $status ) 
  58      {
  59          wp_cache_delete( $id, 'mp_user' );
  60  
  61          $data = $format = $where = $where_format = array();
  62  
  63          $REMOTE_ADDR         = filter_input( INPUT_SERVER, 'REMOTE_ADDR' );
  64          $HTTP_USER_AGENT     = filter_input( INPUT_SERVER, 'HTTP_USER_AGENT' );
  65  
  66          $data['status']         = $status;                                                     $format[] = '%s';
  67          $data['laststatus']         = current_time( 'mysql' );                                            $format[] = '%s';
  68          $data['laststatus_IP']     = ( isset( $REMOTE_ADDR ) )     ? $REMOTE_ADDR : '';                           $format[] = '%s';
  69          $data['laststatus_agent']     = ( isset( $HTTP_USER_AGENT ) ) ? trim( strip_tags( $HTTP_USER_AGENT ) ) : '';     $format[] = '%s';
  70          $data['laststatus_user_id']= MP_WP_User::get_id();                                            $format[] = '%d';
  71  
  72          $where['id']             = ( int ) $id;                                                    $where_format[] = '%d';
  73  
  74          global $wpdb;
  75          return ( $wpdb->update( $wpdb->mp_users, $data, $where, $format, $where_format ) ) ? $data['laststatus'] : false;
  76      }
  77  
  78  	public static function set_status( $id, $status ) 
  79      {
  80          switch( $status ) 
  81          {
  82              case 'active':
  83                      return self::activate( $id );
  84              break;
  85              case 'waiting':
  86                      return self::deactivate( $id );
  87              break;
  88              case 'bounced':
  89                      return self::bounced( $id );
  90              break;
  91              case 'unsubscribed':
  92                      return self::unsubscribed( $id );
  93              break;
  94              case 'delete':
  95                      return self::delete( $id );
  96              break;
  97          }
  98          wp_cache_delete( $id, 'mp_user' );
  99          return true;
 100      }
 101  
 102  	public static function activate( $id ) 
 103      {
 104          $the_status = 'active';
 105          $mp_user = self::get( $id );
 106  
 107          if ( !$mp_user ) return false;
 108          if ( $the_status == $mp_user->status ) return true;
 109  
 110          if ( in_array( $mp_user->status, array( 'waiting' ) ) )
 111          {
 112              $first = ( self::get_var( 'created', 'id', $id ) == self::get_var( 'laststatus', 'id', $id ) );
 113              $update = self::update_status( $id, $the_status );
 114              if ( $update )     
 115              {
 116                  new MP_Stat( 'u', $the_status, 1 );
 117                  do_action( 'MailPress_activate_user', $id, 'MailPress_activate_user' ); 
 118                  if ( $first ) do_action( 'MailPress_activate_user_1st', $id, 'MailPress_activate_user_1st' ); 
 119                  self::send_succesfull_subscription( $mp_user->email, $mp_user->name, $mp_user->confkey );
 120              }
 121              return $update;
 122          }
 123          return false;
 124      }
 125  
 126  	public static function deactivate( $id ) 
 127      {
 128          $the_status = 'waiting';
 129          $status = self::get_status( $id );
 130  
 131          if ( $the_status == $status ) return true;
 132  
 133          if ( in_array( $status, array( 'active', 'bounced', 'unsubscribed' ) ) )
 134          {
 135              $update = self::update_status( $id, $the_status );
 136  
 137              if ( $update )     
 138              {
 139                  if ( 'active' == $status )
 140                  {
 141                      do_action( 'MailPress_deactivate_user', $id, 'MailPress_deactivate_user' ); 
 142                      new MP_Stat( 'u', 'active', -1 );
 143                  }
 144                  else
 145                      new MP_Stat( 'u', $the_status, 1 );
 146              }
 147              return $update;
 148          }
 149          return false;
 150      }
 151  
 152  	public static function bounced( $id ) 
 153      {
 154          $the_status = 'bounced';
 155          $status = self::get_status( $id );
 156  
 157          if ( $the_status == $status ) return true;
 158  
 159          $update = self::update_status( $id, $the_status );
 160          if ( $update ) 
 161          {
 162              do_action( 'MailPress_bounced_user', $id );
 163              new MP_Stat( 'u', 'waiting', -1 );
 164              if ( 'active' == $status ) new MP_Stat( 'u', 'active', -1 );
 165          }
 166          return $update;
 167      }
 168  
 169  	public static function unsubscribed( $id ) 
 170      {
 171          $the_status = 'unsubscribed';
 172          $status = self::get_status( $id );
 173  
 174          if ( $the_status == $status ) return true;
 175  
 176          $update = self::update_status( $id, $the_status );
 177          if ( $update ) 
 178          {
 179              do_action( 'MailPress_unsubscribe_user', $id );
 180              new MP_Stat( 'u', 'waiting', -1 );
 181              if ( 'active' == $status ) new MP_Stat( 'u', 'active', -1 );
 182          }
 183          return $update;
 184      }
 185  
 186  	public static function delete( $id ) 
 187      {
 188          do_action( 'MailPress_delete_user', $id );
 189  
 190          $the_status = self::status_deleted;
 191          $status = self::get_status( $id );
 192  
 193          if ( $the_status == $status ) return true;
 194  
 195          if ( 'waiting' == $status ) new MP_Stat( 'u', 'waiting', -1 );
 196          if ( 'active' == $status ) { new MP_Stat( 'u', 'waiting', -1 ); new MP_Stat( 'u', 'active', -1 ); }
 197  
 198          MP_User_meta::delete( $id );
 199  
 200          wp_cache_delete( $id, 'mp_user' );
 201  
 202          global $wpdb;
 203          return $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_users WHERE id = %d ; ", $id ) );
 204      }
 205  
 206  	public static function is_user( $email = '' ) 
 207      {
 208          return ( MailPress::is_email( $email ) && self::status_deleted != self::get_status_by_email( $email ) ) ; 
 209      }
 210  
 211  	public static function get_id( $key ) 
 212      {
 213          return self::get_var( 'id', 'confkey', $key );
 214      }
 215  
 216  	public static function get_id_by_email( $email ) 
 217      {
 218          return self::get_var( 'id', 'email', $email );
 219      }
 220  
 221  	public static function get_email( $id ) 
 222      {
 223          return self::get_var( 'email', 'id', $id );
 224      }
 225  
 226  	public static function get_status_by_email( $email ) 
 227      {
 228            $result = self::get_var( 'status', 'email', $email );
 229          return ( $result == NULL ) ? self::status_deleted : $result;
 230      }
 231  
 232  	public static function get_key_by_email( $email ) 
 233      {
 234          return self::get_var( 'confkey', 'email', $email );
 235      }
 236  
 237  	public static function get_flag_IP()
 238      {
 239          global $mp_user;
 240          return ( ( 'ZZ' == $mp_user->created_country ) || empty( $mp_user->created_country ) ) ? '' : '<div class="mp_flag mp_flag_' . strtolower( $mp_user->created_country ) . '" title="' . esc_attr( strtolower( $mp_user->created_country ) ) . '"></div>';
 241      }
 242  
 243  /// USER ///
 244  
 245  	public static function insert( $email, $name, $args = array() )
 246      {
 247  
 248          $REMOTE_ADDR         = filter_input( INPUT_SERVER, 'REMOTE_ADDR' );
 249          $HTTP_USER_AGENT     = filter_input( INPUT_SERVER, 'HTTP_USER_AGENT' );
 250  
 251          $defaults = array(     'status'        => 'waiting',
 252                          'confkey'         => md5( uniqid( rand(), 1 ) ),
 253                          'created'        => current_time( 'mysql' ),
 254                          'created_IP'    => trim( $REMOTE_ADDR ),
 255                          'created_agent'    => trim( strip_tags( $HTTP_USER_AGENT ) ),
 256                          'created_user_id'=> MP_WP_User::get_id(),
 257                          'stopPropagation'=> false,
 258           );
 259          $r = wp_parse_args( $args, $defaults );
 260          extract( $r );
 261  
 262          $data = $format = array();
 263  
 264          $data['email']             = $email;             $format[] = '%s';
 265          $data['name']             = $name;             $format[] = '%s';
 266          $data['status']         = $status;         $format[] = '%s';
 267          $data['confkey']         = $confkey;        $format[] = '%s';
 268          $data['created']         = $created;        $format[] = '%s';
 269          $data['created_IP']         = $created_IP;        $format[] = '%s';
 270          $data['created_agent']     = $created_agent;     $format[] = '%s';
 271          $data['created_user_id']    = $created_user_id;    $format[] = '%d';
 272  
 273          $data['created_country']    = MP_Ip::get_country( $data['created_IP'] );                                        $format[] = '%s';
 274          $data['created_US_state']    = ( 'US' == $data['created_country'] ) ? MP_Ip::get_USstate( $data['created_IP'] ) : 'ZZ';     $format[] = '%s';
 275  
 276          $data['laststatus']           = $created;        $format[] = '%s';
 277          $data['laststatus_IP']     = $created_IP;        $format[] = '%s';
 278          $data['laststatus_agent']    = $created_agent;     $format[] = '%s';
 279          $data['laststatus_user_id']= $created_user_id;    $format[] = '%d';
 280  
 281          global $wpdb;
 282          if ( !$wpdb->insert( $wpdb->mp_users, $data, $format ) ) return false;
 283  
 284          $mp_user_id = $wpdb->insert_id;
 285  
 286          new MP_Stat( 'u', 'waiting', 1 );
 287          if ( 'active' == $status ) new MP_Stat( 'u', 'active', 1 );
 288   
 289          do_action( 'MailPress_insert_user', $mp_user_id );
 290          if ( ( 'active' == $status ) && !$stopPropagation ) 
 291          {
 292              do_action( 'MailPress_activate_user', $mp_user_id, 'MailPress_activate_user' );
 293              do_action( 'MailPress_activate_user_1st', $mp_user_id, 'MailPress_activate_user_1st' );
 294          }
 295  
 296          return $mp_user_id;
 297      }
 298  
 299  	public static function update_name( $id, $name ) 
 300      {
 301          $name = stripslashes( $name );
 302  
 303          do_action( 'MailPress_update_name', $id, $name );
 304  
 305          $data = $format = $where = $where_format = array();
 306  
 307          $data['name']         = $name;         $format[] = '%s';
 308          $where['id']         = ( int ) $id;    $where_format[] = '%d';
 309  
 310          global $wpdb;
 311          return $wpdb->update( $wpdb->mp_users, $data, $where, $format, $where_format );
 312      }
 313  
 314  	public static function set_ip( $id, $ip )
 315      {
 316          $data = $format = $where = $where_format = array();
 317  
 318          $data['created_IP']     = $ip;                     $format[] = '%s';
 319          $data['created_country'] = MP_Ip::get_country( $ip );     $format[] = '%s';
 320          $data['created_US_state']= MP_Ip::get_USstate( $ip );     $format[] = '%s';
 321  
 322          $where['id']          = ( int ) $id;    $where_format[] = '%d';
 323  
 324          global $wpdb;
 325          return $wpdb->update( $wpdb->mp_users, $data, $where, $format, $where_format );
 326      }
 327  
 328  
 329  
 330  	public static function add( $email, $name ) 
 331      {
 332          $return = array();
 333          $email  = trim( $email );
 334  
 335          $defaults = MP_Widget::form_defaults();
 336  
 337          if ( !MailPress::is_email( $email ) )
 338          {
 339              $return['result']  = false;
 340              $return['message'] = $defaults['txtvalidemail'];
 341              return $return;
 342          }
 343          
 344          $status = self::get_status_by_email( $email );                                //Test if subscription already exists
 345  
 346          switch ( $status )
 347          {
 348              case self::status_deleted :
 349                  $key = md5( uniqid( rand(), 1 ) );                                //generate key
 350                  if ( self::send_confirmation_subscription( $email, $name, $key ) )            //email was sent
 351                  {
 352                      if ( self::insert( $email, $name, array( 'confkey' => $key ) ) )
 353                      {
 354                          $return['result']  = true;
 355                          $return['message'] = $defaults['txtwaitconf'] ;
 356                          return $return;
 357                      }
 358                      else
 359                      {
 360                          $return['result']  = false;
 361                          $return['message'] = $defaults['txtdberror'];
 362                          return $return;
 363                      }
 364                  }
 365                  $return['result']  = false;
 366                  $return['message'] = $defaults['txterrconf'];
 367                  return $return;
 368              break;
 369              case 'active' :
 370                  $return['result']  = false;
 371                  $return['message'] = $defaults['txtallready'];
 372                  return $return;
 373              break;
 374              case 'unsubscribed' :
 375                  $id = self::get_id_by_email( $email );
 376  
 377                  self::update_name( $id, $name );
 378                  self::set_status( $id, 'waiting' );
 379              case 'waiting' :
 380                  if ( self::send_confirmation_subscription( $email, $name, self::get_key_by_email( $email ) ) )
 381                  {
 382                      $return['result']  = true;
 383                      $return['message'] = $defaults['txtwaitconf'] . ( ( defined( 'MP_DEBUG_LOG' ) ) ? ' <small>( 2 )</small>' : '' );
 384                  }
 385                  else
 386                  {
 387                      $return['result']  = false;
 388                      $return['message'] = $defaults['txterrconf']  . ( ( defined( 'MP_DEBUG_LOG' ) ) ? ' <small>( 2 )</small>' : '' );
 389                  }
 390                  return $return;
 391              break;
 392          }
 393      }
 394  
 395  //// Recipients queries ////
 396  
 397  	public static function get_mailinglists()
 398      {
 399          add_filter( 'MailPress_mailinglists_optgroup',     array( __CLASS__, 'mailinglists_optgroup' ), 8, 2 );
 400  
 401          $draft_dest = array (     ''  => '&#160;', 
 402                          '1' => __( 'to blog', 'MailPress' ), 
 403                          '4' => __( 'all (active + waiting)', 'MailPress' ),
 404                          '5' => __( 'waiting', 'MailPress' ),
 405                        );
 406          return apply_filters( 'MailPress_mailinglists', $draft_dest );
 407      }
 408  
 409  	public static function mailinglists_optgroup( $label, $optgroup ) 
 410      {
 411          if ( __CLASS__ == $optgroup ) return __( 'Subscribers', 'MailPress' );
 412          return $label;
 413      }
 414  
 415  /// MAIL URLs ///
 416  
 417  	private static function _get_url( $action, $key ){
 418          global $wp_rewrite;
 419          global $mp_general;
 420          
 421          switch( $mp_general['subscription_mngt'] )
 422          {
 423              case 'ajax':
 424                  return add_query_arg( array( 'action' => 'mp_mlinks', $action => $key ), admin_url( 'admin-ajax.php' ) );
 425              break;
 426              case 'page_id':
 427                  $p = get_post( $mp_general['id'] );
 428                  $s = ( $wp_rewrite->get_page_permastruct() != '' && isset( $p->post_status ) && 'draft' != $p->post_status )? '?':'&';
 429                  $id = apply_filters( 'MP_User_get_url_general_id', $mp_general['id'], 'page' );
 430                  return get_permalink( $id ) . $s . $action . '=' . $key ;
 431              break;
 432              case 'cat':
 433                  $a = $wp_rewrite->get_category_permastruct();
 434                  $s = ( !empty( $a ) )? '?':'&';
 435                  $id = apply_filters( 'MP_User_get_url_general_id', $mp_general['id'], 'category' );
 436                  return get_category_link( $id ) . $s . $action . '=' . $key ;
 437              break;
 438              default:
 439                  $id = apply_filters( 'MP_User_get_url_general_id', $mp_general['id'], $mp_general['subscription_mngt'] );
 440                  return home_url() . '/?' . $mp_general['subscription_mngt'] . '=' . $id . '&' . $action . '=' . $key ;
 441              break;
 442          }
 443      }
 444   
 445   	public static function get_subscribe_url( $key )
 446       {
 447          return self::_get_url( 'add', $key );
 448       }
 449   
 450   	public static function get_unsubscribe_url( $key )
 451       {
 452          return self::_get_url( 'del', $key );
 453       }
 454   
 455   	public static function get_delall_url( $key )
 456       {
 457          return self::_get_url( 'delall', $key );
 458       }
 459   
 460   	public static function get_view_url( $key, $id )
 461       {
 462          return self::_get_url( 'view', $key ) . "&id=$id";
 463       }
 464   
 465   	public static function get_arch_url( $id, $key = 0 )
 466       {
 467          return self::_get_url( 'arch', $key ) . "&id=$id";
 468       }
 469  
 470  ////    send subscription mail functions     ////
 471  
 472  	public static function send_confirmation_subscription( $email, $name, $key ) 
 473      {
 474          $url                = home_url();
 475  
 476          $mail                = new stdClass();
 477          $mail->Template         = 'new_subscriber';
 478  
 479          $mail->toemail         = $email;
 480          $mail->toname        = $name;
 481  
 482          $mail->subscribe        = self::get_subscribe_url( $key );
 483  
 484          $mail->subject        = sprintf( __( '[%1$s] Waiting for %2$s', 'MailPress' ), get_bloginfo( 'name' ), $mail->toname );
 485  
 486          $message  = sprintf( __( 'Please, confirm your subscription to %1$s emails by clicking the following link :', 'MailPress' ), get_bloginfo( 'name' ) );
 487          $message .= "\n\n";
 488          $message .= '{{subscribe}}';
 489          $message .= "\n\n";
 490          $message .= __( 'If you do not want to receive more emails, ignore this one !', 'MailPress' );
 491          $message .= "\n\n";
 492          $mail->plaintext       = $message;
 493  
 494          $message  = sprintf( __( 'Please, confirm your subscription to %1$s emails by clicking the following link :', 'MailPress' ), '<a href="' . $url . '">' . get_bloginfo( 'name' ) . "</a>" );
 495          $message .= '<br /><br />';
 496          $message .= '<a href="{{subscribe}}">' . __( 'Confirm', 'MailPress' ) . '</a>';
 497          $message .= '<br /><br />';
 498          $message .= __( 'If you do not want to receive more emails, ignore this one !', 'MailPress' );
 499          $message .= '<br /><br />';
 500          $mail->html            = $message;
 501  
 502          return MailPress::mail( $mail );
 503      }
 504  
 505  	public static function send_succesfull_subscription( $email, $name, $key ) 
 506      {
 507          $url         = home_url();
 508  
 509          $mail                = new stdClass();
 510          $mail->Template         = 'confirmed';
 511  
 512          $mail->toemail         = $email;
 513          $mail->toname        = $name;
 514  
 515          $mail->subject        = sprintf( __( '[%1$s] Successful subscription for %2$s', 'MailPress' ), get_bloginfo( 'name' ), $email );
 516  
 517          $message  = sprintf( __( 'We confirm your subscription to %1$s emails', 'MailPress' ), get_bloginfo( 'name' ) );
 518          $message .= "\n\n";
 519          $message .= __( 'Congratulations !', 'MailPress' );
 520          $message .= "\n\n";
 521          $mail->plaintext       = $message;
 522  
 523          $message  = sprintf( __( 'We confirm your subscription to %1$s emails', 'MailPress' ), '<a href="' . $url . '">' . get_bloginfo( 'name' ) . '</a>' );
 524          $message .= '<br /><br />';
 525          $message .= __( 'Congratulations !', 'MailPress' );
 526          $message .= '<br /><br />';
 527          $mail->html            = $message;
 528  
 529          return MailPress::mail( $mail );
 530      }
 531  
 532  /* ajax */
 533  	public static function add_object()
 534      {
 535          MP_WP_List::add_object( self::object_type );
 536      }
 537  
 538  	public static function dim_object() 
 539      {
 540          MP_WP_List::dim_object( self::object_type );
 541      }
 542  
 543  	public static function delete_object() 
 544      {
 545          MP_WP_List::delete_object( self::object_type );
 546      }
 547  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1