[ Index ]

MailPress 544

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

title

Body

[close]

/mp-content/add-ons/ -> MailPress_name_fields.php (source)

   1  <?php
   2  if (class_exists('MailPress') && !class_exists('MailPress_name_fields'))
   3  {
   4  /*
   5  Plugin Name: MailPress_name_fields
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/name_fields/
   7  Description: Users : generate custom fields based on subscriber's name (original idea of Graham)
   8  Version: 5.4.4
   9  */
  10  
  11  class MailPress_name_fields
  12  {
  13  	function __construct()
  14      {
  15  // for wp admin
  16          if (is_admin())
  17          {
  18          // install
  19              register_activation_hook(plugin_basename(__FILE__),     array(__CLASS__, 'install'));
  20          }
  21          add_action('MailPress_insert_user', array(__CLASS__, 'insert_user'), 8, 1);
  22          add_action('MailPress_update_name', array(__CLASS__, 'update_name'), 8, 2);
  23  
  24          add_filter('MailPress_replacements_mp_mail', array(__CLASS__, 'replacements_mp_mail'), 8, 1);
  25      }
  26  
  27  	public static function install()
  28      {
  29          global $wpdb;
  30  
  31          $query = "SELECT DISTINCT id, name FROM $wpdb->mp_users;";
  32  
  33          $users = $wpdb->get_results($query);
  34  
  35          foreach($users as $user) self::update_name($user->id, $user->name);
  36      }
  37  
  38  	public static function insert_user($mp_user_id)
  39      {
  40          $mp_user = MP_User::get($mp_user_id);
  41          if ($mp_user) self::update_name($mp_user_id, $mp_user->name);
  42      }
  43  
  44  	public static function update_name($mp_user_id, $name)
  45      {
  46          //$space = strpos($name,' ');
  47          //$x['firstname'] = ($name != '') ? ucfirst(strtolower($space ? substr($name,0,$space) : $name)) : false;
  48          //$x['lastname']  = ($space) ? ucfirst(strtolower(substr($name,$space+1))) : false;
  49          //$x['fullname']  = ucwords(strtolower($name));
  50  
  51          preg_match('/^(\S+)(?:\s+(.*))?/u', $name, $matches);
  52          $x['firstname']= (empty($matches[1])) ? false : mb_strtoupper(mb_substr($matches[1], 0, 1)) . mb_substr($matches[1], 1);
  53          $x['lastname'] = (empty($matches[2])) ? false : mb_strtoupper(mb_substr($matches[2], 0, 1)) . mb_substr($matches[2], 1);
  54          $x['fullname'] = (empty($matches[0])) ? $name : $x['firstname'] . ' ' . $x['lastname'];
  55  
  56          foreach($x as $key => $value)
  57          {
  58              if ($value === false)
  59              {
  60                  MP_User_meta::delete($mp_user_id, $key);
  61                  continue;
  62              }
  63              if (!MP_User_meta::add($mp_user_id, $key, $value, true))
  64                  MP_User_meta::update($mp_user_id, $key, $value);
  65          }
  66      }
  67  
  68  	public static function replacements_mp_mail($replacements)
  69      {
  70          foreach(array('firstname', 'lastname', 'fullname') as $cf)
  71              if (!isset($replacements ['{{' . $cf . '}}'])) $replacements ['{{' . $cf . '}}'] = __('Friend', MP_TXTDOM);
  72  
  73          return $replacements;
  74      }
  75  }
  76  new MailPress_name_fields();
  77  }


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