[ Index ]

MailPress 7.1

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

title

Body

[close]

/mp-content/add-ons/ -> MailPress_mailinglist.php (source)

   1  <?php
   2  if ( class_exists( 'MailPress' ) && !class_exists( 'MailPress_mailinglist' ) )
   3  {
   4  /*
   5  Plugin Name: MailPress_mailinglist
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/mailinglist/
   7  Description: Mailing lists
   8  Version: 7.1
   9  */
  10  
  11  // 3.
  12  
  13  /** for admin plugin pages */
  14  define ( 'MailPress_page_mailinglists',     'mailpress_mailinglists' );
  15  
  16  /** for admin plugin urls */
  17  $mp_file = 'admin.php';
  18  define ( 'MailPress_mailinglists', add_query_arg( 'page', MailPress_page_mailinglists,     $mp_file ) );
  19  
  20  class MailPress_mailinglist
  21  {
  22      const taxonomy = 'MailPress_mailing_list';
  23  
  24      const option_name_default = 'MailPress_default_mailinglist';
  25  
  26  	function __construct()
  27      {
  28  // for taxonomy
  29          add_action( 'init',             array( __CLASS__, 'init' ), 1 );
  30  
  31  // for wordpress hooks
  32  // register form
  33          add_action( 'user_register',             array( __CLASS__, 'user_register' ), 20, 1 );
  34          add_action( 'MailPress_register_form',     array( __CLASS__, 'register_form' ), 20 ); 
  35  
  36  // for shortcode
  37          add_filter( 'MailPress_form_defaults',     array( __CLASS__, 'form_defaults' ), 8, 1 );
  38          add_filter( 'MailPress_form_options',     array( __CLASS__, 'form_options' ), 8, 1 );
  39          add_filter( 'MailPress_form_submit',     array( __CLASS__, 'form_submit' ), 8, 2 );
  40          add_action( 'MailPress_form',               array( __CLASS__, 'form' ), 1, 2 ); 
  41  
  42  // for sending mails
  43          add_filter( 'MailPress_mailinglists_optgroup',     array( __CLASS__, 'mailinglists_optgroup' ), 8, 2 );
  44          add_filter( 'MailPress_mailinglists',             array( __CLASS__, 'mailinglists' ), 8, 1 );
  45          add_filter( 'MailPress_query_mailinglist',         array( __CLASS__, 'query_mailinglist' ), 8, 2 );
  46  
  47  // for mp_user
  48  
  49          add_action( 'MailPress_activate_user_1st',    array( __CLASS__, 'set_user_mailinglists' ), 1, 1 );
  50          add_action( 'MailPress_delete_user',     array( __CLASS__, 'delete_user' ), 1, 1 );
  51  
  52  // for autoresponder
  53          add_action( 'MailPress_load_Autoresponder_events',    array( __CLASS__, 'load_Autoresponder_events' ) );
  54  
  55  // for sync wordpress user
  56          add_filter( 'MailPress_has_subscriptions', array( __CLASS__, 'has_subscriptions' ), 8, 2 );
  57          add_action( 'MailPress_sync_subscriptions',array( __CLASS__, 'sync_subscriptions' ), 8, 2 );     
  58  
  59  // for wp admin
  60          if ( is_admin() )
  61          {
  62          // install
  63              register_activation_hook(  plugin_basename( __FILE__ ), array( __CLASS__, 'install' ) );
  64          // for link on plugin page
  65              add_filter( 'plugin_action_links',             array( __CLASS__, 'plugin_action_links' ), 10, 2 );
  66          // for role & capabilities
  67              add_filter( 'MailPress_capabilities',         array( __CLASS__, 'capabilities' ), 1, 1 );
  68          // for load admin page
  69              add_filter( 'MailPress_load_admin_page',         array( __CLASS__, 'load_admin_page' ), 10, 1 );
  70          // for settings
  71              add_filter( 'MailPress_settings_tab',         array( __CLASS__, 'settings_tab' ), 10, 1 );
  72          // for settings subscriptions
  73              add_filter( 'MailPress_settings_subscriptions_help',        array( __CLASS__, 'settings_subscriptions_help' ), 30, 1 );
  74              add_action( 'MailPress_settings_subscriptions_form',        array( __CLASS__, 'settings_subscriptions_form'), 30 );
  75          // for meta box in user page
  76              if ( current_user_can( 'MailPress_manage_mailinglists' ) )
  77              {
  78                  add_action( 'MailPress_add_help_tab_user',        array( __CLASS__, 'add_help_tab_user' ), 30 );
  79                  add_action( 'MailPress_update_meta_boxes_user', array( __CLASS__, 'update_meta_boxes_user' ) );
  80                  add_filter( 'MailPress_styles',             array( __CLASS__, 'styles' ), 8, 2 );
  81                  add_filter( 'MailPress_scripts',             array( __CLASS__, 'scripts' ), 8, 2 );
  82                  add_action( 'MailPress_add_meta_boxes_user',     array( __CLASS__, 'add_meta_boxes_user' ), 30, 2 );
  83              }
  84          }
  85  
  86  // for mp_users list
  87          add_action( 'MailPress_users_restrict',     array( __CLASS__, 'users_restrict' ), 10, 1 );
  88          add_filter( 'MailPress_users_columns',     array( __CLASS__, 'users_columns' ), 20, 1 );
  89          add_action( 'MailPress_users_get_list',     array( __CLASS__, 'users_get_list' ), 10, 2 );
  90          add_filter( 'MailPress_users_get_row',     array( __CLASS__, 'users_get_row' ), 20, 4 );
  91  
  92  // for form page ( visitor subscription options )
  93          add_action( 'MailPress_form_visitor_subscription',     array( __CLASS__, 'form_visitor_subscription' ), 8, 1 );
  94          add_action( 'MailPress_visitor_subscription',         array( __CLASS__, 'visitor_subscription' ), 8, 3 );
  95  
  96  // for tracking ( add mailinglist )
  97          add_action( 'MailPress_do_bulk_action_' . MailPress_page_users, array( __CLASS__, 'create_tracking_mailinglist' ), 8, 1 );
  98  
  99  // for ajax
 100          add_action( 'mp_action_add_mlnglst',     array( __CLASS__, 'mp_action_add_mlnglst' ) );
 101          add_action( 'mp_action_delete_mlnglst',     array( __CLASS__, 'mp_action_delete_mlnglst' ) );
 102          add_action( 'mp_action_add_mailinglist',     array( __CLASS__, 'mp_action_add_mailinglist' ) );
 103      }
 104  
 105  	public static function init() 
 106      {
 107          register_taxonomy( self::taxonomy, 'MailPress_user', array( 'hierarchical' => true , 'update_count_callback' => array( __CLASS__, 'update_count_callback' ) ) );
 108      }
 109  
 110  //// Subscriptions ////
 111  
 112  	public static function get_checklist( $mp_user_id = false, $args = '' ) 
 113      {
 114          global $mp_subscriptions;
 115          if ( !isset( $mp_subscriptions['display_mailinglists'] ) ) return false;
 116          if ( empty( $mp_subscriptions['display_mailinglists'] ) ) return false;
 117  
 118          $checklist = '';
 119          $defaults = array (     'htmlname'     => 'keep_mailinglists', 
 120                          'selected'     => false, 
 121                          'type'        => 'checkbox', 
 122                          'show_option_all' => false,
 123                          'htmlstart'    => '', 
 124                          'htmlmiddle'    => '&#160;&#160;', 
 125                          'htmlend'        => "<br />\n"
 126                       );
 127          $r = wp_parse_args( $args, $defaults );
 128          extract( $r );
 129  
 130          if ( $mp_user_id )
 131          {
 132              $mp_user_mls = MP_Mailinglist::get_object_terms( $mp_user_id );
 133          }
 134  
 135          $default_mailing_list = get_option( self::option_name_default );
 136  
 137          $mls = array();
 138          $mailinglists = apply_filters( 'MailPress_mailinglists', array() );
 139          foreach ( $mailinglists as $k => $v ) 
 140          {
 141              $x = str_replace( __CLASS__ . '~', '', $k, $count );
 142              if ( 0 == $count )     continue;    
 143              if ( empty( $x ) )     continue;
 144  
 145              $mls[$x] = $v;
 146          }
 147  
 148          foreach ( $mls as $k => $v )
 149          {
 150              switch ( $type )
 151              {
 152                  case 'checkbox' :
 153                      $checked = checked( $mp_user_id && in_array( $k, $mp_user_mls ), true, false );
 154                      $_type   = ( isset( $mp_subscriptions['display_mailinglists'][$k] ) ) ? 'checkbox' : '';
 155  
 156                      if ( empty( $_type ) && empty( $checked ) ) continue;
 157                      if ( empty( $_type ) )
 158                      {
 159                          $_type   = 'hidden';
 160                          $checked = ' value="on"';
 161                      }
 162  
 163                      $tag          = '<input type="' . $_type . '" name="' . $htmlname . '[' . $k . ']" id="' . $htmlname . '_' . $k . '"' . $checked . ' />';
 164                      $htmlstart2  = ( 'checkbox' == $_type ) ? str_replace( '{{id}}', "{$htmlname}_{$k}", $htmlstart ) : '';
 165                      $htmlmiddle2 = ( 'checkbox' == $_type ) ? $htmlmiddle . str_replace( '&#160;', '', $v ) : "<!-- " . str_replace( '&#160;', '', $v ) . "-->";
 166                      $htmlend2    = ( 'checkbox' == $_type ) ? $htmlend    : "\n";
 167  
 168                      $checklist .= "$htmlstart2$tag$htmlmiddle2$htmlend2";
 169                  break;
 170                  case 'select' :
 171                      if ( !isset( $mp_subscriptions['display_mailinglists'][$k] ) ) continue;
 172  
 173                      if ( $show_option_all )
 174                      {
 175                          $checklist .= '<option value="">' . $show_option_all . '</option>';
 176                          $show_option_all = false;
 177                      }
 178                      $sel = ( $k == $selected ) ? ' selected="selected"' : '';
 179                      $checklist .= '<option value="' . $k . '"' . $sel . '>' . str_replace( '&#160;', '', $v ) . '</option>';
 180                  break;
 181              }
 182          }
 183          if ( 'select' == $type ) $checklist = $htmlstart . '<select name="' . $htmlname . '">' . $checklist . '</select>' . $htmlend;
 184  
 185          return $checklist;
 186      }
 187  
 188  	public static function update_checklist( $mp_user_id, $name = 'keep_mailinglists' ) 
 189      {
 190          global $mp_subscriptions;
 191          if ( !isset( $mp_subscriptions['display_mailinglists'] ) ) return true;
 192          if ( empty( $mp_subscriptions['display_mailinglists'] ) )  return true;
 193  
 194          $mp_user_mls = array();
 195  
 196          $post_ = filter_input_array( INPUT_POST );
 197  
 198          if ( isset( $post_[$name] ) )
 199          {
 200              foreach ( $post_[$name] as $mailinglist_ID => $v )
 201              {
 202                  array_push( $mp_user_mls, $mailinglist_ID );
 203              }
 204          }
 205          MP_Mailinglist::set_object_terms( $mp_user_id, $mp_user_mls );
 206      }
 207  
 208  	public static function get_subscriptions( $id ) 
 209      {
 210          $mp_user_mls = MP_Mailinglist::get_object_terms( $id );
 211  
 212          if ( empty( $mp_user_mls ) ) return false;
 213  
 214          $mls = array();
 215          $mailinglists = apply_filters( 'MailPress_mailinglists', array() );
 216  
 217          foreach ( $mailinglists as $k => $v ) 
 218          {
 219              $x = str_replace( __CLASS__ . '~', '', $k, $count );
 220              if ( 0 == $count )     continue;    
 221              if ( empty( $x ) )     continue;
 222  
 223              $mls[$x] = str_replace( '&#160;', '', $v );
 224          }
 225  
 226          foreach ( $mls as $k => $v )
 227          {
 228              if ( !in_array( $k, $mp_user_mls ) ) unset( $mls[$k] );
 229          }
 230  
 231          if ( empty( $mls ) ) return false;
 232  
 233          return $mls;
 234      }
 235  
 236  //// Plugin ////
 237  
 238  // for taxonomy
 239  	public static function update_count_callback( $mailinglists )
 240      {
 241          global $wpdb;
 242  
 243          foreach ( $mailinglists as $mailinglist ) 
 244          {
 245              $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $wpdb->term_taxonomy a, $wpdb->term_relationships b, $wpdb->mp_users c WHERE a.taxonomy = '" . self::taxonomy . "' AND  a.term_taxonomy_id = b.term_taxonomy_id AND a.term_taxonomy_id = %d AND c.id = b.object_id ", $mailinglist ) );
 246              $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $mailinglist ) );
 247          }
 248      }
 249  
 250  //// Register form ////
 251  
 252  	public static function user_register( $wp_user_id )
 253      {
 254          $user     = get_userdata( $wp_user_id );
 255          $email     = $user->user_email;
 256          $mp_user_id    = MP_User::get_id_by_email( $email );
 257  
 258          self::update_checklist( $mp_user_id );
 259      }
 260  
 261  	public static function register_form()
 262      {
 263          $checklist_mailinglists = self::get_checklist();
 264          if ( empty( $checklist_mailinglists ) ) return;
 265  
 266          echo '<br /><p><label>' . __( 'Mailing lists', 'MailPress' ) . '<br /><span style="color:#777;font-weight:normal;">' . $checklist_mailinglists . '</span></label></p>' . "\r\n";
 267      }
 268  
 269  //// Shortcode ////
 270  
 271  	public static function form_defaults( $x ) { $x['mailinglist'] = false; return $x; }
 272  
 273  	public static function form_options( $x )  { return $x; }
 274  
 275  	public static function form_submit( $shortcode_message, $email ) 
 276      {
 277          $post_ = filter_input_array( INPUT_POST );
 278  
 279          if ( !isset( $post_['mailinglist'] ) )     return $shortcode_message;
 280          if ( !$post_['mailinglist'] )             return $shortcode_message;
 281          $shortcode = 'shortcode_mailinglists';
 282  
 283          $mp_user_id = MP_User::get_id_by_email( $email );
 284          $post_[$shortcode] = MP_Mailinglist::get_object_terms( $mp_user_id );
 285  
 286          $post_[$shortcode] = array_flip( array_map( trim, explode( ',', $post_['mailinglist'] ) ) );
 287  
 288          self::update_checklist( $mp_user_id, $shortcode );
 289  
 290          return $shortcode_message . __( '<br />Mailing lists added', 'MailPress' );
 291      }
 292  
 293  	public static function form( $email, $options )  
 294      {
 295          if ( !$options['mailinglist'] ) return;
 296  
 297          $x = array();
 298          foreach ( array_map( "trim", explode( ',', $options['mailinglist'] ) ) as $k => $v ) if ( MP_Mailinglist::get_name( $v ) ) $x[] = $v;
 299          if ( empty( $x ) ) return;
 300  
 301          echo '<input type="hidden" name="mailinglist" value="' . join( ', ', $x ) . '" />';
 302      }
 303  
 304  //// Sending Mails ////
 305  
 306  	public static function mailinglists_optgroup( $label, $optgroup ) 
 307      {
 308          if ( __CLASS__ == $optgroup ) return __( 'Mailinglists', 'MailPress' );
 309          return $label;
 310      }
 311  
 312  	public static function mailinglists( $draft_dest = array() ) 
 313      {
 314          $args = array( 'hide_empty' => 0, 'hierarchical' => true, 'show_count' => 0, 'orderby' => 'name', 'htmlname' => 'default_mailinglist' );
 315          foreach ( MP_Mailinglist::array_tree( $args ) as $k => $v ) $draft_dest[$k] = $v;
 316          return $draft_dest;
 317      }
 318  
 319  	public static function query_mailinglist( $query, $draft_toemail ) 
 320      {
 321          if ( $query ) return $query;
 322  
 323          $id = str_replace( __CLASS__ . '~', '', $draft_toemail, $count );
 324          if ( 0 == $count ) return $query;
 325          if ( empty( $id ) )  return $query;
 326  
 327          $children = MP_Mailinglist::get_children( $id, ', ', '' );
 328          $ids = ( '' == $children ) ? ' = ' . $id : ' IN ( ' . $id . $children . ' ) ';
 329  
 330          if ( empty( $ids ) ) return $query;
 331  
 332          global $wpdb;
 333          return $wpdb->prepare( "SELECT DISTINCT c.id, c.email, c.name, c.status, c.confkey FROM $wpdb->term_taxonomy a, $wpdb->term_relationships b, $wpdb->mp_users c WHERE a.taxonomy = %s AND a.term_taxonomy_id = b.term_taxonomy_id AND a.term_id $ids AND c.id = b.object_id AND c.status = 'active' ", self::taxonomy );
 334      }
 335  
 336  //// mp_user ////
 337  
 338  	public static function set_user_mailinglists( $mp_user_id, $user_mailinglists = array() )
 339      {
 340          if ( empty( $user_mailinglists ) ) $user_mailinglists = MP_Mailinglist::get_object_terms( $mp_user_id );
 341          MP_Mailinglist::set_object_terms( $mp_user_id, $user_mailinglists );
 342      }
 343  
 344  	public static function delete_user( $mp_user_id )
 345      {
 346          MP_Mailinglist::delete_object( $mp_user_id );
 347  
 348      }
 349  
 350  //// Autoresponders ////
 351  
 352  	public static function load_Autoresponder_events()
 353      {
 354          new MP_Autoresponder_events_mailinglist();
 355      }
 356  
 357  // Sync wordpress user
 358  	public static function has_subscriptions( $has, $mp_user_id )
 359      {
 360          $x = MP_Mailinglist::get_object_terms( $mp_user_id );
 361  
 362          if ( empty( $x ) ) return $has;
 363          return true;
 364      }
 365  
 366  	public static function sync_subscriptions( $oldid, $newid )
 367      {
 368          $old = MP_Mailinglist::get_object_terms( $oldid );
 369          if ( empty( $old ) ) return;
 370          $new = MP_Mailinglist::get_object_terms( $newid );
 371  
 372          MP_Mailinglist::set_object_terms( $newid, array_merge( $old, $new ) );
 373      }
 374  
 375  ////  ADMIN  ////
 376  ////  ADMIN  ////
 377  ////  ADMIN  ////
 378  ////  ADMIN  ////
 379  
 380  // for install
 381  	public static function install()
 382      {
 383          global $wpdb;
 384          if ( !get_option( self::option_name_default ) )
 385          {
 386      // Default mailing list
 387              $name = esc_sql( __( 'Uncategorized', 'MailPress' ) );
 388              $slug = sanitize_title( sanitize_term_field( 'slug', __( 'Uncategorized', 'MailPress' ), 0, self::taxonomy, 'db' ) );
 389              $wpdb->query( "INSERT INTO $wpdb->terms ( name, slug, term_group ) VALUES ( '$name', '$slug', '0' )" );
 390              $term_id = $wpdb->get_var( "SELECT term_id FROM $wpdb->terms WHERE slug = '$slug' " );
 391              $wpdb->query( "INSERT INTO $wpdb->term_taxonomy ( term_id, taxonomy, description, parent, count ) VALUES ( $term_id, '" . self::taxonomy . "', '', '0', '0' )" );
 392              add_option( self::option_name_default, $term_id );
 393          }
 394  
 395      // Synchronize
 396          $default_mailinglist    = get_option( self::option_name_default );
 397          $unmatches = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT a.id FROM $wpdb->mp_users a WHERE NOT EXISTS ( SELECT DISTINCT b.id FROM $wpdb->term_taxonomy c, $wpdb->term_relationships d, $wpdb->mp_users b WHERE c.taxonomy = %s AND c.term_taxonomy_id = d.term_taxonomy_id AND b.id = d.object_id AND b.id = a.id )", self::taxonomy ) );
 398          if ( $unmatches ) foreach ( $unmatches as $unmatch )
 399          {
 400              MP_Mailinglist::set_object_terms( $unmatch->id, array( $default_mailinglist ) );
 401          }
 402      }
 403  
 404  // for link on plugin page
 405  	public static function plugin_action_links( $links, $file )
 406      {
 407          return MailPress::plugin_links( $links, $file, plugin_basename( __FILE__ ), 'subscriptions' );
 408      }
 409  
 410  // for role & capabilities
 411  	public static function capabilities( $capabilities ) 
 412      {
 413          $capabilities['MailPress_manage_mailinglists'] = array(     'name'  => __( 'Mailing lists', 'MailPress' ), 
 414                                                  'group' => 'users', 
 415                                                  'menu'  => 60, 
 416  
 417                                                  'parent'        => false, 
 418                                                  'page_title'    => __( 'MailPress Mailing lists', 'MailPress' ), 
 419                                                  'menu_title'       => '&#160;' . __( 'Mailing lists', 'MailPress' ), 
 420                                                  'page'          => MailPress_page_mailinglists, 
 421                                                  'func'          => array( 'MP_AdminPage', 'body' )
 422                                           );
 423          return $capabilities;
 424      }
 425  
 426  // for load admin page
 427  	public static function load_admin_page( $hub )
 428      {
 429          $hub[MailPress_page_mailinglists] = 'mailinglists';
 430          return $hub;
 431      }
 432  // for settings
 433  	public static function settings_tab( $tabs )
 434      {
 435          $tabs['subscriptions'] = __( 'Subscriptions', 'MailPress' );
 436          return $tabs;
 437      }
 438  
 439  // for settings subscriptions
 440  	public static function settings_subscriptions_help( $content )
 441      {
 442          include  ( MP_ABSPATH . 'mp-admin/includes/settings/subscriptions/mailinglist/help.php' );
 443          return $content;
 444      }
 445  
 446  	public static function settings_subscriptions_form()
 447      {
 448          include  ( MP_ABSPATH . 'mp-admin/includes/settings/subscriptions/mailinglist/form.php' );
 449      }
 450  
 451  // for meta box in user page
 452  	public static function add_help_tab_user()
 453      {
 454          global $current_screen;
 455  
 456          $content = '';
 457          $content .= '<p><strong>' . __( 'Mailing lists :', 'MailPress' ) . '</strong></p>';
 458          $content .= '<p>' . __( 'You can create, modify mailing lists subscriptions for a mp user.', 'MailPress' ) . '</p>';
 459  
 460          $current_screen->add_help_tab( array(     'id'        => 'mailinglists',
 461                                          'title'    => __( 'Mailing lists', 'MailPress' ),
 462                                          'content'    => $content )
 463          );
 464      }
 465  
 466  	public static function update_meta_boxes_user() 
 467      {
 468          if ( !isset( MP_AdminPage::$pst_['id'] ) ) return;
 469          if ( !isset( MP_AdminPage::$pst_['mp_user_mailinglist'] ) ) MP_AdminPage::$pst_['mp_user_mailinglist'] = array();
 470  
 471          MP_Mailinglist::set_object_terms( MP_AdminPage::$pst_['id'], MP_AdminPage::$pst_['mp_user_mailinglist'] );
 472      }
 473  
 474  	public static function styles( $styles, $screen ) 
 475      {
 476          if ( 'mailpress_user' != $screen ) return $styles;
 477  
 478          wp_register_style( 'mp-user-mailinglists', '/' . MP_PATH . 'mp-admin/css/user_mailinglists.css' );
 479  
 480          $styles[] = 'mp-user-mailinglists';
 481  
 482          return $styles;
 483      }
 484  
 485  	public static function scripts( $scripts, $screen ) 
 486      {
 487          if ( 'mailpress_user' != $screen ) return $scripts;
 488  
 489          wp_register_script( 'mp-user-mailinglists', '/' . MP_PATH . 'mp-admin/js/user_mailinglists.js', array( 'mp-lists' ), false, 1 );
 490  
 491          $scripts[] = 'mp-user-mailinglists';
 492  
 493          return $scripts;
 494      }
 495  
 496  	public static function add_meta_boxes_user( $mp_user_id, $screen )
 497      {
 498          add_meta_box( 'mailinglistdiv', __( 'Mailing lists', 'MailPress' ), array( __CLASS__, 'meta_box' ), $screen, 'normal', 'core' );
 499      }
 500  
 501  	public static function meta_box( $mp_user )
 502      {
 503          $mailinglists = get_terms( self::taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
 504          $most_used_ids = array();
 505  
 506          $out = '';
 507          $out .= '<ul id="mailinglist-tabs">' . "\r\n";
 508          $out .= '<li class="tabs"><a href="#mailinglists-all" tabindex="3">' . __( 'All Mailing lists', 'MailPress' ) . '</a>    </li>' . "\r\n";
 509          $out .= '<li class="hide-if-no-js"><a href="#mailinglists-pop" tabindex="3">' . __( 'Most Used' , 'MailPress' ) . '</a></li>'  . "\r\n";
 510          $out .= '</ul>' . "\r\n";
 511  
 512          $out .= '<div id="mailinglists-pop" class="tabs-panel hidden">' . "\r\n";
 513          $out .= '<ul id="mailinglistchecklist-pop" class="mailinglistchecklist form-no-clear" >' . "\r\n";
 514          foreach ( ( array ) $mailinglists as $mailinglist ) 
 515          {
 516              $most_used_ids[] = $mailinglist->term_id;
 517              $id = "popular-mailinglist-$mailinglist->term_id";
 518              
 519              $out .= '<li id="' . $id . '" class="popular-mailinglist">'
 520                      . '<label class="selectit" for="in-' . $id . '">'
 521                      . '<input type="checkbox" id="in-' . $id . '" value="' . ( ( int ) $mailinglist->term_id ) . '" />'
 522                      . esc_html( $mailinglist->name ) 
 523                      . '</label></li>' . "\r\n";
 524          }
 525          $out .= '</ul>' . "\r\n";
 526          $out .= '</div>' . "\r\n";
 527  
 528          $out .= '<div id="mailinglists-all" class="tabs-panel">' . "\r\n";
 529          $out .= '<ul id="mailinglistchecklist" class="list:mailinglist mailinglistchecklist form-no-clear">' . "\r\n";
 530          $out .= self::all_mailinglists_checkboxes( $mp_user->id, false, false, $most_used_ids ) . "\r\n";
 531          $out .= '</ul>' . "\r\n";
 532          $out .= '</div>' . "\r\n";
 533  
 534          $out .= '<div id="mailinglist-adder" class="wp-hidden-children">' . "\r\n";
 535          $out .= '<h4>'
 536                  . '<a id="mailinglist-add-toggle" href="#mailinglist-add" class="hide-if-no-js" tabindex="3">'
 537                  . __( '+ Add New mailing list' , 'MailPress' )
 538                  . '</a>'
 539                  . '</h4>' . "\r\n";
 540          $out .= '<p id="mailinglist-add" class="wp-hidden-child">' . "\r\n";
 541          $out .= '<label class="screen-reader-text">' . __( 'Add New mailinglist' ) . '</label>' . "\r\n";
 542          $out .= '<input type="text" name="newmailinglist" id="newmailinglist" class="form-required form-input-tip" value="' . esc_attr( __( 'New mailing list', 'MailPress' ) ) . '" tabindex="3" aria-required="true"/>' . "\r\n";
 543          $out .= '<label class="screen-reader-text">' . __( 'Parent Mailing list', 'MailPress' ) . ' : </label>' . "\r\n";
 544          $out .= MP_Mailinglist::dropdown( array( 'hide_empty' => 0, 'htmlname' => 'newmailinglist_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __( 'Parent Mailing list', 'MailPress' ), 'tab_index' => 3, 'echo' => false ) ) . "\r\n";
 545          $out .= '<input type="button" id="mailinglist-add-submit" class="add:mailinglistchecklist:mailinglist-add button" value="' . esc_attr( __( 'Add', 'MailPress' ) ) . '" tabindex="3" />' . "\r\n";
 546          $out .= wp_nonce_field( 'add-mailinglist', '_ajax_nonce', false, false ) . "\r\n";
 547          $out .= '<span id="mailinglist-ajax-response"></span>' . "\r\n";
 548          $out .= '</p>' . "\r\n";
 549          $out .= '</div>' . "\r\n";
 550  
 551          echo $out;
 552      }
 553  
 554  	public static function most_used_checkboxes( $taxonomy, $mp_user_id, $default = 0, $number = 10, $echo = true ) 
 555      {
 556          $mailinglists = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number, 'hierarchical' => false ) );
 557          $most_used_ids = array();
 558  
 559          $out = '';
 560          foreach ( ( array ) $mailinglists as $mailinglist ) 
 561          {
 562              $most_used_ids[] = $mailinglist->term_id;
 563              if ( !$echo ) // hack for AJAX use
 564                  continue;
 565              $id = "popular-mailinglist-$mailinglist->term_id";
 566  
 567              $out .= '<li id="' . $id . '" class="popular-mailinglist">'
 568                      . '<label class="selectit" for="in-' . $id . '">'
 569                      . '<input type="checkbox" id="in-' . $id . '" value="' . ( ( int ) $mailinglist->term_id ) . '" />'
 570                      . esc_html( $mailinglist->name )
 571                      . '</label>'
 572                      . '</li>' . "\r\n";
 573          }
 574          echo $out;
 575  
 576          return $most_used_ids;
 577      }
 578  
 579  	public static function all_mailinglists_checkboxes( $mp_user_id = 0, $descendants_and_self = 0, $selected_mailinglists = false, $popular_mailinglists = false ) 
 580      {
 581          $walker = new MP_Mailinglists_Walker_Checklist;
 582  
 583          $descendants_and_self = ( int ) $descendants_and_self;
 584          $args = array();
 585  
 586          if ( is_array( $selected_mailinglists ) )
 587              $args['selected_mailinglists'] = $selected_mailinglists;
 588          elseif ( $mp_user_id )
 589              $args['selected_mailinglists'] = MP_Mailinglist::get_object_terms( $mp_user_id );
 590          else
 591              $args['selected_mailinglists'] = array();
 592  
 593          if ( is_array( $popular_mailinglists ) )
 594              $args['popular_mailinglists'] = $popular_mailinglists;
 595          else
 596              $args['popular_mailinglists'] = MP_Mailinglist::get_all( array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
 597  
 598          if ( $descendants_and_self ) 
 599          {
 600              $mailinglists = MP_Mailinglist::get_all( array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) );
 601              $self = MP_Mailinglist::get( $descendants_and_self );
 602              array_unshift( $mailinglists, $self );
 603          }
 604          else
 605          {
 606              $mailinglists = MP_Mailinglist::get_all( array( 'get' => 'all' ) );
 607          }
 608  
 609          $all_mailinglists_ids = array();
 610          $keys = array_keys( $mailinglists );
 611  
 612          foreach( $keys as $k )
 613          {
 614              if ( in_array( $mailinglists[$k]->term_id, $args['selected_mailinglists'] ) )
 615              {
 616                  $all_mailinglists_ids[] = $mailinglists[$k];
 617                  unset( $mailinglists[$k] );
 618              }
 619          }
 620  
 621          $args['input_name'] = 'mp_user_mailinglist[]';
 622  
 623          $out = '';
 624      // Put checked mailinglists on top
 625          $out .= call_user_func_array( array( &$walker, 'walk' ), array( $all_mailinglists_ids, 0, $args ) );
 626      // Then the rest of them
 627          $out .= call_user_func_array( array( &$walker, 'walk' ), array( $mailinglists, 0, $args ) );
 628  
 629          return $out;
 630      }
 631  
 632  // for mp_users list
 633  	public static function users_restrict( $url_parms )
 634      {
 635          $x = ( isset( $url_parms['mailinglist'] ) ) ? $url_parms['mailinglist'] : '';
 636          $dropdown_options = array( 'show_option_all' => __( 'View all mailing lists', 'MailPress' ), 'hide_empty' => 0, 'hierarchical' => true, 'show_count' => 0, 'orderby' => 'name', 'selected' => $x );
 637          MP_Mailinglist::dropdown( $dropdown_options );
 638      }
 639  
 640  	public static function users_columns( $x )
 641      {
 642          $date = array_pop( $x );
 643          $x['mailinglists']=  __( 'Mailing lists', 'MailPress' );
 644          $x['date']        = $date;
 645          return $x;
 646      }
 647  
 648  	public static function users_get_list( $array, $url_parms )
 649      {
 650          if ( !isset( $url_parms['mailinglist'] ) || empty( $url_parms['mailinglist'] ) ) return $array;
 651  
 652          global $wpdb;
 653  
 654          list( $where, $tables ) = $array;
 655  
 656          $mailinglists = MP_Mailinglist::get_children( $url_parms['mailinglist'], ', ', '' );
 657          $in = ( '' == $mailinglists ) ? ' = ' . $url_parms['mailinglist'] : ' IN ( ' . $url_parms['mailinglist'] . $mailinglists . ' ) ';
 658  
 659          $where .= " AND (      b.taxonomy = '" . self::taxonomy . "' 
 660                       AND b.term_taxonomy_id = c.term_taxonomy_id 
 661                       AND b.term_id " . $in . "  
 662                       AND a.id = c.object_id )";
 663  
 664          $tables .= ", $wpdb->term_taxonomy b, $wpdb->term_relationships c";
 665  
 666          return array( $where, $tables, true );
 667      }
 668  
 669  	public static function users_get_row( $out, $column_name, $mp_user, $url_parms )
 670      {
 671          if ( 'mailinglists' == $column_name )
 672          {
 673              $args = array( 'orderby' => 'name', 'order' => 'ASC', 'fields' => 'all' );
 674              $mp_user_mls = MP_Mailinglist::get_object_terms( $mp_user->id, $args );
 675  
 676              if ( !empty( $mp_user_mls ) ) 
 677              {
 678                  $o = array();
 679                  foreach ( $mp_user_mls as $m )
 680                  {
 681                      $o[] = '<a href="' . add_query_arg('mailinglist', $m->term_id, MailPress_users ) . '">' . esc_html( sanitize_term_field( 'name', $m->name, $m->term_id, self::taxonomy, 'display' ) ) . '</a>';
 682                  }
 683                  $out .= join( ', ', $o );
 684              }
 685              else
 686              {
 687                  $out .= __( '(none)', 'MailPress' );
 688              }
 689          }
 690          return $out;
 691      }
 692  
 693  // for form page ( visitor subscription options )
 694  	public static function form_visitor_subscription( $form )
 695      {
 696          $selected = get_option( self::option_name_default );
 697          if ( isset( $form->settings['visitor']['mailinglist'] ) )
 698              if ( MP_Mailinglist::get( $form->settings['visitor']['mailinglist'] ) ) $selected = $form->settings['visitor']['mailinglist'];
 699  
 700                  echo '<label for="visitor_mailinglist"><small>' . __( 'Mailing list', 'MailPress' ) . '</small></label>' . "\r\n";
 701  
 702          $dropdown_options = array( 'hide_empty' => 0, 'hierarchical' => true, 'show_count' => 0, 'orderby' => 'name', 'selected' => $selected, 'htmlname' => 'settings[visitor][mailinglist]', 'htmlid' => 'visitor_mailinglist' );
 703          MP_Mailinglist::dropdown( $dropdown_options );
 704      }
 705  
 706  	public static function visitor_subscription( $action, $email, $form )
 707      {
 708          $mailinglist_ID = $form->settings['visitor']['mailinglist'];
 709  
 710          if ( !MP_Mailinglist::get( $mailinglist_ID ) ) return;
 711  
 712          if ( !$mp_user_id = MP_User::get_id_by_email( $email ) ) return;
 713  
 714          $user_mailinglists = MP_Mailinglist::get_object_terms( $mp_user_id );
 715          if ( in_array( $mailinglist_ID, $user_mailinglists ) ) return;
 716  
 717          switch ( $action )
 718          {
 719              case 'init' :
 720                  MP_Mailinglist::set_object_terms( $mp_user_id, array( $mailinglist_ID ) );
 721              break;
 722              case 'add' :
 723                  array_push( $user_mailinglists, $mailinglist_ID );
 724                  MP_Mailinglist::set_object_terms( $mp_user_id, $user_mailinglists );
 725              break;
 726          }
 727      }
 728  
 729  // for tracking ( add mailinglist )
 730  	public static function create_tracking_mailinglist( $action )
 731      {
 732          if ( !current_user_can( 'MailPress_manage_mailinglists' ) ) MP_::mp_die( '-1' );
 733          if ( 'create_tracking_mailinglist' != $action )     return false;
 734          if ( !isset( MP_AdminPage::$get_['mail_id'], MP_AdminPage::$get_['track'] ) )    return false;
 735  
 736          $mail_id = MP_AdminPage::$get_['mail_id'];
 737          $track   = MP_AdminPage::$get_['track'];
 738                  
 739          // create mailinglist name
 740          $ml_date = date( 'Ymd' );
 741  
 742          global $wpdb;
 743          $ml_subject = $wpdb->get_var( $wpdb->prepare( "SELECT subject FROM $wpdb->mp_mails WHERE id = %s LIMIT 1;", $mail_id ) );
 744          $ml_subject = substr( $ml_subject, 0 , 10 );
 745  
 746          $ml_track = $track;
 747          foreach ( array( 'http://', 'https://' ) as $http ) $ml_track = str_replace( $http, '', $ml_track );
 748          $ml_track = substr( str_replace( MailPress_tracking_openedmail, __( 'MailOpened', 'MailPress' ), $ml_track ) , 0 , 10 );
 749  
 750          $ml_name = "{$ml_date} {$ml_subject} {$ml_track}";
 751  
 752          // create mailinglist
 753          $ml_id = MP_Mailinglist::get_id( $ml_name );
 754          if ( $ml_id ) MP_Mailinglist::delete( $ml_id );
 755          $ml_id = MP_Mailinglist::insert( array( 'name' => $ml_name, 'description' => $ml_name ) );
 756          if ( !$ml_id ) return false;
 757  
 758          // get_users
 759          global $wpdb;
 760          $users = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT a.user_id as id FROM $wpdb->mp_tracks a, $wpdb->mp_users b WHERE a.mail_id = %d AND a.track = %s AND a.user_id = b.id AND b.status = 'active' ORDER BY 1", $mail_id, $track ) );
 761             if ( empty( $users ) ) return false;
 762          foreach( $users as $user ) 
 763          {
 764              $mls = array();
 765              $mls = MP_Mailinglist::get_object_terms( $user->id );
 766              $mls[] = $ml_id;
 767              $mls = MP_Mailinglist::set_object_terms( $user->id, $mls );
 768          }
 769  
 770          MP_AdminPage::mp_redirect( MP_AdminPage::url( MailPress_users, array( 'mailinglist' => $ml_id ) ) );
 771          die();
 772      }
 773  
 774  
 775  
 776  // for ajax
 777  	public static function mp_action_add_mlnglst()
 778      {
 779          if ( !current_user_can( 'MailPress_manage_mailinglists' ) ) MP_::mp_die( '-1' );
 780  
 781          if ( '' === trim( MP_WP_Ajax::$pst_['name'] ) ) 
 782          {
 783              $x = new WP_Ajax_Response( array(     'what' => 'mailinglist', 
 784                                      'id' => new WP_Error( 'mailinglist_name', __( 'You did not enter a valid mailing list name.', 'MailPress' ) )
 785                                     ) );
 786              $x->send();
 787          }
 788  
 789          if ( MP_Mailinglist::exists( trim( MP_WP_Ajax::$pst_['name'] ) ) ) 
 790          {
 791              $x = new WP_Ajax_Response( array(     'what' => 'mailinglist', 
 792                                      'id' => new WP_Error( __CLASS__ . '::exists', __( 'The mailing list you are trying to create already exists.', 'MailPress' ), array( 'form-field' => 'name' ) ), 
 793                                    ) );
 794              $x->send();
 795          }
 796      
 797          $mailinglist = MP_Mailinglist::insert( MP_WP_Ajax::$pst_, true );
 798  
 799          if ( is_wp_error( $mailinglist ) ) 
 800          {
 801              $x = new WP_Ajax_Response( array(     'what' => 'mailinglist', 
 802                                      'id' => $mailinglist
 803                                    ) );
 804              $x->send();
 805          }
 806  
 807          if ( !$mailinglist || ( !$mailinglist = MP_Mailinglist::get( $mailinglist ) ) )     MP_::mp_die( '0' );
 808  
 809          $level             = 0;
 810          $mailinglist_full_name     = $mailinglist->name;
 811          $_mailinglist         = $mailinglist;
 812          while ( $_mailinglist->parent ) 
 813          {
 814              $_mailinglist         = MP_Mailinglist::get( $_mailinglist->parent );
 815              $mailinglist_full_name     = $_mailinglist->name . ' &#8212; ' . $mailinglist_full_name;
 816              $level++;
 817          }
 818          $mailinglist_full_name = esc_attr( $mailinglist_full_name );
 819  
 820          include  ( MP_ABSPATH . 'mp-admin/mailinglists.php' );
 821          $x = new WP_Ajax_Response( array(     'what' => 'mailinglist', 
 822                                  'id' => $mailinglist->term_id, 
 823                                  'data' => MP_AdminPage::get_row( $mailinglist, array(), $level, $mailinglist_full_name ), 
 824                                  'supplemental' => array( 'name' => $mailinglist_full_name, 'show-link' => sprintf( __( 'Mailing list <a href="#%s">%s</a> added' , 'MailPress' ), "mailinglist-$mailinglist->term_id", $mailinglist_full_name ) )
 825                                ) );
 826          $x->send();
 827      }
 828  
 829  	public static function mp_action_delete_mlnglst() 
 830      {
 831          $id = isset( MP_WP_Ajax::$pst_['id'] )? ( int ) MP_WP_Ajax::$pst_['id'] : 0;
 832          MP_::mp_die( MP_Mailinglist::delete( $id ) ? '1' : '0' );
 833      }
 834  
 835  	public static function mp_action_add_mailinglist()
 836      {
 837          $names = explode( ',', MP_WP_Ajax::$pst_['newmailinglist'] );
 838          $parent = ( int ) MP_WP_Ajax::$pst_['newmailinglist_parent'];
 839          if ( $parent < 0 ) $parent = 0;
 840  
 841          $all_mailinglists_ids = isset( MP_WP_Ajax::$pst_['mp_user_mailinglist'] ) ? ( array ) MP_WP_Ajax::$pst_['mp_user_mailinglist'] : array();
 842  
 843          $most_used_ids = ( isset( MP_WP_Ajax::$pst_['popular_ids'] ) ) ? explode( ',', MP_WP_Ajax::$pst_['popular_ids'] ) : false;
 844  
 845          $x = new WP_Ajax_Response();
 846          foreach ( $names as $name )
 847          {
 848              $name = trim( $name );
 849              $id = MP_Mailinglist::create( $name, $parent );
 850              $all_mailinglists_ids[] = $id;
 851              if ( $parent ) continue;                                        // Do these all at once in a second
 852              $mailinglist = MP_Mailinglist::get( $id );
 853  
 854              $data = self::all_mailinglists_checkboxes( 0, $id, $all_mailinglists_ids, $most_used_ids );
 855  
 856              $x->add( array(    'what' => 'mailinglist', 
 857                          'id'   => $id, 
 858                          'data' => $data, 
 859                          'position' => -1
 860                        ) );
 861          }
 862          if ( $parent ) 
 863          {                                     // Foncy - replace the parent and all its children
 864              $data = self::all_mailinglists_checkboxes( 0, $parent, $all_mailinglists_ids, $most_used_ids );
 865  
 866              $x->add( array(     'what' => 'mailinglist', 
 867                          'id'   => $parent, 
 868                          'old_id' => $parent, 
 869                          'data' => $data, 
 870                          'position' => -1
 871                        ) );
 872          }
 873          $x->send();
 874      }
 875  }
 876  new MailPress_mailinglist();
 877  }


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