Skip to content

Commit

Permalink
Merge pull request #2074 from CityOfPhiladelphia/blog-endpoints
Browse files Browse the repository at this point in the history
feat: updated endpoints for posts
  • Loading branch information
m-atia authored Jun 14, 2024
2 parents 11b64ca + d0dea33 commit ff954c1
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 18 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public function __construct() {
$this->namespace = 'the-latest/v1';
$this->resource_name = 'archives';
$this->category_resource = 'categories';
$this->tag_resource = 'tags';
}

// Register our routes.
Expand Down Expand Up @@ -40,6 +41,15 @@ public function register_routes() {
),
'schema' => array( $this, 'get_category_schema' ),
) );

register_rest_route( $this->namespace, '/' . $this->tag_resource, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_tags' ),
'permission_callback' => '__return_true',
),
'schema' => array( $this, 'get_tags_schema' ),
) );
}

public function set_query_defaults($request){
Expand Down Expand Up @@ -246,11 +256,33 @@ public function get_items( $request ) {
}

/**
* Outputs category data
* Outputs tags data
*
* @param WP_REST_Request $request Current request.
*/
public function get_categories( $request ){
public function get_tags( $request ){

$tags = get_tags(array(
'hide_empty' => false
));

$data = array();

if ( empty( $tags ) ) {
return rest_ensure_response( $array() );
}

foreach ( $tags as $tag ) {
$response = $this->prepare_tag_for_response( $tag, $request );

$data[] = $this ->prepare_response_for_collection( $response );
}

return rest_ensure_response( $data );

}

public function get_categories( $request ){

$categories = get_categories( array( 'parent' => 0 ) );

Expand Down Expand Up @@ -503,6 +535,27 @@ public function prepare_category_for_response( $category, $request ) {

}

public function prepare_tag_for_response ( $tag, $request ) {

$post_data = array();

$schema = $this-> get_tags_schema( $request );

if ( isset( $schema['properties']['id'] ) ) {
$post_data['id'] = (int) $tag->term_id;
}

if (isset( $schema['properties']['name'] )) {
$post_data['name'] = (string) html_entity_decode($tag->name);
}

if (isset( $schema['properties']['slug'] )) {
$post_data['slug'] = (string) $tag->slug;
}

return rest_ensure_response( $post_data );
}

/**
* Get sample schema for a category.
*
Expand Down Expand Up @@ -546,6 +599,39 @@ public function get_category_schema( $request ) {

}

public function get_tags_schema( $request ) {

$schema = array(
// This tells the spec of JSON Schema we are using which is draft 4.
'$schema' => 'http://json-schema.org/draft-04/schema#',
// The title property marks the identity of the resource.
'title' => 'post',
'type' => 'object',
// Specify object properties in the properties attribute.
'properties' => array(
'id' => array(
'description' => esc_html__( 'Unique identifier for the object.', 'phila-gov' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'name'=> array(
'description' => esc_html__( 'Name of the object.', 'phila-gov' ),
'type' => 'string',
'readonly' => true,
),
'slug'=> array(
'description' => esc_html__( 'Slug of the object.', 'phila-gov' ),
'type' => 'string',
'readonly' => true,
),
),
);

return $schema;

}

}

// Function to register our new routes from the controller.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

// register custom fields
register_rest_field( 'post', 'archived', array( 'get_callback' => 'get_archive_status' ));
Expand Down Expand Up @@ -38,24 +38,49 @@ function get_phila_featured_media ( $post ) {

}

function filter_post_by_archived( $args, $request ) {
$archived = $request->get_param( 'archived' );

if ( empty( $archived )) {
return $args;
}
function filter_post_by_archived($args, $request) {

$archived = $request->get_param('archived');

$two_years_ago = date('Y-m-d\TH:i:s', strtotime('-2 years'));

if ( $archived === 'true' ) {
$archived = 'archive_now';
} else if ( $archived === 'false' ){
$archived = 'default';
if ( $archived == 'true') {
$archived = 1;
} else if ( $archived == 'false' ){
$archived = 0;
}

$args['meta_query'][] = array(
'key' => 'phila_archive_post',
'value' => $archived,
'compare' => '=',
);
if ($archived === 1) {
// show everything
$args['meta_query'] = array();
} else if ($archived === 0) {
//archived is false -- show everything that is not archived
$args['meta_query'] = array(
'relation' => 'AND',
'orderby' => array(
'date' =>'DESC',
),
array(
'relation' => 'OR',
array(
'key' => 'phila_archive_post',
'value' => 'do_not_archive',
'compare' => '=',
),
array(
'key' => 'phila_archive_post',
'value' => 'default',
'compare' => '=',
),
array(
'key' => 'phila_archive_post',
'value' => 'archive_now',
'compare' => '!=',
),
),
);
}

return $args;
}
Expand Down
1 change: 1 addition & 0 deletions wp/wp-content/themes/phila.gov-theme/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,7 @@ function phila_get_archive_status( $post_id ) {
} else {
$archived = false;
}

return (bool) $archived;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Breadcrumbs
*/
?>
<div class="grid-container">
<div class="grid-container phila-breadcrumbs">
<div class="grid-x" data-swiftype-index="false">
<div class="cell">
<?php echo phila_breadcrumbs(); ?>
Expand Down

0 comments on commit ff954c1

Please sign in to comment.