shortcodes-resources.php 9.57 KB
<?php 

add_shortcode('resources-list', 'resources_list');

add_action( 'rest_api_init', function () {
  register_rest_route( 'tz/v1', '/rm_favourite', array(
    'methods' => 'GET',
    'callback' => 'favourite_list_item_rm',
  ) );
} );

function favourite_list_item_rm($request) {

  $user_id = get_current_user_id();

  if($user_id && $request['post_id']) {

    $favs = get_user_meta($user_id,'fav_resources',true);

    $fav_idx = array_search($request['post_id'], $favs);

    if($fav_idx !== false) {
      unset($favs[$fav_idx]);
      $response = update_user_meta($user_id,'fav_resources',$favs);
      return new WP_REST_Response(['status'=>200,'response'=>$response,'response_body'=>'rm']);
    }


  }

}

add_action( 'rest_api_init', function () {
  register_rest_route( 'tz/v1', '/favourite', array(
    'methods' => 'GET',
    'callback' => 'favourite_list_item',
  ) );
} );

function favourite_list_item($request) {

  $the_attachment = get_post($request['post_id']);
  $user_id = get_current_user_id();

  if($user_id && $the_attachment) {

    $favs = get_user_meta($user_id,'fav_resources',true);

    if($favs) {
      $favs[] = $request['post_id'];
    } else {
      $favs = [$request['post_id']];
    }

    $response = update_user_meta($user_id,'fav_resources',$favs);

    return new WP_REST_Response(['status'=>200,'response'=>$response,'response_body'=>'add']);


  }

}

function resources_list($atts){

  $tax_queries = [];
  $taxes = explode(',', $atts['tax']);
  $organized_term_filter = [];

  foreach($taxes as $tax) {

    $all_the_terms = get_terms( ['taxonomy'=>$tax, 'hide_empty' => false] ); 

    //Used to keep track of the filters to sue
    $organized_term_filter[$tax] = $all_the_terms;

    $term_ids = wp_list_pluck( $all_the_terms, 'term_id' );

    $tax_queries[] = ['taxonomy' => $tax,
    'field' => 'term_id',
    'terms' => $term_ids];

  }

  try{

    $custom_args = array(
        'post_type' => ['attachment','video'],
        'posts_per_page' => -1,
        'paged' => 1,
        'post_status' => 'any',
        'order' => 'DESC',
        'orderby' => 'rand',
        'tax_query' => ['relation' => 'OR', $tax_queries],
    );

    $custom_query = new \WP_Query($custom_args);

    $favs = get_user_meta(get_current_user_id(),'fav_resources',true);

    ob_start(); 

      if ($custom_query->have_posts()):
    ?>
        <div class="course-list resources hidden">
            <div class='quicksearch-wrapper'>
              <label for="quicksearch">Search:</label>
              <div class='qs-input-wrapper'>
                <input type="text" id="quicksearch" placeholder="Search Keyword..." />
              </div>
            </div>
            <div class="filters">
              <div class='search-sort'>
                <h3><?= __("SORT BY") ?></h3>
                <div class='custom-select'>
                  <select class='resource-list-sort'>
                    <option >Select</option>
                    <option  data-sort-direction="desc" data-sort-value="0">A to Z</option>
                    <option  data-sort-direction="asc" data-sort-value="0">Z to A</option>
                    <option  data-sort-direction="asc" data-sort-value="2">Newest to Oldest</option>
                    <option  data-sort-direction="desc" data-sort-value="2">Oldest to Newest</option>
                  </select>
                </div>
              </div>
              <div class="filter-group clear-filters">
                <div class="category-filter-group ">
                  <span class=''>FILTERS BY:</span>
                  <div class="category">
                      <a href='#' class='clear-all-link hide'>CLEAR ALL</a>
                  </div>
                  </div>
                </div>
            <?php foreach($organized_term_filter as $key => $val){ 
                  $tax = get_taxonomy($key);
              ?> 
              <div class="filter-group">
                <div class="category-filter-group ">
                  <span class=''><?= $tax->labels->name ?></span>
                  <div class="category">
                      <?php
                        foreach($val as $term) { ?>
                            <div class="category-filter"> <input id="<?php echo $term->slug ; ?>" value="<?php echo $term->slug ; ?>" type="checkbox" class="sr-only"><label for="<?php echo $term->slug ; ?>"><?php echo $term->name ; ?></label></div>
                      <?php }; ?>
                  </div>
                  </div>
                </div>
              <?php } ?>
             
            </div>
          </div>
            <table id="resources" class='hidden'>
            <thead><tr><th class="hidden"></th><th></th><th class="hidden"></th></tr></thead>
              <tbody>
                <?php while ($custom_query->have_posts()):
                        $custom_query->the_post();  
                        if(is_array($favs)) {
                          $is_fav = in_array(get_the_ID(), $favs);
                        } else {
                          $is_fav = false;
                        }
                          echo resources(get_the_ID(), $organized_term_filter, $is_fav);
                      endwhile; ?>
              </tbody>          
            </table>

      <?php endif;
        wp_reset_query();
        $output = ob_get_clean(); 
        return $output;

    }catch(Throwable $e) { 
      error_log("resources_list()". $e->getMessage()) ;
    }

}

add_shortcode('resources-fav', 'resources_fav');

function resources_fav(){

  $favs = get_user_meta(get_current_user_id(),'fav_resources',true);

 
  ob_start(); 

    ?>
         <div class="course-list resources">
          <?php if(is_array( $favs) && count($favs) > 0) { ?>
          <table id="resources" class="resources-fav">
            <thead><tr><th class="hidden"></th><th class="hidden"></th><th></th></tr></thead>
              <tbody>
              <?php foreach($favs as $fav){
                      echo resources($fav, null, true);
              }; ?>
              </tbody>    
            </table>
            <?php } else { ?>
              <span class='fav-border-top'></span>
                <p><?= _("Your Favourites List is where you can add the manuals and forms that you use most. To start adding to your list, simply tap on the heart beside any resource listed in the Manuals and Forms page.") ?></p>
            <?php } ?>
            </div>
  <?php 
  wp_reset_query();
  $output = ob_get_clean(); 
  return $output;
   }


  function getIsExternLink($link) {
  if (strpos($link,'brokers') === -1 && strpos($link,'broker') === -1 
    && strpos($link,'localhost') === -1 && strpos($link,'mailto:') === -1 && strpos($link,'tel:') === -1 && strpos($link,'#') !== 0) {
    if ($link !== "/" && $link !== "#" && strpos($link,'/') !== 0) {
      return true;
    }
  }
}

function getFileTypeClass($filename) {
  
  if (strpos($filename,'.pdf') !== false) {
    return('pdf');
  } else if (strpos($filename,'.xls') !== false) {
    return('xls');
  } else if (strpos($filename,'mailto:') !== false) {
    return('mailto');
  } else if (strpos($filename,'tel:') !== false) {
    return('tel');
  } else if (getIsExternLink($filename)) {
    return('extern');
  }
  
}

function resources($id, $taxes, $is_fav = false){

  ob_start(); 
  $post = get_post($id);
  $text = str_replace(']]>', ']]&gt;', apply_filters( 'the_content', strip_shortcodes($post->post_content)));
  $excerpt_length = apply_filters( 'excerpt_length', 20 );
  $text = wp_trim_words( $text, $excerpt_length, ' ...' );

  $updated_at = get_field('updated_at', $post->ID);
  $is_new = get_field('is_new', $post->ID);

  $post_time = get_post_time('U',false, $id );
  $custom_post_time = get_field('custom_upload_time', $id);
  if($custom_post_time) {
    $post_time = strtotime($custom_post_time);
  }

  $categories = [];
  foreach($taxes as $tax => $val) {
    $terms = get_the_terms( $id, $tax );
    if(is_array($terms)) {
      $categories = array_merge($categories, $terms);
    }
  }
// var_dump($categories);exit;
  $cat ="";  
  if(is_array( $categories)){
    foreach( $categories as $category ) { 
      // if($category->parent == $tax){
        // var_dump($category);exit;
      $cat .= " ".$category->slug; 
      // }
    };
  } ?>

 <tr class="table-like__item">
  <td class="item">
    <?php if(get_post_type($id) == 'video') { ?>
      <a target="_blank" role="button" class="item-link video" href="<?php echo get_field('video_url', $id); ?>"><?php echo $post->post_title; ?></a>  
    <?php } else { ?>
      <a target="_blank" role="button" class="item-link <?= getFileTypeClass(wp_get_attachment_url($id)) ?>" href="<?php echo wp_get_attachment_url($id); ?>"><?php echo $post->post_title; ?></a>
    <?php } ?>
    <span class='updated-at'>
      <?php if($is_new && !empty($updated_at)) { ?>
        <b>NEW:</b>  <?= date('M d/Y', strtotime($updated_at)) ?>
      <?php } else if(!empty($updated_at)) { ?>
          Updated: <?= date('M d/Y', strtotime($updated_at)) ?>
      <?php } ?>
    </span>
    <?php if($is_fav) { ?>
      <a data-other-request="/wp-json/tz/v1/favourite/?post_id=<?= $id."&_wpnonce=".wp_create_nonce( 'wp_rest' ) ?>" href='/wp-json/tz/v1/rm_favourite/?post_id=<?= $id."&_wpnonce=".wp_create_nonce( 'wp_rest' ) ?>' class='favourite fav'></a>
    <?php } else { ?>
      <a data-other-request="/wp-json/tz/v1/rm_favourite/?post_id=<?= $id."&_wpnonce=".wp_create_nonce( 'wp_rest' ) ?>" href='/wp-json/tz/v1/favourite/?post_id=<?= $id."&_wpnonce=".wp_create_nonce( 'wp_rest' ) ?>' class='favourite'></a>
    <?php } ?>
  </td> 
  <td class="hidden"> <?php echo $cat; ?></td>
  <td class="hidden"><?= $post_time ?></td>
</tr>
<?php
  $output = ob_get_clean();
  return $output;
}