[ Index ]

MailPress 544

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


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