主题自定义评级,重构代码和清理模板文件的正确方法

时间:2017-07-13 作者:Threaded

在查看了许多收视率插件之后,我决定让事情更简单一些,只需使用ACF和自定义帖子类型来构建自己的收视率插件。到目前为止,一切都按预期进行,但我觉得我的代码可以更加干净和有条理。

我使用根;我已经创建了一个自定义的帖子类型模板,并且正在对我的评分字段(这是一个浮点;例如4.5)进行preg\\u替换,以删除小数,这样我就可以轻松地回显css类中的原始数字,如下所示:

 $ratings_overall = get_field(\'provider_ratings_overall\');
 $ratings_round_overall = preg_replace("/[^0-9]/", \'\', $ratings_overall);

 echo \'<div class="class-name-\' . $ratings_round_overall . \'">
我的问题是,这对我来说并不干净,我的函数文件现在有一个长得离谱的函数字符串,其中包含if/elseif语句,以便填充分级(这些存储在每个分级的模板中,其中只包含我需要在页面上显示星星的html)。

<div class="rating-stars">
  <span class="five-stars">
    <span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>
    <span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>
    <span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>
    <span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>
    <span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>
  </span>
</div>
我的脏函数文件:

    /**
    * Functions for the Review post type
    */
    function rating_overall() {

      $ratings_overall = get_field(\'provider_ratings_overall\');

      if ($ratings_overall == 5.0) {
        echo get_template_part(\'templates/star-rating/five-star-rating\');
      }
      elseif ($ratings_overall == 4.5) {
        echo get_template_part(\'templates/star-rating/four-half-star-rating\');
      }
      elseif ($ratings_overall == 4.0) {
        echo get_template_part(\'templates/star-rating/four-star-rating\');
      }
      elseif ($ratings_overall == 3.5) {
        echo get_template_part(\'templates/star-rating/three-half-star-rating\');
      }
      elseif ($ratings_overall == 3.0) {
        echo get_template_part(\'templates/star-rating/three-star-rating\');
      }
      elseif ($ratings_overall == 2.5) {
        echo get_template_part(\'templates/star-rating/two-half-star-rating\');
      }
      elseif ($ratings_overall == 2.0) {
        echo get_template_part(\'templates/star-rating/two-star-rating\');
      }
      elseif ($ratings_overall == 1.5) {
        echo get_template_part(\'templates/star-rating/one-half-star-rating\');
      }
      elseif ($ratings_overall == 1.0) {
        echo get_template_part(\'templates/star-rating/one-star-rating\');
      }
      elseif ($ratings_overall  == 0.5) {
        echo get_template_part(\'templates/star-rating/half-star-rating\');
      }
      elseif ($ratings_overall == \'\') {
        echo get_template_part(\'templates/star-rating/no-star-rating\');
      }

    }
    function rating_marketing() {

      $ratings_marketing = get_field(\'provider_ratings_marketing\');

      if ($ratings_marketing == 5.0) {
        echo get_template_part(\'templates/star-rating/five-star-rating\');
      }
      elseif ($ratings_marketing == 4.5) {
        echo get_template_part(\'templates/star-rating/four-half-star-rating\');
      }
      elseif ($ratings_marketing == 4.0) {
        echo get_template_part(\'templates/star-rating/four-star-rating\');
      }
      elseif ($ratings_marketing == 3.5) {
        echo get_template_part(\'templates/star-rating/three-half-star-rating\');
      }
      elseif ($ratings_marketing == 3.0) {
        echo get_template_part(\'templates/star-rating/three-star-rating\');
      }
      elseif ($ratings_marketing == 2.5) {
        echo get_template_part(\'templates/star-rating/two-half-star-rating\');
      }
      elseif ($ratings_marketing == 2.0) {
        echo get_template_part(\'templates/star-rating/two-star-rating\');
      }
      elseif ($ratings_marketing == 1.5) {
        echo get_template_part(\'templates/star-rating/one-half-star-rating\');
      }
      elseif ($ratings_marketing == 1.0) {
        echo get_template_part(\'templates/star-rating/one-star-rating\');
      }
      elseif ($ratings_marketing  == 0.5) {
        echo get_template_part(\'templates/star-rating/half-star-rating\');
      }
      elseif ($ratings_marketing == \'\') {
        echo get_template_part(\'templates/star-rating/no-star-rating\');
      }

    }
    function rating_sales() {

      $ratings_sales = get_field(\'provider_ratings_sales\');

      if ($ratings_sales == 5.0) {
        echo get_template_part(\'templates/star-rating/five-star-rating\');
      }
      elseif ($ratings_sales == 4.5) {
        echo get_template_part(\'templates/star-rating/four-half-star-rating\');
      }
      elseif ($ratings_sales == 4.0) {
        echo get_template_part(\'templates/star-rating/four-star-rating\');
      }
      elseif ($ratings_sales == 3.5) {
        echo get_template_part(\'templates/star-rating/three-half-star-rating\');
      }
      elseif ($ratings_sales == 3.0) {
        echo get_template_part(\'templates/star-rating/three-star-rating\');
      }
      elseif ($ratings_sales == 2.5) {
        echo get_template_part(\'templates/star-rating/two-half-star-rating\');
      }
      elseif ($ratings_sales == 2.0) {
        echo get_template_part(\'templates/star-rating/two-star-rating\');
      }
      elseif ($ratings_sales == 1.5) {
        echo get_template_part(\'templates/star-rating/one-half-star-rating\');
      }
      elseif ($ratings_sales == 1.0) {
        echo get_template_part(\'templates/star-rating/one-star-rating\');
      }
      elseif ($ratings_sales  == 0.5) {
        echo get_template_part(\'templates/star-rating/half-star-rating\');
      }
      elseif ($ratings_sales == \'\') {
        echo get_template_part(\'templates/star-rating/no-star-rating\');
      }

    }
    function rating_support() {

      $ratings_support = get_field(\'provider_ratings_support\');

      if ($ratings_support == 5.0) {
        echo get_template_part(\'templates/star-rating/five-star-rating\');
      }
      elseif ($ratings_support == 4.5) {
        echo get_template_part(\'templates/star-rating/four-half-star-rating\');
      }
      elseif ($ratings_support == 4.0) {
        echo get_template_part(\'templates/star-rating/four-star-rating\');
      }
      elseif ($ratings_support == 3.5) {
        echo get_template_part(\'templates/star-rating/three-half-star-rating\');
      }
      elseif ($ratings_support == 3.0) {
        echo get_template_part(\'templates/star-rating/three-star-rating\');
      }
      elseif ($ratings_support == 2.5) {
        echo get_template_part(\'templates/star-rating/two-half-star-rating\');
      }
      elseif ($ratings_support == 2.0) {
        echo get_template_part(\'templates/star-rating/two-star-rating\');
      }
      elseif ($ratings_support == 1.5) {
        echo get_template_part(\'templates/star-rating/one-half-star-rating\');
      }
      elseif ($ratings_support == 1.0) {
        echo get_template_part(\'templates/star-rating/one-star-rating\');
      }
      elseif ($ratings_support  == 0.5) {
        echo get_template_part(\'templates/star-rating/half-star-rating\');
      }
      elseif ($ratings_support == \'\') {
        echo get_template_part(\'templates/star-rating/no-star-rating\');
      }

    }
    function rating_pricing() {

      $ratings_pricing = get_field(\'provider_ratings_pricing\');

      if ($ratings_pricing == 5.0) {
        echo get_template_part(\'templates/star-rating/five-star-rating\');
      }
      elseif ($ratings_pricing == 4.5) {
        echo get_template_part(\'templates/star-rating/four-half-star-rating\');
      }
      elseif ($ratings_pricing == 4.0) {
        echo get_template_part(\'templates/star-rating/four-star-rating\');
      }
      elseif ($ratings_pricing == 3.5) {
        echo get_template_part(\'templates/star-rating/three-half-star-rating\');
      }
      elseif ($ratings_pricing == 3.0) {
        echo get_template_part(\'templates/star-rating/three-star-rating\');
      }
      elseif ($ratings_pricing == 2.5) {
        echo get_template_part(\'templates/star-rating/two-half-star-rating\');
      }
      elseif ($ratings_pricing == 2.0) {
        echo get_template_part(\'templates/star-rating/two-star-rating\');
      }
      elseif ($ratings_pricing == 1.5) {
        echo get_template_part(\'templates/star-rating/one-half-star-rating\');
      }
      elseif ($ratings_pricing == 1.0) {
        echo get_template_part(\'templates/star-rating/one-star-rating\');
      }
      elseif ($ratings_pricing  == 0.5) {
        echo get_template_part(\'templates/star-rating/half-star-rating\');
      }
      elseif ($ratings_pricing == \'\') {
        echo get_template_part(\'templates/star-rating/no-star-rating\');
      }

    }
然后,我会在我的模板中调用这些内容,无论我在哪里需要它们:

rating_overall();
rating_marketing();
rating_sales();
rating_support();
rating_pricing();
我正试图尽可能地解决这个问题,所以我想这更像是一个PHP重构问题,但我也觉得在Wordpress中,或者使用一些Wordpress编码标准,可能有更好的方法来实现这一点?

任何帮助都将不胜感激。

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

始终尝试保留您的代码DRY. 您的代码基本上严重违反了这一原则。为了重构它,让我们考虑一下代码所经历的步骤

确定是否是整体、营销、销售

  • 从db获取这些评级
  • 将评级转换为模板名称
  • 获取模板
    • 所有函数之间的唯一区别是第一部分。所以这是每个函数应该做的唯一事情。由于字段名都有相同的前缀,因此也可以使用该前缀。

      function rating_helper($name) {
          $ratings = get_field(\'provider_ratings_\' . $name);
          // template stuff
      }
      
      function rating_overall() {
          rating_helper(\'overall\');
      }
      function rating_marketing() {
          rating_helper(\'marketing\');
      }
      // ...
      
      下一件事是if/else. 模板名称中的点没有限制,因此无需将数字转换为名称。如果你把它们命名为templates templates/star rating/rating-4.5。php,评级-3.0。php。。。和评级。php(适用于== \'\'), 然后简单地将它们包括在内

      function rating_helper($name) {
          $ratings = get_field(\'provider_ratings_\' . $name);
          echo get_template_part(\'templates/star-rating/rating\', $ratings);
      }
      
      我使用的是get_template_part() 因为。。WordPress提供了它,并为您进行字符串连接,因此在我看来这有点干净。

      如果模板本身唯一改变的是CSS类/显示的星星数量,那么您根本不需要单独的模板!您可以使用$ratings 变量来自rating_helper() 作用

      function rating_helper($name) {
          $ratings = get_field(\'provider_ratings_\' . $name);
          echo get_template_part(\'templates/star-rating/rating\');
      }
      
      和模板/星级/评级。php(preg_replace() 不一定不好,但有点过头了)

      <?php
      if ($ratings != \'\') {
          $css_class = substr($ratings, 0, 1) . substr($ratings, 2, 1);
      } else {
          $css_class = \'0\';
      }
      echo \'<div class="class-name-\' . $css_class . \'">
      
      如果需要显示不同数量的星星,可以使用floor() 必要时加半颗星

      $stars = floor($ratings);
      foreach ($i=0; $i<$stars; $i++) {
          echo \'<span class="star"><i class="nc-icon nc-ic_star_24px"></i></span>\';
      }
      // floor(x.0) == x.0
      // floor(x.5) != x.5
      if ($stars != $ratings) {
          // echo half star
      }
      

    结束