[ Index ]

MailPress 7.2

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

title

Body

[close]

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

   1  <?php
   2  class MP_Autoresponder
   3  {
   4      const taxonomy = MailPress_autoresponder::taxonomy;
   5  
   6  	public static function exists( $term_name ) 
   7      {
   8          $id = term_exists( $term_name, self::taxonomy );
   9          if ( is_array( $id ) )    $id = $id['term_id'];
  10          return $id;
  11      }
  12  
  13  	public static function get( $term_id, $output = OBJECT, $filter = 'raw' ) 
  14      {
  15          $term = get_term( $term_id, self::taxonomy, $output, $filter );
  16          if ( is_wp_error( $term ) )    return false;
  17          $term->slug = self::remove_slug( $term->slug );
  18          if ( !is_array( $term->description ) ) $term->description = unserialize( $term->description );
  19          return $term;
  20      }
  21  
  22  	public static function insert( $term_arr, $wp_error = false ) 
  23      {
  24                  global $wpdb;
  25                  
  26          $term_defaults = array( 'id' => 0, 'name' => '', 'slug' => '', 'description' => '' );
  27          $term_arr = wp_parse_args( $term_arr, $term_defaults );
  28  
  29          $term_arr['name']         = sanitize_text_field( $term_arr['name'] );
  30          $term_arr['description']['desc']= sanitize_text_field( $term_arr['description']['desc'] );
  31  
  32          extract( $term_arr, EXTR_SKIP );
  33  
  34          if ( trim( $name ) == '' ) 
  35          {
  36              if ( ! $wp_error )    return 0;
  37              else                return new WP_Error( 'autoresponder_name', __( 'You did not enter a valid autoresponder name.', 'MailPress' ) );
  38          }
  39  
  40          $slug = self::add_slug( $slug, $name );
  41  
  42          if ( isset( $description['settings'][$description['event']] ) )
  43              $description['settings'] = $description['settings'][$description['event']];
  44          else
  45              unset( $description['settings'] );
  46          $description = $wpdb->_real_escape( serialize( $description ) );
  47  
  48          $id = ( int ) $id;
  49  
  50          // Are we updating or creating?
  51          $update = ( !empty ( $id ) ) ? true : false;
  52  
  53          $args = compact( 'name', 'slug', 'parent', 'description' );
  54  
  55          if ( $update )    $term = wp_update_term( $id,   self::taxonomy, $args );
  56          else            $term = wp_insert_term( $name, self::taxonomy, $args );
  57  
  58          if ( is_wp_error( $term ) ) 
  59          {
  60              if ( $wp_error )    return $term;
  61              else            return 0;
  62          }
  63  
  64          return $term['term_id'];
  65      }
  66  
  67  	public static function delete( $term_id )
  68      {
  69          $meta_key = '_MailPress_autoresponder_' . $term_id;
  70  
  71          global $wpdb;
  72          $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_mailmeta WHERE meta_key = %s ;", $meta_key ) );
  73          $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_usermeta WHERE meta_key = %s ;", $meta_key ) );
  74  
  75          return wp_delete_term( $term_id, self::taxonomy );
  76      }
  77  
  78  	public static function get_all( $args = '' )
  79      {
  80          $defaults = array( 'hide_empty' => 0, 'hierarchical' => 0, 'child_of' => '0', 'parent' => '' );
  81          $args = wp_parse_args( $args, $defaults );
  82          $terms = get_terms( self::taxonomy, $args );
  83          if ( empty( $terms ) ) return array();
  84          foreach ( $terms as $k => $term )
  85          {
  86              $terms[$k]->slug = self::remove_slug( $term->slug );
  87              if ( !is_array( $term->description ) ) $terms[$k]->description = unserialize( $term->description );
  88          }
  89          return $terms;
  90      }
  91  
  92  	public static function get_from_event( $event )
  93      {
  94          $defaults = array( 'hide_empty' => 0, 'hierarchical' => 0, 'child_of' => '0', 'parent' => '' );
  95          $terms = get_terms( self::taxonomy, $defaults );
  96          if ( empty( $terms ) ) return array();
  97          foreach ( $terms as $k => $term )
  98          {
  99              $terms[$k]->slug = self::remove_slug( $term->slug );
 100              if ( !is_array( $term->description ) ) $terms[$k]->description = unserialize( $term->description );
 101              if ( isset( $term->description['active'] ) && ( $event == $term->description['event'] ) ) continue;
 102              unset( $terms[$k] );
 103          }
 104          return $terms;
 105      }
 106  
 107  ////  Object  ////
 108  
 109  	public static function convert_schedule( $value, $time )
 110      {
 111                  if ( is_serialized( $value ) ) $value = unserialize( $value );
 112          if ( !is_array( $value ) )     $value = array( 'Y' => 0, 'M' => ( int ) substr( $value, 0, 2 ), 'W' => 0, 'D' => ( int ) substr( $value, 2, 2 ), 'H' =>  ( int ) substr( $value, 4, 2 ) );
 113  
 114          $Y = date( 'Y', $time ) + $value['Y'];
 115          $M = date( 'n', $time ) + $value['M'];
 116          $D = date( 'j', $time ) + $value['D'] + ( $value['W'] * 7 );
 117          $H = date( 'G', $time ) + $value['H'];
 118          $Mn= date( 'i', $time );
 119          $S = date( 's', $time );
 120          $value['date'] = mktime( $H, $Mn, $S, $M, $D, $Y );
 121                  return $value;
 122      }
 123  
 124  	public static function get_object_terms( $object_id = 0, $args = array() )
 125      {
 126          $_terms = array();
 127          $terms = self::get_all( $args );
 128  
 129          if ( !$terms ) return array();
 130  
 131          foreach( $terms as $term )
 132          {
 133              $meta_key = '_MailPress_autoresponder_' . $term->term_id;
 134              $metadata = MP_Mail_meta::has( $object_id, $meta_key );
 135              $time = time();
 136              if ( $metadata ) foreach ( $metadata as $entry ) $_terms[] =    array( 'term_id'     => $term->term_id, 
 137                                                          'meta_id'     => $entry['meta_id'], 
 138                                                          'mail_id'     => $object_id, 
 139                                                          'schedule'     => self::convert_schedule( $entry['meta_value'], $time ),
 140                                                       );
 141          }
 142  
 143          uasort( $_terms, array( 'self', 'sort_autoresponders' ) );
 144          return $_terms;
 145      }
 146  
 147  	public static function sort_autoresponders( $a, $b ) 
 148      {
 149          return strcmp( $a['schedule']['date'], $b['schedule']['date'] );
 150      }
 151  
 152  	public static function object_have_relations( $object_id )
 153      {
 154          $terms = self::get_all();
 155  
 156          if ( !$terms ) return false;
 157  
 158          foreach( $terms as $term )
 159          {
 160              $meta_key = '_MailPress_autoresponder_' . $term->term_id;
 161              $metadata = MP_Mail_meta::has( $object_id, $meta_key );
 162              if ( $metadata ) return true;
 163          }
 164          return false;
 165      }
 166  
 167  	public static function get_term_objects( $term_id )
 168      {
 169          $meta_key = '_MailPress_autoresponder_' . $term_id;
 170  
 171          global $wpdb;
 172          $metadata = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->mp_mailmeta WHERE meta_key = %s ORDER BY meta_value;", $meta_key ) );
 173          if ( !$metadata ) return array();
 174          $time = time();
 175          foreach ( $metadata as $entry ) $_objects[] =     array(     'term_id'     => $term_id, 
 176                                                  'meta_id'     => $entry->meta_id, 
 177                                                  'mail_id'     => $entry->mp_mail_id, 
 178                                                  'schedule'     => self::convert_schedule( $entry->meta_value, $time ),
 179                                               );
 180          uasort( $_objects, array( 'self', 'sort_autoresponders' ) );
 181          return $_objects;
 182      }
 183  
 184  	public static function get_term_meta_id( $meta_id )
 185      {
 186          $entry = MP_Mail_meta::get_by_id( $meta_id );
 187  
 188          $term_id = str_replace( '_MailPress_autoresponder_', '', $entry->meta_key );
 189          return                             array(     'term_id'     => $term_id, 
 190                                                  'meta_id'     => $entry->meta_id, 
 191                                                  'mail_id'     => $entry->mp_mail_id, 
 192                                                  'schedule'     => self::convert_schedule( $entry->meta_value, time() ),
 193                                               );
 194      }
 195  
 196  ////  Slug  ////
 197  
 198  	public static function add_slug( $slug, $name = false )
 199      {
 200          $slugs = ( $name ) ? array( $slug, $name ) : array( $slug );
 201          foreach ( $slugs as $slug )
 202          {
 203              $slug = self::remove_slug( $slug );
 204              $slug = trim( stripslashes( $slug ) );
 205              $slug = str_replace( '"', '_', $slug );
 206              $slug = str_replace( "'", '_', $slug );
 207              if ( !empty( $slug ) ) break;
 208          }
 209          return $slug = '_' . self::taxonomy . '_' . $slug;
 210      }
 211  
 212  	public static function remove_slug( $slug )
 213      {
 214          return str_ireplace( '_' . self::taxonomy . '_', '', $slug );
 215      }
 216  }


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