@@ -11,20 +11,20 @@ |
||
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 | } |
@@ -12,14 +12,14 @@ |
||
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 | } |
@@ -13,68 +13,68 @@ |
||
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 | } |
@@ -31,7 +31,7 @@ discard block |
||
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 |
||
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 |
||
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 | } |
@@ -16,382 +16,382 @@ |
||
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 | } |
@@ -53,7 +53,7 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 | } |
@@ -4,136 +4,136 @@ |
||
4 | 4 | |
5 | 5 | class Analysis_Background_Service { |
6 | 6 | |
7 | - const ANALYSIS_DONE_FLAG = '_wl_cmkg_analysis_complete_for_term_options_cache'; |
|
8 | - const TERMS_COUNT_TRANSIENT = '_wl_cmkg_analysis_background_service_terms_count'; |
|
9 | - const ENTITIES_PRESENT_FOR_TERM = '_wl_cmkg_analysis_entities_present_for_term_options_cache'; |
|
10 | - |
|
11 | - /** |
|
12 | - * @var Analysis_Service |
|
13 | - */ |
|
14 | - private $analysis_service; |
|
15 | - /** |
|
16 | - * @var Analysis_Background_Process |
|
17 | - */ |
|
18 | - private $analysis_background_process; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var \Wordlift_Log_Service |
|
22 | - */ |
|
23 | - private $log; |
|
24 | - |
|
25 | - public function __construct( $analysis_service ) { |
|
26 | - |
|
27 | - $this->analysis_service = $analysis_service; |
|
28 | - |
|
29 | - $this->analysis_background_process = new Analysis_Background_Process( $this ); |
|
30 | - |
|
31 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
32 | - } |
|
33 | - |
|
34 | - public function start() { |
|
35 | - $this->analysis_background_process->start(); |
|
36 | - } |
|
37 | - |
|
38 | - public function cancel() { |
|
39 | - $this->analysis_background_process->cancel(); |
|
40 | - } |
|
41 | - |
|
42 | - public function stop() { |
|
43 | - $this->analysis_background_process->cancel(); |
|
44 | - $this->analysis_background_process->request_cancel(); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * A list of term ids. |
|
49 | - * |
|
50 | - * @return int|\WP_Error|\WP_Term[] |
|
51 | - */ |
|
52 | - public function next() { |
|
53 | - |
|
54 | - return Terms_Compat::get_terms( |
|
55 | - Terms_Compat::get_public_taxonomies(), |
|
56 | - array( |
|
57 | - 'fields' => 'ids', |
|
58 | - 'hide_empty' => false, |
|
59 | - 'number' => $this->get_batch_size(), |
|
60 | - // 'offset' => $state->index, |
|
61 | - 'meta_query' => array( |
|
62 | - array( |
|
63 | - 'key' => self::ANALYSIS_DONE_FLAG, |
|
64 | - 'compare' => 'NOT EXISTS', |
|
65 | - ), |
|
66 | - ), |
|
67 | - ) |
|
68 | - ); |
|
69 | - } |
|
70 | - |
|
71 | - public function count() { |
|
72 | - |
|
73 | - $count = count( |
|
74 | - Terms_Compat::get_terms( |
|
75 | - Terms_Compat::get_public_taxonomies(), |
|
76 | - array( |
|
77 | - 'fields' => 'ids', |
|
78 | - 'hide_empty' => false, |
|
79 | - // return all terms, we cant pass -1 here. |
|
80 | - 'number' => 0, |
|
81 | - 'meta_query' => array( |
|
82 | - array( |
|
83 | - 'key' => self::ANALYSIS_DONE_FLAG, |
|
84 | - 'compare' => 'NOT EXISTS', |
|
85 | - ), |
|
86 | - ), |
|
87 | - ) |
|
88 | - ) |
|
89 | - ); |
|
90 | - |
|
91 | - $this->log->debug( "Count returned as $count" ); |
|
92 | - |
|
93 | - return $count; |
|
94 | - } |
|
95 | - |
|
96 | - public function get_batch_size() { |
|
97 | - return 10; |
|
98 | - } |
|
99 | - |
|
100 | - public function info() { |
|
101 | - return Analysis_Background_Process::get_state(); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @param $term_ids |
|
106 | - * |
|
107 | - * @return bool |
|
108 | - */ |
|
109 | - public function perform_analysis_for_terms( $term_ids ) { |
|
110 | - |
|
111 | - foreach ( $term_ids as $term_id ) { |
|
112 | - |
|
113 | - $tag = get_term( $term_id ); |
|
114 | - |
|
115 | - // This adds the entities to ttl cache |
|
116 | - $result = $this->analysis_service->get_entities( $tag ); |
|
117 | - |
|
118 | - // then set the analysis complete flag. |
|
119 | - update_term_meta( $term_id, self::ANALYSIS_DONE_FLAG, 1 ); |
|
120 | - |
|
121 | - if ( false !== $result ) { |
|
122 | - if ( count( $result ) > 0 ) { |
|
123 | - update_term_meta( $term_id, self::ENTITIES_PRESENT_FOR_TERM, 1 ); |
|
124 | - } |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * This action fires when the analysis is complete for the current batch |
|
130 | - * |
|
131 | - * @since 3.30.0 |
|
132 | - */ |
|
133 | - do_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch' ); |
|
134 | - |
|
135 | - return true; |
|
136 | - |
|
137 | - } |
|
7 | + const ANALYSIS_DONE_FLAG = '_wl_cmkg_analysis_complete_for_term_options_cache'; |
|
8 | + const TERMS_COUNT_TRANSIENT = '_wl_cmkg_analysis_background_service_terms_count'; |
|
9 | + const ENTITIES_PRESENT_FOR_TERM = '_wl_cmkg_analysis_entities_present_for_term_options_cache'; |
|
10 | + |
|
11 | + /** |
|
12 | + * @var Analysis_Service |
|
13 | + */ |
|
14 | + private $analysis_service; |
|
15 | + /** |
|
16 | + * @var Analysis_Background_Process |
|
17 | + */ |
|
18 | + private $analysis_background_process; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var \Wordlift_Log_Service |
|
22 | + */ |
|
23 | + private $log; |
|
24 | + |
|
25 | + public function __construct( $analysis_service ) { |
|
26 | + |
|
27 | + $this->analysis_service = $analysis_service; |
|
28 | + |
|
29 | + $this->analysis_background_process = new Analysis_Background_Process( $this ); |
|
30 | + |
|
31 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
32 | + } |
|
33 | + |
|
34 | + public function start() { |
|
35 | + $this->analysis_background_process->start(); |
|
36 | + } |
|
37 | + |
|
38 | + public function cancel() { |
|
39 | + $this->analysis_background_process->cancel(); |
|
40 | + } |
|
41 | + |
|
42 | + public function stop() { |
|
43 | + $this->analysis_background_process->cancel(); |
|
44 | + $this->analysis_background_process->request_cancel(); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * A list of term ids. |
|
49 | + * |
|
50 | + * @return int|\WP_Error|\WP_Term[] |
|
51 | + */ |
|
52 | + public function next() { |
|
53 | + |
|
54 | + return Terms_Compat::get_terms( |
|
55 | + Terms_Compat::get_public_taxonomies(), |
|
56 | + array( |
|
57 | + 'fields' => 'ids', |
|
58 | + 'hide_empty' => false, |
|
59 | + 'number' => $this->get_batch_size(), |
|
60 | + // 'offset' => $state->index, |
|
61 | + 'meta_query' => array( |
|
62 | + array( |
|
63 | + 'key' => self::ANALYSIS_DONE_FLAG, |
|
64 | + 'compare' => 'NOT EXISTS', |
|
65 | + ), |
|
66 | + ), |
|
67 | + ) |
|
68 | + ); |
|
69 | + } |
|
70 | + |
|
71 | + public function count() { |
|
72 | + |
|
73 | + $count = count( |
|
74 | + Terms_Compat::get_terms( |
|
75 | + Terms_Compat::get_public_taxonomies(), |
|
76 | + array( |
|
77 | + 'fields' => 'ids', |
|
78 | + 'hide_empty' => false, |
|
79 | + // return all terms, we cant pass -1 here. |
|
80 | + 'number' => 0, |
|
81 | + 'meta_query' => array( |
|
82 | + array( |
|
83 | + 'key' => self::ANALYSIS_DONE_FLAG, |
|
84 | + 'compare' => 'NOT EXISTS', |
|
85 | + ), |
|
86 | + ), |
|
87 | + ) |
|
88 | + ) |
|
89 | + ); |
|
90 | + |
|
91 | + $this->log->debug( "Count returned as $count" ); |
|
92 | + |
|
93 | + return $count; |
|
94 | + } |
|
95 | + |
|
96 | + public function get_batch_size() { |
|
97 | + return 10; |
|
98 | + } |
|
99 | + |
|
100 | + public function info() { |
|
101 | + return Analysis_Background_Process::get_state(); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @param $term_ids |
|
106 | + * |
|
107 | + * @return bool |
|
108 | + */ |
|
109 | + public function perform_analysis_for_terms( $term_ids ) { |
|
110 | + |
|
111 | + foreach ( $term_ids as $term_id ) { |
|
112 | + |
|
113 | + $tag = get_term( $term_id ); |
|
114 | + |
|
115 | + // This adds the entities to ttl cache |
|
116 | + $result = $this->analysis_service->get_entities( $tag ); |
|
117 | + |
|
118 | + // then set the analysis complete flag. |
|
119 | + update_term_meta( $term_id, self::ANALYSIS_DONE_FLAG, 1 ); |
|
120 | + |
|
121 | + if ( false !== $result ) { |
|
122 | + if ( count( $result ) > 0 ) { |
|
123 | + update_term_meta( $term_id, self::ENTITIES_PRESENT_FOR_TERM, 1 ); |
|
124 | + } |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * This action fires when the analysis is complete for the current batch |
|
130 | + * |
|
131 | + * @since 3.30.0 |
|
132 | + */ |
|
133 | + do_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch' ); |
|
134 | + |
|
135 | + return true; |
|
136 | + |
|
137 | + } |
|
138 | 138 | |
139 | 139 | } |
@@ -22,13 +22,13 @@ discard block |
||
22 | 22 | */ |
23 | 23 | private $log; |
24 | 24 | |
25 | - public function __construct( $analysis_service ) { |
|
25 | + public function __construct($analysis_service) { |
|
26 | 26 | |
27 | 27 | $this->analysis_service = $analysis_service; |
28 | 28 | |
29 | - $this->analysis_background_process = new Analysis_Background_Process( $this ); |
|
29 | + $this->analysis_background_process = new Analysis_Background_Process($this); |
|
30 | 30 | |
31 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
31 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | public function start() { |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | ) |
89 | 89 | ); |
90 | 90 | |
91 | - $this->log->debug( "Count returned as $count" ); |
|
91 | + $this->log->debug("Count returned as $count"); |
|
92 | 92 | |
93 | 93 | return $count; |
94 | 94 | } |
@@ -106,21 +106,21 @@ discard block |
||
106 | 106 | * |
107 | 107 | * @return bool |
108 | 108 | */ |
109 | - public function perform_analysis_for_terms( $term_ids ) { |
|
109 | + public function perform_analysis_for_terms($term_ids) { |
|
110 | 110 | |
111 | - foreach ( $term_ids as $term_id ) { |
|
111 | + foreach ($term_ids as $term_id) { |
|
112 | 112 | |
113 | - $tag = get_term( $term_id ); |
|
113 | + $tag = get_term($term_id); |
|
114 | 114 | |
115 | 115 | // This adds the entities to ttl cache |
116 | - $result = $this->analysis_service->get_entities( $tag ); |
|
116 | + $result = $this->analysis_service->get_entities($tag); |
|
117 | 117 | |
118 | 118 | // then set the analysis complete flag. |
119 | - update_term_meta( $term_id, self::ANALYSIS_DONE_FLAG, 1 ); |
|
119 | + update_term_meta($term_id, self::ANALYSIS_DONE_FLAG, 1); |
|
120 | 120 | |
121 | - if ( false !== $result ) { |
|
122 | - if ( count( $result ) > 0 ) { |
|
123 | - update_term_meta( $term_id, self::ENTITIES_PRESENT_FOR_TERM, 1 ); |
|
121 | + if (false !== $result) { |
|
122 | + if (count($result) > 0) { |
|
123 | + update_term_meta($term_id, self::ENTITIES_PRESENT_FOR_TERM, 1); |
|
124 | 124 | } |
125 | 125 | } |
126 | 126 | } |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * |
131 | 131 | * @since 3.30.0 |
132 | 132 | */ |
133 | - do_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch' ); |
|
133 | + do_action('wordlift_vocabulary_analysis_complete_for_terms_batch'); |
|
134 | 134 | |
135 | 135 | return true; |
136 | 136 |
@@ -10,38 +10,38 @@ |
||
10 | 10 | */ |
11 | 11 | class Badge_Generator { |
12 | 12 | |
13 | - /** |
|
14 | - * Returns the term count which needs to be shown on ui. |
|
15 | - * |
|
16 | - * @param $number |
|
17 | - * |
|
18 | - * @return int |
|
19 | - */ |
|
20 | - public static function get_ui_count( $number ) { |
|
21 | - |
|
22 | - $number = (int) $number; |
|
23 | - |
|
24 | - if ( $number < 100 ) { |
|
25 | - return $number; |
|
26 | - } |
|
27 | - |
|
28 | - return 100; |
|
29 | - } |
|
30 | - |
|
31 | - public static function generate_html( $number ) { |
|
32 | - $count_string = self::get_formatted_count_string( $number ); |
|
33 | - return "<span class=\"wl-admin-menu-badge\">$count_string</span>"; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * @param $number |
|
38 | - * |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public static function get_formatted_count_string( $number ) { |
|
42 | - $round = self::get_ui_count( $number ); |
|
43 | - |
|
44 | - return $round < 100 ? "$round" : "$round+"; |
|
45 | - } |
|
13 | + /** |
|
14 | + * Returns the term count which needs to be shown on ui. |
|
15 | + * |
|
16 | + * @param $number |
|
17 | + * |
|
18 | + * @return int |
|
19 | + */ |
|
20 | + public static function get_ui_count( $number ) { |
|
21 | + |
|
22 | + $number = (int) $number; |
|
23 | + |
|
24 | + if ( $number < 100 ) { |
|
25 | + return $number; |
|
26 | + } |
|
27 | + |
|
28 | + return 100; |
|
29 | + } |
|
30 | + |
|
31 | + public static function generate_html( $number ) { |
|
32 | + $count_string = self::get_formatted_count_string( $number ); |
|
33 | + return "<span class=\"wl-admin-menu-badge\">$count_string</span>"; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * @param $number |
|
38 | + * |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public static function get_formatted_count_string( $number ) { |
|
42 | + $round = self::get_ui_count( $number ); |
|
43 | + |
|
44 | + return $round < 100 ? "$round" : "$round+"; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | } |
@@ -17,19 +17,19 @@ discard block |
||
17 | 17 | * |
18 | 18 | * @return int |
19 | 19 | */ |
20 | - public static function get_ui_count( $number ) { |
|
20 | + public static function get_ui_count($number) { |
|
21 | 21 | |
22 | 22 | $number = (int) $number; |
23 | 23 | |
24 | - if ( $number < 100 ) { |
|
24 | + if ($number < 100) { |
|
25 | 25 | return $number; |
26 | 26 | } |
27 | 27 | |
28 | 28 | return 100; |
29 | 29 | } |
30 | 30 | |
31 | - public static function generate_html( $number ) { |
|
32 | - $count_string = self::get_formatted_count_string( $number ); |
|
31 | + public static function generate_html($number) { |
|
32 | + $count_string = self::get_formatted_count_string($number); |
|
33 | 33 | return "<span class=\"wl-admin-menu-badge\">$count_string</span>"; |
34 | 34 | } |
35 | 35 | |
@@ -38,8 +38,8 @@ discard block |
||
38 | 38 | * |
39 | 39 | * @return string |
40 | 40 | */ |
41 | - public static function get_formatted_count_string( $number ) { |
|
42 | - $round = self::get_ui_count( $number ); |
|
41 | + public static function get_formatted_count_string($number) { |
|
42 | + $round = self::get_ui_count($number); |
|
43 | 43 | |
44 | 44 | return $round < 100 ? "$round" : "$round+"; |
45 | 45 | } |
@@ -12,54 +12,54 @@ |
||
12 | 12 | * @author Naveen Muthusamy <[email protected]> |
13 | 13 | */ |
14 | 14 | class Match_Terms { |
15 | - /** |
|
16 | - * @var Term_Count |
|
17 | - */ |
|
18 | - private $term_count; |
|
15 | + /** |
|
16 | + * @var Term_Count |
|
17 | + */ |
|
18 | + private $term_count; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Match_Terms constructor. |
|
22 | - * |
|
23 | - * @param $term_count Term_Count |
|
24 | - */ |
|
25 | - public function __construct( $term_count ) { |
|
20 | + /** |
|
21 | + * Match_Terms constructor. |
|
22 | + * |
|
23 | + * @param $term_count Term_Count |
|
24 | + */ |
|
25 | + public function __construct( $term_count ) { |
|
26 | 26 | |
27 | - $this->term_count = $term_count; |
|
28 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
27 | + $this->term_count = $term_count; |
|
28 | + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
29 | 29 | |
30 | - } |
|
30 | + } |
|
31 | 31 | |
32 | - public function admin_menu() { |
|
33 | - $number = $this->term_count->get_term_count(); |
|
34 | - add_submenu_page( |
|
35 | - 'wl_admin_menu', |
|
36 | - __( 'Match Terms', 'wordlift' ), |
|
37 | - __( 'Match Terms', 'wordlift' ) . ' ' . Badge_Generator::generate_html( $number ), |
|
38 | - 'manage_options', |
|
39 | - 'wl-vocabulary-match-terms', |
|
40 | - array( $this, 'submenu_page_callback' ) |
|
41 | - ); |
|
42 | - remove_submenu_page( 'wl_admin_menu', 'wl_admin_menu' ); |
|
43 | - } |
|
32 | + public function admin_menu() { |
|
33 | + $number = $this->term_count->get_term_count(); |
|
34 | + add_submenu_page( |
|
35 | + 'wl_admin_menu', |
|
36 | + __( 'Match Terms', 'wordlift' ), |
|
37 | + __( 'Match Terms', 'wordlift' ) . ' ' . Badge_Generator::generate_html( $number ), |
|
38 | + 'manage_options', |
|
39 | + 'wl-vocabulary-match-terms', |
|
40 | + array( $this, 'submenu_page_callback' ) |
|
41 | + ); |
|
42 | + remove_submenu_page( 'wl_admin_menu', 'wl_admin_menu' ); |
|
43 | + } |
|
44 | 44 | |
45 | - public function submenu_page_callback() { |
|
45 | + public function submenu_page_callback() { |
|
46 | 46 | |
47 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
48 | - 'wl-vocabulary-reconcile-script', |
|
49 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary', |
|
50 | - array( 'react', 'react-dom', 'wp-i18n', 'wp-polyfill' ), |
|
51 | - true |
|
52 | - ); |
|
47 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
48 | + 'wl-vocabulary-reconcile-script', |
|
49 | + plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary', |
|
50 | + array( 'react', 'react-dom', 'wp-i18n', 'wp-polyfill' ), |
|
51 | + true |
|
52 | + ); |
|
53 | 53 | |
54 | - wp_enqueue_style( |
|
55 | - 'wl-vocabulary-reconcile-script', |
|
56 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary.full.css', |
|
57 | - array(), |
|
58 | - WORDLIFT_VERSION |
|
59 | - ); |
|
60 | - wp_localize_script( 'wl-vocabulary-reconcile-script', '_wlVocabularyMatchTermsConfig', Api_Config::get_api_config() ); |
|
61 | - echo "<div id='wl_cmkg_reconcile_progress' class='wrap'></div>"; |
|
62 | - echo "<div id='wl_cmkg_table' class='wrap'></div>"; |
|
63 | - } |
|
54 | + wp_enqueue_style( |
|
55 | + 'wl-vocabulary-reconcile-script', |
|
56 | + plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary.full.css', |
|
57 | + array(), |
|
58 | + WORDLIFT_VERSION |
|
59 | + ); |
|
60 | + wp_localize_script( 'wl-vocabulary-reconcile-script', '_wlVocabularyMatchTermsConfig', Api_Config::get_api_config() ); |
|
61 | + echo "<div id='wl_cmkg_reconcile_progress' class='wrap'></div>"; |
|
62 | + echo "<div id='wl_cmkg_table' class='wrap'></div>"; |
|
63 | + } |
|
64 | 64 | |
65 | 65 | } |
@@ -22,10 +22,10 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @param $term_count Term_Count |
24 | 24 | */ |
25 | - public function __construct( $term_count ) { |
|
25 | + public function __construct($term_count) { |
|
26 | 26 | |
27 | 27 | $this->term_count = $term_count; |
28 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
28 | + add_action('admin_menu', array($this, 'admin_menu')); |
|
29 | 29 | |
30 | 30 | } |
31 | 31 | |
@@ -33,31 +33,31 @@ discard block |
||
33 | 33 | $number = $this->term_count->get_term_count(); |
34 | 34 | add_submenu_page( |
35 | 35 | 'wl_admin_menu', |
36 | - __( 'Match Terms', 'wordlift' ), |
|
37 | - __( 'Match Terms', 'wordlift' ) . ' ' . Badge_Generator::generate_html( $number ), |
|
36 | + __('Match Terms', 'wordlift'), |
|
37 | + __('Match Terms', 'wordlift').' '.Badge_Generator::generate_html($number), |
|
38 | 38 | 'manage_options', |
39 | 39 | 'wl-vocabulary-match-terms', |
40 | - array( $this, 'submenu_page_callback' ) |
|
40 | + array($this, 'submenu_page_callback') |
|
41 | 41 | ); |
42 | - remove_submenu_page( 'wl_admin_menu', 'wl_admin_menu' ); |
|
42 | + remove_submenu_page('wl_admin_menu', 'wl_admin_menu'); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | public function submenu_page_callback() { |
46 | 46 | |
47 | 47 | Scripts_Helper::enqueue_based_on_wordpress_version( |
48 | 48 | 'wl-vocabulary-reconcile-script', |
49 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary', |
|
50 | - array( 'react', 'react-dom', 'wp-i18n', 'wp-polyfill' ), |
|
49 | + plugin_dir_url(dirname(dirname(__DIR__))).'js/dist/vocabulary', |
|
50 | + array('react', 'react-dom', 'wp-i18n', 'wp-polyfill'), |
|
51 | 51 | true |
52 | 52 | ); |
53 | 53 | |
54 | 54 | wp_enqueue_style( |
55 | 55 | 'wl-vocabulary-reconcile-script', |
56 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary.full.css', |
|
56 | + plugin_dir_url(dirname(dirname(__DIR__))).'js/dist/vocabulary.full.css', |
|
57 | 57 | array(), |
58 | 58 | WORDLIFT_VERSION |
59 | 59 | ); |
60 | - wp_localize_script( 'wl-vocabulary-reconcile-script', '_wlVocabularyMatchTermsConfig', Api_Config::get_api_config() ); |
|
60 | + wp_localize_script('wl-vocabulary-reconcile-script', '_wlVocabularyMatchTermsConfig', Api_Config::get_api_config()); |
|
61 | 61 | echo "<div id='wl_cmkg_reconcile_progress' class='wrap'></div>"; |
62 | 62 | echo "<div id='wl_cmkg_table' class='wrap'></div>"; |
63 | 63 | } |
@@ -4,26 +4,26 @@ |
||
4 | 4 | use Wordlift\Vocabulary\Analysis_Service; |
5 | 5 | |
6 | 6 | class Term_Data_Factory { |
7 | - /** |
|
8 | - * @var Analysis_Service |
|
9 | - */ |
|
10 | - private $analysis_service; |
|
7 | + /** |
|
8 | + * @var Analysis_Service |
|
9 | + */ |
|
10 | + private $analysis_service; |
|
11 | 11 | |
12 | - /** |
|
13 | - * @var Analysis_Service |
|
14 | - */ |
|
15 | - public function __construct( $analysis_service ) { |
|
16 | - $this->analysis_service = $analysis_service; |
|
17 | - } |
|
12 | + /** |
|
13 | + * @var Analysis_Service |
|
14 | + */ |
|
15 | + public function __construct( $analysis_service ) { |
|
16 | + $this->analysis_service = $analysis_service; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param $term \WP_Term |
|
21 | - * |
|
22 | - * @return Term_Data |
|
23 | - */ |
|
24 | - public function get_term_data( $term ) { |
|
25 | - $entities = $this->analysis_service->get_entities( $term ); |
|
26 | - return new Default_Term_Data( $term, $entities ); |
|
27 | - } |
|
19 | + /** |
|
20 | + * @param $term \WP_Term |
|
21 | + * |
|
22 | + * @return Term_Data |
|
23 | + */ |
|
24 | + public function get_term_data( $term ) { |
|
25 | + $entities = $this->analysis_service->get_entities( $term ); |
|
26 | + return new Default_Term_Data( $term, $entities ); |
|
27 | + } |
|
28 | 28 | |
29 | 29 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | /** |
13 | 13 | * @var Analysis_Service |
14 | 14 | */ |
15 | - public function __construct( $analysis_service ) { |
|
15 | + public function __construct($analysis_service) { |
|
16 | 16 | $this->analysis_service = $analysis_service; |
17 | 17 | } |
18 | 18 | |
@@ -21,9 +21,9 @@ discard block |
||
21 | 21 | * |
22 | 22 | * @return Term_Data |
23 | 23 | */ |
24 | - public function get_term_data( $term ) { |
|
25 | - $entities = $this->analysis_service->get_entities( $term ); |
|
26 | - return new Default_Term_Data( $term, $entities ); |
|
24 | + public function get_term_data($term) { |
|
25 | + $entities = $this->analysis_service->get_entities($term); |
|
26 | + return new Default_Term_Data($term, $entities); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | } |
@@ -8,11 +8,11 @@ |
||
8 | 8 | |
9 | 9 | interface Term_Data { |
10 | 10 | |
11 | - /** |
|
12 | - * Should return an array which can be used by ui components. |
|
13 | - * |
|
14 | - * @return array |
|
15 | - */ |
|
16 | - public function get_data(); |
|
11 | + /** |
|
12 | + * Should return an array which can be used by ui components. |
|
13 | + * |
|
14 | + * @return array |
|
15 | + */ |
|
16 | + public function get_data(); |
|
17 | 17 | |
18 | 18 | } |
@@ -12,52 +12,52 @@ |
||
12 | 12 | */ |
13 | 13 | class Entity_List_Utils { |
14 | 14 | |
15 | - /** |
|
16 | - * @param $term_id int Term id. |
|
17 | - * @param $entities array An array of entities |
|
18 | - * |
|
19 | - * @return array<array> An Array of entities with isActive filter. |
|
20 | - */ |
|
21 | - public static function mark_is_active_for_entities( $term_id, $entities ) { |
|
22 | - |
|
23 | - $active_entities = self::get_active_entities( $term_id ); |
|
24 | - |
|
25 | - if ( ! is_array( $entities ) ) { |
|
26 | - return $entities; |
|
27 | - } |
|
28 | - |
|
29 | - foreach ( $entities as &$entity ) { |
|
30 | - $entity_id = $entity['entityId']; |
|
31 | - $entity['isActive'] = in_array( $entity_id, $active_entities, true ); |
|
32 | - } |
|
33 | - |
|
34 | - return $entities; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * @param $term_id |
|
39 | - * |
|
40 | - * @return array<string> An array of Entity URIs |
|
41 | - */ |
|
42 | - public static function get_active_entities( $term_id ) { |
|
43 | - |
|
44 | - // retrieve jsonld data. |
|
45 | - $entity = Entity_List_Factory::get_instance( $term_id ); |
|
46 | - |
|
47 | - $entity_data_list = $entity->get_jsonld_data(); |
|
48 | - |
|
49 | - $active_entity_ids = array(); |
|
50 | - |
|
51 | - foreach ( $entity_data_list as $item ) { |
|
52 | - $sameas = $item['sameAs']; |
|
53 | - if ( is_array( $sameas ) && count( $sameas ) > 0 ) { |
|
54 | - // The entity id is stored on last position, so |
|
55 | - // we get the entity id from there. |
|
56 | - $active_entity_ids[] = array_pop( $sameas ); |
|
57 | - } |
|
58 | - } |
|
59 | - |
|
60 | - return $active_entity_ids; |
|
61 | - } |
|
15 | + /** |
|
16 | + * @param $term_id int Term id. |
|
17 | + * @param $entities array An array of entities |
|
18 | + * |
|
19 | + * @return array<array> An Array of entities with isActive filter. |
|
20 | + */ |
|
21 | + public static function mark_is_active_for_entities( $term_id, $entities ) { |
|
22 | + |
|
23 | + $active_entities = self::get_active_entities( $term_id ); |
|
24 | + |
|
25 | + if ( ! is_array( $entities ) ) { |
|
26 | + return $entities; |
|
27 | + } |
|
28 | + |
|
29 | + foreach ( $entities as &$entity ) { |
|
30 | + $entity_id = $entity['entityId']; |
|
31 | + $entity['isActive'] = in_array( $entity_id, $active_entities, true ); |
|
32 | + } |
|
33 | + |
|
34 | + return $entities; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * @param $term_id |
|
39 | + * |
|
40 | + * @return array<string> An array of Entity URIs |
|
41 | + */ |
|
42 | + public static function get_active_entities( $term_id ) { |
|
43 | + |
|
44 | + // retrieve jsonld data. |
|
45 | + $entity = Entity_List_Factory::get_instance( $term_id ); |
|
46 | + |
|
47 | + $entity_data_list = $entity->get_jsonld_data(); |
|
48 | + |
|
49 | + $active_entity_ids = array(); |
|
50 | + |
|
51 | + foreach ( $entity_data_list as $item ) { |
|
52 | + $sameas = $item['sameAs']; |
|
53 | + if ( is_array( $sameas ) && count( $sameas ) > 0 ) { |
|
54 | + // The entity id is stored on last position, so |
|
55 | + // we get the entity id from there. |
|
56 | + $active_entity_ids[] = array_pop( $sameas ); |
|
57 | + } |
|
58 | + } |
|
59 | + |
|
60 | + return $active_entity_ids; |
|
61 | + } |
|
62 | 62 | |
63 | 63 | } |
@@ -18,17 +18,17 @@ discard block |
||
18 | 18 | * |
19 | 19 | * @return array<array> An Array of entities with isActive filter. |
20 | 20 | */ |
21 | - public static function mark_is_active_for_entities( $term_id, $entities ) { |
|
21 | + public static function mark_is_active_for_entities($term_id, $entities) { |
|
22 | 22 | |
23 | - $active_entities = self::get_active_entities( $term_id ); |
|
23 | + $active_entities = self::get_active_entities($term_id); |
|
24 | 24 | |
25 | - if ( ! is_array( $entities ) ) { |
|
25 | + if ( ! is_array($entities)) { |
|
26 | 26 | return $entities; |
27 | 27 | } |
28 | 28 | |
29 | - foreach ( $entities as &$entity ) { |
|
29 | + foreach ($entities as &$entity) { |
|
30 | 30 | $entity_id = $entity['entityId']; |
31 | - $entity['isActive'] = in_array( $entity_id, $active_entities, true ); |
|
31 | + $entity['isActive'] = in_array($entity_id, $active_entities, true); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | return $entities; |
@@ -39,21 +39,21 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @return array<string> An array of Entity URIs |
41 | 41 | */ |
42 | - public static function get_active_entities( $term_id ) { |
|
42 | + public static function get_active_entities($term_id) { |
|
43 | 43 | |
44 | 44 | // retrieve jsonld data. |
45 | - $entity = Entity_List_Factory::get_instance( $term_id ); |
|
45 | + $entity = Entity_List_Factory::get_instance($term_id); |
|
46 | 46 | |
47 | 47 | $entity_data_list = $entity->get_jsonld_data(); |
48 | 48 | |
49 | 49 | $active_entity_ids = array(); |
50 | 50 | |
51 | - foreach ( $entity_data_list as $item ) { |
|
51 | + foreach ($entity_data_list as $item) { |
|
52 | 52 | $sameas = $item['sameAs']; |
53 | - if ( is_array( $sameas ) && count( $sameas ) > 0 ) { |
|
53 | + if (is_array($sameas) && count($sameas) > 0) { |
|
54 | 54 | // The entity id is stored on last position, so |
55 | 55 | // we get the entity id from there. |
56 | - $active_entity_ids[] = array_pop( $sameas ); |
|
56 | + $active_entity_ids[] = array_pop($sameas); |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 |