rest.php 4.31 KB
<?php

function pull_posts_for_post_list( $data ) {

  $request_uri = sanitize_text_field($_SERVER['REQUEST_URI']);
  $request_uri = base64_encode($request_uri);

  // $BYPASS_CACHE = false;
  // $client = new Predis\Client();

    //  if(!$BYPASS_CACHE) {
    //       if($client->exists($request_uri)) {
    //           return unserialize($client->get($request_uri));
    //       }
    //   }

    $categories = sanitize_textarea_field($_REQUEST['categories']);
    $segment = trim(sanitize_textarea_field($_REQUEST['segment']));

    $exclude = get_for_excluded_cats();
    $exclude_posts = get_global_excluded_posts();
    
    if($segment == 'all') {

      $new_posts = [];

      $numberposts = 500;

      if(isset($_REQUEST['limit'])) {
        $numberposts = $_REQUEST['limit'];
      }

      if(!empty($categories)) {
        $posts = get_posts( array(
          'category' => $categories,
          'numberposts' => $numberposts,
          'order' => 'DESC',
          'suppress_filters' => false
        ) );
      } else {
        $posts = get_posts( array(
          'numberposts' => $numberposts,
          'order' => 'DESC',
          'suppress_filters' => false,
          'category_not_in'=>$exclude,
          'exclude' => $exclude_posts,
        ) );
      }

      foreach($posts as $post) {

        $excerpt = wp_filter_nohtml_kses(get_the_excerpt($post));
        $excerpt = trim(substr($excerpt, 0, strpos($excerpt, '[...]')));
        $article_author = get_field('article_author', $post->ID) ?: null;
        $author_title = get_field('author_title', $post->ID) ?: null;
        $author_company = get_field('author_company', $post->ID) ?: null;
  
        $img = get_the_post_thumbnail_url($post, 'medium');
  
        if(!$img) {
          $img = get_post_meta($post->ID, 'photo_from_source', true);
        }

        $new_posts[] = ['date'=>wp_date( ' F j, Y', get_post_timestamp($post->ID)),
          'article_author'=>$article_author,'author_title'=>$author_title,
          'author_company'=>$author_company,'href'=>get_permalink($post),'title'=>$post->post_title,
          'text'=>$excerpt,'img_src'=>$img];
  
      }

      // $client->set($request_uri, serialize(['posts'=>$new_posts,'last_index'=>0]), 'ex', 3600);
  
      return ['posts'=>$new_posts,'last_index'=>0];

    } else {

      $segment = (int)$segment;

      if(!empty($categories)) {
        $posts = get_posts( array(
          'category' => $categories,
          'numberposts' => 12,
          'order' => 'DESC',
          'suppress_filters' => false
        ) );
      } else {
        $posts = get_posts( array(
          'numberposts' => 12,
          'order' => 'DESC',
          'suppress_filters' => false,
          'category__not_in'=>$exclude,
          'exclude' => $exclude_posts
        ) );
      }


      if ( empty( $posts ) ) {
        return null;
      }
  
      $segments = array_chunk($posts, 4);
  
      $the_segment = $segments[$segment];
      $posts = [];
  
      foreach($the_segment as $post) {
        
        $excerpt = wp_filter_nohtml_kses(get_the_excerpt($post));
        //$excerpt = trim(substr($excerpt, 0, strpos($excerpt, '[...]')));
        $article_author = get_field('article_author', $post->ID) ?: null;
        $author_title = get_field('author_title', $post->ID) ?: null;
        $author_company = get_field('author_company', $post->ID) ?: null;
  
        $img = get_the_post_thumbnail_url($post, 'medium_large');
  
        if(!$img) {
          $img = get_post_meta($post->ID, 'photo_from_source', true);
        }
        $posts[] = ['date'=>wp_date( ' F j, Y', get_post_timestamp($post->ID)),
          'article_author'=>$article_author,'author_title'=>$author_title,
          'author_company'=>$author_company,'href'=>get_permalink($post),'title'=>$post->post_title,
          'text'=>$excerpt,'img_src'=>$img];
  
      }
  
      $last_index = false;
      if( $segment >= (count($segments)-1) ) {
        $last_index = true;
      }

      // $client->set($request_uri, serialize(['posts'=>$posts,'last_index'=>$last_index]), 'ex', 3600);
  
      return ['posts'=>$posts,'last_index'=>$last_index];

    }

}

add_action( 'rest_api_init', function () {
    register_rest_route( '/v1', '/post_list', array(
      'methods' => 'GET',
      'callback' => 'pull_posts_for_post_list',
      'permission_callback' => '__return_true'
    ) );
} );