[ Index ]

MailPress 544

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


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