Plugin Directory

Changeset 2313508


Ignore:
Timestamp:
05/28/2020 06:21:22 AM (6 years ago)
Author:
tnash
Message:

Bumping to version 1.1.0

Location:
wp-user-sentry
Files:
8 added
5 edited

Legend:

Unmodified
Added
Removed
  • wp-user-sentry/trunk/classes/admin-class.php

    r2298658 r2313508  
    4343    );
    4444    add_settings_field(
     45      'notify_login_email_subject',
     46      __('Email Message Subject'),
     47      [ __NAMESPACE__ .'\Admin' ,'fieldEmailSubjectRender'],
     48      'wp-user-sentry-settings',
     49      'wp_user_sentry_settings_section'
     50    );
     51    add_settings_field(
    4552      'notify_login_email',
    4653      __('Email Message Contents'),
     
    4956      'wp_user_sentry_settings_section'
    5057    );
     58
     59    /*
     60     * Test Email Hooks
     61     */
     62    add_action( 'wp_ajax_wp-user-sentry-test-email', [__NAMESPACE__ .'\Admin', 'sendTestEmailCallback' ]);
     63
     64    add_action( 'admin_footer', [__NAMESPACE__ .'\Admin', 'jsFooterRender' ]);
     65
    5166  }
    5267
     
    91106       <option value='1' <?php selected( $options['geo_api_service'], '1' ); ?>>None</option>
    92107       <option value='2' <?php selected( $options['geo_api_service'], '2' ); ?>>ip-api.com</option>
     108      <?php
     109        if( self::testMaxMindPresence() ){
     110          ?>
     111       <option value='3' <?php selected( $options['geo_api_service'], '3' ); ?>>WooCommerce GeoAPI</option>
     112          <?php
     113        }
     114      ?>
    93115   </select>
    94116   <p class="description"><?php _e( 'If enabled, each login IP will be sent to third party service.','wp-user-sentry' ); ?></p>
    95117    <?php
     118    if( class_exists( 'woocommerce' ) && !self::testMaxMindPresence() ){
     119      ?>
     120      <p class="description"><?php _e( 'To use WooCommerce GEO API configure it first.','wp-user-sentry' ); ?></p>
     121      <?php
     122    }
    96123  }
    97124  /**
     
    156183    <textarea name="wp_user_sentry_settings[notify_login_email]" class="large-text code" rows="8" spellcheck="false"><?php echo $contents  ?></textarea>
    157184    <p class="description"><?php _e( 'The following dyanmic parameters may be added:','wp-user-sentry' ); ?> <strong>{displayname}, {user_login}, {ip}, {os}, {browser}, {country}, {flag}, {time}, {profile_url}, {homeurl}</strong></p>
    158     <?php
    159   }
     185    <button type="button" name="wp-user-sentry-test-email" class="button wp-user-sentry-test-email"><?php _e('Send Test Email','wp-user-sentry'); ?></button>
     186    <?php
     187  }
     188  /**
     189   * Input Field for subject
     190   * @access public static
     191   * @since 1.1.0
     192   */
     193  static function fieldEmailSubjectRender(){
     194    $options = get_option( 'wp_user_sentry_settings' );
     195    if( !isset( $options['notify_login_email_subject'] )){
     196      $subject = __('Successful login');
     197    }else{
     198      $subject = $options['notify_login_email_subject'];
     199    }
     200    ?>
     201    <input type="text" name="wp_user_sentry_settings[notify_login_email_subject]" value="<?php echo $subject;  ?>">
     202    <?php
     203  }
     204  /**
     205   * Javascript in Footer
     206   * @access public static
     207   * @since 1.1.0
     208   */
     209  static function jsFooterRender(){
     210    $screen = get_current_screen();
     211    if( $screen->id !== 'settings_page_wp-user-sentry-settings') return;
     212    $ajax_nonce = wp_create_nonce( "wp-user-sentry-test-email" );
     213    ?>
     214    <script type="text/javascript">
     215    jQuery(document).ready(function($) {
     216        $('.wp-user-sentry-test-email').click(function(){
     217            var subject = $('input[name="wp_user_sentry_settings[notify_login_email_subject]"]').val();
     218            var contents = $.trim( $('textarea[name="wp_user_sentry_settings[notify_login_email]"]').val() );
     219            var data = {
     220                action: 'wp-user-sentry-test-email',
     221                security: '<?php echo $ajax_nonce; ?>',
     222                subject: subject,
     223                message: contents
     224            };
     225            $.post(ajaxurl, data, function(response) {
     226              const res = JSON.parse(response);
     227              if(res.type == "success"){
     228                jQuery('#wpbody-content').prepend('<div class="notice notice-success is-dismissible"><p><?php _e( 'Test Email Sent to: ' ); ?>'+ res.email +'</p></div>');
     229              }
     230              else{
     231                jQuery('#wpbody-content').prepend('<div class="notice notice-error is-dismissible"><p><?php _e( 'Test Email Failed to send' ); ?></p></div>');
     232              }
     233            });
     234        });
     235    });
     236    </script>
     237    <?php
     238  }
     239
     240  /**
     241   * Test Email AJAX Callback
     242   * @access public static
     243   * @since 1.1.0
     244   */
     245  static function sendTestEmailCallback(){
     246    check_ajax_referer( 'wp-user-sentry-test-email', 'security');
     247    if( ! current_user_can( 'manage_options' ) ){
     248      exit();
     249    }
     250    $user = wp_get_current_user();
     251    $email = [];
     252    if( isset( $_POST['subject'] )){
     253      $email['subject'] = $_POST['subject'];
     254    }
     255    if( isset( $_POST['message'] )){
     256      $email['message'] = $_POST['message'];
     257    }
     258    if( \wp_user_sentry\Notify::sendEmail( $user, $email ) ){
     259
     260      echo json_encode([
     261        'type' => 'success',
     262        'email'  => $user->user_email
     263      ]);
     264    }else{
     265      echo json_code([
     266        'type' => 'fail'
     267      ]);
     268    }
     269    exit();
     270  }
     271
     272  static public function testMaxMindPresence(){
     273    $return = false;
     274    $wooCommerce_GeoAPI = get_option( 'woocommerce_maxmind_geolocation_settings' );
     275    if( class_exists( 'woocommerce' ) && !empty( $wooCommerce_GeoAPI ) && isset($wooCommerce_GeoAPI['license_key'])) $return = true;
     276    return $return;
     277  }
     278
    160279
    161280}
  • wp-user-sentry/trunk/classes/notify-class.php

    r2298658 r2313508  
    2121    }
    2222    $settings = get_option( 'wp_user_sentry_settings' );
     23    $send = true;
    2324    if( isset( $settings['notify_login_roles'] ) && is_array( $settings['notify_login_roles'] ) ) {
    2425      if( !array_intersect( $user->roles, $settings['notify_login_roles'] ) ){
    25         return true;
     26        $send = false;
    2627      }
    2728    }
     
    2930    if( isset( $settings['notify_login_repeat'] ) && '2' === $settings['notify_login_repeat'] ){
    3031      if( true === \wp_user_sentry\Notify::compareSessions( $user->ID ) ){
    31         return true;
     32        $send = false;
    3233      }
    3334    }
    34 
     35    $send = apply_filters( 'wp_user_sentry_notify', $send, $user->ID );
     36    if( true !== $send ) return true;
    3537    return \wp_user_sentry\Notify::sendEmail( $user );
    3638  }
     
    4244   * @return bool
    4345   */
    44   static function sendEmail( $user ){
     46  static function sendEmail( $user, $email=false ){
    4547    $settings = get_option( 'wp_user_sentry_settings' );
    46     if( isset( $settings['notify_login_email'] ) ){
     48    if( !empty( $email ) && isset( $email['message'] )){
     49      $message = $email['message'];
     50    }elseif( isset( $settings['notify_login_email'] ) ){
    4751      $message = $settings['notify_login_email'];
    4852    }else{
     
    5761    }
    5862    $message = apply_filters( 'wp_user_sentry_email_message', $message );
     63    if( !empty( $email ) && isset( $email['subject'] )){
     64      $subject = $email['subject'];
     65    }elseif( isset( $settings['notify_login_email_subject'] ) ){
     66      $subject = $settings['notify_login_email_subject'];
     67    }else{
     68      $subject = __('Successful login');
     69    }
    5970    $user_info = new \wp_user_sentry\User;
    6071    $device = $user_info->getDevice();
    6172    $profile_url = admin_url( 'profile.php#wp-user-sentry-session' );
    6273
    63     if( isset($settings['geo_api_service']) && '2' === $settings['geo_api_service']){
     74    $country = '';
     75    $flag = '';
     76    if( isset( $settings['geo_api_service'] ) ){
    6477      $geo = $user_info->getCountry( $device['ip'] );
    65       $country = $geo['country'];
    66       $flag = $user_info->emojiFlag($geo['code']);
    67     }else{
    68       $country = '';
    69       $flag = '';
     78      if( !empty( $geo ) ){
     79        $country = $geo['country'];
     80        $flag = $user_info->emojiFlag($geo['code']);
     81      }
    7082    }
    7183    $message = str_replace([
     
    97109    $email = [
    98110      'to' => $user->user_email,
    99       'subject' => __( '['. $blogname .'] Successful login' ),
     111      'subject' => __( '['. $blogname .'] '.$subject ),
    100112      'message' => $message,
    101113      'headers' => ''
  • wp-user-sentry/trunk/classes/user-class.php

    r2298658 r2313508  
    7676 }
    7777
     78/**
     79 * GetCountry Code from 3rd Party Services
     80 * @param  string $IP
     81 * @return string Country Code
     82 */
     83public function getCountry( $ip ){
     84  $settings = get_option( 'wp_user_sentry_settings' );
     85  if( isset($settings['geo_api_service']) && '2' === $settings['geo_api_service']){
     86    return $this->getCountryIPAPI( $ip );
     87  }
     88  if( isset($settings['geo_api_service']) && '3' === $settings['geo_api_service']){
     89    return $this->getCountryWooCommerce( $ip );
     90  }
     91  return false;
     92}
    7893 /**
    7994  * Get Country from ip-api.com
    80   * @access public
     95  * @access protected
    8196  * @param  string $ip
    82   * @return string   Country Name
     97  * @return string   Country Code
    8398  */
    84  public function getCountry( $ip ){
     99 protected function getCountryIPAPI( $ip ){
    85100   $url = 'http://ip-api.com/json/'.$ip.'?fields=country,countryCode,query';
    86101   $json = wp_remote_get( $url );
     
    102117    return false;
    103118 }
     119
     120 /**
     121  * Get Country from WooCommerce MaxMind DB
     122  * @access protected
     123  * @param  string $ip
     124  * @return string   Country Code
     125  * @todo Generate Name from Country Code because Woo Doesn't return it
     126  */
     127protected function getCountryWooCommerce( $ip ){
     128  if( class_exists( 'WC_Geolocation' ) ){
     129    $country = \WC_Geolocation::geolocate_ip( $ip );
     130    if(!empty( $country ) && isset( $country['country'] )){
     131     $code =  $country['country'];
     132     return [
     133       'country' => filter_var( $code, FILTER_SANITIZE_SPECIAL_CHARS ),
     134       'code'    =>filter_var( $code, FILTER_SANITIZE_SPECIAL_CHARS )
     135     ];
     136    }
     137  }
     138  return false;
     139}
     140
    104141
    105142 public function emojiFlag($code) {
  • wp-user-sentry/trunk/readme.txt

    r2298658 r2313508  
    3838Here is a list of action and filter hooks provided by the plugin:
    3939
     40- `wp_user_sentry_notify` Filter to change if email should be sent return is bool, additional info user ID.
    4041- `wp_user_sentry_email_message` Filter to change the email message generated by the plugin.
    4142- `wp_user_sentry_email` Filter to change the email array generated by the plugin prior to sending mail.
     
    4546
    4647== Changelog ==
     481.1.0 - 28th May 2020
     49 - [New Feature] WooCommerce GEOAPI integration for determinging country
     50 - [New Feature] Modify Subject field
     51 - [New Feature] Send Test Email
     52 - [New Feature] Added notify_login_roles to allow plugins/themes to filter if email should be sent
    47531.0.0 - 5th May 2020
    4854 - Released on WordPress.org
  • wp-user-sentry/trunk/wp-user-sentry.php

    r2298658 r2313508  
    99 * Text Domain:     wp-user-sentry
    1010 * Domain Path:     /languages
    11  * Version:         1.0.0
     11 * Version:         1.1.0
    1212 *
    1313 */
     
    3131      require_once __DIR__ . '/classes/' . $class . '-class.php';
    3232    });
    33     add_action( 'show_user_profile', [ __NAMESPACE__ .'\\Profile', 'userProfile'], 0 );
    34     add_action( 'edit_user_profile', [ __NAMESPACE__ .'\\Profile', 'userProfile'], 0 );
    35     add_action( 'wp_login', [ __NAMESPACE__ .'\\Notify' , 'runNotify'] );
    36     add_action( 'admin_init', [ __NAMESPACE__ .'\\Admin', 'adminInit'] );
     33    add_action( 'show_user_profile', [ __NAMESPACE__ .'\\Profile', 'userProfile' ], 0 );
     34    add_action( 'edit_user_profile', [ __NAMESPACE__ .'\\Profile', 'userProfile' ], 0 );
     35    add_action( 'wp_login', [ __NAMESPACE__ .'\\Notify' , 'runNotify' ] );
     36    add_action( 'admin_init', [ __NAMESPACE__ .'\\Admin', 'adminInit' ] );
    3737    add_action( 'admin_menu', function(){
    3838      add_options_page(
Note: See TracChangeset for help on using the changeset viewer.