[ Index ]

MailPress 7.2

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

title

Body

[close]

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

   1  <?php
   2  abstract class MP_newsletter_processor_post_ extends MP_newsletter_processor_
   3  {
   4  	function process( $newsletter, $trace )
   5      {
   6          $this->newsletter = $newsletter;
   7          $this->trace     = $trace;
   8  
   9          $this->post_id  = $this->newsletter['params']['post_id'];
  10          $this->meta_key = $this->newsletter['params']['meta_key'];
  11          $this->post_type= $this->newsletter['params']['post_type'] ?? 'post';
  12  
  13      // detect if post already processed
  14          if ( $this->already_processed() ) 
  15          {
  16              MP_Newsletter_processors::message_report( $this->newsletter, "{$this->post_type} {$this->post_id} already processed", $this->trace );
  17              return false;
  18          }
  19  
  20      // detect if anything else is required
  21          if ( !$this->what_else() ) return false;
  22  
  23          $this->newsletter['query_posts'] = $this->newsletter[$this->args]['query_posts'] ?? array();
  24  
  25          MP_Newsletter_processors::send( $this->newsletter, $this->trace );
  26      }
  27  
  28  	function already_processed()
  29      {
  30          if ( get_post_meta( $this->post_id, $this->meta_key ) )
  31              return true;
  32  
  33          add_post_meta( $this->post_id, $this->meta_key, true, true );
  34          return false;
  35      }
  36  
  37  	function what_else()
  38      {
  39      // detect if any category required
  40  
  41          $cats        = $this->get_cats( 'cat',            'intval' );
  42          $cats_in    = $this->get_cats( 'category__in',    'absint' );
  43          $cats_out    = $this->get_cats( 'category__not_in','absint' );
  44  
  45          if ( !empty( $cats ) ) foreach ( $cats as $cat )
  46          {
  47              $in = ( $cat > 0 );
  48              $cat = abs( $cat );
  49              if ( $in ) {
  50                  $cats_in[] = $cat;
  51                  $cats_in   = array_merge( $cats_in,  get_term_children( $cat, 'category' ) );
  52              } else {
  53                  $cats_out[]= $cat;
  54                  $cats_out  = array_merge( $cats_out, get_term_children( $cat, 'category' ) );
  55              }
  56          }
  57  
  58          if ( !empty( $cats_in ) )
  59          {
  60              $post_categories = wp_get_post_categories( $this->post_id );
  61              sort( $post_categories );
  62  
  63              $cats_in = array_unique( $cats_in );
  64              sort( $cats_in );
  65  
  66              $intersect  = array_intersect( $post_categories, $cats_in );
  67              if ( empty( $intersect ) )
  68              {
  69                  MP_Newsletter_processors::message_report( $this->newsletter, "newsletter categories ( in ) : "     . join( ',', $cats_in ), $this->trace );
  70                  MP_Newsletter_processors::message_report( false, "post categories : " . join( ',', $post_categories ), $this->trace );
  71                  MP_Newsletter_processors::message_report( false, "Post {$this->post_id} not in required categories", $this->trace );
  72                  return false;
  73              }
  74          }
  75  
  76          if ( !empty( $cats_out ) )
  77          {
  78              $post_categories = wp_get_post_categories( $this->post_id );
  79              foreach( $post_categories as $cat ) $post_categories = array_merge( $post_categories, get_term_children( $cat, 'category' ) );
  80              $post_categories = array_unique( $post_categories );
  81              sort( $post_categories );
  82  
  83              $cats_out = array_unique( $cats_out );
  84              sort( $cats_out );
  85  
  86              $diff  = array_diff( $post_categories, $cats_out );
  87              if ( empty( $diff ) )
  88              {
  89                  MP_Newsletter_processors::message_report( $this->newsletter, "newsletter categories ( out ) : "     . join( ',', $cats_out ), $this->trace );
  90                  MP_Newsletter_processors::message_report( false, "post categories : " . join( ',', $post_categories ), $this->trace );
  91                  MP_Newsletter_processors::message_report( false, "Post {$this->post_id} in excluding categories", $this->trace );
  92                  return false;
  93              }
  94          }
  95          return true;
  96      }
  97  
  98  	function get_cats( $arg, $array_map )
  99      {
 100          if ( !isset( $this->newsletter[$this->args]['query_posts'][$arg] ) ) return array();
 101          if ( empty( $this->newsletter[$this->args]['query_posts'][$arg] ) ) return array();
 102          if ( !is_array( $this->newsletter[$this->args]['query_posts'][$arg] ) ) $this->newsletter[$this->args]['query_posts'][$arg] = array( $this->newsletter[$this->args]['query_posts'][$arg] );
 103  
 104          $cats = join( ',', $this->newsletter[$this->args]['query_posts'][$arg] );
 105  
 106          return array_map( $array_map, preg_split( '/[,\s]+/', $cats ) );
 107      }
 108  }


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