PHP logic "and" "&&" 用于 WordPress 站点限制

PHP logic "and" "&&" for WordPress site restriction

我的网站上有两个不同的用户角色:雇主和求职者。每个人都有一种个人资料,但候选人的个人资料应该只看到雇主,没有其他人。

所以我想在 Wordpress 中添加一个限制,例如:

雇主可以看到候选人 候选人看不到其他候选人 候选人可以看到自己的个人资料

这是由插件控制的,但似乎已损坏,因为:

雇主可以看到候选人 候选人可以看到其他候选人 候选人看不到自己的个人资料

在求职者个人资料的 .php 文件中是这样的代码:

<?php
    if (!$show_candidate_public_profile) {
        if ($candidate->get_public_account() || get_current_user_id() == $candidate->get_author_id()) {
            $check = 1;
        } else {
            $check = 2;
        }
    } else {
        if (is_user_logged_in()) {
            if ($show_candidate_public_profile == 2 && get_current_user_id() == $candidate->get_author_id()) {
                if ($user->is_employer() && $candidate->get_public_account()) {
                    $check = 3;
                } else {
                    $check = 4;
                }
            } else {
                if ($candidate->get_public_account() || get_current_user_id() == $candidate->get_author_id()) {
                    $check = 1;
                } else {
                    $check = 2;
                }
            }
        } else {
            $check = 0;
        }
    }

    

以及上面代码之后的结果代码:

     if (!$check) {
        ?>
        <div class="iwj-alert-box">
            <div class="container">
                <span>
                    <?php echo sprintf(__('You must be logged in to view this page. <a href="%s">Login here</a>', 'iwjob'), add_query_arg('redirect_to', $candidate->permalink(), $login_page_id)); ?>
                </span>
            </div>
        </div>
    <?php
    } else {
        if ($check == 2) {
            ?>
            <div class="iwj-alert-box">
                <div class="container">
                    <span>
                        <?php echo esc_html__('This profile is not public now.', 'iwjob'); ?>
                    </span>
                </div>
            </div>
        <?php } elseif ($check == 4) {
            ?>
            <div class="iwj-alert-box">
                <div class="container">
                    <span>
                        <?php echo esc_html__('This profile is not public or only employers can see.', 'iwjob'); ?>
                    </span>
                </div>
            </div>
        <?php } else {
            ?>
            <div class="iw-parallax" data-iw-paraspeed="0.1" style="background-image: url('<?php echo esc_url($cover_image_url); ?>');"></div>
            <div class="iw-parallax-overlay"></div>
            <div class="content-top">
                <div class="container">
                    <div class="info-top">
                        <div class="candidate-logo">

支票 3 是唯一一张显示个人资料的支票。

我尝试更改“&&”“||” “==”但我无法弄清楚这个逻辑是如何工作的。

这么多 php 对我来说太多了。我已经问过插件创建者,但我总是要等 5 天才能回复,我现在需要它。

如果有人能帮助我,我会很高兴。

非常感谢!

马丁

此代码应根据您描述的行为工作(当然这将取决于您的插件的良好功能)。我不得不取消一些条件,因为我不知道它们是什么,而且你也没有提供更多细节,如果你需要它们,你必须稍后添加,但这很简单,而且这段代码更具可读性。

第一部分:

<?php
if(!is_user_logged_in())
    $check=false; //if user is not logged in check is false
else
{
    //check if user is employer or if is the profile owner
    if ($user->is_employer() || get_current_user_id() == $candidate->get_author_id())
        $check = 1; //sets 1 if allowed
    else
        $check = 2; //sets 2 if denied 
}
?>

第二部分:

if (!$check) //is check false? then show login message 
{
?>
    <div class="iwj-alert-box">
        <div class="container">
            <span>
                <?php echo sprintf(__('You must be logged in to view this page. <a href="%s">Login here</a>', 'iwjob'), add_query_arg('redirect_to', $candidate->permalink(), $login_page_id)); ?>
            </span>
        </div>
    </div>

<?php
} 
else //check it's not false, so do more tests
{
    if ($check == 2) //if equals 2 then shows access denied message 
    {
?>
        <div class="iwj-alert-box">
            <div class="container">
                <span>
                    <?php echo esc_html__('This profile is not public now or only employers can see.', 'iwjob'); ?>
                 </span>
             </div>
        </div>
<?php 
     }
     elseif($check == 1) //user is profile owner or is an employer, show everything
     {
?>
            <div class="iw-parallax" data-iw-paraspeed="0.1" style="background-image: url('<?php echo esc_url($cover_image_url); ?>');"></div>
            <div class="iw-parallax-overlay"></div>
            <div class="content-top">
                <div class="container">
                    <div class="info-top">
                        <div class="candidate-logo">        
            

如果您想要更多测试,例如个人资料是否 public,您确实需要提供更多信息。希望对你有帮助。