[ Index ]

MailPress 544

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

title

Body

[close]

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

   1  <?php
   2  class MP_Mailinglist
   3  {
   4      const taxonomy     = MailPress_mailinglist::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          return $term;
  19      }
  20  
  21  	public static function get_id($name = 'General') 
  22      {
  23          $term = get_term_by('name', $name, self::taxonomy);
  24          if ($term) return $term->term_id;
  25          return 0;
  26      }
  27  
  28  	public static function get_name($term_id) 
  29      {
  30          $term_id = (int) $term_id;
  31          $term = self::get($term_id);
  32          return ($term) ? $term->name : false;
  33      }
  34  
  35  	public static function create( $term_name, $parent = 0 ) 
  36      {
  37          if ( $id = self::exists($term_name) ) return $id;
  38          return self::insert( array('name' => trim($term_name), 'parent' => $parent) );
  39      }
  40  
  41  	public static function insert($term_arr, $wp_error = false) 
  42      {
  43          $term_defaults = array('id' => 0, 'name' => '', 'slug' => '', 'parent' => 0, 'description' => '');
  44          $term_arr = wp_parse_args($term_arr, $term_defaults);
  45          extract($term_arr, EXTR_SKIP);
  46  
  47          if ( trim( $name ) == '' ) 
  48          {
  49              if ( ! $wp_error )    return 0;
  50              else                return new WP_Error( 'mailinglist_name', __('You did not enter a mailing list name.', MP_TXTDOM) );
  51          }
  52  
  53          $slug = self::add_slug($slug, $name);
  54          $description = mysql_real_escape_string($description);
  55  
  56          $id = (int) $id;
  57  
  58          // Are we updating or creating?
  59          $update = (!empty ($id) ) ? true : false;
  60  
  61          // hierarchy !
  62          $parent         = (int) $parent;
  63          if ( $parent < 0 )                         $parent = 0;
  64          elseif ( empty($parent) )                    $parent = 0;
  65          elseif (!self::exists( $parent ))                 $parent = 0;
  66           elseif ($id && self::is_ancestor_of($id, $parent))    $parent = 0;
  67  
  68          $args = compact('name', 'slug', 'parent', 'description');
  69  
  70          if ( $update )    $term = wp_update_term($id,   self::taxonomy, $args);
  71          else            $term = wp_insert_term($name, self::taxonomy, $args);
  72  
  73          if ( is_wp_error($term) ) 
  74          {
  75              if ( $wp_error )    return $term;
  76              else            return 0;
  77          }
  78  
  79          return $term['term_id'];
  80      }
  81  
  82  	public static function delete($term_id) 
  83      {
  84          $default         = get_option(MailPress_mailinglist::option_name_default);
  85          if ( $term_id == $default ) return false;
  86  
  87          global $mp_subscriptions;
  88          unset( $mp_subscriptions['display_mailinglists'][$term_id] );
  89          update_option (MailPress::option_name_subscriptions, $mp_subscriptions );
  90  
  91          return wp_delete_term( $term_id, self::taxonomy, array('default' => $default) );
  92      }
  93  
  94  	public static function get_all($args = '')
  95      {
  96          $defaults = array();
  97          $args     = wp_parse_args($args, $defaults);
  98  
  99          $terms    = get_terms(self::taxonomy, $args);
 100          if (empty($terms)) return array();
 101          foreach ($terms as $k => $term) 
 102          {
 103              $terms[$k]->slug = self::remove_slug($terms[$k]->slug);
 104          }
 105          return $terms;
 106      }
 107  
 108  	public static function get_all_ids() 
 109      {
 110          $term_ids = get_terms(self::taxonomy, 'fields=ids&get=all');
 111          return $term_ids;
 112      }
 113  
 114  ////  Hierarchy  ////
 115  
 116  	public static function is_ancestor_of($parent, $child) 
 117      {
 118          if ( is_int($parent) )    $parent = self::get($parent);
 119          if ( is_int($child) )    $child  = self::get($child);
 120  
 121          if ( !$parent->term_id || !$child->parent )    return false;
 122  
 123          if ( $child->parent == $parent->term_id )        return true;
 124  
 125          return self::is_ancestor_of($parent, self::get($child->parent));
 126      }
 127  
 128  	public static function get_children($id, $before = '/', $after = '', $visited = array()) 
 129      {
 130          if ( 0 == $id )    return '';
 131  
 132          $chain = '';
 133          $term_ids = self::get_all_ids();
 134          foreach ( $term_ids as $term_id ) 
 135          {
 136              if ( $term_id == $id )    continue;
 137  
 138              $term = self::get($term_id);
 139  
 140              if ( is_wp_error( $term ) )    return $term;
 141  
 142              if ( $term->parent == $id && !in_array($term->term_id, $visited) ) 
 143              {
 144                  $visited[]      = $term->term_id;
 145                  $chain     .= $before.$term->term_id.$after;
 146                  $chain     .= self::get_children($term->term_id, $before, $after);
 147              }
 148          }
 149          return $chain;
 150      }
 151  
 152  ////  Object  ////
 153  
 154  	public static function get_object_terms( $object_id = 0, $args = array() ) 
 155      {
 156          $object_id = (int) $object_id;
 157  
 158          $defaults = array('fields' => 'ids');
 159          $args = wp_parse_args( $args, $defaults );
 160      
 161          $terms = wp_get_object_terms($object_id, self::taxonomy, $args);
 162          return $terms;
 163      }
 164  
 165  	public static function set_object_terms( $object_id, $object_terms = array() )
 166      {
 167          $object_id = (int) $object_id;
 168          if (!is_array($object_terms) || 0 == count($object_terms) || empty($object_terms)) $object_terms = array(apply_filters('MailPress_mailinglist_default', get_option(MailPress_mailinglist::option_name_default)));
 169          else if ( 1 == count($object_terms) && '' == $object_terms[0] ) return true;
 170  
 171          $object_terms = array_map('intval', $object_terms);
 172          $object_terms = array_unique($object_terms);
 173          if (!is_array($object_terms)) $object_terms = array();
 174  
 175          $in = self::get_object_terms($object_id);
 176          if (!is_array($in)) $in = array();
 177  
 178          $added = array_diff_assoc($object_terms, $in);
 179          foreach ($added as $term_id) do_action('MailPress_mailinglist_new_subscriber', array('mp_user_id' => $object_id, 'mailinglist_id' => $term_id));
 180          $removed = array_diff_assoc($in, $object_terms);
 181          foreach ($removed as $term_id) do_action('MailPress_mailinglist_new_unsubscriber', array('mp_user_id' => $object_id, 'mailinglist_id' => $term_id));
 182          
 183          return wp_set_object_terms($object_id, $object_terms, self::taxonomy);
 184      }
 185  
 186  	public static function delete_object( $object_id )
 187      {
 188          wp_delete_object_term_relationships($object_id, array(self::taxonomy));
 189      }
 190  
 191  ////  Layouts  ////
 192  
 193  	public static function dropdown($args = '') 
 194      {
 195          $defaults = array('child_of'         => 0, 
 196                      'class'        => 'postform', 
 197                      'depth'         => 0, 
 198                      'echo'         => 1, 
 199                      'exclude'         => '', 
 200                      'hide_empty'     => 1, 
 201                      'hierarchical'    => 0, 
 202                      'htmlid'        => 'mailinglist_dropdown', 
 203                      'htmlname'     => 'mailinglist', 
 204                      'order'         => 'ASC', 
 205                      'orderby'         => 'ID', 
 206                      'selected'         => 0, 
 207                      'show_count'     => 0, 
 208                      'show_last_update'=> 0, 
 209                      'show_option_all' => '', 
 210                      'show_option_none'=> '', 
 211                      'tab_index'     => 0
 212                      );
 213  
 214          $r = wp_parse_args( $args, $defaults );
 215          $r['include_last_update_time'] = $r['show_last_update'];
 216          extract( $r );
 217  
 218          $tab_index_attribute = '';
 219          if ( (int) $tab_index > 0 )    $tab_index_attribute = " tabindex=\"$tab_index\"";
 220  
 221          $mailinglists = self::get_all($r);
 222  
 223          $output = '';
 224          if ( ! empty($mailinglists) ) 
 225          {
 226              $htmlid = ($htmlid === true) ? "id='$htmlname'" : "id='$htmlid'" ;
 227              $output = "<select name='$htmlname' $htmlid class='$class' $tab_index_attribute>\n";
 228  
 229              if ( $show_option_all ) 
 230                  $output .= "\t<option value='0'>$show_option_all</option>\n";
 231  
 232              if ( $show_option_none) 
 233                  $output .= "\t<option value='-1'>$show_option_none</option>\n";
 234  
 235              if ( $hierarchical )    $depth = $r['depth'];          // Walk the full depth.
 236              else                $depth = -1;             // Flat.
 237  
 238              $output .= self::walk_dropdown_tree($mailinglists, $depth, $r);
 239              $output .= "</select>\n";
 240          }
 241  
 242          if ( $echo )    echo $output;
 243  
 244          return $output;
 245      }
 246  
 247  	public static function walk_dropdown_tree() 
 248      {
 249          $walker = new MP_Mailinglists_Walker_Dropdown;
 250          $args = func_get_args();
 251          return call_user_func_array(array(&$walker, 'walk'), $args);
 252      }
 253  
 254  	public static function array_tree($args = '') 
 255      {
 256          $defaults = array('child_of'         => 0,
 257                      'class'        => 'postform',
 258                      'depth'         => 0,
 259                      'exclude'         => '',
 260                      'hide_empty'     => 1,
 261                      'hierarchical'    => 0,    
 262                      'htmlname'         => 'mailinglist',
 263                      'order'         => 'ASC',
 264                      'orderby'         => 'ID',
 265                      'selected'         => 0,
 266                      'show_count'     => 0,
 267                      'show_last_update'=> 0, 
 268                      'show_option_all' => '', 
 269                      'show_option_none'=> '',
 270                      'tab_index'     => 0
 271                      );
 272  
 273          $r = wp_parse_args( $args, $defaults );
 274          $r['include_last_update_time'] = $r['show_last_update'];
 275          extract( $r );
 276  
 277          $tab_index_attribute = '';
 278          if ( (int) $tab_index > 0 )    $tab_index_attribute = " tabindex=\"$tab_index\"";
 279  
 280          $mailinglists = self::get_all($r);
 281  
 282          $output = array();
 283          if ( ! empty($mailinglists) ) 
 284          {
 285              if ( $show_option_all ) 
 286                  $output [0] = $show_option_all;
 287  
 288              if ( $show_option_none) 
 289                  $output [-1] = $show_option_none;
 290  
 291              if ( $hierarchical )        $depth = $r['depth'];              // Walk the full depth.
 292              else                    $depth = -1;                 // Flat.
 293  
 294              $output = array_merge(self::walk_array($mailinglists, $depth, $r),$output );
 295          }
 296  
 297          return $output;
 298      }
 299  
 300  	public static function walk_array() 
 301      {
 302          $walker = new MP_Mailinglists_Walker_Array;
 303          $args = func_get_args();
 304          return call_user_func_array(array(&$walker, 'walk'), $args);
 305      }
 306  
 307  ////  Slug  ////
 308  
 309  	public static function add_slug( $slug, $name = false )
 310      {
 311          $slugs = ($name) ? array($slug, $name) : array($slug);
 312          foreach ($slugs as $slug)
 313          {
 314              $slug = self::remove_slug($slug);
 315              $slug = trim(stripslashes($slug));
 316              $slug = str_replace('"', '_', $slug);
 317              $slug = str_replace("'", '_', $slug);
 318              if (!empty($slug)) break;
 319          }
 320          return $slug = '_' . self::taxonomy . '_' . $slug;
 321      }
 322  
 323  	public static function remove_slug( $slug )
 324      {
 325          return str_ireplace('_' . self::taxonomy . '_', '', $slug);
 326      }
 327  }


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