Esempio n. 1
0
  public function posts($args = array(), $noget = array("tax_query")) {
    
    global $wf;
    
    $aa = wp_parse_args($args);
    
    $r = $this->parse_args( 
      $args, 
      array(
        "orderby" => "title",
        "order" => "asc",
        "limit" => $this->post_limit,
        "offset" => 0,
        "type" => "post"
      ),
      "#offset,#limit,#posts_per_page,order_by,orderby,order,type,@before,@after,status,post_status,#author,search,s,tag,category,tax_query,ep",
      array(
        "limit"   => "posts_per_page",
        "order_by" => "orderby",
        "type"    => "post_type",
        "status" => "post_status",
        "search" => "s"
      ),
      $noget
    );

    $ng = $this->to_array($noget);
    
    $type = $r["post_type"];
    
    // set more appropriate ordering defaults if no order was explicitly specified
    if (!isset($args["orderby"]) && !isset($args["order_by"]) && !isset($args["order"])) {
    
      if ($type == "post") {
        $r["orderby"] = "post_date";
        $r["order"] = "desc";
      } else if ($type == "page") {
        $r["orderby"] = "menu_order";
        $r["order"] = "asc";
      }
      
      // ... but also allow developers to override
      
      $r["orderby"] = apply_filters("mp_rest_archive_order_by", $r["orderby"], $type);
      $r["order"] = apply_filters("mp_rest_archive_order", $r["order"], $type);
      
    }
    
    $limit = $this->post_limit;
    
    if (isset($this->post_limits[$type])) {   
      $limit = $this->post_limits[$type];
    }
    
    $post_type = $wf->types($type);
    
    if ($r["posts_per_page"] != -1) {
      $use_limit = max(1, min($limit, $r["posts_per_page"]));
    } else {
      $use_limit = max(1, $limit);
    }
    
    $use_offset = $r["offset"];

    $r["posts_per_page"] = $use_limit;
    
    $page = floor( $use_offset / $use_limit ) + 1;
    
    if (isset($r["ep"])) {
      $ep = $r["ep"];
    } else {
      $ep = $this->endpoint_url( $post_type->rewrite_slug(true) );
    }
  
    unset($r["ep"]);
    
    $pr = $r;
    
    unset($pr["fields"]);
    
    $query = new WP_Query( $pr );

    
    $posts = $wf->wrap( $query->posts );
    
    $found = (int) $query->found_posts;
      
    $mod = floor( $found % $use_limit );

    $final_offset = ( $found ) - $mod;
    
    if ($mod == 0) {
      $final_offset = $final_offset - $use_limit;
    }
    
    $r = $this->remap_args($r, "posts_per_page=limit&post_type=type&post_status=status&s=search");
    
    
    // remove any keys that cannot be overridden with a $_GET
    
    // this is a sub data key
    $sub = WOOF::is_true_arg($aa, "sub");
   

    WOOF::array_remove_keys($r, $ng);
    
    $pages = ceil( $found / $use_limit );
    
    $data = array();
    
    if (!$sub) {
      $data["href"] = $wf->current_url();
    }
    
    $data["found"] = $found;
    $data["limit"] = $use_limit;
    $data["offset"] = $use_offset;
    $data["page"] = $page;
    $data["pages"] = $pages;
    $data["first"] = array( "href" => $ep . "?" . $this->build_query($r, "limit=$use_limit", "offset" ) );
    $data["previous"] = null;
    $data["next"] = null;
    $data["last"] = array( "href" => $ep . "?" . $this->build_query($r, "limit=$use_limit&offset=$final_offset" ) );
    
    if ($page != 1) {
      $data["previous"] = array( "href" => $ep . "?" . $this->build_query($r, "limit=$use_limit&offset=" . ( ( $page - 2 ) * $use_limit ) ) );
    }
    
    if ($found > ($use_offset + $use_limit)) {
      $data["next"] = array( "href" => $ep. "?" . $this->build_query($r, "limit=$use_limit&offset=" . ($page * $use_limit ) ) );
    }
  
    
    $fields = apply_filters( "mp_rest_archive_fields", "", $type );
    
    if ($expand = $wf->has_get("expand")) {
      $expand_fields = $wf->parse_field_list($expand);
      
      if (isset($expand_fields["posts"])) {
        
        $def = MEOW_Post::json_fields();
        
        if (is_array($expand_fields["posts"])) {
          $def = $expand_fields["posts"];
        }
        
        $fields = apply_filters( "mp_rest_single_fields", $def, $type );
      }
      
    }
    
    
    if ($wf->has_get("fields")) {
      $fields = $wf->filter_field_list( apply_filters( "mp_rest_archive_fields_allowed", $fields, $type ) );
    }
  
    // need to allow query params here, and default limits etc

    $data["posts"] = array();

    foreach ( $posts as $post ) {
      $data["posts"][] = apply_filters("mp_rest_archive_json", $post->json($fields), $post );
    }
      
    $json = apply_filters("mp_rest_archive_json", $data, $type );

    return $data;

  }