开源欣赏wordpress之用户新增user-new.php

简介:
require_once( dirname( __FILE__ ) . '/admin.php' );

引入根文件。

 

if ( is_multisite() ) {
    if ( ! current_user_can( 'create_users' ) && ! current_user_can( 'promote_users' ) )
        wp_die( __( 'Cheatin’ uh?' ) );
} elseif ( ! current_user_can( 'create_users' ) ) {
    wp_die( __( 'Cheatin’ uh?' ) );
}

用户权限验证,封装在方法当中了。

 

用户信息输入完后,跳转到列表。是这么个流程。

用户列表有批量操作的多选按钮。同时有新增用户,等管理。

同时可以根据多个条件进行搜索。搜索过程中子页面是跳转的。结果信息直接在头部提示。

我们继续看代码。

 

复制代码
$user_details = null;
    if ( false !== strpos($_REQUEST[ 'email' ], '@') ) {
        $user_details = get_user_by('email', $_REQUEST[ 'email' ]);
    } else {
        if ( is_super_admin() ) {
            $user_details = get_user_by('login', $_REQUEST[ 'email' ]);
        } else {
            wp_redirect( add_query_arg( array('update' => 'enter_email'), 'user-new.php' ) );
            die();
        }
    }
复制代码

一些逻辑验证,方法封装,很多可以具有通用性。比如wp_redirect()就特别具有通用性,一个项目中离不开页面跳转。

 

这种简洁的页面设计就特别有意思。背景变红,边框变红。

这次的错误提示是提交后,后台反馈的。

很有意思。前后端同时验证。

<div class="error below-h2">
        <p><strong>错误</strong>:请填写用户名。</p><p><strong>错误</strong>:此用户名包含无效字符,请输入有效的用户名。</p><p><strong>错误</strong>:电子邮件地址不正确。</p>    </div>

这就是html的内容,是在div层中展示出来的。

 

复制代码
// Adding an existing user to this blog
    $new_user_email = $user_details->user_email;
    $redirect = 'user-new.php';
    $username = $user_details->user_login;
    $user_id = $user_details->ID;
    if ( ( $username != null && !is_super_admin( $user_id ) ) && ( array_key_exists($blog_id, get_blogs_of_user($user_id)) ) ) {
        $redirect = add_query_arg( array('update' => 'addexisting'), 'user-new.php' );
    }
复制代码

进行用户已存在验证。

 

复制代码
$newuser_key = substr( md5( $user_id ), 0, 5 );
            add_option( 'new_user_' . $newuser_key, array( 'user_id' => $user_id, 'email' => $user_details->user_email, 'role' => $_REQUEST[ 'role' ] ) );

            $roles = get_editable_roles();
            $role = $roles[ $_REQUEST['role'] ];
            /* translators: 1: Site name, 2: site URL, 3: role, 4: activation URL */
            $message = __( 'Hi,

You\'ve been invited to join \'%1$s\' at
%2$s with the role of %3$s.

Please click the following link to confirm the invite:
%4$s' );
            wp_mail( $new_user_email, sprintf( __( '[%s] Joining confirmation' ), get_option( 'blogname' ) ), sprintf( $message, get_option( 'blogname' ), home_url(), wp_specialchars_decode( translate_user_role( $role['name'] ) ), home_url( "/newbloguser/$newuser_key/" ) ) );
            $redirect = add_query_arg( array('update' => 'add'), 'user-new.php' );
复制代码

通过验证,各种欢迎。

 

复制代码
if ( isset($_GET['update']) ) {
    $messages = array();
    if ( is_multisite() ) {
        switch ( $_GET['update'] ) {
            case "newuserconfirmation":
                $messages[] = __('Invitation email sent to new user. A confirmation link must be clicked before their account is created.');
                break;
            case "add":
                $messages[] = __('Invitation email sent to user. A confirmation link must be clicked for them to be added to your site.');
                break;
            case "addnoconfirmation":
                $messages[] = __('User has been added to your site.');
                break;
            case "addexisting":
                $messages[] = __('That user is already a member of this site.');
                break;
            case "does_not_exist":
                $messages[] = __('The requested user does not exist.');
                break;
            case "enter_email":
                $messages[] = __('Please enter a valid email address.');
                break;
        }
    } else {
        if ( 'add' == $_GET['update'] )
            $messages[] = __('User added.');
    }
}
复制代码

消息提示,分类提示,很有意思。貌似这个也是封装好的内容。

复制代码
<?php if ( isset($errors) && is_wp_error( $errors ) ) : ?>
    <div class="error">
        <ul>
        <?php
            foreach ( $errors->get_error_messages() as $err )
                echo "<li>$err</li>\n";
        ?>
        </ul>
    </div>
<?php endif;

if ( ! empty( $messages ) ) {
    foreach ( $messages as $msg )
        echo '<div id="message" class="updated"><p>' . $msg . '</p></div>';
} ?>

<?php if ( isset($add_user_errors) && is_wp_error( $add_user_errors ) ) : ?>
    <div class="error">
        <?php
            foreach ( $add_user_errors->get_error_messages() as $message )
                echo "<p>$message</p>";
        ?>
    </div>
<?php endif; ?>
复制代码

错误信息展示,如果有的话,将div展示出来。这就是前端的逻辑,可以卸载tpl中。

 

复制代码
<table class="form-table">
    <tr class="form-field form-required">
        <th scope="row"><label for="user_login"><?php _e('Username'); ?> <span class="description"><?php _e('(required)'); ?></span></label></th>
        <td><input name="user_login" type="text" id="user_login" value="<?php echo esc_attr($new_user_login); ?>" aria-required="true" /></td>
    </tr>
    <tr class="form-field form-required">
        <th scope="row"><label for="email"><?php _e('E-mail'); ?> <span class="description"><?php _e('(required)'); ?></span></label></th>
        <td><input name="email" type="text" id="email" value="<?php echo esc_attr($new_user_email); ?>" /></td>
    </tr>
<?php if ( !is_multisite() ) { ?>
    <tr class="form-field">
        <th scope="row"><label for="first_name"><?php _e('First Name') ?> </label></th>
        <td><input name="first_name" type="text" id="first_name" value="<?php echo esc_attr($new_user_firstname); ?>" /></td>
    </tr>
    <tr class="form-field">
        <th scope="row"><label for="last_name"><?php _e('Last Name') ?> </label></th>
        <td><input name="last_name" type="text" id="last_name" value="<?php echo esc_attr($new_user_lastname); ?>" /></td>
    </tr>
    <tr class="form-field">
        <th scope="row"><label for="url"><?php _e('Website') ?></label></th>
        <td><input name="url" type="text" id="url" class="code" value="<?php echo esc_attr($new_user_uri); ?>" /></td>
    </tr>
<?php
/**
 * Filter the display of the password fields.
 *
 * @since 1.5.1
 *
 * @param bool True or false, based on if you want to show the password fields. Default is true.
 */
if ( apply_filters( 'show_password_fields', true ) ) : ?>
    <tr class="form-field form-required">
        <th scope="row"><label for="pass1"><?php _e('Password'); ?> <span class="description"><?php /* translators: password input field */_e('(required)'); ?></span></label></th>
        <td>
            <input class="hidden" value=" " /><!-- #24364 workaround -->
            <input name="pass1" type="password" id="pass1" autocomplete="off" />
        </td>
    </tr>
    <tr class="form-field form-required">
        <th scope="row"><label for="pass2"><?php _e('Repeat Password'); ?> <span class="description"><?php /* translators: password input field */_e('(required)'); ?></span></label></th>
        <td>
        <input name="pass2" type="password" id="pass2" autocomplete="off" />
        <br />
        <div id="pass-strength-result"><?php _e('Strength indicator'); ?></div>
        <p class="description indicator-hint"><?php _e('Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ &amp; ).'); ?></p>
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="send_password"><?php _e('Send Password?') ?></label></th>
        <td><label for="send_password"><input type="checkbox" name="send_password" id="send_password" <?php checked( $new_user_send_password ); ?> /> <?php _e('Send this password to the new user by email.'); ?></label></td>
    </tr>
<?php endif; ?>
<?php } // !is_multisite ?>
    <tr class="form-field">
        <th scope="row"><label for="role"><?php _e('Role'); ?></label></th>
        <td><select name="role" id="role">
            <?php
            if ( !$new_user_role )
                $new_user_role = !empty($current_role) ? $current_role : get_option('default_role');
            wp_dropdown_roles($new_user_role);
            ?>
            </select>
        </td>
    </tr>
    <?php if ( is_multisite() && is_super_admin() ) { ?>
    <tr>
        <th scope="row"><label for="noconfirmation"><?php _e('Skip Confirmation Email') ?></label></th>
        <td><label for="noconfirmation"><input type="checkbox" name="noconfirmation" id="noconfirmation" value="1" <?php checked( $new_user_ignore_pass ); ?> /> <?php _e( 'Add the user without sending them a confirmation email.' ); ?></label></td>
    </tr>
    <?php } ?>
</table>
复制代码

验证页面的表单设计。

 

复制代码
<?php
/** This action is documented in wp-admin/user-new.php */
do_action( 'user_new_form', 'add-new-user' );
?>

<?php submit_button( __( 'Add New User '), 'primary', 'createuser', true, array( 'id' => 'createusersub' ) ); ?>

</form>
<?php } // current_user_can('create_users') ?>
</div>
<?php
include( ABSPATH . 'wp-admin/admin-footer.php' );
复制代码

一些尾文件的引入。

 

很有意思。php还可以这么玩。


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/3470223.html,如需转载请自行联系原作者

相关文章
|
2月前
|
PHP UED 异构计算
【开源】WordPress一键崩溃宕机插件(整活娱乐)
可一键实现Wordpress崩溃宕机的整活向插件(请勿用于非法途径,仅供整活娱乐)。鼓励关注网站性能的提升,以提供更好的用户体验,提倡为用户提供良好体验和高效速度的原则。
34 4
【开源】WordPress一键崩溃宕机插件(整活娱乐)
|
7月前
|
监控 Linux PHP
Linux(CentOS7上演示)上使用LNMP搭建一个PHP的运行环境,并搭建一个开源的个人博客网站I(下)
Linux(CentOS7上演示)上使用LNMP搭建一个PHP的运行环境,并搭建一个开源的个人博客网站I(下)
53 0
|
7月前
|
关系型数据库 MySQL Linux
Linux(CentOS7上演示)上使用LNMP搭建一个PHP的运行环境,并搭建一个开源的个人博客网站I(上)
Linux(CentOS7上演示)上使用LNMP搭建一个PHP的运行环境,并搭建一个开源的个人博客网站I(上)
110 0
|
关系型数据库 MySQL API
如何修复“PHP 安装缺少 WordPress 所需的 MySQL 扩展”的错误?
在WordPress建站,WordPress定制开发过程中,开发者容易遇见“您的 PHP 安装似乎缺少 WordPress 所需的 MySQL 扩展”的错误提示,如果出现这样的情况,应该怎么办?北京六翼信息有限公司的开发工程师指出,要修复错误“您的 PHP 安装似乎缺少 WordPress 所需的 MySQL 扩展”,您需要确保您的 PHP 安装已正确安装和配置 MySQL 数据库驱动程序 (mysqlnd) 和 mysqli 扩展。只有这样,您才能恢复 WordPress 和 WordPress 数据库之间的正常连接,并让您的网站重新运行。
如何修复“PHP 安装缺少 WordPress 所需的 MySQL 扩展”的错误?
|
供应链 监控 安全
开源PHP区块链数字货币交易平台
开源PHP区块链数字货币交易平台
开源PHP区块链数字货币交易平台
樱花猫wordpress开源免费主题源码
借用制作者的原话,这个主题Sakura是在 Louie 基于 Fuzzz 的 Akina 主题修改的主题 Siren 基础上三次修改
183 0
樱花猫wordpress开源免费主题源码
|
安全 PHP
PHP Everywhere 三个 RCE 漏洞威胁大量 WordPress 网站
PHP Everywhere 三个 RCE 漏洞威胁大量 WordPress 网站
170 0
|
BI PHP 数据库
这是我见过PHP里最好用的开源免费进销存/ERP系统(付源码)
基于thinkphp+layui开发。功能包含:采购、销售、零售、多仓库管理、财务管理等功能 和超详细的报表功能(采购报表、销售报表、零售报表、仓库报表、资金报表等)
13570 0
这是我见过PHP里最好用的开源免费进销存/ERP系统(付源码)
|
消息中间件 监控 网络协议
SaltStack安装Apache/Mysql/PHP部署Wordpress
SaltStack是一个服务器基础架构集中化管理平台,具备配置管理、远程执行、监控等功能,基于Python语言实现,结合轻量级消息队列(ZeroMQ)与Python第三方模块(Pyzmq、PyCrypto、Pyjinjia2、python-msgpack和PyYAML等)构建。 SaltStack 采用 C/S模式,server端就是salt的master,client端就是minion,minion与master之间通过ZeroMQ消息队列通信。 master监听4505和4506端口,4505对应的是ZMQ的PUB system,用来发送消息,4506对应的是REP system是来接受
151 0
适合个人建站的免费开源网站主题汇总(hexo&jekyll&hugo&wordpress&typecho)
适合个人建站的免费开源网站主题汇总(hexo&jekyll&hugo&wordpress&typecho)
1063 0