【问题标题】:is there a way to avoid duplicate line有没有办法避免重复行
【发布时间】:2021-12-15 10:26:41
【问题描述】:

嗨,在 wordpress 网站中,我必须向某些用户输出某些内容,我的代码在下面我的问题是如何避免这种回显行重复

if (is_user_logged_in() and get_current_user_id() != get_the_author_meta('ID')) {
                        if (in_array('customer', (array) $user->roles)) {
                            if (get_current_user_id() == $authorid) { ?>
                                <i class="fa fa-comments send_designer_msg" designer_id="<?php echo get_the_author_meta("ID"); ?>" logo_number="<?php echo $entryno; ?>" aria-hidden="true"></i>
                            <?php }
                        } else { ?>
                            <i class="fa fa-comments send_designer_msg" designer_id="<?php echo get_the_author_meta("ID"); ?>" logo_number="<?php echo $entryno; ?>" aria-hidden="true"></i>
                    <?php }
                    }

任何帮助,谢谢

【问题讨论】:

    标签: javascript php html wordpress logic


    【解决方案1】:

    条件逻辑很难理解。

    您想为 cmets 显示一个图标(基于 fontAwesome) - 仅适用于满足以下要求的登录用户:

    1. 不是这篇文章的“设计者”。
    2. 具有“客户”角色

    不确定,您的条件逻辑设置是否正确,因为它似乎为任何不是“设计者”的用户输出评论标记......请原谅我 - 但 author_id 和 designer_id 之间的区别在哪里?

    更 DRY 的实现可能是以下代码:

    <?php
    $current_user_id = get_current_user_id();
    $designer_id = get_the_author_meta("ID");
    $author_id = '???';
    $show_comment_icon = false;
    
    if (is_user_logged_in() && $current_user_id != $designer_id ) {
        // if user has role "customer" 
        if (in_array ('customer', (array)$user->roles)) {
            // if currently logged in user is the author of the current post?
            if ( $current_user_id == $author_id) { 
                $show_comment_icon = true;
            }
        } else { 
            // if user is not a "customer" 
            $show_comment_icon = true;
        }
     
        // comment icon html template
        $comment_icon = 
        '<i 
            class="fa fa-comments send_designer_msg" 
            data-designer-id="'.$designer_id.'" 
            data-logo-number="'.$entry_no.'" 
            aria-hidden="true">
        </i>';  
        
        // output your html
        if($show_comment_icon){
            echo $comment_icon; 
        }       
    }
    ?>
    

    尽管这个修改后的代码 sn -p 肯定有缺陷: 避免重复代码和提高 php 可读性的一些建议:

    • 选择适当的封闭概念(在 php 和 html 之间切换时)*
    • 从输出中分离“数据编译”和逻辑
    • 在代码中大量插入 cmets
    • 将重复查询的值(例如由函数/类返回)保存在变量中
    • 最终努力修改你的代码(可能没有人会因为你的努力而奖励你——除了你自己,即使在几年后也能理解你的代码)

    在 php 和 html 代码之间封闭/切换 您的代码有效。然而,易读性受到影响。 一个可行的经验法则来决定是否使用相当封闭的 php-in-html 或 html-in-php 是比例。

    如果涉及更多的 php 处理(条件、计算、过滤等),最好使用变量来定义您的 html 输出。

    如果 html 模板部分占主导地位,您可以使用类似这样的内容(类似于默认的 wordpress 循环提案):

    <?php if($condition_fullfilled) :?>
        <p>Condition is fullfilled – there is a lot more html to come!</p>
        .... (200 lines of html markup)
    <?php else: ?>
        <p>Nothing found, matching your conditions</p>
    <?php endif; ?>
    

    【讨论】:

    • 谢谢您,我将使用变量来定义 html 输出,这将解决我的问题,感谢您的解决方案。为什么我没有想到这一点
    猜你喜欢
    • 2020-12-02
    • 2019-09-07
    • 1970-01-01
    • 2022-11-16
    • 2019-05-29
    • 2011-09-13
    • 2012-06-20
    • 1970-01-01
    • 2020-11-20
    相关资源
    最近更新 更多