Completed
Push — master ( 39f38d...a13281 )
by David
03:24
created
src/includes/class-wordlift-jsonld-service.php 2 patches
Indentation   +153 added lines, -153 removed lines patch added patch discarded remove patch
@@ -13,158 +13,158 @@
 block discarded – undo
13 13
  */
14 14
 class Wordlift_Jsonld_Service {
15 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() {
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();
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 ) ),
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_filter( 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 );
164
-			}, $references ) ) );
165
-
166
-		// Finally send the JSON-LD.
167
-		return $jsonld;
168
-	}
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() {
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();
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 ) ),
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_filter( 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 );
164
+            }, $references ) ) );
165
+
166
+        // Finally send the JSON-LD.
167
+        return $jsonld;
168
+    }
169 169
 
170 170
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
 	 * @param \Wordlift_Post_Converter           $converter         A {@link Wordlift_Uri_To_Jsonld_Converter} instance.
60 60
 	 * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance.
61 61
 	 */
62
-	public function __construct( $entity_service, $converter, $website_converter ) {
62
+	public function __construct($entity_service, $converter, $website_converter) {
63 63
 
64 64
 		$this->entity_service    = $entity_service;
65 65
 		$this->converter         = $converter;
@@ -94,11 +94,11 @@  discard block
 block discarded – undo
94 94
 		@ob_clean();
95 95
 
96 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;
97
+		$is_homepage = isset($_REQUEST['homepage']);
98
+		$post_id     = isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
99 99
 
100 100
 		// Send the generated JSON-LD.
101
-		wp_send_json( $this->get_jsonld( $is_homepage, $post_id ) );
101
+		wp_send_json($this->get_jsonld($is_homepage, $post_id));
102 102
 
103 103
 	}
104 104
 
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
 	 *
113 113
 	 * @return array A JSON-LD structure.
114 114
 	 */
115
-	public function get_jsonld( $is_homepage = false, $post_id = null ) {
115
+	public function get_jsonld($is_homepage = false, $post_id = null) {
116 116
 
117 117
 		// Tell NewRelic to ignore us, otherwise NewRelic customers might receive
118 118
 		// e-mails with a low apdex score.
@@ -121,14 +121,14 @@  discard block
 block discarded – undo
121 121
 		Wordlift_NewRelic_Adapter::ignore_apdex();
122 122
 
123 123
 		// Switch to Website converter if is home page.
124
-		if ( $is_homepage ) {
124
+		if ($is_homepage) {
125 125
 			/**
126 126
 			 * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output.
127 127
 			 *
128 128
 			 * @since  3.14.0
129 129
 			 * @api    bool $display_search Whether or not to display json+ld search on the frontend.
130 130
 			 */
131
-			if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) {
131
+			if ( ! apply_filters('wordlift_disable_website_json_ld', false)) {
132 132
 				// Set a reference to the website_converter.
133 133
 				$website_converter = $this->website_converter;
134 134
 
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
 		}
139 139
 
140 140
 		// If no id has been provided return an empty array.
141
-		if ( ! isset( $post_id ) ) {
141
+		if ( ! isset($post_id)) {
142 142
 			return array();
143 143
 		}
144 144
 
@@ -152,16 +152,16 @@  discard block
 block discarded – undo
152 152
 		// Convert each URI to a JSON-LD array, while gathering referenced entities.
153 153
 		// in the references array.
154 154
 		$jsonld = array_merge(
155
-			array( $entity_to_jsonld_converter->convert( $post_id, $references ) ),
155
+			array($entity_to_jsonld_converter->convert($post_id, $references)),
156 156
 			// Convert each URI in the references array to JSON-LD. We don't output
157 157
 			// entities already output above (hence the array_diff).
158
-			array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) {
158
+			array_filter(array_map(function($item) use ($entity_to_jsonld_converter, $references) {
159 159
 
160 160
 				// "2nd level properties" may not output here, e.g. a post
161 161
 				// mentioning an event, located in a place: the place is referenced
162 162
 				// via the `@id` but no other properties are loaded.
163
-				return $entity_to_jsonld_converter->convert( $item, $references );
164
-			}, $references ) ) );
163
+				return $entity_to_jsonld_converter->convert($item, $references);
164
+			}, $references)) );
165 165
 
166 166
 		// Finally send the JSON-LD.
167 167
 		return $jsonld;
Please login to merge, or discard this patch.