[ Index ]

MailPress 7.1

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


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1