开源欣赏wordpress之intall.php

时间:2021-07-08 05:25:03

引导式安装

$weblog_title = isset( $_POST['weblog_title'] ) ? trim( wp_unslash( $_POST['weblog_title'] ) ) : '';
    $user_name = isset($_POST['user_name']) ? trim( wp_unslash( $_POST['user_name'] ) ) : '';
    $admin_password = isset($_POST['admin_password']) ? trim( wp_unslash( $_POST['admin_password'] ) ) : '';
    $admin_email  = isset( $_POST['admin_email']  ) ? trim( wp_unslash( $_POST['admin_email'] ) ) : '';

点评:这是获取值,并处理的代码,简洁清晰。

<form id="setup" method="post" action="install.php?step=2">
    <table class="form-table">
        <tr>
            <th scope="row"><label for="weblog_title"><?php _e( 'Site Title' ); ?></label></th>
            <td><input name=" value="<?php echo esc_attr( $weblog_title ); ?>" /></td>
        </tr>
        <tr>
            <th scope="row"><label for="user_login"><?php _e('Username'); ?></label></th>
            <td>
            <?php
            if ( $user_table ) {
                _e('User(s) already exists.');
            } else {
                ?><input name=" value="<?php echo esc_attr( sanitize_user( $user_name, true ) ); ?>" />
                <p><?php _e( 'Usernames can have only alphanumeric characters, spaces, underscores, hyphens, periods and the @ symbol.' ); ?></p>
            <?php
            } ?>
            </td>
        </tr>
        <?php if ( ! $user_table ) : ?>
        <tr>
            <th scope="row">
                <label for="admin_password"><?php _e('Password, twice'); ?></label>
                <p><?php _e('A password will be automatically generated for you if you leave this blank.'); ?></p>
            </th>
            <td>
                <input name=" value="" />
                <p><input name=" value="" /></p>
                <div id="pass-strength-result"><?php _e('Strength indicator'); ?></div>
                <p><?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>
        <?php endif; ?>
        <tr>
            <th scope="row"><label for="admin_email"><?php _e( 'Your E-mail' ); ?></label></th>
            <td><input name=" value="<?php echo esc_attr( $admin_email ); ?>" />
            <p><?php _e( 'Double-check your email address before continuing.' ); ?></p></td>
        </tr>
        <tr>
            <th scope="row"><label for="blog_public"><?php _e( 'Privacy' ); ?></label></th>
            <td colspan=" <?php checked( $blog_public ); ?> /> <?php _e( 'Allow search engines to index this site.' ); ?></label></td>
        </tr>
    </table>
    <p class="step"><input type="submit" name="Submit" value="<?php esc_attr_e( 'Install WordPress' ); ?>" class="button button-large" /></p>
</form>

点评:这是form表单的内容。逻辑也非常清晰。思维严谨。

// Let's check to make sure WP isn't already installed.
if ( is_blog_installed() ) {
    display_header();
    die( '<h1>' . __( 'Already Installed' ) . '</h1><p>' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '</p><p class="step"><a href="../wp-login.php" class="button button-large">' . __( 'Log In' ) . '</a></p></body></html>' );
}

判断是否已安装代码。
各种提示语,人性化。

if ( ! empty( $wpdb->error ) )
            wp_die( $wpdb->error->get_error_message() );

        display_header();
        // Fill in the data we gathered
        $weblog_title = isset( $_POST['weblog_title'] ) ? trim( wp_unslash( $_POST['weblog_title'] ) ) : '';
        $user_name = isset($_POST['user_name']) ? trim( wp_unslash( $_POST['user_name'] ) ) : '';
        $admin_password = isset($_POST['admin_password']) ? wp_unslash( $_POST['admin_password'] ) : '';
        $admin_password_check = isset($_POST['admin_password2']) ? wp_unslash( $_POST['admin_password2'] ) : '';
        $admin_email  = isset( $_POST['admin_email']  ) ?trim( wp_unslash( $_POST['admin_email'] ) ) : '';
        $;
        // check e-mail address
        $error = false;
        if ( empty( $user_name ) ) {
            // TODO: poka-yoke
            display_setup_form( __( 'Please provide a valid username.' ) );
            $error = true;
        } elseif ( $user_name != sanitize_user( $user_name, true ) ) {
            display_setup_form( __( 'The username you provided has invalid characters.' ) );
            $error = true;
        } elseif ( $admin_password != $admin_password_check ) {
            // TODO: poka-yoke
            display_setup_form( __( 'Your passwords do not match. Please try again.' ) );
            $error = true;
        } else if ( empty( $admin_email ) ) {
            // TODO: poka-yoke
            display_setup_form( __( 'You must provide an email address.' ) );
            $error = true;
        } elseif ( ! is_email( $admin_email ) ) {
            // TODO: poka-yoke
            display_setup_form( __( 'Sorry, that isn’t a valid email address. Email addresses look like <code>username@example.com</code>.' ) );
            $error = true;
        }

        if ( $error === false ) {
            $wpdb->show_errors();
            $result = wp_install($weblog_title, $user_name, $admin_email, $public, '', $admin_password);
            extract( $result, EXTR_SKIP );

点评:注册登录验证,先验证前端的提交是否有错误。碉堡了。