[ Index ]

MailPress 544

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


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