Completed
Pull Request — develop (#1721)
by
unknown
01:39
created
src/classes/jsonld/class-jsonld-article-wrapper.php 1 patch
Indentation   +178 added lines, -178 removed lines patch added patch discarded remove patch
@@ -7,193 +7,193 @@
 block discarded – undo
7 7
 
8 8
 class Jsonld_Article_Wrapper {
9 9
 
10
-	public static $article_types = array(
11
-		'Article',
12
-		'AdvertiserContentArticle',
13
-		'NewsArticle',
14
-		'AnalysisNewsArticle',
15
-		'AskPublicNewsArticle',
16
-		'BackgroundNewsArticle',
17
-		'OpinionNewsArticle',
18
-		'ReportageNewsArticle',
19
-		'ReviewNewsArticle',
20
-		'Report',
21
-		'SatiricalArticle',
22
-		'ScholarlyArticle',
23
-		'MedicalScholarlyArticle',
24
-		'SocialMediaPosting',
25
-		'BlogPosting',
26
-		'LiveBlogPosting',
27
-		'DiscussionForumPosting',
28
-		'TechArticle',
29
-		'APIReference',
30
-	);
31
-
32
-	/**
33
-	 * @var Wordlift_Post_To_Jsonld_Converter
34
-	 */
35
-	private $post_to_jsonld_converter;
36
-	/**
37
-	 * @var \Wordlift_Cached_Post_Converter
38
-	 */
39
-	private $cached_postid_to_jsonld_converter;
40
-	/**
41
-	 * @var \Wordlift_Entity_Uri_Service
42
-	 */
43
-	private $entity_uri_service;
44
-
45
-	/**
46
-	 * Jsonld_Article_Wrapper constructor.
47
-	 *
48
-	 * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter
49
-	 * @param $cached_postid_to_jsonld_converter
50
-	 */
51
-	public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) {
52
-
53
-		$this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled();
54
-
55
-		add_filter(
56
-			'wl_after_get_jsonld',
57
-			array(
58
-				$this,
59
-				'after_get_jsonld',
60
-			),
61
-			PHP_INT_MAX - 100,
62
-			3
63
-		);
64
-
65
-		$this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter;
66
-
67
-		$this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance();
68
-	}
69
-
70
-	public function after_get_jsonld( $jsonld, $post_id, $context ) {
71
-
72
-		// Invalid data structure
73
-		if ( ! is_array( $jsonld ) || ! isset( $jsonld[0] ) || ! is_array( $jsonld[0] ) ) {
74
-			return $jsonld;
75
-		}
76
-
77
-		if ( Jsonld_Context_Enum::PAGE !== $context
78
-			 // Returns true for "1", "true", "on" and "yes". Returns false otherwise.
79
-			 && ! filter_input( INPUT_GET, 'article_wrapper', FILTER_VALIDATE_BOOLEAN ) ) {
80
-			return $jsonld;
81
-		}
82
-
83
-		// Copy the 1st array element
84
-		$post_jsonld = $jsonld[0];
85
-
86
-		// Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be
87
-		// in the schema.org context, i.e. `Article`, not `http://schema.org/Article`.
88
-		if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) {
89
-			return $jsonld;
90
-		}
91
-
92
-		$references      = array();
93
-		$reference_infos = array();
94
-
95
-		// Convert the post as Article.
96
-		$article_jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $reference_infos, new Relations() );
97
-
98
-		$article_jsonld['@id'] = $post_jsonld['@id'] . '#article';
99
-		// Reset the type, since by default the type assigned via the Entity Type taxonomy is used.
100
-		$article_jsonld['@type'] = 'Article';
101
-		$article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] );
102
-
103
-		// Copy over the URLs.
104
-		if ( isset( $post_jsonld['url'] ) ) {
105
-			$article_jsonld['url'] = $post_jsonld['url'];
106
-		}
107
-
108
-		array_unshift( $jsonld, $article_jsonld );
109
-
110
-		$author_jsonld = $this->get_author_linked_entity( $article_jsonld );
111
-
112
-		if ( $author_jsonld ) {
113
-			// Get primary author in case co-authors exist.
114
-			$primary_author = $this->get_primary_author( $article_jsonld['author'] );
115
-
116
-			/**
117
-			 * The author entities can be present in graph for some entity types
118
-			 * for Person and Organization, so check before we add it to graph.
119
-			 * reference : https://schema.org/author
120
-			 */
121
-			if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $primary_author['@id'] ) ) {
122
-				$jsonld[] = $author_jsonld;
123
-			}
124
-		}
125
-
126
-		return $jsonld;
127
-	}
128
-
129
-	private function is_article( $schema_types ) {
130
-
131
-		$array_intersect = array_intersect( self::$article_types, (array) $schema_types );
132
-
133
-		return ! empty( $array_intersect );
134
-	}
135
-
136
-	/**
137
-	 * Get primary author from author array.
138
-	 *
139
-	 * A helper function that checks the structure of the author array and returns the primary author.
140
-	 *
141
-	 * @param array|string $author The author array.
142
-	 *
143
-	 * @return array|string The primary author.
144
-	 *
145
-	 * @since 3.51.4
146
-	 */
147
-	private function get_primary_author( $author ) {
148
-
149
-		// Nested array of co-authors. Return the primary author.
150
-		if ( is_array( $author ) && ! empty( $author ) && ! isset( $author['@id'] ) ) {
151
-			return $author[0];
152
-		}
153
-
154
-		return $author;
155
-	}
156
-
157
-	private function get_author_linked_entity( $article_jsonld ) {
158
-		if ( ! array_key_exists( 'author', $article_jsonld ) ) {
159
-			return false;
160
-		}
10
+    public static $article_types = array(
11
+        'Article',
12
+        'AdvertiserContentArticle',
13
+        'NewsArticle',
14
+        'AnalysisNewsArticle',
15
+        'AskPublicNewsArticle',
16
+        'BackgroundNewsArticle',
17
+        'OpinionNewsArticle',
18
+        'ReportageNewsArticle',
19
+        'ReviewNewsArticle',
20
+        'Report',
21
+        'SatiricalArticle',
22
+        'ScholarlyArticle',
23
+        'MedicalScholarlyArticle',
24
+        'SocialMediaPosting',
25
+        'BlogPosting',
26
+        'LiveBlogPosting',
27
+        'DiscussionForumPosting',
28
+        'TechArticle',
29
+        'APIReference',
30
+    );
31
+
32
+    /**
33
+     * @var Wordlift_Post_To_Jsonld_Converter
34
+     */
35
+    private $post_to_jsonld_converter;
36
+    /**
37
+     * @var \Wordlift_Cached_Post_Converter
38
+     */
39
+    private $cached_postid_to_jsonld_converter;
40
+    /**
41
+     * @var \Wordlift_Entity_Uri_Service
42
+     */
43
+    private $entity_uri_service;
44
+
45
+    /**
46
+     * Jsonld_Article_Wrapper constructor.
47
+     *
48
+     * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter
49
+     * @param $cached_postid_to_jsonld_converter
50
+     */
51
+    public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) {
52
+
53
+        $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled();
54
+
55
+        add_filter(
56
+            'wl_after_get_jsonld',
57
+            array(
58
+                $this,
59
+                'after_get_jsonld',
60
+            ),
61
+            PHP_INT_MAX - 100,
62
+            3
63
+        );
64
+
65
+        $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter;
66
+
67
+        $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance();
68
+    }
69
+
70
+    public function after_get_jsonld( $jsonld, $post_id, $context ) {
71
+
72
+        // Invalid data structure
73
+        if ( ! is_array( $jsonld ) || ! isset( $jsonld[0] ) || ! is_array( $jsonld[0] ) ) {
74
+            return $jsonld;
75
+        }
76
+
77
+        if ( Jsonld_Context_Enum::PAGE !== $context
78
+                // Returns true for "1", "true", "on" and "yes". Returns false otherwise.
79
+             && ! filter_input( INPUT_GET, 'article_wrapper', FILTER_VALIDATE_BOOLEAN ) ) {
80
+            return $jsonld;
81
+        }
82
+
83
+        // Copy the 1st array element
84
+        $post_jsonld = $jsonld[0];
85
+
86
+        // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be
87
+        // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`.
88
+        if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) {
89
+            return $jsonld;
90
+        }
91
+
92
+        $references      = array();
93
+        $reference_infos = array();
94
+
95
+        // Convert the post as Article.
96
+        $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $reference_infos, new Relations() );
97
+
98
+        $article_jsonld['@id'] = $post_jsonld['@id'] . '#article';
99
+        // Reset the type, since by default the type assigned via the Entity Type taxonomy is used.
100
+        $article_jsonld['@type'] = 'Article';
101
+        $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] );
102
+
103
+        // Copy over the URLs.
104
+        if ( isset( $post_jsonld['url'] ) ) {
105
+            $article_jsonld['url'] = $post_jsonld['url'];
106
+        }
107
+
108
+        array_unshift( $jsonld, $article_jsonld );
109
+
110
+        $author_jsonld = $this->get_author_linked_entity( $article_jsonld );
111
+
112
+        if ( $author_jsonld ) {
113
+            // Get primary author in case co-authors exist.
114
+            $primary_author = $this->get_primary_author( $article_jsonld['author'] );
115
+
116
+            /**
117
+             * The author entities can be present in graph for some entity types
118
+             * for Person and Organization, so check before we add it to graph.
119
+             * reference : https://schema.org/author
120
+             */
121
+            if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $primary_author['@id'] ) ) {
122
+                $jsonld[] = $author_jsonld;
123
+            }
124
+        }
125
+
126
+        return $jsonld;
127
+    }
128
+
129
+    private function is_article( $schema_types ) {
130
+
131
+        $array_intersect = array_intersect( self::$article_types, (array) $schema_types );
132
+
133
+        return ! empty( $array_intersect );
134
+    }
135
+
136
+    /**
137
+     * Get primary author from author array.
138
+     *
139
+     * A helper function that checks the structure of the author array and returns the primary author.
140
+     *
141
+     * @param array|string $author The author array.
142
+     *
143
+     * @return array|string The primary author.
144
+     *
145
+     * @since 3.51.4
146
+     */
147
+    private function get_primary_author( $author ) {
148
+
149
+        // Nested array of co-authors. Return the primary author.
150
+        if ( is_array( $author ) && ! empty( $author ) && ! isset( $author['@id'] ) ) {
151
+            return $author[0];
152
+        }
153
+
154
+        return $author;
155
+    }
156
+
157
+    private function get_author_linked_entity( $article_jsonld ) {
158
+        if ( ! array_key_exists( 'author', $article_jsonld ) ) {
159
+            return false;
160
+        }
161 161
 
162
-		$author = $this->get_primary_author( $article_jsonld['author'] );
162
+        $author = $this->get_primary_author( $article_jsonld['author'] );
163 163
 
164
-		if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) {
165
-			return false;
166
-		}
164
+        if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) {
165
+            return false;
166
+        }
167 167
 
168
-		$author_linked_entity_id = $author['@id'];
168
+        $author_linked_entity_id = $author['@id'];
169 169
 
170
-		$author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id );
170
+        $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id );
171 171
 
172
-		if ( ! $author_entity_post instanceof \WP_Post ) {
173
-			return false;
174
-		}
172
+        if ( ! $author_entity_post instanceof \WP_Post ) {
173
+            return false;
174
+        }
175 175
 
176
-		$references      = array();
177
-		$reference_infos = array();
178
-
179
-		return $this->cached_postid_to_jsonld_converter->convert(
180
-			$author_entity_post->ID,
181
-			$references,
182
-			$reference_infos,
183
-			new Relations()
184
-		);
176
+        $references      = array();
177
+        $reference_infos = array();
178
+
179
+        return $this->cached_postid_to_jsonld_converter->convert(
180
+            $author_entity_post->ID,
181
+            $references,
182
+            $reference_infos,
183
+            new Relations()
184
+        );
185 185
 
186
-	}
186
+    }
187 187
 
188
-	private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) {
188
+    private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) {
189 189
 
190
-		foreach ( $jsonld as $item ) {
191
-			if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) {
192
-				return true;
193
-			}
194
-		}
190
+        foreach ( $jsonld as $item ) {
191
+            if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) {
192
+                return true;
193
+            }
194
+        }
195 195
 
196
-		return false;
197
-	}
196
+        return false;
197
+    }
198 198
 
199 199
 }
Please login to merge, or discard this patch.