对注释状态更改运行函数

时间:2012-03-08 作者:Sisir

我正在制作一个审查系统,在以下情况下,我需要找到更新的平均评级:

评论从待定/垃圾/垃圾邮件中获得批准

  • 评论从approve升级approve(自动批准)管理员帖子评论(自动批准)
  • 编辑评论(当用户想要编辑/更新/删除其已做的评论时,从后端或前端自定义仪表板)
    • Here is the code i\'ve written:

      add_action(\'edit_comment\', \'update_business_rating_avg\');
      add_action(\'comment_post\', \'update_business_rating_avg\');
      add_action(\'comment_unapproved_to_approved\', \'update_business_rating_avg\');
      add_action(\'comment_approved_to_unapproved\', \'update_business_rating_avg\');
      add_action(\'comment_spam_to_approved\', \'update_business_rating_avg\');
      add_action(\'comment_approved_to_spam\', \'update_business_rating_avg\');
      
          function update_business_rating_avg($comment){
              //fb($comment);
              $post_id = $comment->comment_post_ID;
              $post_type = get_post_type($post_id);
              if(\'business\' == $post_type){
              //fb($post_type);
                  $args = array(
                                          \'post_id\' => $post_id,
                                          \'status\' => \'approve\'
                              );
                  $comments = get_comments($args);
                  $total_ratings = 0;
                  $avg = 0;
                  foreach($comments as $comment){
                      $total_points += get_comment_meta($comment->comment_ID, \'rating\', true);    
                      $total_ratings++;
                  }
                  if($total_ratings > 0){
                      $avg = $total_points/$total_ratings;
                      $avg = (float)$avg;
                  }
                  $avg = roundToNearestFraction($avg,0.5);
                  update_post_meta($post_id, \'avg_rating\', $avg);
              }
          }
      

      Problem:

      <我仍然缺少一些东西,因为当管理员发布评论时,功能没有触发(它会自动获得批准)。

      检查功能update_business_rating_avg() 以及我如何使用get_comment_meta() 内部foreach。这会扼杀性能吗?有没有更好的方法?自定义SQL parhaps?

      感谢您的帮助,谢谢!

    SOLVED:最终代码

    感谢@Soulseekah将我送到了正确的方向。如果将来有人需要,这里是解决方案的最终代码。SQL是一个复杂的SQL,因为我必须将两者都连接起来wp_commentswp_commentmeta 比较已批准的评论和与当前帖子ID关联的评论。

    add_action(\'edit_comment\', \'update_business_rating_avg\');
    add_action(\'comment_post\', \'update_business_rating_avg\');
    add_action(\'comment_unapproved_to_approved\', \'update_business_rating_avg\');
    add_action(\'comment_approved_to_unapproved\', \'update_business_rating_avg\');
    add_action(\'comment_spam_to_approved\', \'update_business_rating_avg\');
    add_action(\'comment_approved_to_spam\', \'update_business_rating_avg\');
    add_action(\'comment_approved_to_trash\', \'update_business_rating_avg\');
    add_action(\'comment_trash_to_approved\', \'update_business_rating_avg\');
    
    function update_business_rating_avg($comment){
        if ( !is_object( $comment ) ){
            $comment = get_comment( $comment );
        }
        
        $post_id = $comment->comment_post_ID;
        $post_type = get_post_type($post_id);
        if(\'business\' == $post_type){
            $avg = 0;
            global $wpdb;
            $query = "SELECT AVG(meta_value) FROM $wpdb->commentmeta ";
            $query .= "INNER JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID " ;
            $query .= "WHERE $wpdb->commentmeta.meta_key = \'rating\' ";
            $query .= "AND $wpdb->comments.comment_approved = 1 ";
            $query .= "AND $wpdb->comments.comment_post_ID = \'$post_id\'";
            
            if( $result = $wpdb->get_var($wpdb->prepare($query)) ){
                $avg = roundToNearestFraction($result, 0.5);
                update_post_meta($post_id, \'avg_rating\', $avg);
            }else{
                // report error
                //$wpdb->print_error();
            }
        }
    }
    

    2 个回复
    最合适的回答,由SO网友:soulseekah 整理而成

    The comment_post hook is called with a $comment_id as the first argument. You can see why your function is failing. Same goes for the edit_comment hook.

    Simply add the following at the beginning of your business function:

    if ( !is_object( $comment ) )
        $comment = get_comment( $comment );
    

    Do use a custom SQL query for comment metadata retrieval, as right now, with what you have there, you\'ll end up querying the database for each and every comment. Moreover, I would additionally suggest using the built-in SUM and AVG MySQL functions to avoid the extra loops in PHP.

    SELECT SUM(`meta_value`), AVG(`meta_value`)
    FROM `{$wpdb->commentmeta}`
    WHERE `meta_key` = \'rating\'
    AND `comment_id` = \'$sanitized_comment_id\';
    
    SO网友:Jon Dingman

    我发现在if()语句中包含$result对我不起作用。

    if( $result = $wpdb->get_var($wpdb->prepare($query)) ){
    
    因此,如果您有相同的问题,请尝试以下方法:

    $result = $wpdb->get_var($wpdb->prepare($query));
    if( $result ){ ... }
    

    结束

    相关推荐

    Comments and pingbacks issues

    我已经想了很多很多天了。我多次尝试修改代码,总是得到相同的结果。我就是不明白。我想正确地分离pingback和注释,这是我使用以下代码所做的:<?php if ( have_comments() ) : ?> <h2 class=\"h2comments\"><img src=\"http://zoomingjapan.com/wp-content/themes/alltuts-child/images/comments_big.png\" /><