[ Index ]

MailPress 7.2

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


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