[ Index ]

MailPress 544

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

title

Body

[close]

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

   1  <?php
   2  if (class_exists('MailPress') && !class_exists('MailPress_comment_newsletter_subscription') )
   3  {
   4  /*
   5  Plugin Name: MailPress_comment_newsletter_subscription
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/comment_newsletter_subscription/
   7  Description: Subscribe to a default newsletter from comment form   (<span style='color:red;'>required !</span> <span style='color:#D54E21;'>Newsletter</span> add-on)
   8  Version: 5.4.4
   9  */
  10  
  11  class MailPress_comment_newsletter_subscription
  12  {
  13      const option_name = 'MailPress_comment_newsletter_subscription';
  14  
  15      public static $mp_user_id = false;
  16  
  17  	function __construct()
  18      {
  19  // for wordpress hooks
  20  // for comment
  21          add_action('comment_form',                 array(__CLASS__, 'comment_form'));
  22          add_action('comment_post',                 array(__CLASS__, 'comment_post'), 1, 1);
  23  
  24  // for wp admin
  25          if (is_admin())
  26          {
  27          // for link on plugin page
  28              add_filter('plugin_action_links',             array(__CLASS__, 'plugin_action_links'), 10, 2 );
  29          // for settings general
  30              add_action('MailPress_settings_general',         array(__CLASS__, 'settings_general'), 40);
  31              add_action('MailPress_settings_general_update',    array(__CLASS__, 'settings_general_update'));
  32          }
  33      }
  34  
  35  ////    Plugin  ////
  36  
  37  	public static function comment_form($post_id) 
  38      {
  39          $txtsubcomment = __("Notify me of new posts via email.", MP_TXTDOM);
  40  
  41          $settings     = get_option(self::option_name);
  42          $nls         = MP_Newsletter::get_active();
  43          if (!isset($nls[$settings['default']])) return;
  44  
  45          $email     = MP_WP_User::get_email();
  46  
  47          if (is_email($email))
  48          {
  49              $mp_user_id = MP_User::get_id_by_email($email);
  50              if ($mp_user_id)
  51              {
  52                  $subscriptions = MP_Newsletter::get_object_terms($mp_user_id);
  53                  if (isset($subscriptions[$settings['default']])) return;
  54              }
  55          }
  56  ?>
  57  <!-- start of code generated by MailPress -->
  58  <div class='MailPressCommentNewsletterform' style='clear:both;'>
  59      <label for='MailPress_subscribe_to_comment_newsletter'>
  60          <input class='MailPressCommentNewsletterformCheckbox' name='MailPress[subscribe_to_comment_newsletter]' id='MailPress_subscribe_to_comment_newsletter' type='checkbox' style='margin:0;padding:0;width:auto;'<?php checked( isset($settings['checked']) ); ?> />
  61          <span><?php echo $txtsubcomment; ?></span>
  62      </label>
  63  </div>
  64  <!-- end of code generated by MailPress -->
  65  <?php
  66      }
  67  
  68  	public static function comment_post($id) 
  69      {
  70          global $wpdb, $comment;
  71  
  72          $comment     = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = $id LIMIT 1");
  73          if ('spam' == $comment->comment_approved) return;
  74  
  75          $name     = $comment->comment_author;
  76  
  77          $settings     = get_option(self::option_name);
  78          $nls         = MP_Newsletter::get_active();
  79          if (!isset($nls[$settings['default']])) return;
  80  
  81          $email     = MP_WP_User::get_email();
  82  
  83          if (is_email($email))
  84          {
  85              $mp_user_id = MP_User::get_id_by_email($email);
  86              if ($mp_user_id)
  87              {
  88                  MP_User::set_status($mp_user_id, 'active');
  89                  $subscriptions = MP_Newsletter::get_object_terms($mp_user_id);
  90                  if (isset($subscriptions[$settings['default']])) return;
  91              }
  92              else
  93              {
  94                  if (isset($_POST['MailPress']['subscribe_to_comment_newsletter']))
  95                  {
  96                      $mp_user_id = MP_User::insert($email, $name, array('status' => 'active'));
  97                      if (!$mp_user_id) return;
  98                      self::$mp_user_id = $mp_user_id;
  99                      add_filter('MailPress_user_already_inserted', array(__CLASS__, 'user_already_inserted'), 1 );
 100                  }
 101              }
 102              $subscriptions[$settings['default']] = $settings['default'];
 103              MP_Newsletter::set_object_terms($mp_user_id, $subscriptions);
 104          }
 105      }
 106  
 107  	public static function user_already_inserted()
 108      {
 109          return self::$mp_user_id;
 110      }
 111  
 112  ////  ADMIN  ////
 113  ////  ADMIN  ////
 114  ////  ADMIN  ////
 115  ////  ADMIN  ////
 116  
 117  // for link on plugin page
 118  	public static function plugin_action_links($links, $file)
 119      {
 120          return MailPress::plugin_links($links, $file, plugin_basename(__FILE__), 'general');
 121      }
 122  
 123  // for settings general
 124  	public static function settings_general()
 125      {
 126          $settings = get_option(self::option_name);
 127          $args = array(    'htmlname'             => 'comment_newsletter_subscription[default]', 
 128                      'admin'             => true, 
 129                      'type'             => 'select',
 130                      'selected'         => (isset($settings['default'])) ? $settings['default'] : '',
 131          );
 132  ?>
 133              <tr>
 134                  <th class='thtitle'><?php _e('Comment Newsletter subscription', MP_TXTDOM); ?></th>
 135          <td></td>
 136              </tr>
 137              <tr class='mp_sep'>
 138                  <th><?php _e('Default Newsletter', MP_TXTDOM); ?></th>
 139                  <td style='padding:0;'>
 140                      <table>
 141                          <tr>
 142                              <td>
 143                                  <?php echo MailPress_newsletter::get_checklist(false, $args); ?>
 144                              </td>
 145                              <td>
 146                                  &#160;<?php _e('checked by default', MP_TXTDOM); ?>&#160;
 147                                  <input type='checkbox' name='comment_newsletter_subscription[checked]'<?php checked( (isset($settings['checked'])) ); ?> />
 148                              </td>
 149                          </tr>
 150                      </table>
 151                  </td>
 152              </tr>
 153  <?php
 154      }
 155  
 156  	public static function settings_general_update()
 157      {
 158          update_option (self::option_name, $_POST['comment_newsletter_subscription']);
 159      }
 160  }
 161  new MailPress_comment_newsletter_subscription();
 162  }


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