[ Index ]

MailPress 544

[ 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: 5.4.4
   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', $mp_file . '?page=' . MailPress_page_mailinglists);
  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 general
  73              add_action('MailPress_settings_general',         array(__CLASS__, 'settings_general'), 20);
  74              add_action('MailPress_settings_general_update',    array(__CLASS__, 'settings_general_update'));
  75          // for settings subscriptions
  76              add_action('MailPress_settings_subscriptions',     array(__CLASS__, 'settings_subscriptions'), 20);
  77          // for meta box in user page
  78              if (current_user_can('MailPress_manage_mailinglists'))
  79              {
  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_action('MailPress_users_get_row',     array(__CLASS__, 'users_get_row'), 20, 3);
  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' id='{$htmlname}_{$k}' name='{$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>\n";
 177                          $show_option_all = false;
 178                      }
 179                      $sel = ($k == $selected) ? " selected='selected'" : '';
 180                      $checklist .= "<option value=\"$k\"$sel>" . str_replace('&#160;', '', $v) . "</option>\n";
 181                  break;
 182              }
 183          }
 184          if ('select' == $type) $checklist = "\n{$htmlstart}\n<select name='{$htmlname}'>\n{$checklist}</select>\n{$htmlend}\n";
 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          if (isset($_POST[$name]))
 198          {
 199              foreach ($_POST[$name] as $mailinglist_ID => $v)
 200              {
 201                  array_push($mp_user_mls, $mailinglist_ID);
 202              }
 203          }
 204          MP_Mailinglist::set_object_terms( $mp_user_id, $mp_user_mls);
 205      }
 206  
 207  //// Plugin ////
 208  
 209  // for taxonomy
 210  	public static function update_count_callback( $mailinglists )
 211      {
 212          global $wpdb;
 213  
 214          foreach ( $mailinglists as $mailinglist ) 
 215          {
 216              $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 ) );
 217              $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $mailinglist ) );
 218          }
 219      }
 220  
 221  //// Register form ////
 222  
 223  	public static function user_register($wp_user_id)
 224      {
 225          $user     = get_userdata($wp_user_id);
 226          $email     = $user->user_email;
 227          $mp_user_id    = MP_User::get_id_by_email($email);
 228  
 229          self::update_checklist($mp_user_id);
 230      }
 231  
 232  	public static function register_form()
 233      {
 234          $checklist_mailinglists = self::get_checklist();
 235          if (empty($checklist_mailinglists)) return;
 236  ?>
 237      <br />
 238      <p>
 239          <label>
 240              <?php _e('Mailing lists', MP_TXTDOM); ?>
 241              <br />
 242              <span style='color:#777;font-weight:normal;'>
 243                  <?php echo $checklist_mailinglists; ?>
 244              </span>
 245          </label>
 246      </p>
 247  <?php 
 248      }
 249  
 250  //// Shortcode ////
 251  
 252  	public static function form_defaults($x) { $x['mailinglist'] = false; return $x; }
 253  
 254  	public static function form_options($x)  { return $x; }
 255  
 256  	public static function form_submit($shortcode_message, $email) 
 257      { 
 258          if (!isset($_POST['mailinglist']))     return $shortcode_message;
 259          if (!$_POST['mailinglist'])         return $shortcode_message;
 260          $shortcode = 'shortcode_mailinglists';
 261  
 262          $mp_user_id = MP_User::get_id_by_email($email);
 263          $_POST[$shortcode] = MP_Mailinglist::get_object_terms($mp_user_id);
 264  
 265          $_POST[$shortcode] = array_flip(array_map(trim, explode(',', $_POST['mailinglist'])));
 266  
 267          self::update_checklist($mp_user_id, $shortcode);
 268  
 269          return $shortcode_message . __('<br />Mailing lists added', MP_TXTDOM);
 270      }
 271  
 272  	public static function form($email, $options)  
 273      {
 274          if (!$options['mailinglist']) return;
 275  
 276          $x = array();
 277          foreach (array_map("trim", explode(',', $options['mailinglist'])) as $k => $v) if (MP_Mailinglist::get_name($v)) $x[] = $v;
 278          if (empty($x)) return;
 279  
 280          echo "<input type='hidden' name='mailinglist' value='" . join(', ', $x) . "' />\n";
 281      }
 282  
 283  //// Sending Mails ////
 284  
 285  	public static function mailinglists_optgroup( $label, $optgroup ) 
 286      {
 287          if (__CLASS__ == $optgroup) return __('Mailinglists', MP_TXTDOM);
 288          return $label;
 289      }
 290  
 291  	public static function mailinglists( $draft_dest = array() ) 
 292      {
 293          $args = array('hide_empty' => 0, 'hierarchical' => true, 'show_count' => 0, 'orderby' => 'name', 'htmlname' => 'default_mailinglist' );
 294          foreach (MP_Mailinglist::array_tree($args) as $k => $v) $draft_dest[$k] = $v;
 295          return $draft_dest;
 296      }
 297  
 298  	public static function query_mailinglist( $query, $draft_toemail ) 
 299      {
 300          if ($query) return $query;
 301  
 302          $id = str_replace(__CLASS__ . '~', '', $draft_toemail, $count);
 303          if (0 == $count) return $query;
 304          if (empty($id))  return $query;
 305  
 306          $children = MP_Mailinglist::get_children($id, ', ', '');
 307          $ids = ('' == $children) ? ' = ' . $id : ' IN (' . $id . $children . ') ';
 308  
 309          if (empty($ids)) return $query;
 310  
 311          global $wpdb;
 312          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);
 313      }
 314  
 315  //// mp_user ////
 316  
 317  	public static function set_user_mailinglists( $mp_user_id, $user_mailinglists = array() )
 318      {
 319          if (empty($user_mailinglists)) $user_mailinglists = MP_Mailinglist::get_object_terms( $mp_user_id );
 320          MP_Mailinglist::set_object_terms( $mp_user_id, $user_mailinglists );
 321      }
 322  
 323  	public static function delete_user( $mp_user_id )
 324      {
 325          MP_Mailinglist::delete_object( $mp_user_id );
 326  
 327      }
 328  
 329  //// Autoresponders ////
 330  
 331  	public static function load_Autoresponder_events()
 332      {
 333          new MP_Autoresponder_events_mailinglist();
 334      }
 335  
 336  // Sync wordpress user
 337  	public static function has_subscriptions($has, $mp_user_id)
 338      {
 339          $x = MP_Mailinglist::get_object_terms($mp_user_id);
 340  
 341          if (empty($x)) return $has;
 342          return true;
 343      }
 344  
 345  	public static function sync_subscriptions($oldid, $newid)
 346      {
 347          $old = MP_Mailinglist::get_object_terms($oldid);
 348          if (empty($old)) return;
 349          $new = MP_Mailinglist::get_object_terms($newid);
 350  
 351          MP_Mailinglist::set_object_terms($newid, array_merge($old, $new));
 352      }
 353  
 354  ////  ADMIN  ////
 355  ////  ADMIN  ////
 356  ////  ADMIN  ////
 357  ////  ADMIN  ////
 358  
 359  // for install
 360  	public static function install()
 361      {
 362          global $wpdb;
 363          if (!get_option(self::option_name_default))
 364          {
 365      // Default mailing list
 366              $name = $wpdb->escape(__('Uncategorized', MP_TXTDOM));
 367              $slug = sanitize_title(sanitize_term_field('slug', __('Uncategorized', MP_TXTDOM), 0, self::taxonomy, 'db'));
 368              $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '0')");
 369              $term_id = $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE slug = '$slug' ");
 370              $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ($term_id, '" . self::taxonomy . "', '', '0', '0')");
 371              add_option(self::option_name_default, $term_id );
 372          }
 373  
 374      // Synchronize
 375          $default_mailinglist    = get_option(self::option_name_default);
 376          $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));
 377          if ($unmatches) foreach ($unmatches as $unmatch)
 378          {
 379              MP_Mailinglist::set_object_terms($unmatch->id, array($default_mailinglist));
 380          }
 381      }
 382  
 383  // for link on plugin page
 384  	public static function plugin_action_links($links, $file)
 385      {
 386          return MailPress::plugin_links($links, $file, plugin_basename(__FILE__), 'subscriptions');
 387      }
 388  
 389  // for role & capabilities
 390  	public static function capabilities($capabilities) 
 391      {
 392          $capabilities['MailPress_manage_mailinglists'] = array(    'name'  => __('Mailing lists', MP_TXTDOM), 
 393                                                  'group' => 'users', 
 394                                                  'menu'  => 60, 
 395  
 396                                                  'parent'        => false, 
 397                                                  'page_title'    => __('MailPress Mailing lists', MP_TXTDOM), 
 398                                                  'menu_title'       => '&#160;' . __('Mailing lists', MP_TXTDOM), 
 399                                                  'page'          => MailPress_page_mailinglists, 
 400                                                  'func'          => array('MP_AdminPage', 'body')
 401                                          );
 402          return $capabilities;
 403      }
 404  
 405  // for load admin page
 406  	public static function load_admin_page($hub)
 407      {
 408          $hub[MailPress_page_mailinglists] = 'mailinglists';
 409          return $hub;
 410      }
 411  // for settings
 412  	public static function settings_tab($tabs)
 413      {
 414          $tabs['subscriptions'] = __('Subscriptions', MP_TXTDOM);
 415          return $tabs;
 416      }
 417  
 418  // for settings general
 419  	public static function settings_general()
 420      {
 421          $default_mailinglist    = get_option(self::option_name_default);
 422          $dropdown_options = array('hide_empty' => 0, 'hierarchical' => true, 'show_count' => 0, 'orderby' => 'name', 'selected' => $default_mailinglist, 'htmlname' => 'default_mailinglist' );
 423  ?>
 424              <tr>
 425                  <th class='thtitle'><?php _e('Mailing lists', MP_TXTDOM); ?></th>
 426                  <td></td>
 427              </tr>
 428              <tr class='mp_sep'>
 429                  <th><?php _e('Default Mailing list', MP_TXTDOM); ?></th>
 430                  <td style='padding:0;'>
 431                      <?php    MP_Mailinglist::dropdown($dropdown_options); ?>
 432                  </td>
 433              </tr>
 434  <?php
 435      }
 436  
 437  	public static function settings_general_update()
 438      {
 439          update_option (self::option_name_default, $_POST['default_mailinglist']);
 440      }
 441  
 442  // for settings subscriptions
 443  	public static function settings_subscriptions()
 444      {
 445          include  (MP_ABSPATH . 'mp-admin/includes/settings/subscriptions_mailinglist.form.php');
 446      }
 447  
 448  // for meta box in user page
 449  	public static function update_meta_boxes_user() 
 450      {
 451          if (!isset($_POST['id'])) return;
 452          if (!isset($_POST['mp_user_mailinglist'])) $_POST['mp_user_mailinglist'] = array();
 453  
 454          MP_Mailinglist::set_object_terms($_POST['id'], $_POST['mp_user_mailinglist']);
 455      }
 456  
 457  	public static function styles($styles, $screen) 
 458      {
 459          if ('mailpress_user' != $screen) return $styles;
 460  
 461          wp_register_style ( 'mp-user-mailinglists', '/' . MP_PATH . 'mp-admin/css/user_mailinglists.css' );
 462  
 463          $styles[] = 'mp-user-mailinglists';
 464  
 465          return $styles;
 466      }
 467  
 468  	public static function scripts($scripts, $screen) 
 469      {
 470          if ('mailpress_user' != $screen) return $scripts;
 471  
 472          wp_register_script( 'mp-user-mailinglists', '/' . MP_PATH . 'mp-admin/js/user_mailinglists.js', array('mp-lists'), false, 1);
 473  
 474          $scripts[] = 'mp-user-mailinglists';
 475  
 476          return $scripts;
 477      }
 478  
 479  	public static function add_meta_boxes_user($mp_user_id, $screen)
 480      {
 481          add_meta_box('mailinglistdiv', __('Mailing lists', MP_TXTDOM), array(__CLASS__, 'meta_box'), $screen, 'normal', 'core');
 482      }
 483  
 484  	public static function meta_box($mp_user)
 485      { 
 486  ?>
 487  <ul id="mailinglist-tabs">
 488      <li class="tabs">
 489          <a href="#mailinglists-all" tabindex="3">
 490              <?php _e( 'All Mailing lists', MP_TXTDOM  ); ?>
 491          </a>
 492      </li>
 493      <li class="hide-if-no-js">
 494          <a href="#mailinglists-pop" tabindex="3">
 495              <?php _e( 'Most Used' , MP_TXTDOM ); ?>
 496          </a>
 497      </li>
 498  </ul>
 499  
 500  <div id="mailinglists-pop" class="tabs-panel" style="display: none;">
 501      <ul id="mailinglistchecklist-pop" class="mailinglistchecklist form-no-clear" >
 502  <?php $most_used_ids = self::most_used_checkboxes(self::taxonomy, $mp_user->id); ?>
 503      </ul>
 504  </div>
 505  <div id="mailinglists-all" class="tabs-panel">
 506      <ul id="mailinglistchecklist" class="list:mailinglist mailinglistchecklist form-no-clear">
 507          <?php self::all_mailinglists_checkboxes($mp_user->id, false, false, $most_used_ids) ?>
 508      </ul>
 509  </div>
 510  <div id="mailinglist-adder" class="wp-hidden-children">
 511      <h4>
 512          <a id="mailinglist-add-toggle" href="#mailinglist-add" class="hide-if-no-js" tabindex="3">
 513              <?php _e( '+ Add New mailing list' , MP_TXTDOM); ?>
 514          </a>
 515      </h4>
 516      <p id="mailinglist-add" class="wp-hidden-child">
 517          <label class="screen-reader-text">
 518              <?php _e( 'Add New mailinglist' ); ?>
 519          </label>
 520          <input type="text" name="newmailinglist" id="newmailinglist" class="form-required form-input-tip" value="<?php esc_attr_e( 'New mailing list', MP_TXTDOM ); ?>" tabindex="3" aria-required="true"/>
 521          <label class="screen-reader-text">
 522              <?php _e('Parent Mailing list', MP_TXTDOM); ?> :
 523          </label>
 524          <?php MP_Mailinglist::dropdown( array( 'hide_empty' => 0, 'htmlname' => 'newmailinglist_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __('Parent Mailing list', MP_TXTDOM), 'tab_index' => 3 ) ); ?>
 525          <input type="button" id="mailinglist-add-submit" class="add:mailinglistchecklist:mailinglist-add button" value="<?php esc_attr_e( 'Add', MP_TXTDOM  ); ?>" tabindex="3" />
 526  <?php    wp_nonce_field( 'add-mailinglist', '_ajax_nonce', false ); ?>
 527          <span id="mailinglist-ajax-response"></span>
 528      </p>
 529  </div>
 530  <?php
 531      }
 532  
 533  	public static function most_used_checkboxes( $taxonomy, $mp_user_id, $default = 0, $number = 10, $echo = true ) 
 534      {
 535          $mailinglists = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number, 'hierarchical' => false ) );
 536  
 537          $most_used_ids = array();
 538          foreach ( (array) $mailinglists as $mailinglist ) 
 539          {
 540              $most_used_ids[] = $mailinglist->term_id;
 541              if ( !$echo ) // hack for AJAX use
 542                  continue;
 543              $id = "popular-mailinglist-$mailinglist->term_id";
 544  ?>
 545          <li id="<?php echo $id; ?>" class="popular-mailinglist">
 546              <label class="selectit" for="in-<?php echo $id; ?>">
 547              <input id="in-<?php echo $id; ?>" type="checkbox" value="<?php echo (int) $mailinglist->term_id; ?>" />
 548                  <?php echo esc_html( $mailinglist->name ); ?>
 549              </label>
 550          </li>
 551  <?php
 552          }
 553          return $most_used_ids;
 554      }
 555  
 556  	public static function all_mailinglists_checkboxes( $mp_user_id = 0, $descendants_and_self = 0, $selected_mailinglists = false, $popular_mailinglists = false ) 
 557      {
 558          $walker = new MP_Mailinglists_Walker_Checklist;
 559  
 560          $descendants_and_self = (int) $descendants_and_self;
 561          $args = array();
 562  
 563          if ( is_array( $selected_mailinglists ) )
 564              $args['selected_mailinglists'] = $selected_mailinglists;
 565          elseif ( $mp_user_id )
 566              $args['selected_mailinglists'] = MP_Mailinglist::get_object_terms($mp_user_id);
 567          else
 568              $args['selected_mailinglists'] = array();
 569  
 570          if ( is_array( $popular_mailinglists ) )
 571              $args['popular_mailinglists'] = $popular_mailinglists;
 572          else
 573              $args['popular_mailinglists'] = MP_Mailinglist::get_all( array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
 574  
 575          if ( $descendants_and_self ) 
 576          {
 577              $mailinglists = MP_Mailinglist::get_all( array('child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0) );
 578              $self = MP_Mailinglist::get( $descendants_and_self );
 579              array_unshift( $mailinglists, $self );
 580          }
 581          else
 582          {
 583              $mailinglists = MP_Mailinglist::get_all( array('get' => 'all') );
 584          }
 585  
 586          $all_mailinglists_ids = array();
 587          $keys = array_keys( $mailinglists );
 588  
 589          foreach( $keys as $k )
 590          {
 591              if ( in_array($mailinglists[$k]->term_id, $args['selected_mailinglists']) )
 592              {
 593                  $all_mailinglists_ids[] = $mailinglists[$k];
 594                  unset($mailinglists[$k]);
 595              }
 596          }
 597  
 598          $args['input_name'] = 'mp_user_mailinglist[]';
 599      // Put checked mailinglists on top
 600          echo call_user_func_array(array(&$walker, 'walk'), array($all_mailinglists_ids, 0, $args));
 601      // Then the rest of them
 602          echo call_user_func_array(array(&$walker, 'walk'), array($mailinglists, 0, $args));
 603      }
 604  
 605  // for mp_users list
 606  	public static function users_restrict($url_parms)
 607      {
 608          $x = (isset($url_parms['mailinglist'])) ? $url_parms['mailinglist'] : '';
 609          $dropdown_options = array('show_option_all' => __('View all mailing lists', MP_TXTDOM), 'hide_empty' => 0, 'hierarchical' => true, 'show_count' => 0, 'orderby' => 'name', 'selected' => $x );
 610          MP_Mailinglist::dropdown($dropdown_options);
 611      }
 612  
 613  	public static function users_columns($x)
 614      {
 615          $date = array_pop($x);
 616          $x['mailinglists']=  __('Mailing lists', MP_TXTDOM);
 617          $x['date']        = $date;
 618          return $x;
 619      }
 620  
 621  	public static function users_get_list($array, $url_parms)
 622      {
 623          if (!isset($url_parms['mailinglist']) || empty($url_parms['mailinglist'])) return $array;
 624  
 625          global $wpdb;
 626  
 627          list($where, $tables) = $array;
 628  
 629          $mailinglists = MP_Mailinglist::get_children($url_parms['mailinglist'], ', ', '');
 630          $in = ('' == $mailinglists) ? ' = ' . $url_parms['mailinglist'] : ' IN (' . $url_parms['mailinglist'] . $mailinglists . ') ';
 631  
 632          $where .= " AND (      b.taxonomy = '" . self::taxonomy . "' 
 633                       AND b.term_taxonomy_id = c.term_taxonomy_id 
 634                       AND b.term_id " . $in . "  
 635                       AND a.id = c.object_id )";
 636  
 637          $tables .= ", $wpdb->term_taxonomy b, $wpdb->term_relationships c";
 638  
 639          return array($where, $tables, true);
 640      }
 641  
 642  	public static function users_get_row($column_name, $mp_user, $url_parms)
 643      {
 644          if ('mailinglists' != $column_name) return;
 645  
 646          $args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
 647          $mp_user_mls = MP_Mailinglist::get_object_terms( $mp_user->id, $args);
 648  
 649          if ( !empty( $mp_user_mls ) ) 
 650          {
 651              $out = array();
 652              foreach ( $mp_user_mls as $m )
 653                  $out[] = "<a href='" . MailPress_users . "&amp;mailinglist=$m->term_id'>" . esc_html(sanitize_term_field('name', $m->name, $m->term_id, self::taxonomy, 'display')) . "</a>";
 654              echo join( ', ', $out );
 655          }
 656          else
 657          {
 658              _e('Uncategorized ', MP_TXTDOM);
 659          }
 660      }
 661  
 662  // for form page (visitor subscription options)
 663  	public static function form_visitor_subscription($form)
 664      {
 665          $selected = get_option(self::option_name_default);
 666          if (isset($form->settings['visitor']['mailinglist']))
 667              if (MP_Mailinglist::get($form->settings['visitor']['mailinglist'])) $selected = $form->settings['visitor']['mailinglist'];
 668  
 669  ?>
 670                                          <label for='visitor_mailinglist'><small><?php _e('Mailing list', MP_TXTDOM); ?></small></label>
 671  <?php
 672          $dropdown_options = array('hide_empty' => 0, 'hierarchical' => true, 'show_count' => 0, 'orderby' => 'name', 'selected' => $selected, 'htmlname' => 'settings[visitor][mailinglist]', 'htmlid' => 'visitor_mailinglist' );
 673          MP_Mailinglist::dropdown($dropdown_options);
 674      }
 675  
 676  	public static function visitor_subscription($action, $email, $form)
 677      {
 678          $mailinglist_ID = $form->settings['visitor']['mailinglist'];
 679  
 680          if (!MP_Mailinglist::get($mailinglist_ID)) return;
 681  
 682          if (!$mp_user_id = MP_User::get_id_by_email($email)) return;
 683  
 684          $user_mailinglists = MP_Mailinglist::get_object_terms($mp_user_id);
 685          if (in_array($mailinglist_ID, $user_mailinglists)) return;
 686  
 687          switch ($action)
 688          {
 689              case 'init' :
 690                  MP_Mailinglist::set_object_terms( $mp_user_id, array($mailinglist_ID) );
 691              break;
 692              case 'add' :
 693                  array_push($user_mailinglists, $mailinglist_ID);
 694                  MP_Mailinglist::set_object_terms( $mp_user_id, $user_mailinglists );
 695              break;
 696          }
 697      }
 698  
 699  // for tracking (add mailinglist)
 700  	public static function create_tracking_mailinglist($action)
 701      {
 702          if (!current_user_can('MailPress_manage_mailinglists')) MailPress::mp_die('-1');
 703          if ('create_tracking_mailinglist' != $action)     return false;
 704          if (!isset($_GET['mail_id'], $_GET['track']))    return false;
 705  
 706          $mail_id = $_GET['mail_id'];
 707          $track   = $_GET['track'];
 708                  
 709          // create mailinglist name
 710          $ml_date = date('Ymd');
 711  
 712          global $wpdb;
 713          $ml_subject = $wpdb->get_var( $wpdb->prepare("SELECT subject FROM $wpdb->mp_mails WHERE id = %s LIMIT 1;", $mail_id) );
 714          $ml_subject = substr($ml_subject, 0 , 10);
 715  
 716          $ml_track = $track;
 717          foreach (array('http://', 'https://') as $http) $ml_track = str_replace($http, '', $ml_track);
 718          $ml_track = substr( str_replace(MailPress_tracking_openedmail, __('MailOpened', MP_TXTDOM), $ml_track) , 0 , 10);
 719  
 720          $ml_name = "{$ml_date} {$ml_subject} {$ml_track}";
 721  
 722          // create mailinglist
 723          $ml_id = MP_Mailinglist::get_id($ml_name);
 724          if ($ml_id) MP_Mailinglist::delete($ml_id);
 725          $ml_id = MP_Mailinglist::insert(array('name' => $ml_name, 'description' => $ml_name ));
 726          if (!$ml_id) return false;
 727  
 728          // get_users
 729          global $wpdb;
 730          $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) );
 731             if (empty($users)) return false;
 732          foreach($users as $user) 
 733          {
 734              $mls = array();
 735              $mls = MP_Mailinglist::get_object_terms($user->id);
 736              $mls[] = $ml_id;
 737              $mls = MP_Mailinglist::set_object_terms($user->id, $mls);
 738          }
 739  
 740          MailPress::mp_redirect( MailPress::url(MailPress_users, array('mailinglist' => $ml_id) ) );
 741          die();
 742      }
 743  
 744  
 745  
 746  // for ajax    
 747  	public static function mp_action_add_mlnglst()
 748      {
 749          if (!current_user_can('MailPress_manage_mailinglists')) MP_::mp_die('-1');
 750  
 751          if ( '' === trim($_POST['name']) ) 
 752          {
 753              $x = new WP_Ajax_Response( array(    'what' => 'mailinglist', 
 754                                      'id' => new WP_Error( 'mailinglist_name', __('You did not enter a valid mailing list name.', MP_TXTDOM) )
 755                                     ) );
 756              $x->send();
 757          }
 758  
 759          if ( MP_Mailinglist::exists( trim( $_POST['name'] ) ) ) 
 760          {
 761              $x = new WP_Ajax_Response( array(    'what' => 'mailinglist', 
 762                                      'id' => new WP_Error( __CLASS__ . '::exists', __('The mailing list you are trying to create already exists.', MP_TXTDOM), array( 'form-field' => 'name' ) ), 
 763                                    ) );
 764              $x->send();
 765          }
 766      
 767          $mailinglist = MP_Mailinglist::insert( $_POST, true );
 768  
 769          if ( is_wp_error($mailinglist) ) 
 770          {
 771              $x = new WP_Ajax_Response( array(    'what' => 'mailinglist', 
 772                                      'id' => $mailinglist
 773                                    ) );
 774              $x->send();
 775          }
 776  
 777          if ( !$mailinglist || (!$mailinglist = MP_Mailinglist::get( $mailinglist )) )     MP_::mp_die('0');
 778  
 779          $level             = 0;
 780          $mailinglist_full_name     = $mailinglist->name;
 781          $_mailinglist         = $mailinglist;
 782          while ( $_mailinglist->parent ) 
 783          {
 784              $_mailinglist         = MP_Mailinglist::get( $_mailinglist->parent );
 785              $mailinglist_full_name     = $_mailinglist->name . ' &#8212; ' . $mailinglist_full_name;
 786              $level++;
 787          }
 788          $mailinglist_full_name = esc_attr($mailinglist_full_name);
 789  
 790          include  (MP_ABSPATH . 'mp-admin/mailinglists.php');
 791          $x = new WP_Ajax_Response( array(    'what' => 'mailinglist', 
 792                                  'id' => $mailinglist->term_id, 
 793                                  'data' => MP_AdminPage::get_row( $mailinglist, array(), $level, $mailinglist_full_name ), 
 794                                  'supplemental' => array('name' => $mailinglist_full_name, 'show-link' => sprintf(__( 'Mailing list <a href="#%s">%s</a> added' , MP_TXTDOM), "mailinglist-$mailinglist->term_id", $mailinglist_full_name))
 795                                ) );
 796          $x->send();
 797          break;
 798      }
 799  
 800  	public static function mp_action_delete_mlnglst() 
 801      {
 802          $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 803          MP_::mp_die( MP_Mailinglist::delete($id) ? '1' : '0' );
 804      }
 805  
 806  	public static function mp_action_add_mailinglist()
 807      {
 808          $names = explode(',', $_POST['newmailinglist']);
 809          $parent = (int) $_POST['newmailinglist_parent'];
 810          if ($parent < 0) $parent = 0;
 811  
 812          $all_mailinglists_ids = isset($_POST['mp_user_mailinglist']) ? (array) $_POST['mp_user_mailinglist'] : array();
 813  
 814          $most_used_ids = (isset( $_POST['popular_ids'] )) ? explode( ',', $_POST['popular_ids'] ) : false;
 815  
 816          $x = new WP_Ajax_Response();
 817          foreach ( $names as $name )
 818          {
 819              $name = trim($name);
 820              $id = MP_Mailinglist::create( $name, $parent );
 821              $all_mailinglists_ids[] = $id;
 822              if ( $parent ) continue;                                        // Do these all at once in a second
 823              $mailinglist = MP_Mailinglist::get( $id );
 824              ob_start();
 825                  self::all_mailinglists_checkboxes( 0, $id, $all_mailinglists_ids, $most_used_ids );
 826                  $data = ob_get_contents();
 827              ob_end_clean();
 828              $x->add( array(    'what' => 'mailinglist', 
 829                          'id'   => $id, 
 830                          'data' => $data, 
 831                          'position' => -1
 832                        ) );
 833          }
 834          if ( $parent ) 
 835          {                                     // Foncy - replace the parent and all its children
 836              ob_start();
 837                  self::all_mailinglists_checkboxes( 0, $parent, $all_mailinglists_ids, $most_used_ids );
 838                  $data = ob_get_contents();
 839              ob_end_clean();
 840              $x->add( array(    'what' => 'mailinglist', 
 841                          'id'   => $parent, 
 842                          'old_id' => $parent, 
 843                          'data' => $data, 
 844                          'position' => -1
 845                        ) );
 846          }
 847          $x->send();
 848      }
 849  }
 850  new MailPress_mailinglist();
 851  }


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