shortcodes-resources.php 7.8 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',
        'posts_per_page' => -1,
        'paged' => 1,
        'post_status' => 'any',
        'order' => 'DESC',
        'orderby' => 'rand',
        'tax_query' => $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();  
                          $is_fav = in_array(get_the_ID(), $favs);
                          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(); 

    if (is_array( $favs)): ?>
         <div class="course-list resources">
          <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>
            </div>
  <?php endif;
  wp_reset_query();
  $output = ob_get_clean(); 
  return $output;
   }




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);

  $categories = [];
  foreach($taxes as $tax => $val) {
    $terms = get_the_terms( $id, $tax );
    if(is_array($terms)) {
      $categories = array_merge($categories, $terms);
    }
  }

  $cat ="";  
  if(is_array( $categories)){
    foreach( $categories as $category ) { 
      if($category->parent == $tax){
      $cat .= " ".$category->slug; 
      }
    };
  } ?>

 <tr class="table-like__item">
  <td class="item">
    <a target="_blank" role="button" class="item-link" href="<?php echo wp_get_attachment_url($id); ?>"><?php echo $post->post_title; ?></a>
    <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"><?php echo get_post_time('U',false, $id ); ?></td>
</tr>
<?php
  $output = ob_get_clean();
  return $output;
}