[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  if ( class_exists( 'MailPress' ) && !class_exists( 'MailPress_comment_newsletter_subscription' ) )
   3  {
   4  /*
   5  Plugin Name: MailPress_newsletter_default_subscription
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/newsletter_default_subscription/
   7  Description: Newsletters : Subscribe to default newsletter from comment form  ( <span style="color:red;">required !</span> <span style="color:#D54E21;">Newsletter</span> add-on )
   8  Version: 7.1
   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_filter( 'comment_form_default_fields',     array( __CLASS__, 'comment_form_default_fields' ), 1, 1 );
  23          add_action( 'comment_post',                 array( __CLASS__, 'comment_post' ), 1, 1 );
  24  
  25  // for wp admin
  26          if ( is_admin() )
  27          {
  28          // for link on plugin page
  29              add_filter( 'plugin_action_links',             array( __CLASS__, 'plugin_action_links' ), 10, 2 );
  30          // for settings subscriptions
  31              add_filter( 'MailPress_settings_subscriptions_newsletter_help',    array( __CLASS__, 'settings_subscriptions_help' ), 21, 1 );
  32              add_action( 'MailPress_settings_subscriptions_newsletter_form',    array( __CLASS__, 'settings_subscriptions_form'), 21 );
  33          }
  34      }
  35  
  36  ////    Plugin  ////
  37  
  38  	public static function comment_form( $post_id ) 
  39      {
  40          $out = '';
  41  
  42          $txtsubcomment = __( "Notify me of new posts via email.", 'MailPress' );
  43  
  44          $settings     = get_option( self::option_name );
  45          $nls         = MP_Newsletter::get_active();
  46          if ( !isset( $nls[$settings['default']] ) ) return;
  47  
  48          $email     = MP_WP_User::get_email();
  49  
  50          if ( is_email( $email ) )
  51          {
  52              $mp_user_id = MP_User::get_id_by_email( $email );
  53              if ( $mp_user_id )
  54              {
  55                  $subscriptions = MP_Newsletter::get_object_terms( $mp_user_id );
  56                  if ( isset( $subscriptions[$settings['default']] ) ) return;
  57              }
  58          }
  59  
  60          $out .= '<!-- start of code generated by MailPress -->' . "\r\n";
  61          $out .= '<div class="MailPressCommentNewsletterform" style="clear:both;">' . "\r\n";
  62          $out .= '<label for="MailPress_subscribe_to_comment_newsletter">' . "\r\n";
  63          $out .= '<input type="checkbox" name="MailPress[subscribe_to_comment_newsletter]" id="MailPress_subscribe_to_comment_newsletter" class="MailPressCommentNewsletterformCheckbox" style="margin:0;padding:0;width:auto;" ' . checked( isset( $settings['checked'] ), true, false ) . ' />' . "\r\n";
  64          $out .= '<span>' . $txtsubcomment . '</span>' . "\r\n";
  65          $out .= '</label>' . "\r\n";
  66          $out .= '</div>';
  67          $out .= '<!-- end of code generated by MailPress -->' . "\r\n";
  68  
  69          echo $out;
  70      }
  71  
  72  	public static function comment_form_default_fields( $fields ) 
  73      {
  74          $settings     = get_option( self::option_name );
  75          $nls         = MP_Newsletter::get_active();
  76          if ( !isset( $nls[$settings['default']] ) ) return;
  77  
  78          $post_id = get_the_ID();
  79  
  80          $email = MP_WP_User::get_email();
  81  
  82          if ( MailPress::is_email( $email ) )
  83          {
  84              $mp_user_id = MP_User::get_id_by_email( $email );
  85              if ( $mp_user_id )
  86              {
  87                  $subscriptions = MP_Newsletter::get_object_terms( $mp_user_id );
  88                  if ( isset( $subscriptions[$settings['default']] ) ) return;
  89              }
  90          }
  91  
  92          $fields['mailpress-comment-newsletter']  = '<!-- start of code generated by MailPress -->' . "\n";
  93          $fields['mailpress-comment-newsletter'] .= '<p class="comment-form-cookies-consent comment-newsletter-form-mailpress MailPressCommentNewsletterformCheckbox"><input name="MailPress[subscribe_to_comment_newsletter]" id="MailPress_subscribe_to_comment_newsletter" type="checkbox"' . checked( isset( $settings['checked'] ), true, false ) . ' />' . 
  94                                              '<label for="MailPress_subscribe_to_comment_newsletter">' .  __( 'Notify me of new posts via email.', 'MailPress' ) . '</label>' . 
  95                                              '</p>' . "\n";
  96          $fields['mailpress-comment-newsletter'] .= '<!-- end of code generated by MailPress -->' . "\n";
  97          return $fields;
  98      }
  99  
 100  	public static function comment_post( $id ) 
 101      {
 102          global $wpdb, $comment;
 103  
 104          $comment     = $wpdb->get_row( "SELECT * FROM $wpdb->comments WHERE comment_ID = $id LIMIT 1" );
 105          if ( 'spam' == $comment->comment_approved ) return;
 106  
 107          $name     = $comment->comment_author;
 108  
 109          $settings     = get_option( self::option_name );
 110          $nls         = MP_Newsletter::get_active();
 111          if ( !isset( $nls[$settings['default']] ) ) return;
 112  
 113          $email     = MP_WP_User::get_email();
 114  
 115          $post_ = filter_input_array( INPUT_POST );
 116  
 117          if ( MailPress::is_email( $email ) )
 118          {
 119              $mp_user_id = MP_User::get_id_by_email( $email );
 120              if ( $mp_user_id )
 121              {
 122                  MP_User::set_status( $mp_user_id, 'active' );
 123                  $subscriptions = MP_Newsletter::get_object_terms( $mp_user_id );
 124                  if ( isset( $subscriptions[$settings['default']] ) ) return;
 125              }
 126              else
 127              {
 128                  if ( isset( $post_['MailPress']['subscribe_to_comment_newsletter'] ) )
 129                  {
 130                      $mp_user_id = MP_User::insert( $email, $name, array( 'status' => 'active' ) );
 131                      if ( !$mp_user_id ) return;
 132                      self::$mp_user_id = $mp_user_id;
 133                      add_filter( 'MailPress_user_already_inserted', array( __CLASS__, 'user_already_inserted' ), 1 );
 134                  }
 135              }
 136              $subscriptions[$settings['default']] = $settings['default'];
 137              MP_Newsletter::set_object_terms( $mp_user_id, $subscriptions );
 138          }
 139      }
 140  
 141  	public static function user_already_inserted()
 142      {
 143          return self::$mp_user_id;
 144      }
 145  
 146  ////  ADMIN  ////
 147  ////  ADMIN  ////
 148  ////  ADMIN  ////
 149  ////  ADMIN  ////
 150  
 151  // for link on plugin page
 152  	public static function plugin_action_links( $links, $file )
 153      {
 154          return MailPress::plugin_links( $links, $file, plugin_basename( __FILE__ ), 'subscriptions' );
 155      }
 156  
 157  // for settings subscriptions
 158  	public static function settings_subscriptions_help( $content )
 159      {
 160          include  ( MP_ABSPATH . 'mp-admin/includes/settings/subscriptions/newsletter/default_subscription/help.php' );
 161          return $content;
 162      }
 163  
 164  	public static function settings_subscriptions_form()
 165      {
 166          include  ( MP_ABSPATH . 'mp-admin/includes/settings/subscriptions/newsletter/default_subscription/form.php' );
 167      }
 168  }
 169  new MailPress_comment_newsletter_subscription();
 170  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1