Completed
Push — develop ( e43d0b...a2ab04 )
by David
02:49 queued 11s
created

Wordlift_Context_Cards_Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Context Cards Service
4
 *
5
 * @since      3.22.0
6
 * @package    Wordlift
7
 * @subpackage Wordlift/public
8
 */
9
10
class Wordlift_Context_Cards_Service {
11
12
	/**
13
	 * @var string
14
	 */
15
	private $endpoint;
16
17
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
19
		$this->endpoint = '/jsonld';
20
21
		// PHP 5.3 compatibility as `$this` cannot be used in closures.
22
		$that = $this;
23
24
		add_action( 'rest_api_init', function () use ( $that ) {
25
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array(
26
				'methods'  => 'GET',
27
				'callback' => array( $that, 'context_data' ),
28
			) );
29
		} );
30
31
	}
32
33
	public function context_data( $request ) {
34
35
		$entity_uri = urldecode( $request->get_param( 'entity_url' ) );
36
		$entity_id  = url_to_postid( $entity_uri );
37
		$jsonld     = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $entity_id );
38
39
		return $jsonld;
40
41
	}
42
43
	public function enqueue_scripts() {
44
		$show_context_cards = true;
45
		$show_context_cards = apply_filters( 'wl_show_context_cards', $show_context_cards );
46
		if ( $show_context_cards ) {
47
			wp_enqueue_script( 'wordlift-cloud' );
48
			wp_add_inline_script( 'wordlift-cloud', "wordliftCloud.contextCards('a.wl-entity-page-link', '" . get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint . "')" );
49
		}
50
	}
51
52
}
53