Completed
Push — master ( 4512af...87cb41 )
by David
02:42
created

Jsonld_Endpoint::callback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Jsonld;
4
5
use DateInterval;
6
use DateTime;
7
use DateTimeZone;
8
use WP_REST_Response;
9
10
class Jsonld_Endpoint {
11
	/**
12
	 * @var Wordlift_Jsonld_Service
13
	 */
14
	private $jsonld_service;
15
16
	/**
17
	 * Jsonld_Endpoint constructor.
18
	 *
19
	 * @param \Wordlift_Jsonld_Service $jsonld_service
20
	 */
21
	public function __construct( $jsonld_service ) {
22
23
		add_action( 'rest_api_init', function () {
24
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array(
25
				'methods'  => 'GET',
26
				'callback' => array( $this, 'callback' ),
27
				'args'     => array(
28
					'id' => array(
29
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
							return is_numeric( $param );
31
						},
32
						'sanitize_callback' => 'absint',
33
					),
34
				)
35
			) );
36
		} );
37
38
		$this->jsonld_service = $jsonld_service;
0 ignored issues
show
Documentation Bug introduced by
It seems like $jsonld_service of type object<Wordlift_Jsonld_Service> is incompatible with the declared type object<Wordlift\Jsonld\Wordlift_Jsonld_Service> of property $jsonld_service.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
	}
40
41
	public function callback( $request ) {
42
43
		$post_id     = $request['id'];
44
		$is_homepage = ( 0 === $post_id );
45
46
		// Send the generated JSON-LD.
47
		$data     = $this->jsonld_service->get_jsonld( $is_homepage, $post_id );
48
		$response = new WP_REST_Response( $data );
49
50
		$cache_in_seconds = 86400;
51
		$date_timezone    = new DateTimeZone( 'GMT' );
52
		$date_now         = new DateTime( 'now', $date_timezone );
53
		$date_interval    = new DateInterval( "PT{$cache_in_seconds}S" );
54
		$expires          = $date_now->add( $date_interval )->format( 'D, j M Y H:i:s T' );
55
56
		$response->set_headers( array(
57
			'Content-Type'  => 'application/ld+json; charset=' . get_option( 'blog_charset' ),
58
			'Cache-Control' => "max-age=$cache_in_seconds",
59
			'Expires'       => $expires
60
		) );
61
62
		return $response;
63
	}
64
65
}