2

宣言されているのと同じページに投稿するフォームがあります。問題は、送信時に2回呼び出されているように見えることです。コードは次のとおりです。

function user_data_form() {

    add_ajax_validation();

    // Form verication start
    if (isset($_POST["action"])) {
        switch($_POST["action"]) {
            case "validate_form":
                ajax_validate();
                break;
            case "update_user_data":
                $wp_user_object = wp_get_current_user();
                $id = $wp_user_object->ID;
                if ( 0 == $wp_user_object->ID ) {
                    redirect(get_permalink(get_page_by_title( 'Sign in' )), array(
                        "message" => 8
                    ));
                } else {
                    $form_data = array(
                        "verified" => false,
                        "email" => $_POST["email"],
                        "first_name" => $_POST["first_name"],
                        "last_name" => $_POST["last_name"],
                        "password" => $_POST["password"],
                        "old_password" => $_POST["old_password"],
                        "verify_password" => $_POST["verify_password"]
                    );
                    $form_data["verification"] = array(
                        "old_password" => wp_check_password($form_data["old_password"], $wp_user_object->user_pass, $id),
                        "email" => validate("email", $form_data["email"]),
                        "password" => validate("old_password", $form_data["old_password"]),
                        "verify_password" => validate("verify_password", $form_data["old_password"], $form_data["verify_password"])
                    );
                    if ($form_data["verification"]["old_password"]) {
                        if ($form_data["email"] != $wp_user_object->user_email && $form_data["verification"]["email"]) {
                            invalidate_email($id, $form_data["email"]);
                            $message = message('success', 'Account information updated, an email has been dispatched to <strong>'.$form_data["email"].'</strong> with details on how to confirm your new email address');
                            $form_data["email"] = $wp_user_object->user_email;
                        }
                        if ($form_data["first_name"] != $wp_user_object->get("first_name")) {
                            update_user_meta($id, 'first_name', $form_data["first_name"]);
                        }
                        if ($form_data["last_name"] != $wp_user_object->get("last_name")) {
                            update_user_meta($id, 'last_name', $form_data["last_name"]);
                        }
                        if ($form_data["verification"]["password"]["result"] && $form_data["verification"]["verify_password"]["result"]) {
                            wp_set_password($form_data["password"], $id);
                        }
                        if (!message) {
                            $message = message('success', 'Account information updated');
                        }
                    } else {
                        $message = message('error', '<b>Error:</b> Incorrect password');
                    }
                }
                break;
        }
    }
    // Form verication end

    // Create form
    if (!$form_data["verified"]) {
        global $current_user;
        get_currentuserinfo();
        $new_email = get_metadata("user", $current_user->user_ID, "new_email", true);
        $email_expires = get_metadata("user", $current_user->user_ID, "email_expires", true);
        $form = 'You can view and update your account settings using the following form:
                <form action="#" method="post" class="user-data">
                '.$message.'
                <input type="hidden" name="action" value="update_user_data" />
                <div class="fields">
                    <div class="field">
                        <label for="email">Email:</label>
                        <input type="text" name="email" id="email" class="text" data-validation-method="email" class="text" data-validation-ignore="'.encode($current_user->user_email).'" value="'.(($form_data["email"]) ? $form_data["email"] : $current_user->user_email).'" placeholder="youremail@somesite.com" data-validation-response="'.(($form_data["email"] == $current_user->user_email) ? NULL : encode($form_data["verification"]["email"])).'" />
                        '.(($new_email) ? '<span class="fail">An email has been sent to the above address to change email to <strong>'.$new_email.'</strong></span>' : NULL).'
                        <span class="info">You will recieve a validation email to your new email address. If you don\'t confirm within 24 hours your email address will revert to the last validated one.</span>
                    </div>
                    <div class="field">
                        <label for="first_name">First name: <sup>1</sup></label>
                        <input type="text" name="first_name" id="first_name" class="text" value="'.$current_user->user_firstname.'"/>
                        <label for="last_name">Last name: <sup>1</sup></label>
                        <input type="text" name="last_name" id="last_name" class="text" value="'.$current_user->user_lastname.'"/>
                        <span class="info"><sup>1</sup> Required to issue certificates.</span>
                        <div class="fieldgroup">
                            <span class="info">
                                <strong>Note:</strong> Only fill in these fields if you wish to change your password.
                            </span>
                            <div class="field">
                                <label for="password">New password:</label>
                                <input type="password" name="password" id="password" data-validation-method="password" class="text" value="'.$form_data["password"].'" data-validation-response="'.((strlen($form_data["password"]) > 0) ? encode($form_data["verification"]["password"]) : NULL ).'" />
                            </div>
                            <div class="field">
                                <label for="verify_password">Retype your new password:</label>
                                <input type="password" name="verify_password" id="verify_password" class="text" data-validation-method="verify_password" data-validation-requires="password" value="'.$form_data["verify_password"].'" data-validation-response="'.((strlen($form_data["verify_password"]) > 0) ? encode($form_data["verification"]["verify_password"]) : NULL ).'" />
                            </div>
                        </div>
                        <div class="field">
                            <label for="password">Current password:</label>
                            <input type="password" name="old_password" id="old_password" class="text" value=""/>
                            <span class="info">Your current password is required to make changes to your account information. Have you <a href="'.get_permalink( get_page_by_title( 'Reset your password' ) ).'">forgotten your password?</a></span>
                        </div>
                    </div>
                    <input type="submit" id="submitbtn" class="button omega" name="submit" value="Update my account information" />
                </div>
            </form>';
    } else {
        redirect(get_permalink(get_page_by_title( 'My account' )), array(
            "message" => 2
        ));
    }
    return $form;
}

問題は、invalidate_email()2回呼び出されることです。$_POST["action"] == "update_user_data"フォームが送信された後にのみ起動する必要がありますが。2番目の呼び出しがどこから来ているのかわかりません。

これが私のinvalidate_email()関数です:

function invalidate_email($user_id, $new_email) {
    $key = createKey($new_email);
    $activation_url = get_permalink(get_page_by_title( 'Activate' ))."?key=".$key."&id=".$user_id."&type=email";
    update_user_meta($user_id, 'new_email', $new_email);
    update_user_meta($user_id, 'email_activation_key', $key);
    $expires = get_metadata("user", $user_id, "email_expires", true);
    if (!$expires) {
        $expires = time() + 86400000; //86400000 milliseconds = 24 Hours
        update_user_meta($user_id, 'email_expires', $expires);
    }

    $message = "A request to update your email for your account on First steps has been made, please confirm your email address by clicking the following link: <p />";
    $message .= "<a href='".$activation_url."'>".$activation_url."</a> <p />";
    $message .= "If you did not request the change your account may be comprimised and we recommend you change your password from the 'my account' page. <p />";
    $message .= "If you do not confirm your new email address within 24 hours the request will expire and you will have to request a new one.";

    email($new_email, "First steps - Confirm your new email address", "Confirm email address change", $message);
}

およびemail()

function email($to, $subject, $heading, $message, $attachments = NULL) {
    $headers = array('From: First steps <info@rcnhca.org.uk>', 'Content-type: text/html');
    $body = '<table width="100%" style="background:#ECECEC;" cellpadding="0" cellspacing="0">  
                <tr width="100%" style="background: #3393b5;">
                    <td align="center">
                        <table width="500" cellpadding="0" cellspacing="0">
                            <tr>
                                <td align="center" style="height:45px; vertical-align:middle;">
                                    <a href="'.get_bloginfo("url").'"><img alt="First steps for health care assistants" src="'.get_stylesheet_directory_uri().'/library/images/logo.png" width="347" height="32" style="vertical-align:middle;display:block;" /></a>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td align="center" style="padding: 10px 0px;">
                        <table width="500" style="padding: 10px;background:#FFFFFF;border:1px solid #BCBCBC;margin:0px 10px;" cellpadding="0" cellspacing="0">
                            <tr>
                                <td style="color:#000000;font-size:14px;line-height:1.5em;text-align:left;"><font face="verdana">
                                    <h2 style="margin-top: 0.1em; font-size: 18px; padding-bottom: 0.5em; color: #333333; border-bottom: 1px solid #CCCCCC; margin-bottom: 0.7em;">'.$heading.'</h2>
                                    '.$message.'
                                </font></td>
                            </tr>
                        </table>
                        <table width="500">
                            <tr>
                                <td style="font-size:12px;line-height:1.5em;text-align:left;"><font face="verdana">We are committed to keeping your email private, we do not share your address with third-parties.</font></td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table> ';
    wp_mail($to, $subject, $body, $headers, $attachments);
}

Ajax検証(この関数が行うこと)を削除しても発生するため、Ajax呼び出しではないことを確信しています(無視add_ajax_validation()してください)。HTTP_X_REQUESTED_WITHまた、すべてのAjaxがjQueryによって実行されるため、ヘッダーが存在する必要があるため、存在する必要があるものが存在するかどうかを確認しました。

編集: @Oliへの応答

debug_backtrace()関数を呼び出しているものを確認するためにを実行しました。これが私のバックトレースです。

$trace= debug_backtrace();
$funcs = array();
foreach ($trace as $func) {
    array_push($funcs, $func['function']);
}
$funcs = implode('\n', $funcs);

出力1:

invalidate_email
user_data_form
call_user_func
do_shortcode_tag
preg_replace_callback
do_shortcode
pre_process_shortcode
call_user_func_array
apply_filters
wp_trim_excerpt
call_user_func_array
apply_filters
get_the_excerpt
require_once 
load_template
locate_template
get_header
include
require_once
require

出力2:

invalidate_email
user_data_form
call_user_func
do_shortcode_tag
preg_replace_callback
do_shortcode
pre_process_shortcode
call_user_func_array
apply_filters
the_content
include
require_once
require

これcall_user_func()以降は、次の関数呼び出しの結果として、すべてWordPressCMSによって実行されます。add_shortcode('user_data_form', 'user_data_form', 134);

4

3 に答える 3

0

以前、mail()でこれに気づきました。ヘッダーセクションに「To」があり、mail()の最初のパラメーターに「to」の変数がある場合、2回送信されます。ヘッダーの部分を削除して試してみてください。

質問をもっとよく読むべきだった。

于 2013-03-08T14:51:52.597 に答える
0

私はそのようなばかみたいに感じます、しかし私はちょうどこれが自傷行為であることに気づきました。WordPressの自動フォーマット機能を回避しようとするこのコードがありましたwpautop。それらを2回発射するようです。

function pre_process_shortcode($content) {
    global $shortcode_tags;
    // Backup current registered shortcodes and clear them all out
    $orig_shortcode_tags = $shortcode_tags;
    $shortcode_tags = array();
        add_shortcode('registration_form', 'registration_form', 134);
        add_shortcode('login_form', 'login_form', 134);
        add_shortcode('user_data_form', 'user_data_form', 134);
        add_shortcode('activation_code', 'activation_code', 134);
        add_shortcode('logout_code', 'logout_code', 134);
        add_shortcode('reset_password_form', 'reset_password_form', 134);
    // Do the shortcode (only the one above is registered)
    $content = do_shortcode($content);
    // Put the original shortcodes back
    $shortcode_tags = $orig_shortcode_tags;
    return $content;
}

add_filter('the_content', 'pre_process_shortcode', 13);

私はこれをただに変更しました:

    add_shortcode('registration_form', 'registration_form', 134);
    add_shortcode('login_form', 'login_form', 134);
    add_shortcode('user_data_form', 'user_data_form', 134);
    add_shortcode('activation_code', 'activation_code', 134);
    add_shortcode('logout_code', 'logout_code', 134);
    add_shortcode('reset_password_form', 'reset_password_form', 134);

もちろん、これは他の問題を引き起こしますが、少なくともそれはこの問題を解決します、そして私は以前にやりたかったことをする別の方法を見つけるでしょう。私が提供した情報からこれを解決することは決してなかったので、この問題を解決するために時間を費やした人には申し訳ありません。

于 2013-03-08T16:21:16.707 に答える
0

関数var_dump(func_get_args())にどのようなパラメーターを受け取っているかを調査して検出しますinvalidate_email

于 2013-03-08T14:47:47.223 に答える