覆盖PHP if语句

时间:2017-06-19 20:51:26

标签: php wordpress if-statement overwrite

我的公司正在使用WordPress插件CQPIM开发CRM。 它有两个问题,但我现在只讨论一个问题。

该插件的功能取决于用户角色,并将为具有该功能的任何人显示所有活动项目: cqpim_view_all_projects

我们需要重写以下代码段,以便仪表板仅显示分配给当前用户的项目。

原始代码:没有要修改的过滤器的钩子

if(current_user_can('cqpim_view_all_projects')) {
  $index++; ?>
  <li>
    <script>
      jQuery(document).ready(function() {
        jQuery("#progressbar-<?php echo $project->ID; ?>").progressbar({
          value: <?php echo number_format((float)$pc_complete, 2, '.', ''); ?>
        });
      });
    </script>
    <div class="title">
      <a href="<?php echo $edit_url; ?>"><?php if(!empty($project->post_title)) { echo $project->post_title; } else { _e('Untitled', 'cqpim'); } ?></a>
      <?php echo $project_status; ?>
    </div>
    <div class="progress">
      <div id="progressbar-<?php echo $project->ID; ?>"></div>
    </div>
    <ul class="project_stats">
      <li><span class="project_stat_head"><?php _e('Open Tasks' , 'cqpim') ?></span><span class="project_stat"><?php echo $task_count; ?></span></li>
      <li><span class="project_stat_head"><?php _e('Complete' , 'cqpim') ?></span><span class="project_stat"><?php echo number_format((float)$pc_complete, 2, '.', ''); ?>%</span></li>
      <li><span class="project_stat_head"><?php _e('Deadline' , 'cqpim') ?></span><span class="project_stat"><?php echo $finish_date; ?></span></li>
    </ul>
  </li>
<?php
} else {
  $access = false;
  $project_contributors = get_post_meta($project->ID, 'project_contributors', true);
  if(empty($project_contributors)) {
    $project_contributors = array();
  }
  foreach($project_contributors as $contributor) {
    if(!empty($contributor['team_id']) && $assigned == $contributor['team_id']) {
      $access = true;
    }
  }
  if($access == true) {
    $index++; ?>
    <li>
      <script>
        jQuery(document).ready(function() {
          jQuery("#progressbar-<?php echo $project->ID; ?>").progressbar({
            value: <?php echo number_format((float)$pc_complete, 2, '.', ''); ?>
          });
        });
      </script>
      <div class="title">
        <a href="<?php echo $edit_url; ?>"><?php if(!empty($project->post_title)) { echo $project->post_title; } else { _e('Untitled', 'cqpim'); } ?></a>
        <?php echo $project_status; ?>
      </div>
      <div class="progress">
        <div id="progressbar-<?php echo $project->ID; ?>"></div>
      </div>
      <ul class="project_stats">
        <li><span class="project_stat_head"><?php _e('Open Tasks', 'cqpim'); ?></span><span class="project_stat"><?php echo $task_count; ?></span></li>
        <li><span class="project_stat_head"><?php _e('Complete' , 'cqpim') ?></span><span class="project_stat"><?php echo number_format((float)$pc_complete, 2, '.', ''); ?>%</span></li>
        <li><span class="project_stat_head"><?php _e('Deadline' , 'cqpim') ?></span><span class="project_stat"><?php echo $finish_date; ?></span></li>
      </ul>
    </li>
    <?php
  }
}

我修改过的代码:它在插件根目录下有效,但我想在某些时候更新此插件而不会丢失所有文件,因此需要一种方法来覆盖原始的IF语句 < / p>

if(current_user_can('cqpim_view_all_projects')) {
  $index++; 
  $access = false;
  $project_contributors = get_post_meta($project->ID, 'project_contributors', true);
  if(empty($project_contributors)) {                                    
    $project_contributors = array();
  }
  foreach($project_contributors as $contributor) {
    if(!empty($contributor['team_id']) && $assigned == $contributor['team_id']) {
      $access = true;
    }
  }
  if($access == true) {
    $index++; ?>
  <li>
    <script>
      jQuery(document).ready(function() {
        jQuery("#progressbar-<?php echo $project->ID; ?>").progressbar({
          value: <?php echo number_format((float)$pc_complete, 2, '.', ''); ?>
        });
      });
    </script>
    <div class="title">
      <a href="<?php echo $edit_url; ?>"><?php if(!empty($project->post_title)) { echo $project->post_title; } else { _e('Untitled', 'cqpim'); } ?></a>
      <?php echo $project_status; ?>
    </div>
    <div class="progress">
      <div id="progressbar-<?php echo $project->ID; ?>"></div>
    </div>
    <ul class="project_stats">
      <li><span class="project_stat_head"><?php _e('Open Tasks', 'cqpim'); ?></span><span class="project_stat"><?php echo $task_count; ?></span></li>
      <li><span class="project_stat_head"><?php _e('Complete' , 'cqpim') ?></span><span class="project_stat"><?php echo number_format((float)$pc_complete, 2, '.', ''); ?>%</span></li>
      <li><span class="project_stat_head"><?php _e('Deadline' , 'cqpim') ?></span><span class="project_stat"><?php echo $finish_date; ?></span></li>
    </ul>
  </li>
  <?php
  }
}

1 个答案:

答案 0 :(得分:1)

如果此插件没有挂钩或操作,请检查插件是否使用了get_template_part / locate_template函数或类似的函数。一个例子是woocommerce,它使用actions / filters和locate_template,因此您可以挂钩或覆盖主题文件夹中的模板。

您是否尝试过询问开发人员?该插件已付款,因此您可以在那里获得某种支持。要求他们包含钩子或在你自己的主题中否决模板的能力。