Plugin Directory

Changeset 972008


Ignore:
Timestamp:
08/25/2014 02:55:27 AM (12 years ago)
Author:
jakemgold
Message:

Refactored for performance / maintainability, Updated layout of controls for WordPress 3.8+.

Location:
simple-user-password-generator/trunk
Files:
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • simple-user-password-generator/trunk/readme.txt

    r723729 r972008  
    33Donate link: http://10up.com/plugins/simple-user-password-generator-wordpress/
    44Tags: users, password, security
    5 Requires at least: 3.2
    6 Tested up to: 3.6
    7 Stable tag: 2.0.2
     5Requires at least: 3.8
     6Tested up to: 4.0
     7Stable tag: 3.0
    88License: GPLv2
    99
     
    3333== Changelog ==
    3434
     35= 3.0 =
     36* Refactored for performance / maintainability
     37* Updated layout of controls for WordPress 3.8+
     38
    3539= 2.0.2 =
    3640* Refactored / improved JavaScript (faster)
  • simple-user-password-generator/trunk/simple-user-password-generator.js

    r723729 r972008  
    1 var supg_pass1 = document.getElementById('pass1');
    2 var supg_send_password = document.getElementById('send_password');
    3 jQuery(supg_pass1).closest('td').append('<p style="clear:both;margin:0;"><input type="button" name="simple-user-generate-password" id="simple-user-generate-password" value="' + simple_user_password_generator_l10n.Generate + '" onclick="simple_user_generate_password();" class="button" style="width: auto;" /></p>');
    4 jQuery(supg_send_password).closest('td').append('<br /><label for="reset_password_notice"><input type="checkbox" id="reset_password_notice" name="reset_password_notice" /> ' + simple_user_password_generator_l10n.PassChange + '</label>')
     1( function( window, settings ) {
    52
    6 jQuery(supg_pass1).on('keyup',function(){
    7     var passval = this.value;
    8     if ( '' == passval ) jQuery(supg_send_password).attr('disabled','disabled');
    9     else jQuery(supg_send_password).removeAttr('disabled');
    10 });
     3    "use strict";
     4    var document = window.document;
    115
    12 function simple_user_generate_password() {
    13     jQuery.post( ajaxurl, { action: 'simple_user_generate_password' }, function(response){
    14         document.getElementById('pass2').value = response;
    15         supg_pass1.value = response;
    16         jQuery(supg_pass1).trigger('keyup');
    17         jQuery(supg_send_password).attr('checked',true);
    18         jQuery(document.getElementById('reset_password_notice')).attr('checked',true);
    19     });
    20 }
     6    var Cache = {
     7        pass1 : null,
     8        pass2 : null,
     9        send : null
     10    };
     11
     12    function initialize() {
     13        Cache.send = document.getElementById('send_password');
     14        Cache.pass1 = document.getElementById('pass1');
     15        Cache.pass2 = document.getElementById('pass2');
     16
     17        document.getElementById('pass-strength-result').insertAdjacentHTML('beforeBegin','<input type="button" name="simple-user-generate-password" id="simple-user-generate-password" value="' + simple_user_password_generator_l10n.Generate + '" class="button" style="width: auto; margin-top: 13px;" /><br />');
     18        Cache.send.parentNode.insertAdjacentHTML('afterend','<br /><label for="reset_password_notice"><input type="checkbox" id="reset_password_notice" name="reset_password_notice" /> ' + simple_user_password_generator_l10n.PassChange + '</label>');
     19
     20        jQuery( document.getElementById('simple-user-generate-password') ).on('click',function(){
     21            jQuery.post( ajaxurl, { action: 'simple_user_generate_password' }, function(response){
     22                Cache.pass2.value = response;
     23                Cache.pass1.value = response;
     24                jQuery(Cache.pass1).trigger('keyup');
     25                Cache.send.setAttribute('checked','true');
     26                document.getElementById('reset_password_notice').setAttribute('checked','true');
     27            });
     28        });
     29
     30        jQuery(Cache.pass1).add(Cache.pass2).on('keyup',function(){
     31            if ( '' == this.value || Cache.pass1.value != Cache.pass2.value ) {
     32                Cache.send.setAttribute('disabled','disabled');
     33            } else {
     34                Cache.send.removeAttribute('disabled');
     35            }
     36        });
     37    }
     38
     39    initialize();
     40
     41} )(window);
  • simple-user-password-generator/trunk/simple-user-password-generator.php

    r723729 r972008  
    44 Plugin URI: http://10up.com/plugins/simple-user-password-generator-wordpress/
    55 Description: Allows administrators to generate a secure password when adding new users.
    6  Version: 2.0.2
     6 Version: 3.0
    77 Author: Jake Goldman, 10up
    88 Author URI: http://10up.com
     
    1010*/
    1111
    12 /**
    13  * add field to user profiles
    14  */
     12if ( ! class_exists( 'Simple_User_Password_Generator' ) ) :
    1513 
    16 class simple_user_password_generator {
    17    
    18     public function __construct() {
    19         add_action( 'admin_init', array( $this, 'admin_init' ) );
    20         add_action( 'wp_ajax_simple_user_generate_password', array( $this, 'ajax_generate_password' ) );
    21         add_action( 'admin_print_scripts-user-new.php', array( $this, 'enqueue_script' ) );
    22         add_action( 'admin_print_scripts-user-edit.php', array( $this, 'enqueue_script' ) );
    23         add_action( 'user_register', array( $this, 'user_register' ) );
    24         add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ), 1 );
    25         add_action( 'user_profile_update_errors', array( $this, 'user_profile_update_errors' ), 10, 3 );
     14class Simple_User_Password_Generator {
     15
     16    /**
     17     * Handles initializing this class and returning the singleton instance after it's been cached.
     18     *
     19     * @return null|Simple_User_Password_Generator
     20     */
     21    public static function get_instance() {
     22        // Store the instance locally to avoid private static replication
     23        static $instance = null;
     24
     25        if ( null === $instance ) {
     26            $instance = new self();
     27            self::_add_actions();
     28        }
     29
     30        return $instance;
    2631    }
    27    
    28     public function admin_init() {
     32
     33    /**
     34     * An empty constructor
     35     */
     36    public function __construct() { /* Purposely do nothing here */ }
     37
     38    /**
     39     * Handles registering hooks that initialize this plugin.
     40     */
     41    public static function _add_actions() {
     42        add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
     43        add_action( 'wp_ajax_simple_user_generate_password', array( __CLASS__, 'ajax_generate_password' ) );
     44        add_action( 'user_register', array( __CLASS__, 'user_register' ) );
     45        add_action( 'user_profile_update_errors', array( __CLASS__, 'user_profile_update_errors' ), 10, 3 );
     46    }
     47
     48    /**
     49     * Setup localization and later admin hooks
     50     */
     51    public static function admin_init() {
    2952        load_plugin_textdomain( 'simple-user-password-generator', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' );
     53
     54        add_action( 'admin_print_scripts-user-new.php', array( __CLASS__, 'enqueue_script' ) );
     55        add_action( 'admin_print_scripts-user-edit.php', array( __CLASS__, 'enqueue_script' ) );
     56        add_action( 'edit_user_profile', array( __CLASS__, 'edit_user_profile' ), 1 );
    3057    }
    31    
    32     public function enqueue_script() {
    33         if ( ! apply_filters( 'show_password_fields', true ) || ! current_user_can( 'edit_users' ) )
    34             return;
    35            
    36         wp_enqueue_script( 'simple-user-password-generator', plugin_dir_url( __FILE__ ) . 'simple-user-password-generator.js', array( 'jquery' ), '2.0', true );
    3758
    38         $js_trans = array(
    39             'Generate' => esc_attr( __( 'Generate Password', 'simple-user-password-generator' ) ),
    40             'PassChange' => __( 'Encourage the user to change their password, once logged in.', 'simple-user-password-generator' )
    41         );
    42         wp_localize_script( 'simple-user-password-generator', 'simple_user_password_generator_l10n', $js_trans );   
     59    /**
     60     * Load scripts for generate password button
     61     */
     62    public static function enqueue_script() {
     63        if ( apply_filters( 'show_password_fields', true ) && current_user_can( 'edit_users' ) ) {
     64            wp_enqueue_script( 'simple-user-password-generator', plugin_dir_url( __FILE__ ) . 'simple-user-password-generator.js', array( 'jquery' ), '2.0', true );
     65            wp_localize_script( 'simple-user-password-generator', 'simple_user_password_generator_l10n', array(
     66                'Generate' => esc_attr__( 'Generate Password', 'simple-user-password-generator' ),
     67                'PassChange' => __( 'Encourage the user to change their password, once logged in.', 'simple-user-password-generator' )
     68            ) );
     69        }
    4370    }
    44    
    45     public function ajax_generate_password() {
     71
     72    /**
     73     * AJAX callback with generated password
     74     */
     75    public static function ajax_generate_password() {
    4676        die( wp_generate_password() );
    4777    }
    48    
    49     public function user_register( $user_id ) {
    50         if ( current_user_can( 'add_users' ) && ! empty( $_POST['reset_password_notice'] ) )
    51             update_user_option( $user_id, 'default_password_nag', true, true );
    52     }
    5378
    54     public function edit_user_profile( $profileuser ) {
     79    /**
     80     * Add new field to the edit user screen
     81     *
     82     * @param $profileuser
     83     */
     84    public static function edit_user_profile( $profileuser ) {
    5585        wp_nonce_field( 'simple-user-password-generator-reset', '_simple_user_password_generator' );
    5686    ?>
    5787    <table class="form-table">
    5888        <tr>
    59             <th scope="row"><label for="send_password"><?php _e('Send Password?') ?></label></th>
    60             <td><label for="send_password"><input type="checkbox" name="send_password" id="send_password" disabled="disabled" /> <?php _e('Send this password to the user by email.','simple-user-password-generator'); ?></label></td>
     89            <th scope="row"><label for="send_password"><?php _e( 'Send Password?' ) ?></label></th>
     90            <td><label for="send_password"><input type="checkbox" name="send_password" id="send_password" disabled="disabled" /> <?php _e( 'Send this password to the user by email.', 'simple-user-password-generator' ); ?></label></td>
    6191        </tr>
    6292    </table>
     
    6494    }
    6595
    66     public function user_profile_update_errors( $errors, $update, $user ) {
    67         if ( ! current_user_can( 'edit_users' ) || empty( $_POST['_simple_user_password_generator'] ) || ! wp_verify_nonce( $_POST['_simple_user_password_generator'], 'simple-user-password-generator-reset' ) )
    68             return;
     96    /**
     97     * Set password nag option and send password to user after saving profile
     98     *
     99     * @param $errors
     100     * @param $update
     101     * @param $user
     102     */
     103    public static function user_profile_update_errors( $errors, $update, $user ) {
     104        if ( current_user_can( 'edit_users' ) && !empty( $_POST['_simple_user_password_generator'] ) && wp_verify_nonce( $_POST['_simple_user_password_generator'], 'simple-user-password-generator-reset' ) ) {
     105            // enable password change nag
     106            if ( ! empty( $_POST['reset_password_notice'] ) ) {
     107                update_user_option( $user->ID, 'default_password_nag', true, true );
     108            }
    69109
    70         $this->user_register( $user->ID );
     110            // send password to user by email
     111            if ( $update && !empty( $user->user_pass ) && !empty( $_POST['send_password'] ) ) {
     112                $blogname = wp_specialchars_decode( get_bloginfo('name'), ENT_QUOTES );
     113                $message  = sprintf( __('Username: %s'), $user->user_login ) . "\r\n";
     114                $message .= sprintf( __('Password: %s'), $user->user_pass ) . "\r\n";
     115                $message .= wp_login_url() . "\r\n";
    71116
    72         if ( ! $update || empty( $user->user_pass ) || empty( $_POST['send_password'] ) )
    73             return;
    74 
    75         $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    76         $message  = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
    77         $message .= sprintf(__('Password: %s'), $user->user_pass) . "\r\n";
    78         $message .= wp_login_url() . "\r\n";
    79 
    80         wp_mail($user->user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
     117                wp_mail( $user->user_email, sprintf( __('[%s] Your username and password'), $blogname ), $message );
     118            }
     119        }
    81120    }
    82121}
    83122
    84 $simple_local_password_generator = new simple_user_password_generator;
     123Simple_User_Password_Generator::get_instance();
     124
     125endif;
Note: See TracChangeset for help on using the changeset viewer.