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