首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >投票过滤wordpress评论

投票过滤wordpress评论
EN

Code Review用户
提问于 2019-07-31 11:45:02
回答 1查看 54关注 0票数 3

我有一个网站,根据用户的评论,过滤评论“最喜欢的第一,最不喜欢的第一”等等.

我有一个自定义的评论评分系统和系统,与+1和-1's。

几天前,我想按最受欢迎和最不喜欢的方式过滤评论,就像我说的那样,并编写了以下代码:

在我的functions.php里

代码语言:javascript
复制
function bestanswers($a){

    global $wpdb;

    $results = $wpdb->get_results( 'SELECT * FROM wp_comments LEFT JOIN wp_custom_scoring ON wp_custom_scoring.entryID = wp_comments.comment_ID WHERE class="point" and comment_post_ID='.intval($a).' AND type="plus" GROUP BY entryID ORDER BY COUNT(entryID) DESC');

    if(empty($results)){
        echo 'Its Empty!';
        return false;
    }else{
        return $results;
    }
}

再次在我的functions.php中:

代码语言:javascript
复制
function score($a,$b){

    if($b == "minus"){
    global $wpdb;
    $results = $wpdb->get_results( 'SELECT count(id) as total,type,class,entryID FROM wp_custom_scoring WHERE type="minus"  and class="point" and entryID='.intval($a));

        if($results[0]->total!= 0){
            return "-".$results[0]->total;
        }else{
            return 0;
        } 


    }elseif($b == "plus"){
    global $wpdb;
    $results = $wpdb->get_results( 'SELECT count(id) as total,type,class,entryID FROM wp_custom_scoring WHERE type="plus"  and class="point" and entryID ='.intval($a));

        if($results[0]->total != 0){
            return "+".$results[0]->total;
        }else{
            return 0;
        } 

    }else{
    global $wpdb;
    $results = $wpdb->get_results( 'SELECT count(id) as total,class,entryID FROM wp_custom_scoring WHERE  class="fav" and entryID='.intval($a));

    return $results[0]->total;
    }


}

在comments.php中:

代码语言:javascript
复制
if(isset($_GET['filter']) and sanitize_text_field(esc_attr($_GET['filter'])) == 'bestanswers'){

foreach (bestanswers($postID) as $bests) { 
if ( score($bests->comment_ID,"plus") + score($bests->comment_ID,"minus") > 0 ){
echo $bests->comment_ID;
comment_text($bests->comment_ID);
///...etc with my custom theme template
}

它起作用了,我可以得到我想要的一切,但在我看来,它看起来很难看。有什么更好的方法吗?或者对我的代码有什么改进吗?

EN

回答 1

Code Review用户

回答已采纳

发布于 2019-07-31 23:15:41

以下是我的建议清单:

  • 若要使数据库对象在自定义函数的作用域中可用,请将其作为参数传递。我知道global声明是WPers常用的,但我认为它是草率的开发。如果要坚持使用global,那么每个自定义函数只需执行一次。
  • bestanswers()中,使用换行符、缩进、操作符周围的空格、ALLCAPS关键字、双引用完整字符串和引用字符串中的值来使您的SQL更容易阅读。*也就是说,对于最安全和最一致的项目,您应该在为查询提供变量时使用准备好的语句。$results = $wpdb->get_results( "SELECT * FROM wp_comments LEFT wp_custom_scoring ON wp_custom_scoring.entryID = wp_comments.comment_ID wp_custom_scoring.entryID= 'point‘AND comment_post_ID =“wp_comments.comment_ID]。(int)$a .“并按entryID顺序按计数(EntryID)DESC输入= 'plus‘组”);
  • empty()做了两件事。它检查变量!isset()或松散地计算为"falsey“。您知道$result将是set,因为您已经在前面的代码行中无条件地声明了它。(未经测试.)如果(!$results ){ echo 'Its‘空!’;返回false;} Or {返回$results;}或如果您不需要echo:返回$results?$results: false;
  • scores()中,您的$b参数只确定WHERE子句,这样您就可以将代码干涸如下:函数分数($wpdb,$a,$b) { if (in_array($b,){ $where = "class = 'point‘并键入=’{$b}‘“);}$a= "class = 'fav'";} $results = $wpdb->get_results(“从wp_custom_scoring中选择COUNT(id)作为总计”,其中entryID =。(int)$a .“和{$where}";if (!$results x->$$results->total){返回0;}$a === 'plus') {返回”+{$结果->总计}“;}{ === ($a ===‘--’){返回”{$结果->总计}“;}{返回$结果->总计;}但我再次敦促您考虑在整个应用程序中使用准备好的语句作为一致/稳定/安全的技术。

最终,我不知道脚本的...etc.部分中出现了什么,但是如果您要进行两个函数调用来计算:score($bests->comment_ID,"plus") + score($bests->comment_ID,"minus") > 0,那么创建一个执行此计算并返回和的定制函数可能更有意义。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/225262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档