Changeset 2313508
- Timestamp:
- 05/28/2020 06:21:22 AM (6 years ago)
- Location:
- wp-user-sentry
- Files:
-
- 8 added
- 5 edited
-
tags/1.1.0 (added)
-
tags/1.1.0/classes (added)
-
tags/1.1.0/classes/admin-class.php (added)
-
tags/1.1.0/classes/notify-class.php (added)
-
tags/1.1.0/classes/profile-class.php (added)
-
tags/1.1.0/classes/user-class.php (added)
-
tags/1.1.0/readme.txt (added)
-
tags/1.1.0/wp-user-sentry.php (added)
-
trunk/classes/admin-class.php (modified) (4 diffs)
-
trunk/classes/notify-class.php (modified) (5 diffs)
-
trunk/classes/user-class.php (modified) (2 diffs)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/wp-user-sentry.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-user-sentry/trunk/classes/admin-class.php
r2298658 r2313508 43 43 ); 44 44 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( 45 52 'notify_login_email', 46 53 __('Email Message Contents'), … … 49 56 'wp_user_sentry_settings_section' 50 57 ); 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 51 66 } 52 67 … … 91 106 <option value='1' <?php selected( $options['geo_api_service'], '1' ); ?>>None</option> 92 107 <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 ?> 93 115 </select> 94 116 <p class="description"><?php _e( 'If enabled, each login IP will be sent to third party service.','wp-user-sentry' ); ?></p> 95 117 <?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 } 96 123 } 97 124 /** … … 156 183 <textarea name="wp_user_sentry_settings[notify_login_email]" class="large-text code" rows="8" spellcheck="false"><?php echo $contents ?></textarea> 157 184 <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 160 279 161 280 } -
wp-user-sentry/trunk/classes/notify-class.php
r2298658 r2313508 21 21 } 22 22 $settings = get_option( 'wp_user_sentry_settings' ); 23 $send = true; 23 24 if( isset( $settings['notify_login_roles'] ) && is_array( $settings['notify_login_roles'] ) ) { 24 25 if( !array_intersect( $user->roles, $settings['notify_login_roles'] ) ){ 25 return true;26 $send = false; 26 27 } 27 28 } … … 29 30 if( isset( $settings['notify_login_repeat'] ) && '2' === $settings['notify_login_repeat'] ){ 30 31 if( true === \wp_user_sentry\Notify::compareSessions( $user->ID ) ){ 31 return true;32 $send = false; 32 33 } 33 34 } 34 35 $send = apply_filters( 'wp_user_sentry_notify', $send, $user->ID ); 36 if( true !== $send ) return true; 35 37 return \wp_user_sentry\Notify::sendEmail( $user ); 36 38 } … … 42 44 * @return bool 43 45 */ 44 static function sendEmail( $user ){46 static function sendEmail( $user, $email=false ){ 45 47 $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'] ) ){ 47 51 $message = $settings['notify_login_email']; 48 52 }else{ … … 57 61 } 58 62 $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 } 59 70 $user_info = new \wp_user_sentry\User; 60 71 $device = $user_info->getDevice(); 61 72 $profile_url = admin_url( 'profile.php#wp-user-sentry-session' ); 62 73 63 if( isset($settings['geo_api_service']) && '2' === $settings['geo_api_service']){ 74 $country = ''; 75 $flag = ''; 76 if( isset( $settings['geo_api_service'] ) ){ 64 77 $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 } 70 82 } 71 83 $message = str_replace([ … … 97 109 $email = [ 98 110 'to' => $user->user_email, 99 'subject' => __( '['. $blogname .'] Successful login'),111 'subject' => __( '['. $blogname .'] '.$subject ), 100 112 'message' => $message, 101 113 'headers' => '' -
wp-user-sentry/trunk/classes/user-class.php
r2298658 r2313508 76 76 } 77 77 78 /** 79 * GetCountry Code from 3rd Party Services 80 * @param string $IP 81 * @return string Country Code 82 */ 83 public 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 } 78 93 /** 79 94 * Get Country from ip-api.com 80 * @access p ublic95 * @access protected 81 96 * @param string $ip 82 * @return string Country Name97 * @return string Country Code 83 98 */ 84 p ublic function getCountry( $ip ){99 protected function getCountryIPAPI( $ip ){ 85 100 $url = 'http://ip-api.com/json/'.$ip.'?fields=country,countryCode,query'; 86 101 $json = wp_remote_get( $url ); … … 102 117 return false; 103 118 } 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 */ 127 protected 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 104 141 105 142 public function emojiFlag($code) { -
wp-user-sentry/trunk/readme.txt
r2298658 r2313508 38 38 Here is a list of action and filter hooks provided by the plugin: 39 39 40 - `wp_user_sentry_notify` Filter to change if email should be sent return is bool, additional info user ID. 40 41 - `wp_user_sentry_email_message` Filter to change the email message generated by the plugin. 41 42 - `wp_user_sentry_email` Filter to change the email array generated by the plugin prior to sending mail. … … 45 46 46 47 == Changelog == 48 1.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 47 53 1.0.0 - 5th May 2020 48 54 - Released on WordPress.org -
wp-user-sentry/trunk/wp-user-sentry.php
r2298658 r2313508 9 9 * Text Domain: wp-user-sentry 10 10 * Domain Path: /languages 11 * Version: 1. 0.011 * Version: 1.1.0 12 12 * 13 13 */ … … 31 31 require_once __DIR__ . '/classes/' . $class . '-class.php'; 32 32 }); 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' ] ); 37 37 add_action( 'admin_menu', function(){ 38 38 add_options_page(
Note: See TracChangeset
for help on using the changeset viewer.