Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
fix(latest): fixes latest filter
Browse files Browse the repository at this point in the history
fixes #101
  • Loading branch information
Eduardo Campaña committed Feb 5, 2019
1 parent 3da600c commit 64b90ad
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion includes/class-frontity-filter-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function filter($response, $post, $request)
{
$embed = is_string($request->get_param('_embed'));
$context = $request->get_param('context');

// Checks if the request is after data to populate a WP_REST_Response.
if ($context === 'view') {
$filters = array_filter(explode(',', $request->get_param('excludeFields')));
Expand Down
17 changes: 10 additions & 7 deletions includes/class-frontity-rest-api-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function latest_general_endpoint($request)
!empty($custom_post_types) ? $custom_post_types : get_post_types()
);

return $this->get_latest_from_cpt($custom_post_types);
return $this->get_latest_from_cpt($custom_post_types, $request);
}

// Return the data for /wp-json/wp/v2/latest/(?P<id>\w+).
Expand All @@ -192,11 +192,11 @@ function latest_individual_endpoint($request)
array($custom_post_type)
);

return $this->get_latest_from_cpt($custom_post_type);
return $this->get_latest_from_cpt($custom_post_type, $request);
}

// Get latest info of each custom post type.
private function get_latest_from_cpt($cpts)
private function get_latest_from_cpt($cpts, $request)
{
$result = array();
foreach ($cpts as &$cpt) {
Expand All @@ -210,16 +210,19 @@ private function get_latest_from_cpt($cpts)
} else {
$link = get_post_type_archive_link($cpt);
}
$data = array(

$latest = array(
"id" => $cpt,
"link" => $link,
"count" => intval(wp_count_posts($cpt)->publish),
"name" => $cpt_object->label,
"name" => ($cpt === 'post' ? get_bloginfo('name') : $cpt_object->label),
"slug" => $cpt_object->name,
"taxonomy" => 'latest'
);
if ($cpt === 'post') $data['name'] = get_bloginfo('name');
$result[] = apply_filters('rest_prepare_latest', $data);

$response = new WP_REST_Response($latest);
$request->set_param('context', 'view');
$result[] = apply_filters('rest_prepare_latest', $response, $latest, $request)->data;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion includes/class-frontity.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private function define_public_hooks()
$this->loader->add_filter('rest_prepare_comment', $frontity_filter_fields, 'filter', 20, 3);
$this->loader->add_filter('rest_prepare_taxonomy', $frontity_filter_fields, 'filter', 20, 3);
$this->loader->add_filter('rest_prepare_user', $frontity_filter_fields, 'filter', 20, 3);
// $this->loader->add_filter('rest_prepare_latest', $frontity_filter_fields, 'filter', 20, 3);
$this->loader->add_filter('rest_prepare_latest', $frontity_filter_fields, 'filter', 20, 3);

$frontity_purifier = new Frontity_Purifier();
$this->loader->add_action('registered_post_type', $frontity_purifier, 'add_post_type_filters');
Expand Down

0 comments on commit 64b90ad

Please sign in to comment.