[ Index ]

MailPress 544

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

title

Body

[close]

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

   1  <?php
   2  if (class_exists('MailPress') && !class_exists('MailPress_comment') )
   3  {
   4  /*
   5  Plugin Name: MailPress_comment
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/comment/
   7  Description: Subscribe to comments
   8  Version: 5.4.4
   9  */
  10  
  11  class MailPress_comment
  12  {
  13      const meta_key = '_MailPress_subscribe_to_comments_';
  14      const option   = 'MailPress_comment';
  15  
  16  	function __construct()
  17      {
  18  // for wordpress hooks
  19  // for comment
  20          add_action('comment_form',                     array(__CLASS__, 'comment_form'));
  21          add_action('comment_post',                     array(__CLASS__, 'comment_post'), 8, 1);
  22          add_action('wp_set_comment_status',             array(__CLASS__, 'approve_comment'));
  23  // for post
  24          add_action('delete_post',                     array(__CLASS__, 'delete_post'));
  25  
  26  // for sending mails
  27          add_filter('MailPress_mailinglists_optgroup',     array(__CLASS__, 'mailinglists_optgroup'), 5, 2);
  28          add_filter('MailPress_mailinglists',             array(__CLASS__, 'mailinglists'), 5, 1);
  29          add_filter('MailPress_query_mailinglist',         array(__CLASS__, 'query_mailinglist'), 5, 2);
  30  
  31  // for mp_user
  32          add_action('MailPress_activate_user',             array(__CLASS__, 'activate_user'), 8, 1);
  33          add_action('MailPress_deactivate_user',             array(__CLASS__, 'deactivate_user'), 8, 1);
  34          add_action('MailPress_unsubscribe_user',             array(__CLASS__, 'delete_user'), 8, 1);
  35          add_action('MailPress_delete_user',             array(__CLASS__, 'delete_user'), 8, 1);    
  36  
  37  // for autoresponder
  38          add_action('MailPress_load_Autoresponder_events',    array(__CLASS__, 'load_Autoresponder_events'));
  39  
  40  // for sync wordpress user
  41          add_filter('MailPress_has_subscriptions',         array(__CLASS__, 'has_subscriptions'), 8, 2);
  42          add_action('MailPress_sync_subscriptions',        array(__CLASS__, 'sync_subscriptions'), 8, 2);    
  43  
  44  // for wp admin
  45          if (is_admin())
  46          {
  47          // for link on plugin page
  48              add_filter('plugin_action_links',             array(__CLASS__, 'plugin_action_links'), 10, 2 );
  49          // for role & capabilities
  50              add_filter('MailPress_capabilities',         array(__CLASS__, 'capabilities'), 1, 1);
  51          // for settings
  52              add_filter('MailPress_settings_tab',         array(__CLASS__, 'settings_tab'), 8, 10);
  53          // for settings subscriptions
  54              add_action('MailPress_settings_subscriptions',     array(__CLASS__, 'settings_subscriptions'), 10);
  55  
  56          // for meta box in user page
  57              if (current_user_can('MailPress_manage_comments'))
  58              {
  59                  add_action('MailPress_update_meta_boxes_user',     array(__CLASS__, 'update_meta_boxes_user'));
  60                  add_action('MailPress_add_meta_boxes_user',     array(__CLASS__, 'add_meta_boxes_user'), 10, 2);
  61              }
  62          // for dashboard
  63              add_action('MailPress_load_Dashboard_widgets',     array(__CLASS__, 'load_Dashboard_widgets'));
  64          }
  65  
  66  // for mp_users list
  67          add_filter('MailPress_users_columns',             array(__CLASS__, 'users_columns'), 30, 1);
  68          add_action('MailPress_users_get_row',             array(__CLASS__, 'users_get_row'), 30, 3);
  69  
  70  // for posts list
  71          add_filter('manage_edit_columns',                array(__CLASS__, 'manage_edit_columns'), 10, 1);
  72          add_action('manage_posts_custom_column',            array(__CLASS__, 'manage_posts_custom_column'), 10, 2);
  73  
  74  // for comments list
  75          add_filter('manage_edit-comments_columns',        array(__CLASS__, 'manage_edit_comments_columns'), 10, 1);
  76          add_action('manage_comments_custom_column',        array(__CLASS__, 'manage_comments_custom_column'), 10, 2);
  77      }
  78  
  79  //// Subscriptions ////
  80  
  81  	public static function get_checklist($mp_user_id = false, $args = '') 
  82      {
  83          $checklist = '';
  84          $defaults = array (    'htmlname'     => 'keep_comment_sub', 
  85                          'echo'     => 1, 
  86  
  87                          'type'    => 'checkbox', 
  88  
  89                          'htmlstart'    => '', 
  90                          'htmlmiddle'=> '&#160;&#160;', 
  91                          'htmlend'    => "<br />\n"
  92                      );
  93          $r = wp_parse_args( $args, $defaults );
  94          extract( $r );
  95  
  96          $comment_subs = self::get_comment_subs($mp_user_id);
  97          foreach ($comment_subs as $comment_sub)
  98          {
  99              $typ2         = 'checkbox';
 100              $k         = $comment_sub->meta_id;
 101              $v         = apply_filters('the_title', $comment_sub->post_title );
 102  
 103              $tag          = "<input type='$typ2' id='{$htmlname}_{$k}' name='{$htmlname}[{$k}]' checked='checked' />";
 104              $htmlstart2  = str_replace('{{id}}', "{$htmlname}_{$k}", $htmlstart);
 105              $htmlmiddle2 = $htmlmiddle . str_replace('&#160;', '', $v);
 106              $htmlend2    = $htmlend;
 107  
 108              $checklist .= "$htmlstart2$tag$htmlmiddle2$htmlend2";
 109          }
 110          return $checklist;
 111      }
 112  
 113  	public static function update_checklist($mp_user_id)
 114      {
 115          $comment_subs = self::get_comment_subs($mp_user_id);
 116          foreach ($comment_subs as $comment_sub)
 117          {
 118              if (isset($_POST['keep_comment_sub'][$comment_sub->meta_id])) continue;
 119              delete_post_meta($comment_sub->post_id, self::meta_key, $mp_user_id);
 120              new MP_Stat('c', $comment_sub->post_id, -1);
 121          }
 122      }
 123  
 124  	public static function get_comment_subs($id) 
 125      {
 126          global $wpdb;
 127          return $wpdb->get_results( $wpdb->prepare("SELECT a.meta_id, a.post_id, b.post_title FROM $wpdb->postmeta a, $wpdb->posts b WHERE a.meta_key = %s AND a.meta_value = %s AND a.post_id = b.ID;", self::meta_key, $id ) );
 128      }
 129  
 130  ////    Plugin  ////
 131  
 132  	public static function comment_form($post_id) 
 133      {
 134          $txtsubcomment = __("Notify me of follow-up comments via email.", MP_TXTDOM);
 135  
 136          $email = MP_WP_User::get_email();
 137  
 138          if (is_email($email))
 139          {
 140              $i = MP_User::get_id_by_email($email);
 141              if ($i)
 142              {
 143                  $is_subscriber = self::is_subscriber($post_id, $i);
 144                  if ($is_subscriber)
 145                  {
 146                             $url = MP_User::get_unsubscribe_url(MP_User::get_key_by_email($email));
 147  ?>
 148  <!-- start of code generated by MailPress -->
 149  <div class='MailPressCommentform' style='clear:both;'>
 150      <span><?php printf(__('You are subscribed to this entry. %1$s.', MP_TXTDOM), sprintf('<a href="%1$s">%2$s</a>', $url, __('Manage your subscriptions', MP_TXTDOM))); ?></span>
 151  </div>
 152  <!-- end of code generated by MailPress -->
 153                      
 154  <?php
 155                      return;
 156                  }
 157              }
 158          }
 159  ?>
 160  <!-- start of code generated by MailPress -->
 161  <div class='MailPressCommentform' style='clear:both;'>
 162          <input type='hidden' name='MailPress[subscribe_to_comments_on]' value='on' />
 163      <label for='MailPress_subscribe_to_comments'>
 164          <input class='MailPressCommentformCheckbox' name='MailPress[subscribe_to_comments]' id='MailPress_subscribe_to_comments' type='checkbox' style='margin:0;padding:0;width:auto;'<?php checked( get_option(self::option) ); ?> />
 165          <span><?php echo $txtsubcomment; ?></span>
 166      </label>
 167  </div>
 168  <!-- end of code generated by MailPress -->
 169  <?php
 170      }
 171  
 172  	public static function comment_post($id) 
 173      {
 174          global $wpdb, $comment;
 175  
 176          $comment     = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = $id LIMIT 1");
 177          if ('spam' == $comment->comment_approved) return;
 178  
 179          $post_id     = $comment->comment_post_ID;
 180  
 181          $email     = MP_WP_User::get_email();
 182          $name     = $comment->comment_author;
 183  
 184          if (is_email($email))
 185          {
 186              $mp_user_id = MP_User::get_id_by_email($email);
 187              if (!$mp_user_id) $mp_user_id = apply_filters('MailPress_user_already_inserted', false);
 188  
 189              if ($mp_user_id)
 190              {
 191                  if (isset($_POST['MailPress']['subscribe_to_comments']) && !self::is_subscriber($post_id, $mp_user_id)) 
 192                  {
 193                      add_post_meta($post_id, self::meta_key, $mp_user_id);
 194                      new MP_Stat('c', $post_id, 1);
 195                  }
 196              }
 197              else
 198              {
 199                  if (isset($_POST['MailPress']['subscribe_to_comments']))
 200                  {
 201                      $mp_user_id = MP_User::insert($email, $name);
 202                      if ($mp_user_id)
 203                      {
 204                          add_post_meta($post_id, self::meta_key, $mp_user_id);
 205                          new MP_Stat('c', $post_id, 1);
 206                          new MP_Stat('u', 'comment', 1);
 207                          do_action('MailPress_new commenter', $mp_user_id, 'MailPress_new commenter');
 208                      }
 209                  }
 210              }
 211          }
 212          if ('1' == $comment->comment_approved) self::approve_comment($id);
 213      }
 214  
 215  	public static function approve_comment($id) 
 216      {
 217          global $wpdb, $comment;
 218  
 219          $comment    = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = $id LIMIT 1");
 220  
 221          if (!$comment) return false;
 222          
 223          if ('1' != $comment->comment_approved) return true;
 224  
 225          $post = &get_post($comment->comment_post_ID);
 226  
 227          $mail = new stdClass();
 228  
 229          $mail->Template    = 'comments';
 230  
 231          $mail->recipients_query = "SELECT c.id, c.email, c.name, c.status, c.confkey from $wpdb->comments a, $wpdb->postmeta b, $wpdb->mp_users c WHERE a.comment_ID = $id AND a.comment_post_ID  = b.post_id AND b.meta_value = c.id AND b.meta_key = '" . self::meta_key . "' AND a.comment_author_email <> c.email AND c.status IN ('waiting', 'active') ;";
 232  
 233          $mail->the_title    = apply_filters('the_title', $post->post_title );
 234  
 235          $mail->subject    = sprintf( __('[%1$s] New Comment (%2$s)', MP_TXTDOM), get_bloginfo('name'), $mail->the_title);
 236  
 237          $mail->content    = apply_filters('comment_text', get_comment_text() );
 238  
 239              $mail->advanced = new stdClass();
 240              $mail->advanced->comment = $comment;
 241              $mail->advanced->post    = $post;
 242  
 243          /* deprecated */
 244              $mail->p = new stdClass();
 245              $mail->p->id    = $comment->comment_post_ID;
 246              $mail->c = new stdClass();
 247              $mail->c->id       = $id;
 248          /* deprecated */
 249  
 250          return MailPress::mail($mail);
 251      }
 252  
 253  ////  Post ////
 254  
 255  	public static function delete_post($id)
 256      {
 257          global $wpdb;
 258          new MP_Stat('c', $id, (-1 * $wpdb->get_var("SELECT sum(scount) FROM $wpdb->mp_stats WHERE stype = 'c' AND slib = '$id';")));
 259      }
 260  
 261  //// Sending Mails ////
 262  
 263  	public static function mailinglists_optgroup( $label, $optgroup ) 
 264      {
 265          if (__CLASS__ == $optgroup) return __('Comments', MP_TXTDOM);
 266          return $label;
 267      }
 268  
 269  	public static function mailinglists( $draft_dest = array() ) 
 270      {
 271          $draft_dest['2'] = __('to comments', MP_TXTDOM);
 272          $draft_dest['3'] = __('to blog & comments', MP_TXTDOM);
 273          return $draft_dest;
 274      }
 275  
 276  	public static function query_mailinglist( $query, $draft_toemail ) 
 277      {
 278          if ($query) return $query;
 279  
 280          global $wpdb;
 281  
 282          switch ($draft_toemail)
 283          {
 284              case '2' :
 285                  return $wpdb->prepare( "SELECT DISTINCT id, email, name, status, confkey FROM $wpdb->mp_users a, $wpdb->postmeta b WHERE a.id = b.meta_value AND a.status in ('waiting', 'active') AND b.meta_key = %s ;",  self::meta_key );
 286              break;
 287              case '3' :
 288                  return $wpdb->prepare( "SELECT id, email, name, status, confkey FROM $wpdb->mp_users WHERE status = 'active' UNION SELECT DISTINCT id, email, name, status, confkey FROM $wpdb->mp_users a, $wpdb->postmeta b WHERE a.id = b.meta_value AND a.status = 'waiting' AND b.meta_key = %s ;", self::meta_key );
 289              break;
 290          }
 291          return $query;
 292      }
 293  
 294  //// post & mp_user ////
 295  
 296  	public static function is_subscriber($post_id, $mp_user_id) 
 297      {
 298          global $wpdb;
 299          return $wpdb->get_var($wpdb->prepare( "SELECT count(*) FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s;", $post_id, self::meta_key, $mp_user_id));
 300      }
 301  
 302  //// post ////
 303  
 304  	public static function has_subscribers($post_id) 
 305      {
 306          global $wpdb;
 307          return $wpdb->get_var($wpdb->prepare( "SELECT count(*) FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s;", $post_id, self::meta_key ));
 308      }
 309  
 310  //// mp_user ////
 311  
 312  	public static function has_subscribed($mp_user_id) 
 313      {
 314          global $wpdb;
 315          return $wpdb->get_var($wpdb->prepare( "SELECT count(*) FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s;", self::meta_key, $mp_user_id));
 316      }
 317  
 318  	public static function unsubscribe($mp_user_id) 
 319      {
 320          global $wpdb;
 321          return $wpdb->query($wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s;", self::meta_key, $mp_user_id));
 322      }
 323  
 324  	public static function activate_user($mp_user_id) 
 325      {
 326          if (self::has_subscribed($mp_user_id)) new MP_Stat('u', 'comment', -1);
 327      }
 328  
 329  	public static function deactivate_user($mp_user_id) 
 330      {
 331          if (self::has_subscribed($mp_user_id)) new MP_Stat('u', 'comment', 1);
 332      }
 333  
 334  	public static function delete_user($mp_user_id) 
 335      {
 336          if (self::has_subscribed($mp_user_id)) new MP_Stat('u', 'comment', (-1 * self::unsubscribe($mp_user_id)));
 337      }
 338  
 339  //// Autoresponders ////
 340  
 341  	public static function load_Autoresponder_events()
 342      {
 343          new MP_Autoresponder_events_comment();
 344      }
 345  
 346  // Sync wordpress user
 347  
 348  	public static function has_subscriptions($has, $mp_user_id)
 349      {
 350          $x = self::has_subscribed($mp_user_id);
 351  
 352          if (!$x) return $has;
 353          return true;
 354      }
 355  
 356  	public static function sync_subscriptions($oldid, $newid)
 357      {
 358          if (!self::has_subscriptions(false, $oldid)) return;
 359          global $wpdb;
 360          return $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_key = %s AND meta_value = %s ;", $newid, self::meta_key, $oldid) );
 361      }
 362  
 363  ////  ADMIN  ////
 364  ////  ADMIN  ////
 365  ////  ADMIN  ////
 366  ////  ADMIN  ////
 367  
 368  // for link on plugin page
 369  	public static function plugin_action_links($links, $file)
 370      {
 371          return MailPress::plugin_links($links, $file, plugin_basename(__FILE__), 'subscriptions');
 372      }
 373  
 374  // for role & capabilities
 375  	public static function capabilities($capabilities) 
 376      {
 377          $capabilities['MailPress_manage_comments'] = array(    'name'  => __('Comments', MP_TXTDOM), 
 378                                                  'group' => 'users'
 379                                          );
 380          return $capabilities;
 381      }
 382  
 383  // for settings
 384  	public static function settings_tab($tabs)
 385      {
 386          $tabs['subscriptions'] = __('Subscriptions', MP_TXTDOM);
 387          return $tabs;
 388      }
 389  
 390  // for settings subscriptions
 391  	public static function settings_subscriptions()
 392      {
 393          include  (MP_ABSPATH . 'mp-admin/includes/settings/subscriptions_comment.form.php');
 394      }
 395  
 396  // for meta box in user page
 397  	public static function update_meta_boxes_user() 
 398      {
 399          if (!isset($_POST['id'])) return;
 400          if (!self::has_subscribers($_POST['id'])) return;
 401  
 402          if (!isset($_POST['keep_comment_sub'])) $_POST['keep_comment_sub'] = array();
 403  
 404          self::update_checklist($_POST['id']);
 405      }
 406  
 407  	public static function add_meta_boxes_user($mp_user_id, $screen)
 408      {
 409          if (!self::has_subscribed($mp_user_id)) return;
 410  
 411          add_meta_box('commentdiv', __('Comments', MP_TXTDOM), array(__CLASS__, 'meta_box'), $screen, 'normal', 'core');
 412      }
 413  
 414  	public static function meta_box($mp_user)
 415      { 
 416          $check_comments = self::get_checklist($mp_user->id);
 417          if ($check_comments) echo $check_comments;
 418      }
 419  
 420  // for dashboard
 421  	public static function load_Dashboard_widgets() 
 422      {
 423          new MP_Dashboard_widgets_comment();
 424      }
 425  
 426  // for mp_users list
 427  	public static function users_columns($x)
 428      {
 429          $date = array_pop($x);
 430          $x['comments']=  __('Comments', MP_TXTDOM);
 431          $x['date']        = $date;
 432          return $x;
 433      }
 434  
 435  	public static function users_get_row($column_name, $mp_user, $url_parms)
 436      {
 437          if ('comments' != $column_name) return;
 438          $out = array();
 439  
 440          global $wpdb;
 441          $posts = $wpdb->get_results( $wpdb->prepare( "SELECT post_id as id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s ;", self::meta_key, $mp_user->id) );
 442  
 443          if (!empty($posts))
 444          { 
 445              foreach ($posts as $post)
 446              {
 447                  $post = get_post($post->id);
 448                  $out[] = $post->post_title;
 449              }
 450              echo join( ', ', $out );
 451              return;
 452          }
 453      }
 454  
 455  // for posts list
 456  	public static function manage_edit_columns($x)
 457      {
 458          $x['mp_users']=  __('Subscribers', MP_TXTDOM);
 459          return $x;
 460      }
 461  
 462  	public static function manage_posts_custom_column($column_name, $post_id)
 463      {
 464          if ('mp_users' != $column_name) return;
 465  
 466          global $wpdb;
 467          $count = self::has_subscribers($post_id);
 468          if ($count) echo $count;
 469      }
 470  
 471  // for comments list
 472  	public static function manage_edit_comments_columns($x)
 473      {
 474          $x['mp_subscribed']=  __('Subscriber', MP_TXTDOM);
 475          return $x;
 476      }
 477  
 478  	public static function manage_comments_custom_column($column_name, $comment_id)
 479      {
 480          if ('mp_subscribed' != $column_name) return;
 481  
 482          $comment = get_comment( $comment_id );
 483          if (!empty($comment->comment_post_ID))
 484          {
 485              $post_id = $comment->comment_post_ID;
 486              $mp_user_id = MP_User::get_id_by_email($comment->comment_author_email);
 487              if ($mp_user_id)
 488              {
 489                  global $wpdb;
 490                  $is_subscriber = self::is_subscriber($post_id, $mp_user_id);
 491                  if ($is_subscriber)
 492                  {
 493                      _e('yes', MP_TXTDOM);
 494                      return;
 495                  }
 496              }
 497          }
 498          _e('no', MP_TXTDOM);
 499      }
 500  }
 501  new MailPress_comment();
 502  }


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