shortcodes-resources.php 6.24 KB
<?php 

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


function resources_list(){

  try{

    $custom_args = array(
        'post_type' => 'resource',
        'posts_per_page' => -1,
        'paged' => 1,
        'post_status' => 'any',
        'order' => 'DESC',
        'orderby' => 'rand',
    );
    $custom_query = new \WP_Query($custom_args);
   
    ob_start(); 
    $uniqid = uniqid();
      if ($custom_query->have_posts()):
    ?>
        <div class="course-list resources">
            <label for="quicksearch">Search programs:</label><input type="text" id="quicksearch" placeholder="" />
            <div class="filters">
            <a href="#" id="filter-more">FILTERS <span aria-hidden="true" class="glyphicon glyphicon-chevron-up"></span></a>
            <div class="filter-group">
                <div class="category-filter-group ">
                  <div class="category">
                  <?php $terms = get_terms( array( 'taxonomy' =>  'resource_category' ,'parent'   => 27) );
                        foreach($terms 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 class="type">
                      <?php $terms = get_terms( array( 'taxonomy' =>  'resource_category' ,'parent'   => 32) );
                        foreach($terms as $term){ ?>
                            <div class="category-type"> <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 class="btn-group sort-button-group"> 
                <label>SORT BY:</label>
                  <button class="btn btn-light atoz" data-sort-direction="asc" data-sort-value="1" type="button"><spam class="text">A to Z</span></button> 
                  <button class="btn btn-light" data-sort-direction="asc" data-sort-value="3" type="button">Date <span aria-hidden="true" class="glyphicon glyphicon-chevron-up"></span></button> 
                </div>
            </div>
            </div>
            <table id="resources">
            <thead><tr><th class="hidden"></th><th class="hidden"></th><th></th><th class="hidden"></th><th></th></tr></thead>
              <tbody>
                <?php while ($custom_query->have_posts()): $custom_query->the_post();  
                             echo resources(get_the_ID());
                      endwhile; ?>
              </tbody>          
            </table>
          </div>
          <div class="modal fade" id="login-window" tabindex="-1" aria-labelledby="login-window" aria-hidden="true" style="display: none;">
            <div class="modal-dialog modal-dialog-centered ">
              <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="btn-close align-self-sm-end" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>	
                <div class="modal-body d-flex justify-content-center flex-column">
                  <h2 class="modal-title align-self-center">You must be logged in to add resources to your dashboard</h2>
              <a title="LOG IN" href="my-account/" class="login-btn">Login now</a>
                <p class="align-self-center">Don’t have a login? <a title="Sign up" href="my-account/" class="">Sign up</a> for an account.<p>
                </div>
              </div>
            </div>
          </div>       
      <?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(){

  $favorites = get_user_favorites(get_current_user_id());

 
  ob_start(); 
  $uniqid = uniqid();

    if (is_array( $favorites)): ?>
         <div class="course-list resources">
        <table id="resources" class="resources-fav">
            <thead><tr><th class="hidden"></th><th class="hidden"></th><th></th><th class="hidden"></th><th></th></tr></thead>
              <tbody>
              <?php foreach($favorites as $fav){
                             echo resources($fav);
              }; ?>
              </tbody>    
            </table>
            </div>
  <?php endif; ?>
  <?php
  wp_reset_query();
  $output = ob_get_clean(); 
  return $output;
   }




function resources($id){

  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', 35 );
  $text = wp_trim_words( $text, $excerpt_length, ' ...' );
  $categories = get_the_terms( $id, 'resource_category' );
  $cat ="";  
  $type ="";
  if(is_array( $categories)){
    foreach( $categories as $category ) { 
      if($category->parent == 27){
      $cat .= " ".$category->slug; 
      }
      if($category->parent == 32){
        $type .= " ".$category->slug; 
      }
    };
  } 
  $target ="";
  $link = get_permalink($id);
  $resources_link = get_post_meta($id, 'resources_link', true);
  if($resources_link !="" ){
    $link = $resources_link;
    $target = "target='_blank'";
  }
  ?>

 <tr class="table-like__item <?php echo $type; ?>">
  <td class="hidden"> <?php echo $type; ?></td>
  <td class="item"><div class="item-content"><a class="r-link" role="button" <?php echo $target; ?> href="<?php echo $link; ?>"><?php echo $post->post_title; ?></a>
    <?php echo  $text; ?>
  </td> 
  <td class="hidden"> <?php echo $cat; ?></td>
  <td class="hidden"><?php echo get_post_time('U',false, $id ); ?></td>
  <td class="heart">
 <?php if(!is_user_logged_in()){ ?>
        <button class="simplefavorite-button" data-bs-toggle="modal" data-bs-target="#login-window" style="">Favorite <i class="sf-icon-star-empty"></i></button>
    <?php }else{
        echo do_shortcode('[favorite_button post_id="'.$id.'"]');
    }  ?>
</td>
</tr>
<?php
  $output = ob_get_clean();
  return $output;
}