[ Index ]

MailPress 7.2

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

title

Body

[close]

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

   1  <?php
   2  if ( class_exists( 'MailPress' ) && !class_exists( 'MailPress_newsletter' ) )
   3  {
   4  /*
   5  Plugin Name: MailPress_newsletter
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/newsletter/
   7  Description: Newsletters : for posts
   8  Version: 7.2
   9  */
  10  
  11  class MailPress_newsletter
  12  {
  13      const meta_key = '_MailPress_newsletter';
  14      const log_name = 'newsletter';
  15      const option_post_limits    = 'MailPress_post_limits_newsletter';
  16      const option_name_default   = 'MailPress_default_newsletter';
  17  
  18      const xml_files = array( 'post', 'daily', 'weekly', 'monthly' );
  19  
  20  	function __construct()
  21      {
  22  // for wordpress hooks
  23  // for plugin
  24          add_action( 'init',     array( __CLASS__, 'init' ), 99 );
  25  
  26  // register form
  27          add_action( 'user_register',             array( __CLASS__, 'user_register' ), 10, 1 );
  28          add_action( 'MailPress_register_form',     array( __CLASS__, 'register_form' ), 10 );
  29  
  30  // for shortcode
  31          add_filter( 'MailPress_form_defaults',     array( __CLASS__, 'form_defaults' ), 8, 1 );
  32          add_filter( 'MailPress_form_options',     array( __CLASS__, 'form_options' ), 8, 1 );
  33          add_filter( 'MailPress_form_submit',         array( __CLASS__, 'form_submit' ), 8, 2 );
  34          add_action( 'MailPress_form',               array( __CLASS__, 'form' ), 1, 2 );
  35  
  36  // for newsletter
  37          add_action( 'MailPress_register_newsletter',array( __CLASS__, 'register' ), 1 );
  38  
  39  // for scheduling and processing newsletters
  40          add_action( 'mp_schedule_newsletters',     array( __CLASS__, 'schedule' ), 8, 1 );
  41          add_action( 'mp_process_newsletter',         array( __CLASS__, 'process' ) );
  42          add_action( 'mp_process_post_newsletter',     array( __CLASS__, 'process' ) );
  43  
  44  // for sending mails
  45          add_filter( 'MailPress_mailinglists_optgroup',     array( __CLASS__, 'mailinglists_optgroup' ), 50, 2 );
  46          add_filter( 'MailPress_mailinglists',             array( __CLASS__, 'mailinglists' ), 50, 1 );
  47          add_filter( 'MailPress_query_mailinglist',         array( __CLASS__, 'query_mailinglist' ), 50, 2 );
  48          add_filter( 'MailPress_query_list_id',             array( __CLASS__, 'query_list_id' ), 50, 2 );
  49  
  50  // for sync wordpress user
  51          add_filter( 'MailPress_has_subscriptions', array( __CLASS__, 'has_subscriptions' ), 8, 2 );
  52          add_action( 'MailPress_sync_subscriptions',array( __CLASS__, 'sync_subscriptions' ), 8, 2 );    
  53  
  54  // for wp admin
  55          if ( is_admin() )
  56          {
  57          // install
  58              register_activation_hook(  plugin_basename( __FILE__ ), array( __CLASS__, 'install' ) );
  59              register_deactivation_hook( plugin_basename( __FILE__ ), array( __CLASS__, 'uninstall' ) );
  60          // for link on plugin page
  61              add_filter( 'plugin_action_links',             array( __CLASS__, 'plugin_action_links' ), 10, 2 );
  62          // for role & capabilities
  63              add_filter( 'MailPress_capabilities',         array( __CLASS__, 'capabilities' ), 1, 1 );
  64          // for settings
  65              add_filter( 'MailPress_scripts',                    array( __CLASS__, 'scripts' ), 8, 2 );
  66              add_filter( 'MailPress_settings_tab',         array( __CLASS__, 'settings_tab' ), 10, 1 );
  67          // for settings subscriptions
  68              add_filter( 'MailPress_settings_subscriptions_help',    array( __CLASS__, 'settings_subscriptions_help' ), 20, 1 );
  69              add_action( 'MailPress_settings_subscriptions_form',    array( __CLASS__, 'settings_subscriptions_form'), 20 );
  70          // for settings logs
  71              add_action( 'MailPress_settings_logs_form',         array( __CLASS__, 'settings_logs_form' ), 50 );
  72          // for meta box in user page
  73              if ( current_user_can( 'MailPress_manage_newsletters' ) )
  74              {
  75                  add_action( 'MailPress_add_help_tab_user',        array( __CLASS__, 'add_help_tab_user' ), 20 );
  76                  add_action( 'MailPress_update_meta_boxes_user',    array( __CLASS__, 'update_meta_boxes_user' ) );
  77                  add_action( 'MailPress_add_meta_boxes_user',     array( __CLASS__, 'add_meta_boxes_user' ), 20, 2 );
  78              }
  79          // for meta box in write post
  80              if ( current_user_can( 'MailPress_test_newsletters' ) )
  81                  add_action( 'do_meta_boxes',                 array( __CLASS__, 'add_meta_boxes_post' ), 8, 3 );
  82          }
  83  
  84  // for mp_users list
  85          add_action( 'MailPress_users_restrict',     array( __CLASS__, 'users_restrict' ), 9, 1 );
  86          add_filter( 'MailPress_users_columns',     array( __CLASS__, 'users_columns' ), 10, 1 );
  87          add_action( 'MailPress_users_get_list',     array( __CLASS__, 'users_get_list' ), 10, 2 );
  88          add_filter( 'MailPress_users_get_row',     array( __CLASS__, 'users_get_row' ), 10, 4 );
  89  
  90  // for ajax
  91          add_action( 'mp_action_mp_meta_box_post_test',     array( __CLASS__, 'mp_action_mp_meta_box_post_test' ) );
  92      }
  93  
  94  //// Subscriptions ////
  95  
  96  	public static function get_checklist( $mp_user_id = false, $args = '' ) 
  97      {
  98          global $mp_subscriptions;
  99          if ( !isset( $mp_subscriptions['newsletters'] ) ) return false;
 100          if ( empty( $mp_subscriptions['newsletters'] ) ) return false;
 101  
 102          $checklist = '';
 103          $defaults = array (     'htmlname'     => 'keep_newsletters',
 104                          'admin'         => 0, 
 105                          'selected'     => false, 
 106                          'type'        => 'checkbox',
 107                          'show_option_all'  => false, 
 108                             'htmlstart'    => '', 
 109                          'htmlmiddle'    => '&#160;&#160;', 
 110                          'htmlend'        => "<br />\n"
 111                       );
 112          $r = wp_parse_args( $args, $defaults );
 113          extract( $r );
 114  
 115          $lib_nl = ( $admin ) ? 'admin' : 'blog';
 116  
 117          if ( $mp_user_id )     $in = MP_Newsletter::get_object_terms( $mp_user_id );
 118          else                $in = MP_Newsletter::get_defaults();
 119  
 120          global $mp_registered_newsletters;
 121          $nls = MP_Newsletter::get_active();
 122  
 123          foreach ( $nls as $k => $v )
 124          {
 125              if ( $mp_registered_newsletters[$k]['descriptions'][$lib_nl] )
 126                  switch ( $type )
 127                  {
 128                      case 'checkbox' :
 129                          $checked = checked( isset( $in[$k] ), true, false );
 130                          $_type   = 'checkbox';
 131  
 132                          $v         = $mp_registered_newsletters[$k]['descriptions'][$lib_nl];
 133  
 134                          $tag          = '<input type="' . $_type . '" name="' . $htmlname . '[' . $k . ']" id="' . $htmlname . '_' . $k . '"'. $checked . ' />';
 135                          $htmlstart2  = str_replace( '{{id}}', "{$htmlname}_{$k}", $htmlstart );
 136                          $htmlmiddle2 = $htmlmiddle . str_replace( '&#160;', '', $v );
 137                          $htmlend2    = $htmlend;
 138  
 139                          $checklist .= "$htmlstart2$tag$htmlmiddle2$htmlend2";
 140                      break;
 141                      case 'select' :
 142                          if ( $show_option_all )
 143                          {
 144                              $checklist .= '<option value="">' . $show_option_all . '</option>';
 145                              $show_option_all = false;
 146                          }
 147                          $sel = ( $k == $selected ) ? ' selected="selected"' : '';
 148                          $checklist .= '<option value="' . $k . '"' . $sel . '>' . $mp_registered_newsletters[$k]['descriptions'][$lib_nl] . '</option>';
 149                      break;
 150                  }
 151          }
 152  //        if ( 'select' == $type ) $checklist = $htmlstart . '<select name="' . $htmlname . '">' . $checklist . '</select>' . $htmlend;
 153          $htmlid = ( isset( $htmlid ) ) ? ' id="' . $htmlid . '"' : ''; 
 154          if ( 'select' == $type ) $checklist = $htmlstart . '<select name="' . $htmlname . '"' . $htmlid . '>' . $checklist . '</select>' . $htmlend;
 155  
 156          return $checklist;
 157      }
 158  
 159  	public static function update_checklist( $mp_user_id, $name = 'keep_newsletters' ) 
 160      {
 161          $post_ = filter_input_array( INPUT_POST );
 162  
 163          if ( isset( $post_[$name] ) ) MP_Newsletter::set_object_terms( $mp_user_id, $post_[$name] );
 164      }
 165  
 166  	public static function get_subscriptions( $id ) 
 167      {
 168          global $mp_subscriptions;
 169          if ( !isset( $mp_subscriptions['newsletters'] ) ) return false;
 170          if ( empty( $mp_subscriptions['newsletters'] ) ) return false;
 171  
 172          $mp_user_nls = MP_Newsletter::get_object_terms( $id );
 173  
 174          if ( empty( $mp_user_nls ) ) return false;
 175  
 176          return $mp_user_nls;
 177      }
 178  
 179  	public static function list_unsubscribe( $mp_user_id, $newsletter )
 180      {
 181          $in = MP_Newsletter::get_object_terms( $mp_user_id );
 182  
 183          if ( !is_array( $in ) ) return;
 184          if ( empty( $in ) )     return;
 185  
 186          unset( $in[$newsletter] );
 187  
 188          MP_Newsletter::set_object_terms( $mp_user_id, $in );
 189      }
 190  
 191  //// Plugin ////
 192  
 193  	public static function init()
 194      {
 195          do_action( 'MailPress_register_newsletter' );
 196          do_action( 'MailPress_registered_newsletters' );
 197  
 198          new MP_Newsletter_schedulers();
 199      }
 200  
 201  //// Register form ////
 202  
 203  	public static function user_register( $wp_user_id )
 204      {
 205          $post_ = filter_input_array( INPUT_POST );
 206  
 207          $user     = get_userdata( $wp_user_id );
 208          $email     = $user->user_email;
 209          $mp_user_id= MP_User::get_id_by_email( $email );
 210                  
 211          if( !isset( $post_['keep_newsletters'] ) ) $post_['keep_newsletters'] = MP_Newsletter::get_defaults();
 212  
 213          self::update_checklist( $mp_user_id );
 214      }
 215  
 216  	public static function register_form()
 217      {
 218          $checklist_newsletters = self::get_checklist();
 219          if ( empty( $checklist_newsletters ) ) return;
 220  
 221          $out  = '    <br />' . "\r\n";
 222          $out .= '    <p>' . "\r\n";
 223          $out .= '        <label>' . "\r\n";
 224          $out .= __( 'Newsletters', 'MailPress' ) . "\r\n";
 225          $out .= '            <br />' . "\r\n";
 226          $out .= '            <span style="color:#777;font-weight:normal;">' . "\r\n";
 227          $out .= $checklist_newsletters . "\r\n";
 228          $out .= '            </span>' . "\r\n";
 229          $out .= '        </label>' . "\r\n";
 230          $out .= '    </p>' . "\r\n";
 231  
 232          echo $out;
 233      }
 234  
 235  //// Shortcode ////
 236  
 237  	public static function form_defaults( $x ) { $x['newsletter'] = false; return $x; }
 238  
 239  	public static function form_options( $x )  { return $x; }
 240  
 241  	public static function form_submit( $shortcode_message, $email )  
 242      {
 243          $post_ = filter_input_array( INPUT_POST );
 244  
 245          if ( !isset( $post_['newsletter'] ) )    return $shortcode_message;
 246          if ( !$post_['newsletter'] )         return $shortcode_message;
 247          $shortcode = 'shortcode_newsletters';
 248  
 249          $mp_user_id = MP_User::get_id_by_email( $email );
 250          $post_[$shortcode] = MP_Newsletter::get_object_terms( $mp_user_id );
 251  
 252          $post_[$shortcode] = array_flip( array_map( trim, explode( ',', $post_['newsletter'] ) ) );
 253  
 254          self::update_checklist( $mp_user_id, $shortcode );
 255  
 256          return $shortcode_message . __( '<br />Newsletters added', 'MailPress' );
 257      }
 258  
 259  	public static function form( $email, $options )  
 260      {
 261          if ( !$options['newsletter'] ) return;
 262  
 263          global $mp_registered_newsletters;
 264          $x = array();
 265          foreach ( array_map( trim, explode( ',', $options['newsletter'] ) ) as $k => $v ) if ( isset( $mp_registered_newsletters[$v] ) && $mp_registered_newsletters[$v]['allowed'] ) $x[] = $v;
 266          if ( empty( $x ) ) return;
 267  
 268          echo '<input type="hidden" name="newsletter" value="' . join( ', ', $x ) . '" />';
 269      }
 270  
 271  ////  Newsletter  ////
 272  
 273  	public static function register() 
 274      {
 275          $advanced_path = 'advanced/' . get_current_blog_id() . '/newsletters/post';
 276          $args = array(     'root'         => MP_UPL_ABSPATH . $advanced_path,
 277                      'root_filter'     => 'MailPress_advanced_newsletters_root',
 278                      'files'        => self::xml_files,
 279  
 280                      'post_type'    => 'post',
 281           );
 282  
 283          MP_Newsletter::register_files( $args );
 284      }
 285  
 286  //// Scheduling & Processing  ////
 287  
 288  	public static function schedule( $args = array() )
 289      {
 290          extract( $args );
 291  
 292          wp_unschedule_hook( 'mp_process_newsletter' );
 293          MP_Newsletter_schedulers::schedule( $event );
 294      }
 295  
 296  	public static function process( $args )
 297      {
 298          extract( $args );
 299  
 300          new MP_Newsletter_processors();
 301  
 302          MP_Newsletter_processors::process( $newsletter );
 303      }
 304  
 305  //// Sending Mails ////
 306  
 307  	public static function mailinglists_optgroup( $label, $optgroup ) 
 308      {
 309          if ( __CLASS__ == $optgroup ) return __( 'Newsletters', 'MailPress' );
 310          return $label;
 311      }
 312  
 313  	public static function mailinglists( $draft_dest = array() )
 314      {
 315          $x = MP_Newsletter::get_active();
 316          foreach ( $x as $k => $v ) $draft_dest[__CLASS__ . '~' . $k] = $v;
 317          return $draft_dest;
 318      }
 319  
 320  	public static function query_mailinglist( $query, $draft_toemail )
 321      {
 322          if ( $query ) return $query;
 323  
 324          $id = str_replace( __CLASS__ . '~', '', $draft_toemail, $count );
 325          if ( 0 == $count ) return $query;
 326          if ( empty( $id ) ) return $query;
 327  
 328          global $mp_registered_newsletters;
 329          if ( !isset( $mp_registered_newsletters[$id] ) ) return $query;
 330  
 331          $in = ( $mp_registered_newsletters[$id]['default'] ) ? 'NOT' : '';
 332  
 333          return MP_Newsletter::get_query_newsletter( $id, $in );
 334      }
 335  
 336  	public static function query_list_id( $list_id, $draft_toemail )
 337      {
 338          if ( $list_id ) return $list_id;
 339  
 340          $id = str_replace( __CLASS__ . '~', '', $draft_toemail, $count );
 341          if ( 0 == $count ) return $list_id;
 342          if ( empty( $id ) ) return $list_id;
 343  
 344          return __CLASS__ . '.' . $id;
 345      }
 346  
 347  // Sync wordpress user
 348  
 349  	public static function has_subscriptions( $has, $mp_user_id )
 350      {
 351          $x = MP_Newsletter::get_object_terms( $mp_user_id );
 352  
 353          if ( empty( $x ) ) return $has;
 354          return true;
 355      }
 356  
 357  	public static function sync_subscriptions( $oldid, $newid )
 358      {
 359          $old = MP_Newsletter::get_object_terms( $oldid );
 360          if ( empty( $old ) ) return;
 361          $new = MP_Newsletter::get_object_terms( $newid );
 362  
 363          MP_Newsletter::set_object_terms( $newid, array_merge( $old, $new ) );
 364      }
 365  
 366  ////  ADMIN  ////
 367  ////  ADMIN  ////
 368  ////  ADMIN  ////
 369  ////  ADMIN  ////
 370  
 371  // install
 372  	public static function install() 
 373      {
 374          self::uninstall();
 375  
 376          MP_Log::set_option( self::log_name );
 377  
 378          include  ( MP_ABSPATH . 'mp-admin/includes/install/newsletter.php' );
 379  
 380          $now4cron = current_time( 'timestamp', 'gmt' );
 381          wp_schedule_single_event( $now4cron - 1, 'mp_schedule_newsletters', array( 'args' => array( 'event' => '** Install **' ) ) );
 382  
 383          $twicedaily = gmmktime( 0, 0, 0, gmdate( 'n', $now4cron ), gmdate( 'j', $now4cron ), gmdate( 'Y', $now4cron ) ) - get_option( 'gmt_offset' ) * 3600;
 384          wp_schedule_event( $twicedaily, 'twicedaily', 'mp_schedule_newsletters', array( 'args' => array( 'event' => '** Twice daily **' ) ) );
 385      }
 386  
 387  	public static function uninstall() 
 388      {
 389          wp_unschedule_hook( 'mp_process_newsletter' );
 390          wp_unschedule_hook( 'mp_schedule_newsletters' );
 391      }
 392  
 393  // for link on plugin page
 394  	public static function plugin_action_links( $links, $file )
 395      {
 396          return MailPress::plugin_links( $links, $file, plugin_basename( __FILE__ ), 'subscriptions' );
 397      }
 398  
 399  // for role & capabilities
 400  	public static function capabilities( $capabilities ) 
 401      {
 402          $capabilities['MailPress_manage_newsletters'] = array(     'name'  => __( 'Newsletters', 'MailPress' ), 
 403                                                  'group' => 'users'
 404                                           );
 405          $capabilities['MailPress_test_newsletters'] = array(     'name'  => __( 'Post test', 'MailPress' ), 
 406                                                  'group' => 'admin'
 407                                           );
 408          return $capabilities;
 409      }
 410  
 411  // for settings
 412  
 413  	public static function get_post_limits()
 414      {
 415          global $mp_general;
 416  
 417          if ( isset( $mp_general['post_limits'] ) )
 418          {
 419              $pl = $mp_general['post_limits'];
 420              update_option( self::option_post_limits, $mp_general['post_limits'] );
 421              unset( $mp_general['post_limits'] );
 422              update_option( MailPress::option_name_general, $mp_general );
 423              return $pl;
 424          }
 425  
 426          return get_option( self::option_post_limits );
 427      }
 428  
 429  	public static function scripts( $scripts, $screen ) 
 430      {
 431          if ( $screen != MailPress_page_settings ) return $scripts;
 432  
 433          wp_register_script( 'mp-newsletter',     '/' . MP_PATH . 'mp-admin/js/settings_newsletter.js', array(), false, 1 );
 434          $scripts[] = 'mp-newsletter';
 435          return $scripts;
 436      }
 437  
 438  	public static function settings_tab( $tabs )
 439      {
 440          $tabs['subscriptions'] = __( 'Subscriptions', 'MailPress' );
 441          return $tabs;
 442      }
 443  
 444  // for settings subscriptions
 445  	public static function settings_subscriptions_help( $content )
 446      {
 447          include  ( MP_ABSPATH . 'mp-admin/includes/settings/subscriptions/newsletter/help.php' );
 448          return $content;
 449      }
 450  
 451  	public static function settings_subscriptions_form()
 452      {
 453          include  ( MP_ABSPATH . 'mp-admin/includes/settings/subscriptions/newsletter/form.php' );
 454      }
 455  
 456  // for settings logs
 457  	public static function settings_logs_form( $logs )
 458      {
 459          MP_AdminPage::log_form( self::log_name, $logs, __( 'Newsletter', 'MailPress' ) );
 460      }
 461  
 462  // for meta box in user page
 463  	public static function add_help_tab_user()
 464      {
 465          global $current_screen;
 466  
 467          $content = '';
 468          $content .= '<p><strong>' . __( 'Newsletters :', 'MailPress' ) . '</strong></p>';
 469          $content .= '<p>' . __( 'You can modify newsletters subscriptions for a mp user.', 'MailPress' ) . '</p>';
 470  
 471          $current_screen->add_help_tab( array(     'id'        => 'newsletters',
 472                                          'title'    => __( 'Newsletters', 'MailPress' ),
 473                                          'content'    => $content )
 474          );
 475      }
 476  
 477  	public static function update_meta_boxes_user() 
 478      {
 479          if ( !isset( MP_AdminPage::$pst_['id'] ) ) return;
 480          if ( !isset( MP_AdminPage::$pst_['keep_newsletters'] ) ) MP_AdminPage::$pst_['keep_newsletters'] = array();
 481  
 482          self::update_checklist( MP_AdminPage::$pst_['id'] );
 483      }
 484  
 485  	public static function add_meta_boxes_user( $mp_user_id, $screen )
 486      {
 487          add_meta_box( 'newsletterdiv', __( 'Newsletters', 'MailPress' ), array( __CLASS__, 'meta_box' ), $screen, 'normal', 'core' );
 488      }
 489  
 490  	public static function meta_box( $mp_user )
 491      { 
 492          $check_newsletters = self::get_checklist( $mp_user->id, array( 'admin' => true ) );
 493          echo ( $check_newsletters ) ? $check_newsletters : __( 'Nothing to subscribe for ...', 'MailPress' ) . ' <a href="' . MailPress_settings . '&amp;tab=subscriptions">' . __( 'Settings' ) . '</a>';
 494      }
 495  
 496  // for meta box in write post  ////
 497  	public static function add_meta_boxes_post( $page, $type, $post )
 498      {
 499          if ( 'post' != $page ) return;
 500          if ( 'side' != $type ) return;
 501  
 502          wp_register_style( 'mp-meta-box-post-test',     '/' . MP_PATH . 'mp-admin/css/mp-meta-box-post-test.css' );
 503          wp_enqueue_style( 'mp-meta-box-post-test' );
 504  
 505          wp_register_script( 'mp-meta-box-post-test',     '/' . MP_PATH . 'mp-includes/js/meta_boxes/post/test.js', array( 'wp-ajax-response' ), false, 1 );
 506          wp_enqueue_script( 'mp-meta-box-post-test' );
 507  
 508          wp_register_script( 'mp-thickbox',         '/' . MP_PATH . 'mp-includes/js/mp_thickbox.js', array( 'thickbox' ), false, 1 );
 509          wp_enqueue_script( 'mp-thickbox' );
 510  
 511          add_meta_box( 'MailPress_post_test_div', __( 'MailPress test', 'MailPress' ), array( __CLASS__, 'meta_box_post_test' ), 'post', 'side', 'core' );
 512      }
 513  
 514  	public static function meta_box_post_test( $post ) 
 515      {
 516          include  ( MP_ABSPATH . 'mp-includes/meta_boxes/post/test.php' );
 517      }
 518  
 519  
 520  //// Mp_users list  ////
 521  
 522  	public static function users_restrict( $url_parms )
 523      {
 524          $x = ( isset( $url_parms['newsletter'] ) ) ? $url_parms['newsletter'] : '';
 525          $dropdown_options = array( 'show_option_all' => __( 'View all newsletters', 'MailPress' ), 'selected' => $x );
 526          self::dropdown( $dropdown_options );
 527      }
 528  
 529  	public static function users_columns( $x )
 530      {
 531          $date = array_pop( $x );
 532          $x['newsletters']=  __( 'Newsletters', 'MailPress' );
 533          $x['date']        = $date;
 534          return $x;
 535      }
 536  
 537  	public static function users_get_list( $array, $url_parms )
 538      {
 539          if ( !isset( $url_parms['newsletter'] ) || empty( $url_parms['newsletter'] ) ) return $array;
 540  
 541          global $mp_registered_newsletters;
 542          global $wpdb;
 543  
 544          if ( !isset( $mp_registered_newsletters[$url_parms['newsletter']] ) ) return $array;
 545  
 546          list( $where, $tables ) = $array;
 547  
 548          $newsletter = $mp_registered_newsletters[$url_parms['newsletter']];
 549          $in     = ( $newsletter['default'] ) ? 'NOT' : '';
 550  
 551          $where .= " AND $in EXISTS 	( 
 552                      SELECT DISTINCT z.mp_user_id
 553                      FROM     $wpdb->mp_usermeta z
 554                      WHERE z.meta_key = '" . self::meta_key . "'
 555                      AND     z.meta_value = '" . $newsletter['id'] . "'
 556                      AND     z.mp_user_id = a.id
 557                   ) ";
 558  
 559          return array( $where, $tables, true );
 560      }
 561  
 562  	public static function users_get_row( $out, $column_name, $mp_user, $url_parms )
 563      {
 564          if ( 'newsletters' == $column_name )
 565          {
 566              $o = array();
 567  
 568              $newsletters = MP_Newsletter::get_object_terms( $mp_user->id );
 569              if ( !empty( $newsletters ) )
 570              {
 571                  foreach( $newsletters as $k => $v )
 572                  {
 573                      $o[] = '<a href="' . add_query_arg( 'newsletter', $k, MailPress_users ) . '">' . $v . '</a>';
 574                  }
 575                  $out .= join( ', ', $o );
 576              }
 577              else
 578              {
 579                  $out .= __( 'None ', 'MailPress' );
 580              }
 581          }
 582          return $out;
 583      }
 584  
 585  	public static function dropdown( $args = '' )
 586      {
 587          $defaults = array( 'class'        => 'postform',
 588                      'echo'         => 1,
 589                      'htmlid'        => 'newsletter_dropdown',
 590                      'htmlname'     => 'newsletter',
 591                      'selected'     => 0
 592                       );
 593  
 594          $r = wp_parse_args( $args, $defaults );
 595          extract( $r );
 596  
 597          $x = MP_Newsletter::get_active();
 598  
 599          $output = '';
 600          if ( ! empty( $x ) )
 601          {
 602              if ( $show_option_all ) $list[0] = $show_option_all;
 603              foreach( $x as $k => $v ) $list[$k] = $v;
 604  
 605              $htmlid = ( $htmlid === true ) ? 'id="' . $htmlname . '"' : 'id="' . $htmlid . '"' ;
 606              $output = '<select name="' . $htmlname . '" ' . $htmlid . ' class="' . $class . '">';
 607              $output .= MP_::select_option( $list, $selected, false );
 608              $output .= '</select>' . "\n";
 609          }
 610  
 611          if ( $echo )    echo $output;
 612  
 613          return $output;
 614      }
 615  
 616  // for ajax
 617  	public static function mp_action_mp_meta_box_post_test() 
 618      {
 619          global $mp_registered_newsletters;
 620  
 621          $post_ = filter_input_array( INPUT_POST );
 622  
 623          $post_id = $post_['post_id'];
 624          if ( empty( $post_id ) )            return new WP_Error( 'post', __( 'post not saved', 'MailPress' ) );
 625          $post = get_post( $post_id );
 626          if ( !$post )                    return new WP_Error( 'post', __( 'post not saved', 'MailPress' ) );
 627  
 628          $newsletter = MP_Newsletter::get( $post_['newsletter'] );
 629          if ( !$newsletter )                return new WP_Error( 'newsletter', __( 'unknown newsletter', 'MailPress' ) );
 630  
 631          $theme = $post_['theme'];
 632          if ( empty( $theme ) && isset( $newsletter['mail']['Theme'] ) ) $theme = $newsletter['mail']['Theme'];
 633  
 634          update_user_meta( MP_WP_User::get_id(), "_MailPress_post_$post_id", array( 'toemail' => $post_['toemail'], 'theme' => $theme, 'newsletter' => $post_['newsletter'] ) );    
 635  
 636          $newsletter['mail']['Theme']         = $theme;
 637          $newsletter['mail']['subject']    = __( '(Test)', 'MailPress' ) . ' ' . $newsletter['mail']['subject'];
 638          $newsletter['mail']['the_title']     = apply_filters( 'the_title', $post->post_title );
 639  
 640          $newsletter['query_posts']     = array( 'p'    =>    $post_id );
 641  
 642          $mail            = new stdClass();
 643          $mail->id        = MP_Mail::get_id( 'send_post_ajax' );
 644          $mail->toemail     = $post_['toemail'];
 645  
 646          $rc = MP_Newsletter::send( $newsletter, false, $mail );
 647  
 648          if ( !$rc ) MP_Mail::delete( $mail->id );
 649                  
 650          $args = array( 'id' => $mail->id, 'action' => 'mp_ajax', 'mp_action' => 'iview', 'TB_iframe' => 'true' );
 651          $view_url = esc_url( add_query_arg( $args, admin_url( 'admin-ajax.php' ) ) );
 652  
 653          $x = new WP_Ajax_Response( array    ( 
 654                          'what' => 'mp_post_test', 
 655                          'id' => $mail->id, 
 656                          'data' => !$rc ? __( 'Sending mail failed !', 'MailPress' ) : sprintf( '<span id="mail-%1$s">%2$s</span>', $mail->id , sprintf( __( '%1$sView%2$s sent mail', 'MailPress' ), sprintf( '<a href="%1$s" class="thickbox thickbox-preview">', $view_url ), '</a>' ) )
 657                           )
 658           );
 659          $x->send();
 660      }
 661  }
 662  new MailPress_newsletter();
 663  }


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