Completed
Pull Request — master (#1464)
by Naveen
01:07
created
src/wordlift/analysis/class-v1-analysis-service.php 2 patches
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@
 block discarded – undo
12 12
  */
13 13
 class V1_Analysis_Service extends Abstract_Analysis_Service {
14 14
 
15
-	public function get_analysis_response( $data, $content_type, $post_id ) {
16
-		return $this->api_service->post_custom_content_type( 'analysis/single', $data, $content_type );
17
-	}
15
+    public function get_analysis_response( $data, $content_type, $post_id ) {
16
+        return $this->api_service->post_custom_content_type( 'analysis/single', $data, $content_type );
17
+    }
18 18
 }
19 19
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@
 block discarded – undo
12 12
  */
13 13
 class V1_Analysis_Service extends Abstract_Analysis_Service {
14 14
 
15
-	public function get_analysis_response( $data, $content_type, $post_id ) {
16
-		return $this->api_service->post_custom_content_type( 'analysis/single', $data, $content_type );
15
+	public function get_analysis_response($data, $content_type, $post_id) {
16
+		return $this->api_service->post_custom_content_type('analysis/single', $data, $content_type);
17 17
 	}
18 18
 }
19 19
\ No newline at end of file
Please login to merge, or discard this patch.
src/wordlift/analysis/response/class-analysis-response-ops.php 2 patches
Indentation   +360 added lines, -360 removed lines patch added patch discarded remove patch
@@ -16,365 +16,365 @@
 block discarded – undo
16 16
 
17 17
 class Analysis_Response_Ops {
18 18
 
19
-	/**
20
-	 * The analysis response json.
21
-	 *
22
-	 * @since 3.21.5
23
-	 * @access private
24
-	 * @var mixed $json Holds the analysis response json.
25
-	 */
26
-	private $json;
27
-
28
-	/**
29
-	 * Holds the {@link Wordlift_Entity_Uri_Service}.
30
-	 *
31
-	 * @since 3.21.5
32
-	 * @access private
33
-	 * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
34
-	 */
35
-	private $entity_uri_service;
36
-
37
-
38
-	/**
39
-	 * @var Entity_Helper
40
-	 */
41
-	private $entity_helper;
42
-	/**
43
-	 * @var Entity_Provider_Registry
44
-	 */
45
-	private $entity_provider_registry;
46
-
47
-	/**
48
-	 * @var int $post_id
49
-	 */
50
-	private $post_id;
51
-
52
-	/**
53
-	 * Analysis_Response_Ops constructor.
54
-	 *
55
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}.
56
-	 * @param Entity_Helper $entity_helper The {@link Entity_Helper}.
57
-	 * @param Entity_Provider_Registry $entity_provider_registry Entity Provider registry.
58
-	 * @param mixed $json The analysis response json.
59
-	 *
60
-	 * @since 3.21.5
61
-	 */
62
-	public function __construct( $entity_uri_service, $entity_helper, $entity_provider_registry, $json, $post_id ) {
63
-
64
-		$this->json                     = $json;
65
-		$this->entity_uri_service       = $entity_uri_service;
66
-		$this->entity_helper            = $entity_helper;
67
-		$this->entity_provider_registry = $entity_provider_registry;
68
-		$this->post_id = $post_id;
69
-	}
70
-
71
-	/**
72
-	 * Switches remote entities, i.e. entities with id outside the local dataset, to local entities.
73
-	 *
74
-	 * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs
75
-	 * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential
76
-	 * matches from the local vocabulary.
77
-	 *
78
-	 * If found, the entity id is swapped with the local id and the remote id is added to the sameAs.
79
-	 *
80
-	 *
81
-	 * @return Analysis_Response_Ops The current Analysis_Response_Ops instance.
82
-	 */
83
-	public function make_entities_local() {
84
-
85
-		if ( ! isset( $this->json->entities ) ) {
86
-			return $this;
87
-		}
88
-		// Get the URIs.
89
-		$uris     = array_keys( get_object_vars( $this->json->entities ) );
90
-		$mappings = $this->entity_helper->map_many_to_local( $uris );
91
-
92
-		foreach ( $mappings as $external_uri => $internal_uri ) {
93
-
94
-			// Move the data from the external URI to the internal URI.
95
-			if ( ! isset( $this->json->entities->{$internal_uri} ) ) {
96
-				$this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri};
97
-			}
98
-
99
-			// Ensure sameAs is an array.
100
-			if ( ! isset( $this->json->entities->{$internal_uri}->sameAs )
101
-			     || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) {
102
-				$this->json->entities->{$internal_uri}->sameAs = array();
103
-			}
104
-
105
-			// Add the external URI as sameAs.
106
-			$this->json->entities->{$internal_uri}->sameAs[] = $external_uri;
107
-
108
-			// Finally remove the external URI.
109
-			unset( $this->json->entities->{$external_uri} );
110
-		}
111
-
112
-		// Set the internal uri in the annotation for the entityMatch in annotations.
113
-		if ( isset( $this->json->annotations ) ) {
114
-			foreach ( $this->json->annotations as $key => $annotation ) {
115
-				if ( isset( $annotation->entityMatches ) ) {
116
-					foreach ( $annotation->entityMatches as $match ) {
117
-						if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) {
118
-							$match->entityId = $mappings[ $match->entityId ];
119
-						}
120
-					}
121
-				}
122
-			}
123
-		}
124
-
125
-		return $this;
126
-	}
127
-
128
-	/**
129
-	 * Add occurrences by parsing the provided html content.
130
-	 *
131
-	 * @param string $content The html content with annotations.
132
-	 *
133
-	 * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance.
134
-	 *
135
-	 * @since 3.23.7 refactor the regex pattern to take into account that there might be css classes between textannotation
136
-	 *  and disambiguated.
137
-	 *
138
-	 * @link https://github.com/insideout10/wordlift-plugin/issues/1001
139
-	 */
140
-	public function add_occurrences( $content ) {
141
-
142
-		// Try to get all the disambiguated annotations and bail out if an error occurs.
143
-		if ( false === preg_match_all(
144
-				'|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|',
145
-				$content,
146
-				$matches,
147
-				PREG_OFFSET_CAPTURE
148
-			) ) {
149
-			return $this;
150
-		}
151
-
152
-		if ( empty( $matches ) ) {
153
-			return $this;
154
-		}
155
-
156
-		$parse_data = array_reduce( range( 0, count( $matches[1] ) - 1 ), function ( $carry, $i ) use ( $matches ) {
157
-			if ( empty( $matches[0] ) ) {
158
-				return $carry;
159
-			}
160
-
161
-			$start         = $matches[0][ $i ][1];
162
-			$end           = $start + strlen( $matches[0][ $i ][0] );
163
-			$annotation_id = $matches[1][ $i ][0];
164
-			$item_id       = $matches[2][ $i ][0];
165
-			$text          = $matches[3][ $i ][0];
166
-
167
-			$annotation               = new StdClass;
168
-			$annotation->annotationId = $annotation_id;
169
-			$annotation->start        = $start;
170
-			$annotation->end          = $end;
171
-			$annotation->text         = $text;
172
-
173
-			$entity_match                = new StdClass;
174
-			$entity_match->confidence    = 100;
175
-			$entity_match->entityId      = $item_id;
176
-			$annotation->entityMatches[] = $entity_match;
177
-
178
-			$carry['annotations'][ $annotation_id ] = $annotation;
179
-			$carry['occurrences'][ $item_id ][]     = $annotation_id;
180
-
181
-			return $carry;
182
-		}, array( 'annotations' => array(), 'occurrences' => array(), ) );
183
-
184
-		$annotations = $parse_data['annotations'];
185
-		$occurrences = $parse_data['occurrences'];
186
-
187
-		foreach ( array_keys( $occurrences ) as $item_id ) {
188
-
189
-			// If the entity isn't there, add it.
190
-			if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) {
191
-				$entity = $this->entity_provider_registry->get_local_entity( $item_id );
192
-
193
-				// Entity not found in the local vocabulary, continue to the next one.
194
-				if ( false === $entity ) {
195
-					continue;
196
-				}
197
-
198
-				$this->json->entities->{$item_id} = $entity;
199
-			}
200
-		}
201
-
202
-		// Here we're adding back some data structures required by the client-side code.
203
-		//
204
-		// We're adding:
205
-		//  1. the .entities[entity_id].occurrences array with the annotations' ids.
206
-		//  2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map.
207
-		//
208
-		// Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee
209
-		// function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in
210
-		// `embedAnalysis`.
211
-
212
-		if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) {
213
-			$occurrences_processor = Occurrences_Factory::get_instance( $this->post_id );
214
-			$this->json = $occurrences_processor->add_occurences_to_entities( $occurrences, $this->json, $this->post_id );
215
-		}
216
-
217
-		// Add the missing annotations. This allows the analysis response to work also if we didn't receive results
218
-		// from the analysis API.
219
-		foreach ( $annotations as $annotation_id => $annotation ) {
220
-
221
-			if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) {
222
-				$this->json->annotations->{$annotation_id} = $annotation;
223
-			}
224
-
225
-		}
226
-
227
-		return $this;
228
-	}
229
-
230
-	/**
231
-	 * Add local entities
232
-	 *
233
-	 * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance.
234
-	 *
235
-	 * @since 3.27.6
236
-	 *
237
-	 * @link https://github.com/insideout10/wordlift-plugin/issues/1178
238
-	 */
239
-	public function add_local_entities() {
240
-
241
-		// Populating the local entities object
242
-		$entities = array();
243
-		foreach ( $this->json->annotations as $annotation ) {
244
-			foreach ( $annotation->entityMatches as $entity_matches ) {
245
-
246
-				$entity_id         = $this->entity_uri_service->get_post_id_from_url( $entity_matches->entityId );
247
-				$serialized_entity = wl_serialize_entity( $entity_id );
248
-
249
-				if ( $serialized_entity ) {
250
-					$serialized_entity['entityId'] = $serialized_entity['id'];
251
-					unset( $serialized_entity['id'] );
252
-
253
-					$entities[ $entity_matches->entityId ] = $serialized_entity;
254
-				}
255
-
256
-			}
257
-		}
258
-
259
-		// Adding occurrences and annotations data structures required by the client-side code.
260
-		foreach ( $entities as $entity_id => $entity ) {
261
-			foreach ( $this->json->annotations as $annotation ) {
262
-				if ( $annotation->entityMatches[0]->entityId === $entity_id ) {
263
-					$entities[ $entity_id ]['occurrences'][]                                  = $annotation->annotationId;
264
-					$entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId;
265
-				}
266
-			}
267
-		}
268
-
269
-		$this->json->entities = $entities;
270
-
271
-		return $this;
272
-
273
-	}
274
-
275
-
276
-	/**
277
-	 * Return the JSON response.
278
-	 *
279
-	 * @return mixed The JSON response.
280
-	 * @since 3.24.2
281
-	 */
282
-	public function get_json() {
283
-
284
-		return $this->json;
285
-	}
286
-
287
-	/**
288
-	 * This function should be invoked after `make_entities_local` after this
289
-	 * method.
290
-	 *
291
-	 * @param $excluded_uris array An array of entity URIs to be excluded.
292
-	 *
293
-	 * @return $this
294
-	 * @since 3.32.3.1
295
-	 */
296
-	public function remove_excluded_entities( $excluded_uris ) {
297
-
298
-		// If we didnt receive array, return early.
299
-		if ( ! is_array( $excluded_uris ) ) {
300
-			return $this;
301
-		}
302
-
303
-		// We may also receive an array of null, make sure to filter uris when receiving.
304
-		$excluded_uris = array_filter( $excluded_uris, 'is_string' );
305
-
306
-		$this->remove_entities_with_excluded_uris( $excluded_uris );
307
-
308
-		$this->remove_annotations_with_excluded_uris( $excluded_uris );
309
-
310
-		return $this;
311
-	}
312
-
313
-
314
-	/**
315
-	 * Get the string representation of the JSON.
316
-	 *
317
-	 * @return false|string The string representation or false in case of error.
318
-	 */
319
-	public function to_string() {
320
-
321
-		// Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+.
322
-		$options = ( version_compare( PHP_VERSION, '5.4', '>=' )
323
-			? 256 : 0 );
324
-
325
-		return wp_json_encode( $this->json, $options );
326
-	}
327
-
328
-	/**
329
-	 * Remove all the entities with the excluded URIs.
330
-	 * @param array $excluded_uris The array of URIs to be excluded.
331
-	 */
332
-	private function remove_entities_with_excluded_uris( array $excluded_uris ) {
333
-		// Remove the excluded entity uris.
334
-		if ( isset( $this->json->entities ) ) {
335
-			foreach ( $excluded_uris as $excluded_uri ) {
336
-
337
-				if ( isset( $this->json->entities->{$excluded_uri} ) ) {
338
-					// Remove this entity.
339
-					unset( $this->json->entities->{$excluded_uri} );
340
-					// Also remove the annotations.
341
-				}
342
-			}
343
-		}
344
-	}
345
-
346
-	/**
347
-	 * Remove all the annotations with the excluded entity URIs.
348
-	 * @param array $excluded_uris The array of URIs to be excluded.
349
-	 *
350
-	 * @return void
351
-	 */
352
-	private function remove_annotations_with_excluded_uris( array $excluded_uris ) {
353
-		if ( isset( $this->json->annotations ) ) {
354
-			foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) {
355
-
356
-				if ( ! isset( $annotation_data->entityMatches ) ) {
357
-					continue;
358
-				}
359
-
360
-				foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) {
361
-
362
-					$entity_uri = $entity_match_data->entityId;
363
-
364
-					if ( ! in_array( $entity_uri, $excluded_uris ) ) {
365
-						continue;
366
-					}
367
-					unset( $annotation_data->entityMatches[ $entity_match_key ] );
368
-				}
369
-
370
-				if ( count( $annotation_data->entityMatches ) === 0 ) {
371
-					// Remove the annotation if we have zero empty annotation matches.
372
-					unset( $this->json->annotations->{$annotation_key} );
373
-				}
374
-
375
-			}
376
-		}
377
-
378
-	}
19
+    /**
20
+     * The analysis response json.
21
+     *
22
+     * @since 3.21.5
23
+     * @access private
24
+     * @var mixed $json Holds the analysis response json.
25
+     */
26
+    private $json;
27
+
28
+    /**
29
+     * Holds the {@link Wordlift_Entity_Uri_Service}.
30
+     *
31
+     * @since 3.21.5
32
+     * @access private
33
+     * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
34
+     */
35
+    private $entity_uri_service;
36
+
37
+
38
+    /**
39
+     * @var Entity_Helper
40
+     */
41
+    private $entity_helper;
42
+    /**
43
+     * @var Entity_Provider_Registry
44
+     */
45
+    private $entity_provider_registry;
46
+
47
+    /**
48
+     * @var int $post_id
49
+     */
50
+    private $post_id;
51
+
52
+    /**
53
+     * Analysis_Response_Ops constructor.
54
+     *
55
+     * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}.
56
+     * @param Entity_Helper $entity_helper The {@link Entity_Helper}.
57
+     * @param Entity_Provider_Registry $entity_provider_registry Entity Provider registry.
58
+     * @param mixed $json The analysis response json.
59
+     *
60
+     * @since 3.21.5
61
+     */
62
+    public function __construct( $entity_uri_service, $entity_helper, $entity_provider_registry, $json, $post_id ) {
63
+
64
+        $this->json                     = $json;
65
+        $this->entity_uri_service       = $entity_uri_service;
66
+        $this->entity_helper            = $entity_helper;
67
+        $this->entity_provider_registry = $entity_provider_registry;
68
+        $this->post_id = $post_id;
69
+    }
70
+
71
+    /**
72
+     * Switches remote entities, i.e. entities with id outside the local dataset, to local entities.
73
+     *
74
+     * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs
75
+     * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential
76
+     * matches from the local vocabulary.
77
+     *
78
+     * If found, the entity id is swapped with the local id and the remote id is added to the sameAs.
79
+     *
80
+     *
81
+     * @return Analysis_Response_Ops The current Analysis_Response_Ops instance.
82
+     */
83
+    public function make_entities_local() {
84
+
85
+        if ( ! isset( $this->json->entities ) ) {
86
+            return $this;
87
+        }
88
+        // Get the URIs.
89
+        $uris     = array_keys( get_object_vars( $this->json->entities ) );
90
+        $mappings = $this->entity_helper->map_many_to_local( $uris );
91
+
92
+        foreach ( $mappings as $external_uri => $internal_uri ) {
93
+
94
+            // Move the data from the external URI to the internal URI.
95
+            if ( ! isset( $this->json->entities->{$internal_uri} ) ) {
96
+                $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri};
97
+            }
98
+
99
+            // Ensure sameAs is an array.
100
+            if ( ! isset( $this->json->entities->{$internal_uri}->sameAs )
101
+                 || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) {
102
+                $this->json->entities->{$internal_uri}->sameAs = array();
103
+            }
104
+
105
+            // Add the external URI as sameAs.
106
+            $this->json->entities->{$internal_uri}->sameAs[] = $external_uri;
107
+
108
+            // Finally remove the external URI.
109
+            unset( $this->json->entities->{$external_uri} );
110
+        }
111
+
112
+        // Set the internal uri in the annotation for the entityMatch in annotations.
113
+        if ( isset( $this->json->annotations ) ) {
114
+            foreach ( $this->json->annotations as $key => $annotation ) {
115
+                if ( isset( $annotation->entityMatches ) ) {
116
+                    foreach ( $annotation->entityMatches as $match ) {
117
+                        if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) {
118
+                            $match->entityId = $mappings[ $match->entityId ];
119
+                        }
120
+                    }
121
+                }
122
+            }
123
+        }
124
+
125
+        return $this;
126
+    }
127
+
128
+    /**
129
+     * Add occurrences by parsing the provided html content.
130
+     *
131
+     * @param string $content The html content with annotations.
132
+     *
133
+     * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance.
134
+     *
135
+     * @since 3.23.7 refactor the regex pattern to take into account that there might be css classes between textannotation
136
+     *  and disambiguated.
137
+     *
138
+     * @link https://github.com/insideout10/wordlift-plugin/issues/1001
139
+     */
140
+    public function add_occurrences( $content ) {
141
+
142
+        // Try to get all the disambiguated annotations and bail out if an error occurs.
143
+        if ( false === preg_match_all(
144
+                '|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|',
145
+                $content,
146
+                $matches,
147
+                PREG_OFFSET_CAPTURE
148
+            ) ) {
149
+            return $this;
150
+        }
151
+
152
+        if ( empty( $matches ) ) {
153
+            return $this;
154
+        }
155
+
156
+        $parse_data = array_reduce( range( 0, count( $matches[1] ) - 1 ), function ( $carry, $i ) use ( $matches ) {
157
+            if ( empty( $matches[0] ) ) {
158
+                return $carry;
159
+            }
160
+
161
+            $start         = $matches[0][ $i ][1];
162
+            $end           = $start + strlen( $matches[0][ $i ][0] );
163
+            $annotation_id = $matches[1][ $i ][0];
164
+            $item_id       = $matches[2][ $i ][0];
165
+            $text          = $matches[3][ $i ][0];
166
+
167
+            $annotation               = new StdClass;
168
+            $annotation->annotationId = $annotation_id;
169
+            $annotation->start        = $start;
170
+            $annotation->end          = $end;
171
+            $annotation->text         = $text;
172
+
173
+            $entity_match                = new StdClass;
174
+            $entity_match->confidence    = 100;
175
+            $entity_match->entityId      = $item_id;
176
+            $annotation->entityMatches[] = $entity_match;
177
+
178
+            $carry['annotations'][ $annotation_id ] = $annotation;
179
+            $carry['occurrences'][ $item_id ][]     = $annotation_id;
180
+
181
+            return $carry;
182
+        }, array( 'annotations' => array(), 'occurrences' => array(), ) );
183
+
184
+        $annotations = $parse_data['annotations'];
185
+        $occurrences = $parse_data['occurrences'];
186
+
187
+        foreach ( array_keys( $occurrences ) as $item_id ) {
188
+
189
+            // If the entity isn't there, add it.
190
+            if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) {
191
+                $entity = $this->entity_provider_registry->get_local_entity( $item_id );
192
+
193
+                // Entity not found in the local vocabulary, continue to the next one.
194
+                if ( false === $entity ) {
195
+                    continue;
196
+                }
197
+
198
+                $this->json->entities->{$item_id} = $entity;
199
+            }
200
+        }
201
+
202
+        // Here we're adding back some data structures required by the client-side code.
203
+        //
204
+        // We're adding:
205
+        //  1. the .entities[entity_id].occurrences array with the annotations' ids.
206
+        //  2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map.
207
+        //
208
+        // Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee
209
+        // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in
210
+        // `embedAnalysis`.
211
+
212
+        if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) {
213
+            $occurrences_processor = Occurrences_Factory::get_instance( $this->post_id );
214
+            $this->json = $occurrences_processor->add_occurences_to_entities( $occurrences, $this->json, $this->post_id );
215
+        }
216
+
217
+        // Add the missing annotations. This allows the analysis response to work also if we didn't receive results
218
+        // from the analysis API.
219
+        foreach ( $annotations as $annotation_id => $annotation ) {
220
+
221
+            if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) {
222
+                $this->json->annotations->{$annotation_id} = $annotation;
223
+            }
224
+
225
+        }
226
+
227
+        return $this;
228
+    }
229
+
230
+    /**
231
+     * Add local entities
232
+     *
233
+     * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance.
234
+     *
235
+     * @since 3.27.6
236
+     *
237
+     * @link https://github.com/insideout10/wordlift-plugin/issues/1178
238
+     */
239
+    public function add_local_entities() {
240
+
241
+        // Populating the local entities object
242
+        $entities = array();
243
+        foreach ( $this->json->annotations as $annotation ) {
244
+            foreach ( $annotation->entityMatches as $entity_matches ) {
245
+
246
+                $entity_id         = $this->entity_uri_service->get_post_id_from_url( $entity_matches->entityId );
247
+                $serialized_entity = wl_serialize_entity( $entity_id );
248
+
249
+                if ( $serialized_entity ) {
250
+                    $serialized_entity['entityId'] = $serialized_entity['id'];
251
+                    unset( $serialized_entity['id'] );
252
+
253
+                    $entities[ $entity_matches->entityId ] = $serialized_entity;
254
+                }
255
+
256
+            }
257
+        }
258
+
259
+        // Adding occurrences and annotations data structures required by the client-side code.
260
+        foreach ( $entities as $entity_id => $entity ) {
261
+            foreach ( $this->json->annotations as $annotation ) {
262
+                if ( $annotation->entityMatches[0]->entityId === $entity_id ) {
263
+                    $entities[ $entity_id ]['occurrences'][]                                  = $annotation->annotationId;
264
+                    $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId;
265
+                }
266
+            }
267
+        }
268
+
269
+        $this->json->entities = $entities;
270
+
271
+        return $this;
272
+
273
+    }
274
+
275
+
276
+    /**
277
+     * Return the JSON response.
278
+     *
279
+     * @return mixed The JSON response.
280
+     * @since 3.24.2
281
+     */
282
+    public function get_json() {
283
+
284
+        return $this->json;
285
+    }
286
+
287
+    /**
288
+     * This function should be invoked after `make_entities_local` after this
289
+     * method.
290
+     *
291
+     * @param $excluded_uris array An array of entity URIs to be excluded.
292
+     *
293
+     * @return $this
294
+     * @since 3.32.3.1
295
+     */
296
+    public function remove_excluded_entities( $excluded_uris ) {
297
+
298
+        // If we didnt receive array, return early.
299
+        if ( ! is_array( $excluded_uris ) ) {
300
+            return $this;
301
+        }
302
+
303
+        // We may also receive an array of null, make sure to filter uris when receiving.
304
+        $excluded_uris = array_filter( $excluded_uris, 'is_string' );
305
+
306
+        $this->remove_entities_with_excluded_uris( $excluded_uris );
307
+
308
+        $this->remove_annotations_with_excluded_uris( $excluded_uris );
309
+
310
+        return $this;
311
+    }
312
+
313
+
314
+    /**
315
+     * Get the string representation of the JSON.
316
+     *
317
+     * @return false|string The string representation or false in case of error.
318
+     */
319
+    public function to_string() {
320
+
321
+        // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+.
322
+        $options = ( version_compare( PHP_VERSION, '5.4', '>=' )
323
+            ? 256 : 0 );
324
+
325
+        return wp_json_encode( $this->json, $options );
326
+    }
327
+
328
+    /**
329
+     * Remove all the entities with the excluded URIs.
330
+     * @param array $excluded_uris The array of URIs to be excluded.
331
+     */
332
+    private function remove_entities_with_excluded_uris( array $excluded_uris ) {
333
+        // Remove the excluded entity uris.
334
+        if ( isset( $this->json->entities ) ) {
335
+            foreach ( $excluded_uris as $excluded_uri ) {
336
+
337
+                if ( isset( $this->json->entities->{$excluded_uri} ) ) {
338
+                    // Remove this entity.
339
+                    unset( $this->json->entities->{$excluded_uri} );
340
+                    // Also remove the annotations.
341
+                }
342
+            }
343
+        }
344
+    }
345
+
346
+    /**
347
+     * Remove all the annotations with the excluded entity URIs.
348
+     * @param array $excluded_uris The array of URIs to be excluded.
349
+     *
350
+     * @return void
351
+     */
352
+    private function remove_annotations_with_excluded_uris( array $excluded_uris ) {
353
+        if ( isset( $this->json->annotations ) ) {
354
+            foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) {
355
+
356
+                if ( ! isset( $annotation_data->entityMatches ) ) {
357
+                    continue;
358
+                }
359
+
360
+                foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) {
361
+
362
+                    $entity_uri = $entity_match_data->entityId;
363
+
364
+                    if ( ! in_array( $entity_uri, $excluded_uris ) ) {
365
+                        continue;
366
+                    }
367
+                    unset( $annotation_data->entityMatches[ $entity_match_key ] );
368
+                }
369
+
370
+                if ( count( $annotation_data->entityMatches ) === 0 ) {
371
+                    // Remove the annotation if we have zero empty annotation matches.
372
+                    unset( $this->json->annotations->{$annotation_key} );
373
+                }
374
+
375
+            }
376
+        }
377
+
378
+    }
379 379
 
380 380
 }
Please login to merge, or discard this patch.
Spacing   +72 added lines, -72 removed lines patch added patch discarded remove patch
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
 	 *
60 60
 	 * @since 3.21.5
61 61
 	 */
62
-	public function __construct( $entity_uri_service, $entity_helper, $entity_provider_registry, $json, $post_id ) {
62
+	public function __construct($entity_uri_service, $entity_helper, $entity_provider_registry, $json, $post_id) {
63 63
 
64 64
 		$this->json                     = $json;
65 65
 		$this->entity_uri_service       = $entity_uri_service;
@@ -82,23 +82,23 @@  discard block
 block discarded – undo
82 82
 	 */
83 83
 	public function make_entities_local() {
84 84
 
85
-		if ( ! isset( $this->json->entities ) ) {
85
+		if ( ! isset($this->json->entities)) {
86 86
 			return $this;
87 87
 		}
88 88
 		// Get the URIs.
89
-		$uris     = array_keys( get_object_vars( $this->json->entities ) );
90
-		$mappings = $this->entity_helper->map_many_to_local( $uris );
89
+		$uris     = array_keys(get_object_vars($this->json->entities));
90
+		$mappings = $this->entity_helper->map_many_to_local($uris);
91 91
 
92
-		foreach ( $mappings as $external_uri => $internal_uri ) {
92
+		foreach ($mappings as $external_uri => $internal_uri) {
93 93
 
94 94
 			// Move the data from the external URI to the internal URI.
95
-			if ( ! isset( $this->json->entities->{$internal_uri} ) ) {
95
+			if ( ! isset($this->json->entities->{$internal_uri} )) {
96 96
 				$this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri};
97 97
 			}
98 98
 
99 99
 			// Ensure sameAs is an array.
100
-			if ( ! isset( $this->json->entities->{$internal_uri}->sameAs )
101
-			     || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) {
100
+			if ( ! isset($this->json->entities->{$internal_uri}->sameAs)
101
+			     || ! is_array($this->json->entities->{$internal_uri}->sameAs)) {
102 102
 				$this->json->entities->{$internal_uri}->sameAs = array();
103 103
 			}
104 104
 
@@ -106,16 +106,16 @@  discard block
 block discarded – undo
106 106
 			$this->json->entities->{$internal_uri}->sameAs[] = $external_uri;
107 107
 
108 108
 			// Finally remove the external URI.
109
-			unset( $this->json->entities->{$external_uri} );
109
+			unset($this->json->entities->{$external_uri} );
110 110
 		}
111 111
 
112 112
 		// Set the internal uri in the annotation for the entityMatch in annotations.
113
-		if ( isset( $this->json->annotations ) ) {
114
-			foreach ( $this->json->annotations as $key => $annotation ) {
115
-				if ( isset( $annotation->entityMatches ) ) {
116
-					foreach ( $annotation->entityMatches as $match ) {
117
-						if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) {
118
-							$match->entityId = $mappings[ $match->entityId ];
113
+		if (isset($this->json->annotations)) {
114
+			foreach ($this->json->annotations as $key => $annotation) {
115
+				if (isset($annotation->entityMatches)) {
116
+					foreach ($annotation->entityMatches as $match) {
117
+						if (isset($match->entityId) && isset($mappings[$match->entityId])) {
118
+							$match->entityId = $mappings[$match->entityId];
119 119
 						}
120 120
 					}
121 121
 				}
@@ -137,32 +137,32 @@  discard block
 block discarded – undo
137 137
 	 *
138 138
 	 * @link https://github.com/insideout10/wordlift-plugin/issues/1001
139 139
 	 */
140
-	public function add_occurrences( $content ) {
140
+	public function add_occurrences($content) {
141 141
 
142 142
 		// Try to get all the disambiguated annotations and bail out if an error occurs.
143
-		if ( false === preg_match_all(
143
+		if (false === preg_match_all(
144 144
 				'|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|',
145 145
 				$content,
146 146
 				$matches,
147 147
 				PREG_OFFSET_CAPTURE
148
-			) ) {
148
+			)) {
149 149
 			return $this;
150 150
 		}
151 151
 
152
-		if ( empty( $matches ) ) {
152
+		if (empty($matches)) {
153 153
 			return $this;
154 154
 		}
155 155
 
156
-		$parse_data = array_reduce( range( 0, count( $matches[1] ) - 1 ), function ( $carry, $i ) use ( $matches ) {
157
-			if ( empty( $matches[0] ) ) {
156
+		$parse_data = array_reduce(range(0, count($matches[1]) - 1), function($carry, $i) use ($matches) {
157
+			if (empty($matches[0])) {
158 158
 				return $carry;
159 159
 			}
160 160
 
161
-			$start         = $matches[0][ $i ][1];
162
-			$end           = $start + strlen( $matches[0][ $i ][0] );
163
-			$annotation_id = $matches[1][ $i ][0];
164
-			$item_id       = $matches[2][ $i ][0];
165
-			$text          = $matches[3][ $i ][0];
161
+			$start         = $matches[0][$i][1];
162
+			$end           = $start + strlen($matches[0][$i][0]);
163
+			$annotation_id = $matches[1][$i][0];
164
+			$item_id       = $matches[2][$i][0];
165
+			$text          = $matches[3][$i][0];
166 166
 
167 167
 			$annotation               = new StdClass;
168 168
 			$annotation->annotationId = $annotation_id;
@@ -175,23 +175,23 @@  discard block
 block discarded – undo
175 175
 			$entity_match->entityId      = $item_id;
176 176
 			$annotation->entityMatches[] = $entity_match;
177 177
 
178
-			$carry['annotations'][ $annotation_id ] = $annotation;
179
-			$carry['occurrences'][ $item_id ][]     = $annotation_id;
178
+			$carry['annotations'][$annotation_id] = $annotation;
179
+			$carry['occurrences'][$item_id][]     = $annotation_id;
180 180
 
181 181
 			return $carry;
182
-		}, array( 'annotations' => array(), 'occurrences' => array(), ) );
182
+		}, array('annotations' => array(), 'occurrences' => array(),));
183 183
 
184 184
 		$annotations = $parse_data['annotations'];
185 185
 		$occurrences = $parse_data['occurrences'];
186 186
 
187
-		foreach ( array_keys( $occurrences ) as $item_id ) {
187
+		foreach (array_keys($occurrences) as $item_id) {
188 188
 
189 189
 			// If the entity isn't there, add it.
190
-			if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) {
191
-				$entity = $this->entity_provider_registry->get_local_entity( $item_id );
190
+			if ( ! is_bool($this->json) && ! isset($this->json->entities->{$item_id} )) {
191
+				$entity = $this->entity_provider_registry->get_local_entity($item_id);
192 192
 
193 193
 				// Entity not found in the local vocabulary, continue to the next one.
194
-				if ( false === $entity ) {
194
+				if (false === $entity) {
195 195
 					continue;
196 196
 				}
197 197
 
@@ -209,16 +209,16 @@  discard block
 block discarded – undo
209 209
 		// function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in
210 210
 		// `embedAnalysis`.
211 211
 
212
-		if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) {
213
-			$occurrences_processor = Occurrences_Factory::get_instance( $this->post_id );
214
-			$this->json = $occurrences_processor->add_occurences_to_entities( $occurrences, $this->json, $this->post_id );
212
+		if ( ! is_bool($this->json) && isset($this->json->entities)) {
213
+			$occurrences_processor = Occurrences_Factory::get_instance($this->post_id);
214
+			$this->json = $occurrences_processor->add_occurences_to_entities($occurrences, $this->json, $this->post_id);
215 215
 		}
216 216
 
217 217
 		// Add the missing annotations. This allows the analysis response to work also if we didn't receive results
218 218
 		// from the analysis API.
219
-		foreach ( $annotations as $annotation_id => $annotation ) {
219
+		foreach ($annotations as $annotation_id => $annotation) {
220 220
 
221
-			if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) {
221
+			if ( ! is_bool($this->json) && ! isset($this->json->annotations->{$annotation_id} )) {
222 222
 				$this->json->annotations->{$annotation_id} = $annotation;
223 223
 			}
224 224
 
@@ -240,28 +240,28 @@  discard block
 block discarded – undo
240 240
 
241 241
 		// Populating the local entities object
242 242
 		$entities = array();
243
-		foreach ( $this->json->annotations as $annotation ) {
244
-			foreach ( $annotation->entityMatches as $entity_matches ) {
243
+		foreach ($this->json->annotations as $annotation) {
244
+			foreach ($annotation->entityMatches as $entity_matches) {
245 245
 
246
-				$entity_id         = $this->entity_uri_service->get_post_id_from_url( $entity_matches->entityId );
247
-				$serialized_entity = wl_serialize_entity( $entity_id );
246
+				$entity_id         = $this->entity_uri_service->get_post_id_from_url($entity_matches->entityId);
247
+				$serialized_entity = wl_serialize_entity($entity_id);
248 248
 
249
-				if ( $serialized_entity ) {
249
+				if ($serialized_entity) {
250 250
 					$serialized_entity['entityId'] = $serialized_entity['id'];
251
-					unset( $serialized_entity['id'] );
251
+					unset($serialized_entity['id']);
252 252
 
253
-					$entities[ $entity_matches->entityId ] = $serialized_entity;
253
+					$entities[$entity_matches->entityId] = $serialized_entity;
254 254
 				}
255 255
 
256 256
 			}
257 257
 		}
258 258
 
259 259
 		// Adding occurrences and annotations data structures required by the client-side code.
260
-		foreach ( $entities as $entity_id => $entity ) {
261
-			foreach ( $this->json->annotations as $annotation ) {
262
-				if ( $annotation->entityMatches[0]->entityId === $entity_id ) {
263
-					$entities[ $entity_id ]['occurrences'][]                                  = $annotation->annotationId;
264
-					$entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId;
260
+		foreach ($entities as $entity_id => $entity) {
261
+			foreach ($this->json->annotations as $annotation) {
262
+				if ($annotation->entityMatches[0]->entityId === $entity_id) {
263
+					$entities[$entity_id]['occurrences'][]                                  = $annotation->annotationId;
264
+					$entities[$entity_id]['annotations'][$annotation->annotationId]['id'] = $annotation->annotationId;
265 265
 				}
266 266
 			}
267 267
 		}
@@ -293,19 +293,19 @@  discard block
 block discarded – undo
293 293
 	 * @return $this
294 294
 	 * @since 3.32.3.1
295 295
 	 */
296
-	public function remove_excluded_entities( $excluded_uris ) {
296
+	public function remove_excluded_entities($excluded_uris) {
297 297
 
298 298
 		// If we didnt receive array, return early.
299
-		if ( ! is_array( $excluded_uris ) ) {
299
+		if ( ! is_array($excluded_uris)) {
300 300
 			return $this;
301 301
 		}
302 302
 
303 303
 		// We may also receive an array of null, make sure to filter uris when receiving.
304
-		$excluded_uris = array_filter( $excluded_uris, 'is_string' );
304
+		$excluded_uris = array_filter($excluded_uris, 'is_string');
305 305
 
306
-		$this->remove_entities_with_excluded_uris( $excluded_uris );
306
+		$this->remove_entities_with_excluded_uris($excluded_uris);
307 307
 
308
-		$this->remove_annotations_with_excluded_uris( $excluded_uris );
308
+		$this->remove_annotations_with_excluded_uris($excluded_uris);
309 309
 
310 310
 		return $this;
311 311
 	}
@@ -319,24 +319,24 @@  discard block
 block discarded – undo
319 319
 	public function to_string() {
320 320
 
321 321
 		// Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+.
322
-		$options = ( version_compare( PHP_VERSION, '5.4', '>=' )
323
-			? 256 : 0 );
322
+		$options = (version_compare(PHP_VERSION, '5.4', '>=')
323
+			? 256 : 0);
324 324
 
325
-		return wp_json_encode( $this->json, $options );
325
+		return wp_json_encode($this->json, $options);
326 326
 	}
327 327
 
328 328
 	/**
329 329
 	 * Remove all the entities with the excluded URIs.
330 330
 	 * @param array $excluded_uris The array of URIs to be excluded.
331 331
 	 */
332
-	private function remove_entities_with_excluded_uris( array $excluded_uris ) {
332
+	private function remove_entities_with_excluded_uris(array $excluded_uris) {
333 333
 		// Remove the excluded entity uris.
334
-		if ( isset( $this->json->entities ) ) {
335
-			foreach ( $excluded_uris as $excluded_uri ) {
334
+		if (isset($this->json->entities)) {
335
+			foreach ($excluded_uris as $excluded_uri) {
336 336
 
337
-				if ( isset( $this->json->entities->{$excluded_uri} ) ) {
337
+				if (isset($this->json->entities->{$excluded_uri} )) {
338 338
 					// Remove this entity.
339
-					unset( $this->json->entities->{$excluded_uri} );
339
+					unset($this->json->entities->{$excluded_uri} );
340 340
 					// Also remove the annotations.
341 341
 				}
342 342
 			}
@@ -349,27 +349,27 @@  discard block
 block discarded – undo
349 349
 	 *
350 350
 	 * @return void
351 351
 	 */
352
-	private function remove_annotations_with_excluded_uris( array $excluded_uris ) {
353
-		if ( isset( $this->json->annotations ) ) {
354
-			foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) {
352
+	private function remove_annotations_with_excluded_uris(array $excluded_uris) {
353
+		if (isset($this->json->annotations)) {
354
+			foreach ($this->json->annotations as $annotation_key => &$annotation_data) {
355 355
 
356
-				if ( ! isset( $annotation_data->entityMatches ) ) {
356
+				if ( ! isset($annotation_data->entityMatches)) {
357 357
 					continue;
358 358
 				}
359 359
 
360
-				foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) {
360
+				foreach ($annotation_data->entityMatches as $entity_match_key => $entity_match_data) {
361 361
 
362 362
 					$entity_uri = $entity_match_data->entityId;
363 363
 
364
-					if ( ! in_array( $entity_uri, $excluded_uris ) ) {
364
+					if ( ! in_array($entity_uri, $excluded_uris)) {
365 365
 						continue;
366 366
 					}
367
-					unset( $annotation_data->entityMatches[ $entity_match_key ] );
367
+					unset($annotation_data->entityMatches[$entity_match_key]);
368 368
 				}
369 369
 
370
-				if ( count( $annotation_data->entityMatches ) === 0 ) {
370
+				if (count($annotation_data->entityMatches) === 0) {
371 371
 					// Remove the annotation if we have zero empty annotation matches.
372
-					unset( $this->json->annotations->{$annotation_key} );
372
+					unset($this->json->annotations->{$annotation_key} );
373 373
 				}
374 374
 
375 375
 			}
Please login to merge, or discard this patch.
src/wordlift/analysis/response/class-analysis-response-ops-factory.php 2 patches
Indentation   +67 added lines, -67 removed lines patch added patch discarded remove patch
@@ -13,83 +13,83 @@
 block discarded – undo
13 13
 use Wordlift\Entity\Entity_Helper;
14 14
 
15 15
 class Analysis_Response_Ops_Factory {
16
-	/**
17
-	 * @var \Wordlift_Entity_Uri_Service
18
-	 */
19
-	private $entity_uri_service;
20
-	/**
21
-	 * @var \Wordlift_Entity_Service
22
-	 */
23
-	private $entity_service;
24
-	/**
25
-	 * @var \Wordlift_Entity_Type_Service
26
-	 */
27
-	private $entity_type_service;
28
-	/**
29
-	 * @var \Wordlift_Post_Image_Storage
30
-	 */
31
-	private $post_image_storage;
32
-	/**
33
-	 * @var Entity_Helper
34
-	 */
35
-	private $entity_helper;
16
+    /**
17
+     * @var \Wordlift_Entity_Uri_Service
18
+     */
19
+    private $entity_uri_service;
20
+    /**
21
+     * @var \Wordlift_Entity_Service
22
+     */
23
+    private $entity_service;
24
+    /**
25
+     * @var \Wordlift_Entity_Type_Service
26
+     */
27
+    private $entity_type_service;
28
+    /**
29
+     * @var \Wordlift_Post_Image_Storage
30
+     */
31
+    private $post_image_storage;
32
+    /**
33
+     * @var Entity_Helper
34
+     */
35
+    private $entity_helper;
36 36
 
37
-	private static $instance;
38
-	/**
39
-	 * @var Entity_Provider_Registry
40
-	 */
41
-	private $entity_provider_registry;
37
+    private static $instance;
38
+    /**
39
+     * @var Entity_Provider_Registry
40
+     */
41
+    private $entity_provider_registry;
42 42
 
43
-	/**
44
-	 * Analysis_Response_Ops constructor.
45
-	 *
46
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}.
47
-	 * @param Entity_Helper $entity_helper The {@link Entity_Helper}.
48
-	 *
49
-	 * @since 3.25.1
50
-	 */
51
-	public function __construct( $entity_uri_service, $entity_helper, $entity_provider_registry ) {
43
+    /**
44
+     * Analysis_Response_Ops constructor.
45
+     *
46
+     * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}.
47
+     * @param Entity_Helper $entity_helper The {@link Entity_Helper}.
48
+     *
49
+     * @since 3.25.1
50
+     */
51
+    public function __construct( $entity_uri_service, $entity_helper, $entity_provider_registry ) {
52 52
 
53
-		$this->entity_uri_service       = $entity_uri_service;
54
-		$this->entity_helper            = $entity_helper;
55
-		$this->entity_provider_registry = $entity_provider_registry;
56
-		self::$instance                 = $this;
53
+        $this->entity_uri_service       = $entity_uri_service;
54
+        $this->entity_helper            = $entity_helper;
55
+        $this->entity_provider_registry = $entity_provider_registry;
56
+        self::$instance                 = $this;
57 57
 
58
-	}
58
+    }
59 59
 
60
-	public static function get_instance() {
60
+    public static function get_instance() {
61 61
 
62
-		return self::$instance;
63
-	}
62
+        return self::$instance;
63
+    }
64 64
 
65
-	public function create( $json, $post_id ) {
65
+    public function create( $json, $post_id ) {
66 66
 
67
-		return new Analysis_Response_Ops(
68
-			$this->entity_uri_service,
69
-			$this->entity_helper,
70
-			$this->entity_provider_registry,
71
-			$json,
72
-			$post_id );
73
-	}
67
+        return new Analysis_Response_Ops(
68
+            $this->entity_uri_service,
69
+            $this->entity_helper,
70
+            $this->entity_provider_registry,
71
+            $json,
72
+            $post_id );
73
+    }
74 74
 
75
-	/**
76
-	 * Create an Analysis_Response_Ops instance given the provided http response.
77
-	 *
78
-	 * @param array $response {
79
-	 *
80
-	 * @type string $body The response body.
81
-	 * }
82
-	 *
83
-	 * @return Analysis_Response_Ops A new Analysis_Response_Ops instance.
84
-	 * @throws \Exception if the provided response doesn't contain a `body` element.
85
-	 */
86
-	public function create_with_response( $response, $post_id ) {
75
+    /**
76
+     * Create an Analysis_Response_Ops instance given the provided http response.
77
+     *
78
+     * @param array $response {
79
+     *
80
+     * @type string $body The response body.
81
+     * }
82
+     *
83
+     * @return Analysis_Response_Ops A new Analysis_Response_Ops instance.
84
+     * @throws \Exception if the provided response doesn't contain a `body` element.
85
+     */
86
+    public function create_with_response( $response, $post_id ) {
87 87
 
88
-		if ( ! isset( $response['body'] ) ) {
89
-			throw new \Exception( "`body` is required in response." );
90
-		}
88
+        if ( ! isset( $response['body'] ) ) {
89
+            throw new \Exception( "`body` is required in response." );
90
+        }
91 91
 
92
-		return $this->create( json_decode( $response['body'] ), $post_id );
93
-	}
92
+        return $this->create( json_decode( $response['body'] ), $post_id );
93
+    }
94 94
 
95 95
 }
96 96
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
 	 *
49 49
 	 * @since 3.25.1
50 50
 	 */
51
-	public function __construct( $entity_uri_service, $entity_helper, $entity_provider_registry ) {
51
+	public function __construct($entity_uri_service, $entity_helper, $entity_provider_registry) {
52 52
 
53 53
 		$this->entity_uri_service       = $entity_uri_service;
54 54
 		$this->entity_helper            = $entity_helper;
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
 		return self::$instance;
63 63
 	}
64 64
 
65
-	public function create( $json, $post_id ) {
65
+	public function create($json, $post_id) {
66 66
 
67 67
 		return new Analysis_Response_Ops(
68 68
 			$this->entity_uri_service,
@@ -83,13 +83,13 @@  discard block
 block discarded – undo
83 83
 	 * @return Analysis_Response_Ops A new Analysis_Response_Ops instance.
84 84
 	 * @throws \Exception if the provided response doesn't contain a `body` element.
85 85
 	 */
86
-	public function create_with_response( $response, $post_id ) {
86
+	public function create_with_response($response, $post_id) {
87 87
 
88
-		if ( ! isset( $response['body'] ) ) {
89
-			throw new \Exception( "`body` is required in response." );
88
+		if ( ! isset($response['body'])) {
89
+			throw new \Exception("`body` is required in response.");
90 90
 		}
91 91
 
92
-		return $this->create( json_decode( $response['body'] ), $post_id );
92
+		return $this->create(json_decode($response['body']), $post_id);
93 93
 	}
94 94
 
95 95
 }
96 96
\ No newline at end of file
Please login to merge, or discard this patch.
src/wordlift/analysis/class-no-editor-analysis-service.php 2 patches
Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -13,12 +13,12 @@
 block discarded – undo
13 13
  */
14 14
 class No_Editor_Analysis_Service extends Abstract_Analysis_Service {
15 15
 
16
-	public function get_analysis_response( $data, $content_type, $post_id ) {
16
+    public function get_analysis_response( $data, $content_type, $post_id ) {
17 17
 
18
-		$v2_analysis_request = new No_Editor_Analysis_Request( $post_id );
18
+        $v2_analysis_request = new No_Editor_Analysis_Request( $post_id );
19 19
 
20
-		$json_encoded_body = wp_json_encode( $v2_analysis_request->get_data() );
20
+        $json_encoded_body = wp_json_encode( $v2_analysis_request->get_data() );
21 21
 
22
-		return $this->api_service->post_custom_content_type( 'analysis/v2/analyze', $json_encoded_body, $content_type );
23
-	}
22
+        return $this->api_service->post_custom_content_type( 'analysis/v2/analyze', $json_encoded_body, $content_type );
23
+    }
24 24
 }
25 25
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -13,12 +13,12 @@
 block discarded – undo
13 13
  */
14 14
 class No_Editor_Analysis_Service extends Abstract_Analysis_Service {
15 15
 
16
-	public function get_analysis_response( $data, $content_type, $post_id ) {
16
+	public function get_analysis_response($data, $content_type, $post_id) {
17 17
 
18
-		$v2_analysis_request = new No_Editor_Analysis_Request( $post_id );
18
+		$v2_analysis_request = new No_Editor_Analysis_Request($post_id);
19 19
 
20
-		$json_encoded_body = wp_json_encode( $v2_analysis_request->get_data() );
20
+		$json_encoded_body = wp_json_encode($v2_analysis_request->get_data());
21 21
 
22
-		return $this->api_service->post_custom_content_type( 'analysis/v2/analyze', $json_encoded_body, $content_type );
22
+		return $this->api_service->post_custom_content_type('analysis/v2/analyze', $json_encoded_body, $content_type);
23 23
 	}
24 24
 }
25 25
\ No newline at end of file
Please login to merge, or discard this patch.
src/wordlift/analysis/class-analysis-service-factory.php 2 patches
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -17,18 +17,18 @@
 block discarded – undo
17 17
  */
18 18
 class Analysis_Service_Factory {
19 19
 
20
-	/**
21
-	 * Get the analysis service conditionally.
22
-	 *
23
-	 * @return Analysis_Service
24
-	 */
25
-	public static function get_instance( $post_id ) {
26
-		// We want this analysis to happen only when the editor is not present.
27
-		if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) {
28
-			return No_Editor_Analysis_Service::get_instance();
29
-		}
20
+    /**
21
+     * Get the analysis service conditionally.
22
+     *
23
+     * @return Analysis_Service
24
+     */
25
+    public static function get_instance( $post_id ) {
26
+        // We want this analysis to happen only when the editor is not present.
27
+        if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) {
28
+            return No_Editor_Analysis_Service::get_instance();
29
+        }
30 30
 
31
-		return V1_Analysis_Service::get_instance();
32
-	}
31
+        return V1_Analysis_Service::get_instance();
32
+    }
33 33
 
34 34
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -22,9 +22,9 @@
 block discarded – undo
22 22
 	 *
23 23
 	 * @return Analysis_Service
24 24
 	 */
25
-	public static function get_instance( $post_id ) {
25
+	public static function get_instance($post_id) {
26 26
 		// We want this analysis to happen only when the editor is not present.
27
-		if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) {
27
+		if (No_Editor_Analysis_Feature::can_no_editor_analysis_be_used($post_id)) {
28 28
 			return No_Editor_Analysis_Service::get_instance();
29 29
 		}
30 30
 
Please login to merge, or discard this patch.
src/wordlift/analysis/class-abstract-analysis-service.php 1 patch
Indentation   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -15,19 +15,19 @@
 block discarded – undo
15 15
  */
16 16
 abstract class Abstract_Analysis_Service extends Singleton implements  Analysis_Service {
17 17
 
18
-	/**
19
-	 * Wordlift api service.
20
-	 *
21
-	 * @var \Wordlift_Api_Service
22
-	 */
23
-	protected $api_service;
18
+    /**
19
+     * Wordlift api service.
20
+     *
21
+     * @var \Wordlift_Api_Service
22
+     */
23
+    protected $api_service;
24 24
 
25
-	/**
26
-	 * Abstract_Analysis_Service constructor.
27
-	 */
28
-	public function __construct() {
29
-		parent::__construct();
30
-		$this->api_service = \Wordlift_Api_Service::get_instance();
31
-	}
25
+    /**
26
+     * Abstract_Analysis_Service constructor.
27
+     */
28
+    public function __construct() {
29
+        parent::__construct();
30
+        $this->api_service = \Wordlift_Api_Service::get_instance();
31
+    }
32 32
 
33 33
 }
Please login to merge, or discard this patch.
src/wordlift/analysis/occurrences/class-occurrences-factory.php 2 patches
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -10,17 +10,17 @@
 block discarded – undo
10 10
 
11 11
 class Occurrences_Factory {
12 12
 
13
-	/**
14
-	 * @param $post_id
15
-	 *
16
-	 * @return Occurrences
17
-	 */
18
-	public static function get_instance( $post_id ) {
19
-		if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) {
20
-			return No_Annotation_Strategy::get_instance();
21
-		}
22
-		return Default_Strategy::get_instance();
23
-	}
13
+    /**
14
+     * @param $post_id
15
+     *
16
+     * @return Occurrences
17
+     */
18
+    public static function get_instance( $post_id ) {
19
+        if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) {
20
+            return No_Annotation_Strategy::get_instance();
21
+        }
22
+        return Default_Strategy::get_instance();
23
+    }
24 24
 
25 25
 
26 26
 }
27 27
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -15,8 +15,8 @@
 block discarded – undo
15 15
 	 *
16 16
 	 * @return Occurrences
17 17
 	 */
18
-	public static function get_instance( $post_id ) {
19
-		if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) {
18
+	public static function get_instance($post_id) {
19
+		if (No_Editor_Analysis_Feature::can_no_editor_analysis_be_used($post_id)) {
20 20
 			return No_Annotation_Strategy::get_instance();
21 21
 		}
22 22
 		return Default_Strategy::get_instance();
Please login to merge, or discard this patch.
src/wordlift/analysis/class-no-editor-analysis-request.php 2 patches
Indentation   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -11,35 +11,35 @@
 block discarded – undo
11 11
 class No_Editor_Analysis_Request {
12 12
 
13 13
 
14
-	private $post_id;
14
+    private $post_id;
15 15
 
16
-	public function __construct( $post_id ) {
17
-		$this->post_id = $post_id;
18
-	}
16
+    public function __construct( $post_id ) {
17
+        $this->post_id = $post_id;
18
+    }
19 19
 
20
-	/**
21
-	 * @return array
22
-	 */
23
-	public function get_data() {
20
+    /**
21
+     * @return array
22
+     */
23
+    public function get_data() {
24 24
 
25
-		$permalink = get_permalink( $this->post_id );
25
+        $permalink = get_permalink( $this->post_id );
26 26
 
27
-		// TODO: remove this Https.
28
-		$permalink = str_replace( 'https', 'http', $permalink );
29
-		// TODO: remove this Https.
27
+        // TODO: remove this Https.
28
+        $permalink = str_replace( 'https', 'http', $permalink );
29
+        // TODO: remove this Https.
30 30
 
31
-		$post_content_response = wp_remote_get( $permalink  );
31
+        $post_content_response = wp_remote_get( $permalink  );
32 32
 
33
-		$page_body = wp_remote_retrieve_body( $post_content_response );
33
+        $page_body = wp_remote_retrieve_body( $post_content_response );
34 34
 
35
-		return array(
36
-			'html'     => array( 'page' => $page_body ),
37
-			'language' => \Wordlift_Configuration_Service::get_instance()->get_language_code(),
38
-			'scope'    => 'all',
39
-			'matches'  => 1,
40
-			'links'    => 'no',
41
-		);
35
+        return array(
36
+            'html'     => array( 'page' => $page_body ),
37
+            'language' => \Wordlift_Configuration_Service::get_instance()->get_language_code(),
38
+            'scope'    => 'all',
39
+            'matches'  => 1,
40
+            'links'    => 'no',
41
+        );
42 42
 
43
-	}
43
+    }
44 44
 
45 45
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
 
14 14
 	private $post_id;
15 15
 
16
-	public function __construct( $post_id ) {
16
+	public function __construct($post_id) {
17 17
 		$this->post_id = $post_id;
18 18
 	}
19 19
 
@@ -22,18 +22,18 @@  discard block
 block discarded – undo
22 22
 	 */
23 23
 	public function get_data() {
24 24
 
25
-		$permalink = get_permalink( $this->post_id );
25
+		$permalink = get_permalink($this->post_id);
26 26
 
27 27
 		// TODO: remove this Https.
28
-		$permalink = str_replace( 'https', 'http', $permalink );
28
+		$permalink = str_replace('https', 'http', $permalink);
29 29
 		// TODO: remove this Https.
30 30
 
31
-		$post_content_response = wp_remote_get( $permalink  );
31
+		$post_content_response = wp_remote_get($permalink);
32 32
 
33
-		$page_body = wp_remote_retrieve_body( $post_content_response );
33
+		$page_body = wp_remote_retrieve_body($post_content_response);
34 34
 
35 35
 		return array(
36
-			'html'     => array( 'page' => $page_body ),
36
+			'html'     => array('page' => $page_body),
37 37
 			'language' => \Wordlift_Configuration_Service::get_instance()->get_language_code(),
38 38
 			'scope'    => 'all',
39 39
 			'matches'  => 1,
Please login to merge, or discard this patch.
src/wordlift/analysis/class-analysis-service.php 2 patches
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -14,15 +14,15 @@
 block discarded – undo
14 14
  */
15 15
 interface Analysis_Service {
16 16
 
17
-	/**
18
-	 * Return analysis response from the service.
19
-	 *
20
-	 * @param array  $data The analysis data.
21
-	 * @param String $content_type Content type for the request.
22
-	 * @param int    $post_id Post id.
23
-	 *
24
-	 * @return string|object|\WP_Error A {@link WP_Error} instance or the actual response content.
25
-	 */
26
-	public function get_analysis_response( $data, $content_type, $post_id );
17
+    /**
18
+     * Return analysis response from the service.
19
+     *
20
+     * @param array  $data The analysis data.
21
+     * @param String $content_type Content type for the request.
22
+     * @param int    $post_id Post id.
23
+     *
24
+     * @return string|object|\WP_Error A {@link WP_Error} instance or the actual response content.
25
+     */
26
+    public function get_analysis_response( $data, $content_type, $post_id );
27 27
 
28 28
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -23,6 +23,6 @@
 block discarded – undo
23 23
 	 *
24 24
 	 * @return string|object|\WP_Error A {@link WP_Error} instance or the actual response content.
25 25
 	 */
26
-	public function get_analysis_response( $data, $content_type, $post_id );
26
+	public function get_analysis_response($data, $content_type, $post_id);
27 27
 
28 28
 }
Please login to merge, or discard this patch.