Completed
Push — develop ( 1d77a8...e2550f )
by David
02:34
created

Wordlift_Jsonld_Service   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 118
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B get() 0 64 4
1
<?php
2
/**
3
 * Define the Wordlift_Jsonld_Service class to support JSON-LD.
4
 *
5
 * @since   3.8.0
6
 * @package Wordlift
7
 */
8
9
/**
10
 * This class exports an entity using JSON-LD.
11
 *
12
 * @since 3.8.0
13
 */
14
class Wordlift_Jsonld_Service {
15
16
	/**
17
	 * A {@link Wordlift_Entity_Service} instance.
18
	 *
19
	 * @since  3.8.0
20
	 * @access private
21
	 * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
22
	 */
23
	private $entity_service;
24
25
	/**
26
	 * A {@link Wordlift_Post_Converter} instance.
27
	 *
28
	 * @since  3.8.0
29
	 * @access private
30
	 * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance.
31
	 */
32
	private $converter;
33
34
35
	/**
36
	 * A {@link Wordlift_Website_Jsonld_Converter} instance.
37
	 *
38
	 * @since  3.14.0
39
	 * @access private
40
	 * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance.
41
	 */
42
	private $website_converter;
43
44
	/**
45
	 * Create a JSON-LD service.
46
	 *
47
	 * @since 3.8.0
48
	 *
49
	 * @param \Wordlift_Entity_Service           $entity_service    A {@link Wordlift_Entity_Service} instance.
50
	 * @param \Wordlift_Post_Converter           $converter         A {@link Wordlift_Uri_To_Jsonld_Converter} instance.
51
	 * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance.
52
	 */
53
	public function __construct( $entity_service, $converter, $website_converter ) {
54
55
		$this->entity_service    = $entity_service;
56
		$this->converter         = $converter;
57
		$this->website_converter = $website_converter;
58
59
	}
60
61
	/**
62
	 * Process calls to the AJAX 'wl_jsonld' endpoint.
63
	 *
64
	 * @since 3.8.0
65
	 */
66
	public function get() {
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
67
68
		// Tell NewRelic to ignore us, otherwise NewRelic customers might receive
69
		// e-mails with a low apdex score.
70
		//
71
		// See https://github.com/insideout10/wordlift-plugin/issues/521
72
		Wordlift_NewRelic_Adapter::ignore_apdex();
73
74
		// Clear the buffer to be sure someone doesn't mess with our response.
75
		//
76
		// See https://github.com/insideout10/wordlift-plugin/issues/406.
77
		// See https://codex.wordpress.org/AJAX_in_Plugins.
78
		ob_clean();
79
80
		// Switch to Website converter if is home page.
81
		if ( isset( $_REQUEST['homepage'] ) ) {
82
			/**
83
			 * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output.
84
			 *
85
			 * @since  3.14.0
86
			 * @api    bool $display_search Whether or not to display json+ld search on the frontend.
87
			 */
88
			if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) {
89
				// Set a reference to the website_converter.
90
				$website_converter = $this->website_converter;
91
92
				// Send JSON-LD.
93
				wp_send_json( $website_converter->create_schema() );
94
			}
95
		}
96
97
		// If no id has been provided return an empty array.
98
		if ( ! isset( $_REQUEST['id'] ) ) {
99
			wp_send_json( array() );
100
		}
101
102
		// Get the id.
103
		$id = $_REQUEST['id'];
104
105
		// An array of references which is captured when converting an URI to a
106
		// json which we gather to further expand our json-ld.
107
		$references = array();
108
109
		// Set a reference to the entity_to_jsonld_converter to use in the closures.
110
		$entity_to_jsonld_converter = $this->converter;
111
112
		// Convert each URI to a JSON-LD array, while gathering referenced entities.
113
		// in the references array.
114
		$jsonld = array_merge(
115
			array( $entity_to_jsonld_converter->convert( $id, $references ) ),
0 ignored issues
show
Unused Code introduced by
The call to Wordlift_Post_Converter::convert() has too many arguments starting with $references.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116
			// Convert each URI in the references array to JSON-LD. We don't output
117
			// entities already output above (hence the array_diff).
118
			array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) {
119
120
				// "2nd level properties" may not output here, e.g. a post
121
				// mentioning an event, located in a place: the place is referenced
122
				// via the `@id` but no other properties are loaded.
123
				return $entity_to_jsonld_converter->convert( $item, $references );
0 ignored issues
show
Unused Code introduced by
The call to Wordlift_Post_Converter::convert() has too many arguments starting with $references.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
124
			}, $references ) );
125
126
		// Finally send the JSON-LD.
127
		wp_send_json( $jsonld );
128
129
	}
130
131
}
132