Completed
Push — master ( 9ba92a...6f958b )
by David
02:43
created

Wordlift_Jsonld_Service::get_instance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
	 * The singleton instance for the JSON-LD service.
46
	 *
47
	 * @since 3.15.1
48
	 *
49
	 * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service.
50
	 */
51
	private static $instance;
52
53
	/**
54
	 * Create a JSON-LD service.
55
	 *
56
	 * @since 3.8.0
57
	 *
58
	 * @param \Wordlift_Entity_Service           $entity_service    A {@link Wordlift_Entity_Service} instance.
59
	 * @param \Wordlift_Post_Converter           $converter         A {@link Wordlift_Uri_To_Jsonld_Converter} instance.
60
	 * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance.
61
	 */
62
	public function __construct( $entity_service, $converter, $website_converter ) {
63
64
		$this->entity_service    = $entity_service;
65
		$this->converter         = $converter;
66
		$this->website_converter = $website_converter;
67
68
		self::$instance = $this;
69
70
	}
71
72
	/**
73
	 * Get the singleton instance for the JSON-LD service.
74
	 *
75
	 * @since 3.15.1
76
	 *
77
	 * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service.
78
	 */
79
	public static function get_instance() {
80
81
		return self::$instance;
82
	}
83
84
	/**
85
	 * Process calls to the AJAX 'wl_jsonld' endpoint.
86
	 *
87
	 * @since 3.8.0
88
	 */
89
	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...
90
		// Clear the buffer to be sure someone doesn't mess with our response.
91
		//
92
		// See https://github.com/insideout10/wordlift-plugin/issues/406.
93
		// See https://codex.wordpress.org/AJAX_in_Plugins.
94
		@ob_clean();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
95
96
		// Get the parameter from the request.
97
		$is_homepage = isset( $_REQUEST['homepage'] );
98
		$post_id     = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null;
99
100
		// Send the generated JSON-LD.
101
		wp_send_json( $this->get_jsonld( $is_homepage, $post_id ) );
102
103
	}
104
105
	/**
106
	 * Get the JSON-LD.
107
	 *
108
	 * @since 3.15.1
109
	 *
110
	 * @param bool     $is_homepage Whether the JSON-LD for the homepage is being requested.
111
	 * @param int|null $post_id     The JSON-LD for the specified {@link WP_Post} id.
112
	 *
113
	 * @return array A JSON-LD structure.
114
	 */
115
	public function get_jsonld( $is_homepage = false, $post_id = null ) {
116
117
		// Tell NewRelic to ignore us, otherwise NewRelic customers might receive
118
		// e-mails with a low apdex score.
119
		//
120
		// See https://github.com/insideout10/wordlift-plugin/issues/521
121
		Wordlift_NewRelic_Adapter::ignore_apdex();
122
123
		// Switch to Website converter if is home page.
124
		if ( $is_homepage ) {
125
			/**
126
			 * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output.
127
			 *
128
			 * @since  3.14.0
129
			 * @api    bool $display_search Whether or not to display json+ld search on the frontend.
130
			 */
131
			if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) {
132
				// Set a reference to the website_converter.
133
				$website_converter = $this->website_converter;
134
135
				// Send JSON-LD.
136
				return $website_converter->create_schema();
137
			}
138
		}
139
140
		// If no id has been provided return an empty array.
141
		if ( ! isset( $post_id ) ) {
142
			return array();
143
		}
144
145
		// An array of references which is captured when converting an URI to a
146
		// json which we gather to further expand our json-ld.
147
		$references = array();
148
149
		// Set a reference to the entity_to_jsonld_converter to use in the closures.
150
		$entity_to_jsonld_converter = $this->converter;
151
152
		// Convert each URI to a JSON-LD array, while gathering referenced entities.
153
		// in the references array.
154
		$jsonld = array_merge(
155
			array( $entity_to_jsonld_converter->convert( $post_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...
156
			// Convert each URI in the references array to JSON-LD. We don't output
157
			// entities already output above (hence the array_diff).
158
			array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) {
159
160
				// "2nd level properties" may not output here, e.g. a post
161
				// mentioning an event, located in a place: the place is referenced
162
				// via the `@id` but no other properties are loaded.
163
				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...
164
			}, $references ) );
165
166
		// Finally send the JSON-LD.
167
		return $jsonld;
168
	}
169
170
}
171