[ Index ]

MailPress 7.2

[ 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: 7.2
   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          add_filter( 'MailPress_query_list_id',             array( __CLASS__, 'query_list_id' ), 20, 2 );
  20      }
  21  
  22  //// List-Unsubscribe ////
  23  
  24  	public static function list_unsubscribe( $mp_user_id, $mailinglist_id )
  25      {
  26          MP_User::list_unsubscribed( $mp_user_id );
  27      }
  28  
  29  //// Sending Mails ////
  30  
  31  	public static function mailinglists_optgroup( $label, $optgroup ) 
  32      {
  33          if ( __CLASS__ == $optgroup ) return __( 'WP User Roles', 'MailPress' );
  34          return $label;
  35      }
  36  
  37  	public static function mailinglists( $draft_dest = array() ) 
  38      {
  39          global $wpdb, $wp_roles;
  40  
  41          $query = "SELECT COUNT( * ) FROM $wpdb->usermeta WHERE meta_key = '" . $wpdb->get_blog_prefix( get_current_blog_id() ) . "capabilities' AND meta_value LIKE '%%%s%%'";
  42  
  43          foreach ( $wp_roles->get_names() as $role => $name )
  44              if ( $wpdb->get_var( sprintf( $query, $wpdb->esc_like( $role ) ) ) )
  45                  $draft_dest[__CLASS__ . '~' . $role] = sprintf( __( 'To all "%1$s"', 'MailPress' ), translate_user_role( $name ) );
  46  
  47          return $draft_dest;
  48      }
  49  
  50  	public static function query_mailinglist( $query, $draft_toemail ) 
  51      {
  52          if ( $query ) return $query;
  53  
  54          $role = str_replace( __CLASS__ . '~', '', $draft_toemail, $count );
  55          if ( 0 == $count ) return $query;
  56          if ( empty( $role ) )  return $query;
  57  
  58          $users = array();
  59          $results = get_users( array( 'role' => $role, 'fields' => array( 'user_email' ) ) );
  60                  foreach ( $results as $result ) $users[] = $result->user_email;
  61          if ( empty( $users ) ) return $query;
  62  
  63          global $wpdb;
  64          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' ";
  65      }
  66  
  67  	public static function query_list_id( $list_id, $draft_toemail ) 
  68      {
  69          if ( $list_id ) return $list_id;
  70  
  71          $role = str_replace( __CLASS__ . '~', '', $draft_toemail, $count );
  72          if ( 0 == $count ) return $list_id;
  73          if ( empty( $role ) )  return $list_id;
  74  
  75          return  __CLASS__ . '.' . $role;
  76      }
  77  }
  78  new MailPress_mailinglist_user_role();
  79  }


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