[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  class MP_Newsletter
   3  {
   4  // for newsletters
   5  	public static function register( $newsletter = array() )
   6      {
   7          if ( empty( $newsletter['id'] ) ) return;
   8  
   9          global $mp_subscriptions, $mp_registered_newsletters;
  10  
  11          $newsletter['allowed'] = ( isset( $mp_subscriptions['newsletters'][$newsletter['id']] ) );
  12          $newsletter['default'] = ( isset( $mp_subscriptions['default_newsletters'][$newsletter['id']] ) );
  13  
  14          $mp_registered_newsletters[$newsletter['id']] = $newsletter;
  15      }
  16  
  17  	public static function get( $id ) 
  18      {
  19          global $mp_registered_newsletters;
  20  
  21          if ( !isset( $mp_registered_newsletters[$id] ) ) return false;
  22          return $mp_registered_newsletters[$id];
  23      }
  24  
  25  	public static function get_all( $lib = 'admin' ) 
  26      {
  27          global $mp_registered_newsletters;
  28  
  29          $x = array();
  30          foreach ( $mp_registered_newsletters as $k => $v ) $x[$k] = $v['descriptions'][$lib];
  31          ksort( $x );
  32  
  33          return $x;
  34      }
  35  
  36  	public static function get_active( $lib = 'admin' ) 
  37      {
  38          global $mp_registered_newsletters;
  39  
  40          $x = array();
  41          if ( !empty( $mp_registered_newsletters ) )
  42          {
  43              foreach ( $mp_registered_newsletters as $k => $v ) if ( $v['allowed'] ) $x[$k] = $v['descriptions'][$lib];
  44              ksort( $x );
  45          }
  46          return $x;
  47      }
  48  
  49  	public static function get_active_by_scheduler( $scheduler ) 
  50      {
  51          global $mp_registered_newsletters;
  52  
  53          $x = array();
  54  
  55          foreach ( $mp_registered_newsletters as $k => $v )
  56          {
  57              if ( $v['allowed'] && $scheduler == $v['scheduler']['id'] )
  58              {
  59                  if ( self::as_subscribers( $k ) )
  60                  {
  61                      $x[$k] = $v;
  62                  }
  63              }
  64          }
  65  
  66          ksort( $x );
  67  
  68          return $x;
  69      }
  70  
  71  	public static function get_defaults()
  72      {
  73          global $mp_registered_newsletters;
  74  
  75          $x = array();
  76          foreach( $mp_registered_newsletters as $n ) if ( $n['default'] ) $x[$n['id']] = $n['id'];
  77          ksort( $x );
  78  
  79          return $x;
  80      }
  81  
  82  	public static function get_templates() 
  83      {
  84          global $mp_registered_newsletters;
  85  
  86          $x = array();
  87          foreach ( $mp_registered_newsletters as $k => $v ) $x[] = $v['mail']['Template'];
  88  
  89          return array_unique( $x );
  90      }
  91  
  92  ////  Object  ////
  93  
  94  	public static function get_object_terms( $mp_user_id = false ) 
  95      {
  96          global $mp_registered_newsletters;
  97  
  98          $x = self::get_active();
  99  
 100          $a = ( $mp_user_id ) ? MP_User_meta::get( $mp_user_id, MailPress_newsletter::meta_key ) : '';
 101  
 102          $y = ( is_array( $a ) ) ? array_flip( $a ) : ( ( empty( $a ) ) ? array() : array( $a => 1 ) );
 103  
 104          foreach ( $x as $k => $v )
 105          {
 106              if ( $mp_registered_newsletters[$k]['default'] &&  isset( $y[$k] ) ) unset( $x[$k] );
 107              if ( !$mp_registered_newsletters[$k]['default'] && !isset( $y[$k] ) ) unset( $x[$k] );
 108          }
 109          return $x;
 110      }
 111  
 112  	public static function set_object_terms( $mp_user_id, $object_terms = array() )
 113      {
 114          global $mp_registered_newsletters;
 115          $x = self::get_active();
 116  
 117          MP_User_meta::delete( $mp_user_id, MailPress_newsletter::meta_key );
 118  
 119          foreach ( $x as $k => $v ) 
 120          {
 121              $default = ( isset( $mp_registered_newsletters[$k]['default'] ) && $mp_registered_newsletters[$k]['default'] );
 122  
 123              if     ( $default && !isset( $object_terms[$k] ) ) MP_User_meta::add( $mp_user_id, MailPress_newsletter::meta_key, $k );
 124              elseif ( !$default &&  isset( $object_terms[$k] ) ) MP_User_meta::add( $mp_user_id, MailPress_newsletter::meta_key, $k );
 125          }
 126      }
 127  
 128  	public static function reverse_subscriptions( $id ) 
 129      {
 130          global $wpdb;
 131  
 132          $mp_users = $wpdb->get_results( $wpdb->prepare( "SELECT mp_user_id AS id FROM $wpdb->mp_usermeta WHERE meta_key = %s AND meta_value = %s ;", MailPress_newsletter::meta_key, $id ) );
 133  
 134          $to_be_reversed = array();
 135          foreach( $mp_users as $mp_user ) $to_be_reversed[] = $mp_user->id;
 136          $not_in  = ( empty( $to_be_reversed ) ) ? '' : 'WHERE id NOT IN ( ' . join( ', ', $to_be_reversed ) . ' )';
 137  
 138          $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_usermeta WHERE meta_key = %s AND meta_value =  %s ", MailPress_newsletter::meta_key, $id ) );
 139          $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->mp_usermeta ( mp_user_id, meta_key, meta_value ) SELECT id, %s, %s FROM $wpdb->mp_users $not_in;", MailPress_newsletter::meta_key, $id ) );
 140      }
 141  
 142  	public static function as_subscribers( $id ) 
 143      {
 144          global $wpdb, $mp_registered_newsletters;
 145  
 146          if ( !isset( $mp_registered_newsletters[$id] ) ) return false;
 147  
 148          $in = ( $mp_registered_newsletters[$id]['default'] ) ? 'NOT' : '';
 149  
 150          return $wpdb->get_var( self::get_query_newsletter( $id, $in, true ) );
 151      }
 152  
 153  	public static function get_query_newsletter( $id, $in = 'NOT', $count = false ) 
 154      {
 155          global $wpdb;
 156  
 157          $cols = ($count) ? 'count(*)' : 'DISTINCT a.id, a.email, a.name, a.status, a.confkey';
 158  
 159          return $wpdb->prepare( "SELECT $cols FROM $wpdb->mp_users a 
 160                          WHERE status = 'active' 
 161                          AND     $in EXISTS 	( 
 162                              SELECT DISTINCT b.mp_user_id 
 163                              FROM     $wpdb->mp_usermeta b 
 164                              WHERE b.meta_key   = %s
 165                              AND     b.meta_value = %s 
 166                              AND     b.mp_user_id = a.id ) ;", MailPress_newsletter::meta_key, $id );
 167      }
 168  
 169  //// *** ////
 170  
 171  	public static function post_limits( $limits ) 
 172      {
 173          $pl = MailPress_newsletter::get_post_limits();
 174  
 175          if ( isset( $pl ) && ( $pl ) ) return 'LIMIT 0, ' . $pl;
 176  
 177          return $limits;
 178      }
 179  
 180  	public static function send( $newsletter, $qp = true, $mail = false, $trace = false )
 181      {
 182          if ( !isset( $newsletter['query_posts'] ) ) return 'noqp';
 183  
 184          if ( !$mail )
 185          {
 186              $in     = ( $newsletter['default'] ) ? 'NOT' : '';
 187              $mail    = new stdClass();
 188              $mail->recipients_query = self::get_query_newsletter( $newsletter['id'], $in );
 189          }
 190  
 191          $rc = 'npst';
 192  
 193          if ( isset( $newsletter['mail'] ) )
 194              foreach( $newsletter['mail'] as $k => $v )
 195                  if ( !empty( $newsletter['mail'][$k] ) ) $mail->{$k} = $newsletter['mail'][$k];
 196  
 197          $mail->newsletter = $newsletter;
 198  
 199          add_filter( 'post_limits', array( __CLASS__, 'post_limits' ), 8, 1 );
 200  
 201          if ( $qp )
 202          {
 203              query_posts( $newsletter['query_posts'] );
 204                  while ( have_posts() ) { $qp = false; break; }    
 205              wp_reset_query();
 206  
 207          }
 208          $qp = apply_filters( 'MailPress_newsletter_send_qp', $qp );
 209          if ( !$qp )
 210          {
 211              query_posts( $newsletter['query_posts'] );
 212                  $rc = MailPress::mail( $mail );
 213              wp_reset_query();
 214          }
 215  
 216          remove_filter( 'post_limits', array( __CLASS__, 'post_limits' ), 8, 1 );
 217  
 218          return $rc;
 219      }
 220  
 221  ////  Xml Files  ////
 222  
 223  	public static function register_files( $args )
 224      {
 225          $defaults = array( 'file' => array() );
 226  
 227          extract( wp_parse_args( $args, $defaults ) );        
 228  
 229          if ( !$_post_type = get_post_type_object( $post_type ) ) return;
 230  
 231          if ( isset( $root_filter ) ) $root = apply_filters( $root_filter, $root );
 232          if ( empty( $files ) ) return;
 233  
 234          $xml = '';
 235          foreach( $files as $file )
 236          {
 237              $fullpath = "$root/$file.xml";
 238              if ( !is_file( $fullpath ) ) continue;
 239  
 240              ob_start();
 241                  include( $fullpath );
 242                  $xml .= trim( ob_get_contents() );
 243              ob_end_clean();
 244          }
 245          if ( empty( $xml ) ) return;
 246  
 247          self::register_xml( $xml );
 248      }
 249  
 250  	public static function register_taxonomy( $args )
 251      {
 252          $defaults = array( 'file' => array(), 'get_terms_args' => array() );
 253  
 254          extract( wp_parse_args( $args, $defaults ) );
 255  
 256          if ( !$_post_type = get_post_type_object( $post_type ) ) return;
 257          if ( !taxonomy_exists( $taxonomy ) ) return;
 258  
 259          if ( isset( $root_filter ) ) $root = apply_filters( $root_filter, $root );
 260             $dir  = @opendir( $root );
 261          if ( $dir ) while ( ( $file = readdir( $dir ) ) !== false ) if ( preg_match( "/{$taxonomy}-[0-9]*\.xml/", $file ) ) $files[] = substr( $file, 0, -4 );
 262          if ( $dir ) @closedir( $dir );
 263          if ( empty( $files ) ) return;
 264  
 265          $terms = ( 'category' == $taxonomy ) ? get_categories( $get_terms_args ) : get_terms( $taxonomy, $get_terms_args );
 266          if ( empty( $terms ) ) return;
 267          if ( is_wp_error( $terms ) ) return;
 268  
 269          $xml = '';
 270          foreach( $files as $file )
 271          {
 272              $fullpath = "$root/$file.xml";
 273              if ( !is_file( $fullpath ) ) continue;
 274  
 275                  if ( $folder == $file )
 276                  {
 277                  foreach ( $terms as $term )
 278                  {
 279                      if ( 'category' == $taxonomy ) $category = $term; // backward compatibility
 280                      ob_start();
 281                          include( $fullpath );
 282                          $xml .= trim( ob_get_contents() );
 283                      ob_end_clean();
 284                  }
 285                  }
 286                  else
 287                  {
 288                  ob_start();
 289                      include( $fullpath );
 290                      $xml .= trim( ob_get_contents() );
 291                  ob_end_clean();
 292                  }
 293          }
 294          if ( empty( $xml ) ) return;
 295  
 296          self::register_xml( $xml );
 297      }
 298  
 299  	public static function register_xml( $xml )
 300      {
 301          $xml = '<?xml version="1.0" encoding="UTF-8"?><newsletters>' . $xml . '</newsletters>';
 302          $newsletters = new MP_Xml( $xml );
 303          foreach( $newsletters->object->children as $newsletter ) self::register( self::convert_xml( $newsletter ) );
 304      }
 305  
 306  	public static function convert_xml( $child )
 307      {
 308          if ( isset( $child->textValue ) && !empty( $child->textValue ) ) $array = ( is_numeric( $child->textValue ) ) ? ( int ) $child->textValue : $child->textValue;
 309          if ( isset( $child->attributes ) ) foreach( $child->attributes as $k => $v ) $array[$k] = ( is_numeric( $v ) ) ? ( int ) $v : $v;
 310          if ( isset( $child->children ) )   foreach( $child->children as $children ) 
 311          {
 312              if ( !isset( $array[$children->name] ) )
 313                  $array[$children->name]   = self::convert_xml( $children );
 314              elseif ( is_array( $array[$children->name] ) )
 315                  $array[$children->name][] = self::convert_xml( $children );
 316              else
 317              {
 318                  $array[$children->name] = array( $array[$children->name] );
 319                  $array[$children->name][] = self::convert_xml( $children );
 320              }
 321          }
 322          return ( isset( $array ) ) ? $array : false;
 323      }
 324  }


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