反序列化数组结果不起作用

时间:2018-03-09 04:20:06

标签: php jquery wordpress

以下是我使用的引用块,我的问题是它没有进入if块而是使用" api_error"进入else块。

function hb_delete_team_member(){
extract( $_POST );

$wp_optimizemember_subscr_gateway = get_user_meta($freelancer_id, 'wp_optimizemember_subscr_gateway', true); 
$wp_optimizemember_subscr_id = get_user_meta($freelancer_id, 'wp_optimizemember_subscr_id', true);
$current_user_jobtype = get_current_user_account_type();

if( OPTIMIZEMEMBER_CURRENT_USER_ACCESS_LEVEL > 1 ){
    $get_jobs_via_invitations = hb_freelancer_hired_by_invitations( $freelancer_id );
}else{
    $get_jobs_via_invitations = hb_free_client_freelancer_hired_by_invitations( $freelancer_id );
}

if(user_can( $freelancer_id, "access_optimizemember_level2" )) {
    $status = "deleted";
    if(!empty($wp_optimizemember_subscr_gateway) || !empty($wp_optimizemember_subscr_id)) {
        // deleting freelancer from my team
        delete_user_meta( $client_id, "hb_invitation_to_freelancer_" . $freelancer_id );
        delete_user_meta( $client_id, "hb_free_client_invitation_to_freelancer_" . $freelancer_id ); // free client job hire request
        delete_user_meta( $client_id, "hb_free_client_request_to_freelancer_" . $freelancer_id ); // deleting free client invitation sent
        delete_user_meta( $client_id, "hb_request_to_freelancer_" . $freelancer_id );

        // deleting client from freelancer account for restriction to insiders section
        delete_user_meta( $freelancer_id, "hb_hired_by_client_" . $client_id );
        delete_user_meta( $freelancer_id, "hb_hired_by_free_client_" . $client_id );

        // saving contract end time
        update_user_meta( $freelancer_id, "hb_client_" . $client_id . "_contract_ends_at", time() );

        //Update Number of Invites on Member Deletion
        $invitation_total = get_user_meta( $client_id, 'hb_invitation_total', true );
        $invitation_total = $invitation_total - 1;
        update_user_meta( $client_id, "hb_invitation_total", $invitation_total );

        if ( $get_jobs_via_invitations == 1 ){
        //Determine If The VA is Hired by Someone
            update_user_meta( $freelancer_id, "is_hired", "false");
        } else {
            update_user_meta( $freelancer_id, "is_hired", "true");
        }
    } else {
        if ( $get_jobs_via_invitations == 1 ){ // if freelancer has no more jobs through invitation, change his membership level to 0

            $result = downgrade_freelancer_membership( $freelancer_id );
            if ( ! empty ($result) && !preg_match ("/^Error\:/i", $result) && is_array ($user = @unserialize ($result))){

                // deleting freelancer from my team
                delete_user_meta( $client_id, "hb_invitation_to_freelancer_" . $freelancer_id );
                delete_user_meta( $client_id, "hb_free_client_invitation_to_freelancer_" . $freelancer_id ); // free client job hire request
                delete_user_meta( $client_id, "hb_free_client_request_to_freelancer_" . $freelancer_id ); // deleting free client invitation sent
                delete_user_meta( $client_id, "hb_request_to_freelancer_" . $freelancer_id );

                // deleting client from freelancer account for restriction to insiders section
                delete_user_meta( $freelancer_id, "hb_hired_by_client_" . $client_id );
                delete_user_meta( $freelancer_id, "hb_hired_by_free_client_" . $client_id );

                // saving contract end time
                update_user_meta( $freelancer_id, "hb_client_" . $client_id . "_contract_ends_at", time() );

                //Update Number of Invites on Member Deletion
                $invitation_total = get_user_meta( $client_id, 'hb_invitation_total', true );
                $invitation_total = $invitation_total - 1;
                update_user_meta( $client_id, "hb_invitation_total", $invitation_total );

                //Determine If The VA is Hired by Someone
                update_user_meta( $freelancer_id, "is_hired", "false");

            }else{
                $status = "api_error";
            }

        }else{ // if freelancer has any job through invitation, his membership level 2 remains

        // deleting freelancer from my team
        delete_user_meta( $client_id, "hb_invitation_to_freelancer_" . $freelancer_id );
        delete_user_meta( $client_id, "hb_free_client_invitation_to_freelancer_" . $freelancer_id ); // free client job hire request
        delete_user_meta( $client_id, "hb_free_client_request_to_freelancer_" . $freelancer_id ); // deleting free client invitation sent
        delete_user_meta( $client_id, "hb_request_to_freelancer_" . $freelancer_id );

        // deleting client from freelancer account for restriction to insiders section
        delete_user_meta( $freelancer_id, "hb_hired_by_client_" . $client_id );
        delete_user_meta( $freelancer_id, "hb_hired_by_free_client_" . $client_id );

        // saving contract end time
        update_user_meta( $freelancer_id, "hb_client_" . $client_id . "_contract_ends_at", time() );

        //Update Number of Invites on Member Deletion
        $invitation_total = get_user_meta( $client_id, 'hb_invitation_total', true );
        $invitation_total = $invitation_total - 1;
        update_user_meta( $client_id, "hb_invitation_total", $invitation_total );

        //Determine If The VA is Hired by Someone
        update_user_meta( $freelancer_id, "is_hired", "true");

        }
    }   
}
echo $status;
die();
}
add_action('wp_ajax_hb_end_contract', 'hb_delete_team_member' );
add_action('wp_ajax_nopriv_hb_end_contract', 'hb_delete_team_member' );

我试图回显$ result,输出是一个类似这样的文本:仍然订阅{a:1,a:3}。

我不知道为什么它会回来" api_error",如果我发表评论

if ( ! empty ($result) && !preg_match ("/^Error\:/i", $result) /*&& is_array ($user = @unserialize ($result))*/)

反序列化它的工作正常。

1 个答案:

答案 0 :(得分:1)

我正在阅读这些条件:

<?php
$message = 'Error: something went wrong.';
$user    = serialize(['name' => 'Foo Bar', 'ID' => '23']);

foreach([$message, $user] as $result)
{
    if (
        !empty ($result) && 
        !preg_match ("/^Error\:/i", $result) && 
        is_array ($user = @unserialize ($result))
    )
        echo "Success. Deleted user with ID: " . $user["ID"];
    else
        echo "API error reads: " . $result;

    print "\n";

    // Supposedly not working:
    if (
        !empty ($result) &&
        !preg_match ("/^Error\:/i", $result) &&
        is_array ($user = @unserialize ($result))
    ) {
        echo "Success. Deleted User with ID: " . $user["ID"];
    } else {
        echo "API error reads: " . $result;
    }

    print "\n\n";
}

输出:

API error reads: Error: something went wrong.
API error reads: Error: something went wrong.

Success. Deleted user with ID: 23
Success. Deleted User with ID: 23