[ Index ]

MailPress 544

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

title

Body

[close]

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

   1  <?php
   2  if (class_exists('MailPress') && !class_exists('MailPress_mailinglist_user_role') )
   3  {
   4  /*
   5  Plugin Name: MailPress_mailinglist_user_role
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/mailinglist_user_role/
   7  Description: New Mail : Mailing lists : based on WP user roles (<span style='color:red;'>beware !</span> <span style='color:#D54E21;'>Sync_wordpress_user</span> add-on STRONGLY required)
   8  Version: 5.4.4
   9  */
  10  
  11  class MailPress_mailinglist_user_role
  12  {
  13  	function __construct()
  14      {
  15  // for sending mails
  16          add_filter('MailPress_mailinglists_optgroup',     array(__CLASS__, 'mailinglists_optgroup'), 20, 2);
  17          add_filter('MailPress_mailinglists',             array(__CLASS__, 'mailinglists'), 20, 1);
  18          add_filter('MailPress_query_mailinglist',         array(__CLASS__, 'query_mailinglist'), 20, 2);
  19      }
  20  
  21  //// Sending Mails ////
  22  
  23  	public static function mailinglists_optgroup( $label, $optgroup ) 
  24      {
  25          if (__CLASS__ == $optgroup) return __('WP User Roles', MP_TXTDOM);
  26          return $label;
  27      }
  28  
  29  	public static function mailinglists( $draft_dest = array() ) 
  30      {
  31          global $wpdb, $wp_roles;
  32  
  33          $query = "SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '" . $wpdb->get_blog_prefix(get_current_blog_id()) . "capabilities' AND meta_value LIKE '%%%s%%'";
  34  
  35          foreach ( $wp_roles->get_names() as $role => $name )
  36              if ( $wpdb->get_var( sprintf( $query, $wpdb->esc_like( $role ) ) ) )
  37                  $draft_dest[__CLASS__ . '~' . $role] = sprintf( __('To all "%1$s"', MP_TXTDOM), translate_user_role( $name ) );
  38  
  39          return $draft_dest;
  40      }
  41  
  42  	public static function query_mailinglist( $query, $draft_toemail ) 
  43      {
  44          if ($query) return $query;
  45  
  46          $role = str_replace(__CLASS__ . '~', '', $draft_toemail, $count);
  47          if (0 == $count) return $query;
  48          if (empty($role))  return $query;
  49  
  50          $users = array();
  51          $results = get_users( array( 'role' => $role, 'fields' => array('user_email') ) );
  52                  foreach ($results as $result) $users[] = $result->user_email;
  53          if (empty($users)) return $query;
  54  
  55          global $wpdb;
  56          return "SELECT DISTINCT c.id, c.email, c.name, c.status, c.confkey FROM $wpdb->mp_users c WHERE c.email IN ('" . join("', '", $users) . "') AND c.status = 'active' ";
  57      }
  58  }
  59  new MailPress_mailinglist_user_role();
  60  }


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