Completed
Push — develop ( 68850b...c1f360 )
by David
01:29 queued 12s
created
src/classes/analysis/response/class-analysis-response-ops.php 2 patches
Indentation   +381 added lines, -381 removed lines patch added patch discarded remove patch
@@ -16,386 +16,386 @@
 block discarded – undo
16 16
 
17 17
 class Analysis_Response_Ops {
18 18
 
19
-	/**
20
-	 * The analysis response json.
21
-	 *
22
-	 * @since 3.21.5
23
-	 * @access private
24
-	 * @var mixed $json Holds the analysis response json.
25
-	 */
26
-	private $json;
27
-
28
-	/**
29
-	 * Holds the {@link Wordlift_Entity_Uri_Service}.
30
-	 *
31
-	 * @since 3.21.5
32
-	 * @access private
33
-	 * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
34
-	 */
35
-	private $entity_uri_service;
36
-
37
-	/**
38
-	 * @var Entity_Helper
39
-	 */
40
-	private $entity_helper;
41
-
42
-	/**
43
-	 * @var int $post_id
44
-	 */
45
-	private $post_id;
46
-
47
-	/**
48
-	 * Analysis_Response_Ops constructor.
49
-	 *
50
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}.
51
-	 * @param Entity_Helper                $entity_helper The {@link Entity_Helper}.
52
-	 * @param mixed                        $json The analysis response json.
53
-	 *
54
-	 * @since 3.21.5
55
-	 */
56
-	public function __construct( $entity_uri_service, $entity_helper, $json, $post_id ) {
57
-		$this->json               = $json;
58
-		$this->entity_uri_service = $entity_uri_service;
59
-		$this->entity_helper      = $entity_helper;
60
-		$this->post_id            = $post_id;
61
-	}
62
-
63
-	/**
64
-	 * Switches remote entities, i.e. entities with id outside the local dataset, to local entities.
65
-	 *
66
-	 * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs
67
-	 * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential
68
-	 * matches from the local vocabulary.
69
-	 *
70
-	 * If found, the entity id is swapped with the local id and the remote id is added to the sameAs.
71
-	 *
72
-	 * @return Analysis_Response_Ops The current Analysis_Response_Ops instance.
73
-	 */
74
-	public function make_entities_local() {
75
-
76
-		if ( ! isset( $this->json->entities ) ) {
77
-			return $this;
78
-		}
79
-
80
-		// Get the URIs.
81
-		$uris     = array_keys( get_object_vars( $this->json->entities ) );
82
-		$mappings = $this->entity_helper->map_many_to_local( $uris );
83
-
84
-		foreach ( $mappings as $external_uri => $internal_uri ) {
85
-
86
-			// Move the data from the external URI to the internal URI.
87
-			if ( ! isset( $this->json->entities->{$internal_uri} ) ) {
88
-				$this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri};
89
-			}
90
-
91
-			// Ensure sameAs is an array.
92
-			if ( ! isset( $this->json->entities->{$internal_uri}->sameAs )
93
-				 || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) {
94
-				$this->json->entities->{$internal_uri}->sameAs = array();
95
-			}
96
-
97
-			// Add the external URI as sameAs.
98
-			$this->json->entities->{$internal_uri}->sameAs[] = $external_uri;
99
-
100
-			// Finally remove the external URI.
101
-			unset( $this->json->entities->{$external_uri} );
102
-		}
103
-
104
-		// Set the internal uri in the annotation for the entityMatch in annotations.
105
-		if ( isset( $this->json->annotations ) ) {
106
-			// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
107
-			foreach ( $this->json->annotations as $key => $annotation ) {
108
-				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
109
-				if ( isset( $annotation->entityMatches ) ) {
110
-					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
111
-					foreach ( $annotation->entityMatches as $match ) {
112
-						// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
113
-						if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) {
114
-							// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
115
-							$match->entityId = $mappings[ $match->entityId ];
116
-						}
117
-					}
118
-				}
119
-			}
120
-		}
121
-
122
-		return $this;
123
-	}
124
-
125
-	/**
126
-	 * Add occurrences by parsing the provided html content.
127
-	 *
128
-	 * @param string $content The html content with annotations.
129
-	 *
130
-	 * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance.
131
-	 *
132
-	 * @since 3.23.7 refactor the regex pattern to take into account that there might be css classes between textannotation
133
-	 *  and disambiguated.
134
-	 *
135
-	 * @link https://github.com/insideout10/wordlift-plugin/issues/1001
136
-	 */
137
-	public function add_occurrences( $content ) {
138
-
139
-		// Try to get all the disambiguated annotations and bail out if an error occurs.
140
-		if ( false === preg_match_all(
141
-			'|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|',
142
-			$content,
143
-			$matches,
144
-			PREG_OFFSET_CAPTURE
145
-		) ) {
146
-			return $this;
147
-		}
148
-
149
-		if ( empty( $matches ) ) {
150
-			return $this;
151
-		}
152
-
153
-		$parse_data = array_reduce(
154
-			range( 0, count( $matches[1] ) - 1 ),
155
-			function ( $carry, $i ) use ( $matches ) {
156
-				if ( empty( $matches[0] ) ) {
157
-					return $carry;
158
-				}
159
-
160
-				$start         = $matches[0][ $i ][1];
161
-				$end           = $start + strlen( $matches[0][ $i ][0] );
162
-				$annotation_id = $matches[1][ $i ][0];
163
-				$item_id       = $matches[2][ $i ][0];
164
-				$text          = $matches[3][ $i ][0];
165
-
166
-				$annotation = new StdClass();
167
-				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
168
-				$annotation->annotationId = $annotation_id;
169
-				$annotation->start        = $start;
170
-				$annotation->end          = $end;
171
-				$annotation->text         = $text;
172
-
173
-				$entity_match             = new StdClass();
174
-				$entity_match->confidence = 100;
175
-				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
176
-				$entity_match->entityId = $item_id;
177
-				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
178
-				$annotation->entityMatches[] = $entity_match;
179
-
180
-				$carry['annotations'][ $annotation_id ] = $annotation;
181
-				$carry['occurrences'][ $item_id ][]     = $annotation_id;
182
-
183
-				return $carry;
184
-			},
185
-			array(
186
-				'annotations' => array(),
187
-				'occurrences' => array(),
188
-			)
189
-		);
190
-
191
-		$annotations = $parse_data['annotations'];
192
-		$occurrences = $parse_data['occurrences'];
193
-
194
-		$entity_provider_registry = Entity_Provider_Registry::get_instance();
195
-
196
-		foreach ( array_keys( $occurrences ) as $item_id ) {
197
-
198
-			// If the entity isn't there, add it.
199
-			if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) {
200
-				$entity = $entity_provider_registry->get_local_entity( $item_id );
201
-
202
-				// Entity not found in the local vocabulary, continue to the next one.
203
-				if ( false === $entity ) {
204
-					continue;
205
-				}
206
-
207
-				$this->json->entities->{$item_id} = $entity;
208
-			}
209
-		}
210
-
211
-		// Here we're adding back some data structures required by the client-side code.
212
-		//
213
-		// We're adding:
214
-		// 1. the .entities[entity_id].occurrences array with the annotations' ids.
215
-		// 2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map.
216
-		//
217
-		// Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee
218
-		// function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in
219
-		// `embedAnalysis`.
220
-
221
-		if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) {
222
-			$occurrences_processor = Occurrences_Factory::get_instance( $this->post_id );
223
-			$this->json            = $occurrences_processor->add_occurrences_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
-					// Keep the `id` for compatibility with existing analysis, since it appears that no-editor-analysis.js
262
-					// is using it, ie.:
263
-					// `Each child in a list should have a unique "key" prop.`
264
-					//
265
-					// unset( $serialized_entity['id'] );
266
-
267
-					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
268
-					$entities[ $entity_matches->entityId ] = $serialized_entity;
269
-				}
270
-			}
271
-		}
272
-
273
-		// Adding occurrences and annotations data structures required by the client-side code.
274
-		foreach ( $entities as $entity_id => $entity ) {
275
-			foreach ( $this->json->annotations as $annotation ) {
276
-				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
277
-				if ( $annotation->entityMatches[0]->entityId === $entity_id ) {
278
-					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
279
-					$entities[ $entity_id ]['occurrences'][] = $annotation->annotationId;
280
-					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
281
-					$entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId;
282
-				}
283
-			}
284
-		}
285
-
286
-		$this->json->entities = $entities;
287
-
288
-		return $this;
289
-
290
-	}
291
-
292
-	/**
293
-	 * Return the JSON response.
294
-	 *
295
-	 * @return mixed The JSON response.
296
-	 * @since 3.24.2
297
-	 */
298
-	public function get_json() {
299
-
300
-		return $this->json;
301
-	}
302
-
303
-	/**
304
-	 * This function should be invoked after `make_entities_local` after this
305
-	 * method.
306
-	 *
307
-	 * @param $excluded_uris array An array of entity URIs to be excluded.
308
-	 *
309
-	 * @return $this
310
-	 * @since 3.32.3.1
311
-	 */
312
-	public function remove_excluded_entities( $excluded_uris ) {
313
-
314
-		// If we didnt receive array, return early.
315
-		if ( ! is_array( $excluded_uris ) ) {
316
-			return $this;
317
-		}
318
-
319
-		// We may also receive an array of null, make sure to filter uris when receiving.
320
-		$excluded_uris = array_filter( $excluded_uris, 'is_string' );
321
-
322
-		$this->remove_entities_with_excluded_uris( $excluded_uris );
323
-
324
-		$this->remove_annotations_with_excluded_uris( $excluded_uris );
325
-
326
-		return $this;
327
-	}
328
-
329
-	/**
330
-	 * Get the string representation of the JSON.
331
-	 *
332
-	 * @return false|string The string representation or false in case of error.
333
-	 */
334
-	public function to_string() {
335
-
336
-		// Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+.
337
-		$options = ( version_compare( PHP_VERSION, '5.4', '>=' )
338
-			? 256 : 0 );
339
-
340
-		return wp_json_encode( $this->json, $options );
341
-	}
342
-
343
-	/**
344
-	 * Remove all the entities with the excluded URIs.
345
-	 *
346
-	 * @param array $excluded_uris The array of URIs to be excluded.
347
-	 */
348
-	private function remove_entities_with_excluded_uris( array $excluded_uris ) {
349
-		// Remove the excluded entity uris.
350
-		if ( isset( $this->json->entities ) ) {
351
-			foreach ( $excluded_uris as $excluded_uri ) {
352
-
353
-				if ( isset( $this->json->entities->{$excluded_uri} ) ) {
354
-					// Remove this entity.
355
-					unset( $this->json->entities->{$excluded_uri} );
356
-					// Also remove the annotations.
357
-				}
358
-			}
359
-		}
360
-	}
361
-
362
-	/**
363
-	 * Remove all the annotations with the excluded entity URIs.
364
-	 *
365
-	 * @param array $excluded_uris The array of URIs to be excluded.
366
-	 *
367
-	 * @return void
368
-	 */
369
-	private function remove_annotations_with_excluded_uris( array $excluded_uris ) {
370
-		if ( isset( $this->json->annotations ) ) {
371
-			foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) {
372
-
373
-				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
374
-				if ( ! isset( $annotation_data->entityMatches ) ) {
375
-					continue;
376
-				}
377
-
378
-				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
379
-				foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) {
380
-
381
-					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
382
-					$entity_uri = $entity_match_data->entityId;
383
-
384
-					if ( ! in_array( $entity_uri, $excluded_uris, true ) ) {
385
-						continue;
386
-					}
387
-					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
388
-					unset( $annotation_data->entityMatches[ $entity_match_key ] );
389
-				}
390
-
391
-				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
392
-				if ( count( $annotation_data->entityMatches ) === 0 ) {
393
-					// Remove the annotation if we have zero empty annotation matches.
394
-					unset( $this->json->annotations->{$annotation_key} );
395
-				}
396
-			}
397
-		}
398
-
399
-	}
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_occurrences_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
+                    // Keep the `id` for compatibility with existing analysis, since it appears that no-editor-analysis.js
262
+                    // is using it, ie.:
263
+                    // `Each child in a list should have a unique "key" prop.`
264
+                    //
265
+                    // unset( $serialized_entity['id'] );
266
+
267
+                    // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
268
+                    $entities[ $entity_matches->entityId ] = $serialized_entity;
269
+                }
270
+            }
271
+        }
272
+
273
+        // Adding occurrences and annotations data structures required by the client-side code.
274
+        foreach ( $entities as $entity_id => $entity ) {
275
+            foreach ( $this->json->annotations as $annotation ) {
276
+                // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
277
+                if ( $annotation->entityMatches[0]->entityId === $entity_id ) {
278
+                    // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
279
+                    $entities[ $entity_id ]['occurrences'][] = $annotation->annotationId;
280
+                    // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
281
+                    $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId;
282
+                }
283
+            }
284
+        }
285
+
286
+        $this->json->entities = $entities;
287
+
288
+        return $this;
289
+
290
+    }
291
+
292
+    /**
293
+     * Return the JSON response.
294
+     *
295
+     * @return mixed The JSON response.
296
+     * @since 3.24.2
297
+     */
298
+    public function get_json() {
299
+
300
+        return $this->json;
301
+    }
302
+
303
+    /**
304
+     * This function should be invoked after `make_entities_local` after this
305
+     * method.
306
+     *
307
+     * @param $excluded_uris array An array of entity URIs to be excluded.
308
+     *
309
+     * @return $this
310
+     * @since 3.32.3.1
311
+     */
312
+    public function remove_excluded_entities( $excluded_uris ) {
313
+
314
+        // If we didnt receive array, return early.
315
+        if ( ! is_array( $excluded_uris ) ) {
316
+            return $this;
317
+        }
318
+
319
+        // We may also receive an array of null, make sure to filter uris when receiving.
320
+        $excluded_uris = array_filter( $excluded_uris, 'is_string' );
321
+
322
+        $this->remove_entities_with_excluded_uris( $excluded_uris );
323
+
324
+        $this->remove_annotations_with_excluded_uris( $excluded_uris );
325
+
326
+        return $this;
327
+    }
328
+
329
+    /**
330
+     * Get the string representation of the JSON.
331
+     *
332
+     * @return false|string The string representation or false in case of error.
333
+     */
334
+    public function to_string() {
335
+
336
+        // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+.
337
+        $options = ( version_compare( PHP_VERSION, '5.4', '>=' )
338
+            ? 256 : 0 );
339
+
340
+        return wp_json_encode( $this->json, $options );
341
+    }
342
+
343
+    /**
344
+     * Remove all the entities with the excluded URIs.
345
+     *
346
+     * @param array $excluded_uris The array of URIs to be excluded.
347
+     */
348
+    private function remove_entities_with_excluded_uris( array $excluded_uris ) {
349
+        // Remove the excluded entity uris.
350
+        if ( isset( $this->json->entities ) ) {
351
+            foreach ( $excluded_uris as $excluded_uri ) {
352
+
353
+                if ( isset( $this->json->entities->{$excluded_uri} ) ) {
354
+                    // Remove this entity.
355
+                    unset( $this->json->entities->{$excluded_uri} );
356
+                    // Also remove the annotations.
357
+                }
358
+            }
359
+        }
360
+    }
361
+
362
+    /**
363
+     * Remove all the annotations with the excluded entity URIs.
364
+     *
365
+     * @param array $excluded_uris The array of URIs to be excluded.
366
+     *
367
+     * @return void
368
+     */
369
+    private function remove_annotations_with_excluded_uris( array $excluded_uris ) {
370
+        if ( isset( $this->json->annotations ) ) {
371
+            foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) {
372
+
373
+                // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
374
+                if ( ! isset( $annotation_data->entityMatches ) ) {
375
+                    continue;
376
+                }
377
+
378
+                // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
379
+                foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) {
380
+
381
+                    // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
382
+                    $entity_uri = $entity_match_data->entityId;
383
+
384
+                    if ( ! in_array( $entity_uri, $excluded_uris, true ) ) {
385
+                        continue;
386
+                    }
387
+                    // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
388
+                    unset( $annotation_data->entityMatches[ $entity_match_key ] );
389
+                }
390
+
391
+                // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
392
+                if ( count( $annotation_data->entityMatches ) === 0 ) {
393
+                    // Remove the annotation if we have zero empty annotation matches.
394
+                    unset( $this->json->annotations->{$annotation_key} );
395
+                }
396
+            }
397
+        }
398
+
399
+    }
400 400
 
401 401
 }
Please login to merge, or discard this patch.
Spacing   +71 added lines, -71 removed lines patch added patch discarded remove patch
@@ -53,7 +53,7 @@  discard block
 block discarded – undo
53 53
 	 *
54 54
 	 * @since 3.21.5
55 55
 	 */
56
-	public function __construct( $entity_uri_service, $entity_helper, $json, $post_id ) {
56
+	public function __construct($entity_uri_service, $entity_helper, $json, $post_id) {
57 57
 		$this->json               = $json;
58 58
 		$this->entity_uri_service = $entity_uri_service;
59 59
 		$this->entity_helper      = $entity_helper;
@@ -73,24 +73,24 @@  discard block
 block discarded – undo
73 73
 	 */
74 74
 	public function make_entities_local() {
75 75
 
76
-		if ( ! isset( $this->json->entities ) ) {
76
+		if ( ! isset($this->json->entities)) {
77 77
 			return $this;
78 78
 		}
79 79
 
80 80
 		// Get the URIs.
81
-		$uris     = array_keys( get_object_vars( $this->json->entities ) );
82
-		$mappings = $this->entity_helper->map_many_to_local( $uris );
81
+		$uris     = array_keys(get_object_vars($this->json->entities));
82
+		$mappings = $this->entity_helper->map_many_to_local($uris);
83 83
 
84
-		foreach ( $mappings as $external_uri => $internal_uri ) {
84
+		foreach ($mappings as $external_uri => $internal_uri) {
85 85
 
86 86
 			// Move the data from the external URI to the internal URI.
87
-			if ( ! isset( $this->json->entities->{$internal_uri} ) ) {
87
+			if ( ! isset($this->json->entities->{$internal_uri} )) {
88 88
 				$this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri};
89 89
 			}
90 90
 
91 91
 			// Ensure sameAs is an array.
92
-			if ( ! isset( $this->json->entities->{$internal_uri}->sameAs )
93
-				 || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) {
92
+			if ( ! isset($this->json->entities->{$internal_uri}->sameAs)
93
+				 || ! is_array($this->json->entities->{$internal_uri}->sameAs)) {
94 94
 				$this->json->entities->{$internal_uri}->sameAs = array();
95 95
 			}
96 96
 
@@ -98,21 +98,21 @@  discard block
 block discarded – undo
98 98
 			$this->json->entities->{$internal_uri}->sameAs[] = $external_uri;
99 99
 
100 100
 			// Finally remove the external URI.
101
-			unset( $this->json->entities->{$external_uri} );
101
+			unset($this->json->entities->{$external_uri} );
102 102
 		}
103 103
 
104 104
 		// Set the internal uri in the annotation for the entityMatch in annotations.
105
-		if ( isset( $this->json->annotations ) ) {
105
+		if (isset($this->json->annotations)) {
106 106
 			// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
107
-			foreach ( $this->json->annotations as $key => $annotation ) {
107
+			foreach ($this->json->annotations as $key => $annotation) {
108 108
 				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
109
-				if ( isset( $annotation->entityMatches ) ) {
109
+				if (isset($annotation->entityMatches)) {
110 110
 					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
111
-					foreach ( $annotation->entityMatches as $match ) {
111
+					foreach ($annotation->entityMatches as $match) {
112 112
 						// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
113
-						if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) {
113
+						if (isset($match->entityId) && isset($mappings[$match->entityId])) {
114 114
 							// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
115
-							$match->entityId = $mappings[ $match->entityId ];
115
+							$match->entityId = $mappings[$match->entityId];
116 116
 						}
117 117
 					}
118 118
 				}
@@ -134,34 +134,34 @@  discard block
 block discarded – undo
134 134
 	 *
135 135
 	 * @link https://github.com/insideout10/wordlift-plugin/issues/1001
136 136
 	 */
137
-	public function add_occurrences( $content ) {
137
+	public function add_occurrences($content) {
138 138
 
139 139
 		// Try to get all the disambiguated annotations and bail out if an error occurs.
140
-		if ( false === preg_match_all(
140
+		if (false === preg_match_all(
141 141
 			'|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|',
142 142
 			$content,
143 143
 			$matches,
144 144
 			PREG_OFFSET_CAPTURE
145
-		) ) {
145
+		)) {
146 146
 			return $this;
147 147
 		}
148 148
 
149
-		if ( empty( $matches ) ) {
149
+		if (empty($matches)) {
150 150
 			return $this;
151 151
 		}
152 152
 
153 153
 		$parse_data = array_reduce(
154
-			range( 0, count( $matches[1] ) - 1 ),
155
-			function ( $carry, $i ) use ( $matches ) {
156
-				if ( empty( $matches[0] ) ) {
154
+			range(0, count($matches[1]) - 1),
155
+			function($carry, $i) use ($matches) {
156
+				if (empty($matches[0])) {
157 157
 					return $carry;
158 158
 				}
159 159
 
160
-				$start         = $matches[0][ $i ][1];
161
-				$end           = $start + strlen( $matches[0][ $i ][0] );
162
-				$annotation_id = $matches[1][ $i ][0];
163
-				$item_id       = $matches[2][ $i ][0];
164
-				$text          = $matches[3][ $i ][0];
160
+				$start         = $matches[0][$i][1];
161
+				$end           = $start + strlen($matches[0][$i][0]);
162
+				$annotation_id = $matches[1][$i][0];
163
+				$item_id       = $matches[2][$i][0];
164
+				$text          = $matches[3][$i][0];
165 165
 
166 166
 				$annotation = new StdClass();
167 167
 				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
@@ -177,8 +177,8 @@  discard block
 block discarded – undo
177 177
 				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
178 178
 				$annotation->entityMatches[] = $entity_match;
179 179
 
180
-				$carry['annotations'][ $annotation_id ] = $annotation;
181
-				$carry['occurrences'][ $item_id ][]     = $annotation_id;
180
+				$carry['annotations'][$annotation_id] = $annotation;
181
+				$carry['occurrences'][$item_id][]     = $annotation_id;
182 182
 
183 183
 				return $carry;
184 184
 			},
@@ -193,14 +193,14 @@  discard block
 block discarded – undo
193 193
 
194 194
 		$entity_provider_registry = Entity_Provider_Registry::get_instance();
195 195
 
196
-		foreach ( array_keys( $occurrences ) as $item_id ) {
196
+		foreach (array_keys($occurrences) as $item_id) {
197 197
 
198 198
 			// If the entity isn't there, add it.
199
-			if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) {
200
-				$entity = $entity_provider_registry->get_local_entity( $item_id );
199
+			if ( ! is_bool($this->json) && ! isset($this->json->entities->{$item_id} )) {
200
+				$entity = $entity_provider_registry->get_local_entity($item_id);
201 201
 
202 202
 				// Entity not found in the local vocabulary, continue to the next one.
203
-				if ( false === $entity ) {
203
+				if (false === $entity) {
204 204
 					continue;
205 205
 				}
206 206
 
@@ -218,16 +218,16 @@  discard block
 block discarded – undo
218 218
 		// function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in
219 219
 		// `embedAnalysis`.
220 220
 
221
-		if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) {
222
-			$occurrences_processor = Occurrences_Factory::get_instance( $this->post_id );
223
-			$this->json            = $occurrences_processor->add_occurrences_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_occurrences_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,15 +248,15 @@  discard block
 block discarded – undo
248 248
 
249 249
 		// Populating the local entities object
250 250
 		$entities = array();
251
-		foreach ( $this->json->annotations as $annotation ) {
251
+		foreach ($this->json->annotations as $annotation) {
252 252
 			// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
253
-			foreach ( $annotation->entityMatches as $entity_matches ) {
253
+			foreach ($annotation->entityMatches as $entity_matches) {
254 254
 
255 255
 				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
256
-				$entity_id         = $this->entity_uri_service->get_post_id_from_url( $entity_matches->entityId );
257
-				$serialized_entity = wl_serialize_entity( $entity_id );
256
+				$entity_id         = $this->entity_uri_service->get_post_id_from_url($entity_matches->entityId);
257
+				$serialized_entity = wl_serialize_entity($entity_id);
258 258
 
259
-				if ( $serialized_entity ) {
259
+				if ($serialized_entity) {
260 260
 					$serialized_entity['entityId'] = $serialized_entity['id'];
261 261
 					// Keep the `id` for compatibility with existing analysis, since it appears that no-editor-analysis.js
262 262
 					// is using it, ie.:
@@ -265,20 +265,20 @@  discard block
 block discarded – undo
265 265
 					// unset( $serialized_entity['id'] );
266 266
 
267 267
 					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
268
-					$entities[ $entity_matches->entityId ] = $serialized_entity;
268
+					$entities[$entity_matches->entityId] = $serialized_entity;
269 269
 				}
270 270
 			}
271 271
 		}
272 272
 
273 273
 		// Adding occurrences and annotations data structures required by the client-side code.
274
-		foreach ( $entities as $entity_id => $entity ) {
275
-			foreach ( $this->json->annotations as $annotation ) {
274
+		foreach ($entities as $entity_id => $entity) {
275
+			foreach ($this->json->annotations as $annotation) {
276 276
 				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
277
-				if ( $annotation->entityMatches[0]->entityId === $entity_id ) {
277
+				if ($annotation->entityMatches[0]->entityId === $entity_id) {
278 278
 					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
279
-					$entities[ $entity_id ]['occurrences'][] = $annotation->annotationId;
279
+					$entities[$entity_id]['occurrences'][] = $annotation->annotationId;
280 280
 					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
281
-					$entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId;
281
+					$entities[$entity_id]['annotations'][$annotation->annotationId]['id'] = $annotation->annotationId;
282 282
 				}
283 283
 			}
284 284
 		}
@@ -309,19 +309,19 @@  discard block
 block discarded – undo
309 309
 	 * @return $this
310 310
 	 * @since 3.32.3.1
311 311
 	 */
312
-	public function remove_excluded_entities( $excluded_uris ) {
312
+	public function remove_excluded_entities($excluded_uris) {
313 313
 
314 314
 		// If we didnt receive array, return early.
315
-		if ( ! is_array( $excluded_uris ) ) {
315
+		if ( ! is_array($excluded_uris)) {
316 316
 			return $this;
317 317
 		}
318 318
 
319 319
 		// We may also receive an array of null, make sure to filter uris when receiving.
320
-		$excluded_uris = array_filter( $excluded_uris, 'is_string' );
320
+		$excluded_uris = array_filter($excluded_uris, 'is_string');
321 321
 
322
-		$this->remove_entities_with_excluded_uris( $excluded_uris );
322
+		$this->remove_entities_with_excluded_uris($excluded_uris);
323 323
 
324
-		$this->remove_annotations_with_excluded_uris( $excluded_uris );
324
+		$this->remove_annotations_with_excluded_uris($excluded_uris);
325 325
 
326 326
 		return $this;
327 327
 	}
@@ -334,10 +334,10 @@  discard block
 block discarded – undo
334 334
 	public function to_string() {
335 335
 
336 336
 		// Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+.
337
-		$options = ( version_compare( PHP_VERSION, '5.4', '>=' )
338
-			? 256 : 0 );
337
+		$options = (version_compare(PHP_VERSION, '5.4', '>=')
338
+			? 256 : 0);
339 339
 
340
-		return wp_json_encode( $this->json, $options );
340
+		return wp_json_encode($this->json, $options);
341 341
 	}
342 342
 
343 343
 	/**
@@ -345,14 +345,14 @@  discard block
 block discarded – undo
345 345
 	 *
346 346
 	 * @param array $excluded_uris The array of URIs to be excluded.
347 347
 	 */
348
-	private function remove_entities_with_excluded_uris( array $excluded_uris ) {
348
+	private function remove_entities_with_excluded_uris(array $excluded_uris) {
349 349
 		// Remove the excluded entity uris.
350
-		if ( isset( $this->json->entities ) ) {
351
-			foreach ( $excluded_uris as $excluded_uri ) {
350
+		if (isset($this->json->entities)) {
351
+			foreach ($excluded_uris as $excluded_uri) {
352 352
 
353
-				if ( isset( $this->json->entities->{$excluded_uri} ) ) {
353
+				if (isset($this->json->entities->{$excluded_uri} )) {
354 354
 					// Remove this entity.
355
-					unset( $this->json->entities->{$excluded_uri} );
355
+					unset($this->json->entities->{$excluded_uri} );
356 356
 					// Also remove the annotations.
357 357
 				}
358 358
 			}
@@ -366,32 +366,32 @@  discard block
 block discarded – undo
366 366
 	 *
367 367
 	 * @return void
368 368
 	 */
369
-	private function remove_annotations_with_excluded_uris( array $excluded_uris ) {
370
-		if ( isset( $this->json->annotations ) ) {
371
-			foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) {
369
+	private function remove_annotations_with_excluded_uris(array $excluded_uris) {
370
+		if (isset($this->json->annotations)) {
371
+			foreach ($this->json->annotations as $annotation_key => &$annotation_data) {
372 372
 
373 373
 				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
374
-				if ( ! isset( $annotation_data->entityMatches ) ) {
374
+				if ( ! isset($annotation_data->entityMatches)) {
375 375
 					continue;
376 376
 				}
377 377
 
378 378
 				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
379
-				foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) {
379
+				foreach ($annotation_data->entityMatches as $entity_match_key => $entity_match_data) {
380 380
 
381 381
 					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
382 382
 					$entity_uri = $entity_match_data->entityId;
383 383
 
384
-					if ( ! in_array( $entity_uri, $excluded_uris, true ) ) {
384
+					if ( ! in_array($entity_uri, $excluded_uris, true)) {
385 385
 						continue;
386 386
 					}
387 387
 					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
388
-					unset( $annotation_data->entityMatches[ $entity_match_key ] );
388
+					unset($annotation_data->entityMatches[$entity_match_key]);
389 389
 				}
390 390
 
391 391
 				// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
392
-				if ( count( $annotation_data->entityMatches ) === 0 ) {
392
+				if (count($annotation_data->entityMatches) === 0) {
393 393
 					// Remove the annotation if we have zero empty annotation matches.
394
-					unset( $this->json->annotations->{$annotation_key} );
394
+					unset($this->json->annotations->{$annotation_key} );
395 395
 				}
396 396
 			}
397 397
 		}
Please login to merge, or discard this patch.
src/classes/analysis/entity-provider/class-post-entity-provider.php 1 patch
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -14,41 +14,41 @@  discard block
 block discarded – undo
14 14
 
15 15
 class Post_Entity_Provider implements Entity_Provider {
16 16
 
17
-	/**
18
-	 * @var \Wordlift_Entity_Uri_Service
19
-	 */
20
-	private $entity_uri_service;
21
-	/**
22
-	 * @var \Wordlift_Entity_Type_Service
23
-	 */
24
-	private $entity_type_service;
25
-	/**
26
-	 * @var \Wordlift_Post_Image_Storage
27
-	 */
28
-	private $post_image_storage;
17
+    /**
18
+     * @var \Wordlift_Entity_Uri_Service
19
+     */
20
+    private $entity_uri_service;
21
+    /**
22
+     * @var \Wordlift_Entity_Type_Service
23
+     */
24
+    private $entity_type_service;
25
+    /**
26
+     * @var \Wordlift_Post_Image_Storage
27
+     */
28
+    private $post_image_storage;
29 29
 
30
-	public function __construct( $entity_uri_service, $entity_type_service, $post_image_storage ) {
31
-		$this->entity_uri_service  = $entity_uri_service;
32
-		$this->entity_type_service = $entity_type_service;
33
-		$this->post_image_storage  = $post_image_storage;
34
-	}
30
+    public function __construct( $entity_uri_service, $entity_type_service, $post_image_storage ) {
31
+        $this->entity_uri_service  = $entity_uri_service;
32
+        $this->entity_type_service = $entity_type_service;
33
+        $this->post_image_storage  = $post_image_storage;
34
+    }
35 35
 
36
-	public function get_entity( $uri ) {
37
-		$content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
36
+    public function get_entity( $uri ) {
37
+        $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
38 38
 
39
-		if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) {
40
-			return false;
41
-		}
39
+        if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) {
40
+            return false;
41
+        }
42 42
 
43
-		$entity = $content->get_bag();
44
-		$type   = $this->entity_type_service->get( $entity->ID );
45
-		$images = $this->post_image_storage->get( $entity->ID );
43
+        $entity = $content->get_bag();
44
+        $type   = $this->entity_type_service->get( $entity->ID );
45
+        $images = $this->post_image_storage->get( $entity->ID );
46 46
 
47
-		return (object) array(
48
-			'id'          => $uri,
49
-			'entityId'    => $uri,
50
-			'label'       => $entity->post_title,
51
-			/*
47
+        return (object) array(
48
+            'id'          => $uri,
49
+            'entityId'    => $uri,
50
+            'label'       => $entity->post_title,
51
+            /*
52 52
 			 * As of 2020.06.29 we're comment out the `post_content` because Gutenberg posts will return here
53 53
 			 * the whole Gutenberg source including potentially our own wordlift/classification block, which means
54 54
 			 * that data may grow quickly to more than a 100 KBytes and could break web servers.
@@ -60,13 +60,13 @@  discard block
 block discarded – undo
60 60
 			 *
61 61
 			 * PS: We didn't test using the WordLift Post Excerpt Helper.
62 62
 			 */
63
-			// 'description' => $entity->post_content,
64
-			'description' => '',
65
-			'sameAs'      => wl_schema_get_value( $entity->ID, 'sameAs' ),
66
-			'mainType'    => str_replace( 'wl-', '', $type['css_class'] ),
67
-			'types'       => wl_get_entity_rdf_types( $entity->ID ),
68
-			'images'      => $images,
69
-		);
70
-	}
63
+            // 'description' => $entity->post_content,
64
+            'description' => '',
65
+            'sameAs'      => wl_schema_get_value( $entity->ID, 'sameAs' ),
66
+            'mainType'    => str_replace( 'wl-', '', $type['css_class'] ),
67
+            'types'       => wl_get_entity_rdf_types( $entity->ID ),
68
+            'images'      => $images,
69
+        );
70
+    }
71 71
 
72 72
 }
Please login to merge, or discard this patch.
src/classes/analysis/entity-provider/class-term-entity-provider.php 1 patch
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -15,42 +15,42 @@
 block discarded – undo
15 15
 
16 16
 class Term_Entity_Provider implements Entity_Provider {
17 17
 
18
-	/**
19
-	 * @var Type_Service
20
-	 */
21
-	private $term_type_service;
22
-
23
-	public function __construct() {
24
-		$this->term_type_service = Type_Service::get_instance();
25
-	}
26
-
27
-	public function get_entity( $uri ) {
28
-
29
-		$content = Wordpress_Term_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
30
-
31
-		if ( ! isset( $content ) ) {
32
-			return false;
33
-		}
34
-
35
-		$term    = $content->get_bag();
36
-		$term_id = $term->term_id;
37
-
38
-		$schema = $this->term_type_service->get_schema( $term_id );
39
-		// @todo: For now we dont support images
40
-		// $images = $this->post_image_storage->get( $term_entity->ID );
41
-		$same_as = get_term_meta( $term_id, 'entity_same_as' );
42
-		$same_as = $same_as ? $same_as : array();
43
-
44
-		return (object) array(
45
-			'id'          => $uri,
46
-			'entityId'    => $uri,
47
-			'label'       => $term->name,
48
-			'description' => '',
49
-			'sameAs'      => $same_as,
50
-			'mainType'    => str_replace( 'wl-', '', $schema['css_class'] ),
51
-			'types'       => $this->term_type_service->get_entity_types_labels( $term_id ),
52
-			'images'      => array(),
53
-		);
54
-	}
18
+    /**
19
+     * @var Type_Service
20
+     */
21
+    private $term_type_service;
22
+
23
+    public function __construct() {
24
+        $this->term_type_service = Type_Service::get_instance();
25
+    }
26
+
27
+    public function get_entity( $uri ) {
28
+
29
+        $content = Wordpress_Term_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
30
+
31
+        if ( ! isset( $content ) ) {
32
+            return false;
33
+        }
34
+
35
+        $term    = $content->get_bag();
36
+        $term_id = $term->term_id;
37
+
38
+        $schema = $this->term_type_service->get_schema( $term_id );
39
+        // @todo: For now we dont support images
40
+        // $images = $this->post_image_storage->get( $term_entity->ID );
41
+        $same_as = get_term_meta( $term_id, 'entity_same_as' );
42
+        $same_as = $same_as ? $same_as : array();
43
+
44
+        return (object) array(
45
+            'id'          => $uri,
46
+            'entityId'    => $uri,
47
+            'label'       => $term->name,
48
+            'description' => '',
49
+            'sameAs'      => $same_as,
50
+            'mainType'    => str_replace( 'wl-', '', $schema['css_class'] ),
51
+            'types'       => $this->term_type_service->get_entity_types_labels( $term_id ),
52
+            'images'      => array(),
53
+        );
54
+    }
55 55
 
56 56
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-configuration-service.php 2 patches
Indentation   +778 added lines, -778 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
 use Wordlift\Api\Default_Api_Service;
14 14
 
15 15
 if ( ! defined( 'ABSPATH' ) ) {
16
-	exit;
16
+    exit;
17 17
 }
18 18
 
19 19
 /**
@@ -23,786 +23,786 @@  discard block
 block discarded – undo
23 23
  */
24 24
 class Wordlift_Configuration_Service {
25 25
 
26
-	/**
27
-	 * The entity base path option name.
28
-	 *
29
-	 * @since 3.6.0
30
-	 */
31
-	const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
32
-
33
-	/**
34
-	 * The skip wizard (admin installation wizard) option name.
35
-	 *
36
-	 * @since 3.9.0
37
-	 */
38
-	const SKIP_WIZARD = 'wl_skip_wizard';
39
-
40
-	/**
41
-	 * The skip installation notice.
42
-	 *
43
-	 * @since 3.40.3
44
-	 */
45
-	const SKIP_INSTALLATION_NOTICE = 'wl_skip_installation_notice';
46
-
47
-	/**
48
-	 * WordLift's key option name.
49
-	 *
50
-	 * @since 3.9.0
51
-	 */
52
-	const KEY = 'key';
53
-
54
-	/**
55
-	 * WordLift's configured language option name.
56
-	 *
57
-	 * @since 3.9.0
58
-	 */
59
-	const LANGUAGE = 'site_language';
60
-
61
-	/**
62
-	 * WordLift's configured country code.
63
-	 *
64
-	 * @since 3.18.0
65
-	 */
66
-	const COUNTRY_CODE = 'country_code';
67
-
68
-	/**
69
-	 * The alternateName option name.
70
-	 *
71
-	 * @since 3.38.6
72
-	 */
73
-	const ALTERNATE_NAME = 'wl-alternate-name';
74
-
75
-	/**
76
-	 * The publisher entity post ID option name.
77
-	 *
78
-	 * @since 3.9.0
79
-	 */
80
-	const PUBLISHER_ID = 'publisher_id';
81
-
82
-	/**
83
-	 * The dataset URI option name
84
-	 *
85
-	 * @since 3.10.0
86
-	 */
87
-	const DATASET_URI = 'redlink_dataset_uri';
88
-
89
-	/**
90
-	 * The link by default option name.
91
-	 *
92
-	 * @since 3.11.0
93
-	 */
94
-	const LINK_BY_DEFAULT = 'link_by_default';
95
-
96
-	/**
97
-	 * The analytics enable option.
98
-	 *
99
-	 * @since 3.21.0
100
-	 */
101
-	const ANALYTICS_ENABLE = 'analytics_enable';
102
-
103
-	/**
104
-	 * The analytics entity uri dimension option.
105
-	 *
106
-	 * @since 3.21.0
107
-	 */
108
-	const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
109
-
110
-	/**
111
-	 * The analytics entity type dimension option.
112
-	 *
113
-	 * @since 3.21.0
114
-	 */
115
-	const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
116
-
117
-	/**
118
-	 * The user preferences about sharing data option.
119
-	 *
120
-	 * @since 3.19.0
121
-	 */
122
-	const SEND_DIAGNOSTIC = 'send_diagnostic';
123
-
124
-	/**
125
-	 * The package type configuration key.
126
-	 *
127
-	 * @since 3.20.0
128
-	 */
129
-	const PACKAGE_TYPE = 'package_type';
130
-	/**
131
-	 * The dataset ids connected to the current key
132
-	 *
133
-	 * @since 3.38.5
134
-	 */
135
-	const NETWORK_DATASET_IDS = 'network_dataset_ids';
136
-
137
-	/**
138
-	 * The {@link Wordlift_Log_Service} instance.
139
-	 *
140
-	 * @since 3.16.0
141
-	 *
142
-	 * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
143
-	 */
144
-	private $log;
145
-
146
-	/**
147
-	 * Create a Wordlift_Configuration_Service's instance.
148
-	 *
149
-	 * @since 3.6.0
150
-	 */
151
-	protected function __construct() {
152
-
153
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
154
-
155
-		// Sync some configuration properties when key is validated.
156
-		add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
157
-
158
-	}
159
-
160
-	/**
161
-	 * @param $response \Wordlift\Api\Response
162
-	 *
163
-	 * @return void
164
-	 */
165
-	public function sync( $response ) {
166
-		if ( ! $response->is_success() ) {
167
-			return;
168
-		}
169
-		$data = json_decode( $response->get_body(), true );
170
-		if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
171
-			return;
172
-		}
173
-		$this->set_network_dataset_ids( $data['networkDatasetId'] );
174
-	}
175
-
176
-	/**
177
-	 * The Wordlift_Configuration_Service's singleton instance.
178
-	 *
179
-	 * @since  3.6.0
180
-	 *
181
-	 * @access private
182
-	 * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
183
-	 */
184
-	private static $instance = null;
185
-
186
-	/**
187
-	 * Get the singleton instance.
188
-	 *
189
-	 * @return \Wordlift_Configuration_Service
190
-	 * @since 3.6.0
191
-	 */
192
-	public static function get_instance() {
193
-
194
-		if ( ! isset( self::$instance ) ) {
195
-			self::$instance = new self();
196
-		}
197
-
198
-		return self::$instance;
199
-	}
200
-
201
-	/**
202
-	 * Get a configuration given the option name and a key. The option value is
203
-	 * expected to be an array.
204
-	 *
205
-	 * @param string $option The option name.
206
-	 * @param string $key A key in the option value array.
207
-	 * @param string $default The default value in case the key is not found (by default an empty string).
208
-	 *
209
-	 * @return mixed The configuration value or the default value if not found.
210
-	 * @since 3.6.0
211
-	 */
212
-	private function get( $option, $key, $default = '' ) {
213
-
214
-		$options = get_option( $option, array() );
215
-
216
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
217
-	}
218
-
219
-	/**
220
-	 * Set a configuration parameter.
221
-	 *
222
-	 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
223
-	 * @param string $key The value key.
224
-	 * @param mixed  $value The value.
225
-	 *
226
-	 * @since 3.9.0
227
-	 */
228
-	private function set( $option, $key, $value ) {
229
-
230
-		$values         = get_option( $option );
231
-		$values         = isset( $values ) ? $values : array();
232
-		$values[ $key ] = $value;
233
-		update_option( $option, $values );
234
-
235
-	}
236
-
237
-	/**
238
-	 * Get the entity base path, by default 'entity'.
239
-	 *
240
-	 * @return string The entity base path.
241
-	 * @since 3.6.0
242
-	 */
243
-	public function get_entity_base_path() {
244
-
245
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
246
-	}
247
-
248
-	/**
249
-	 * Get the entity base path.
250
-	 *
251
-	 * @param string $value The entity base path.
252
-	 *
253
-	 * @since 3.9.0
254
-	 */
255
-	public function set_entity_base_path( $value ) {
256
-
257
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
258
-
259
-	}
260
-
261
-	/**
262
-	 * Whether the installation skip wizard should be skipped.
263
-	 *
264
-	 * @return bool True if it should be skipped otherwise false.
265
-	 * @since 3.9.0
266
-	 */
267
-	public function is_skip_wizard() {
268
-
269
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
270
-	}
271
-
272
-	/**
273
-	 * Set the skip wizard parameter.
274
-	 *
275
-	 * @param bool $value True to skip the wizard. We expect a boolean value.
276
-	 *
277
-	 * @since 3.9.0
278
-	 */
279
-	public function set_skip_wizard( $value ) {
280
-
281
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
282
-
283
-	}
284
-
285
-	/**
286
-	 * Get WordLift's key.
287
-	 *
288
-	 * @return string WordLift's key or an empty string if not set.
289
-	 * @since 3.9.0
290
-	 */
291
-	public function get_key() {
292
-
293
-		return $this->get( 'wl_general_settings', self::KEY, '' );
294
-	}
295
-
296
-	/**
297
-	 * Set WordLift's key.
298
-	 *
299
-	 * @param string $value WordLift's key.
300
-	 *
301
-	 * @since 3.9.0
302
-	 */
303
-	public function set_key( $value ) {
304
-
305
-		$this->set( 'wl_general_settings', self::KEY, $value );
306
-	}
307
-
308
-	/**
309
-	 * Get WordLift's configured language, by default 'en'.
310
-	 *
311
-	 * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
312
-	 *
313
-	 * @return string WordLift's configured language code ('en' by default).
314
-	 * @since 3.9.0
315
-	 */
316
-	public function get_language_code() {
317
-
318
-		$language = get_locale();
319
-		if ( ! $language ) {
320
-			return 'en';
321
-		}
322
-
323
-		return substr( $language, 0, 2 );
324
-	}
325
-
326
-	/**
327
-	 * @param string $value WordLift's language code.
328
-	 *
329
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/1466
330
-	 *
331
-	 * Set WordLift's language code, used when storing strings to the Linked Data dataset.
332
-	 *
333
-	 * @deprecated As of 3.32.7 this below method has no effect on setting the language, we use the
334
-	 * language code form WordPress directly.
335
-	 *
336
-	 * @since 3.9.0
337
-	 */
338
-	public function set_language_code( $value ) {
339
-
340
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
341
-
342
-	}
343
-
344
-	/**
345
-	 * Set the user preferences about sharing diagnostic with us.
346
-	 *
347
-	 * @param string $value The user preferences(yes/no).
348
-	 *
349
-	 * @since 3.19.0
350
-	 */
351
-	public function set_diagnostic_preferences( $value ) {
352
-
353
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
354
-
355
-	}
356
-
357
-	/**
358
-	 * Get the user preferences about sharing diagnostic.
359
-	 *
360
-	 * @since 3.19.0
361
-	 */
362
-	public function get_diagnostic_preferences() {
363
-
364
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
365
-	}
366
-
367
-	/**
368
-	 * Get WordLift's configured country code, by default 'us'.
369
-	 *
370
-	 * @return string WordLift's configured country code ('us' by default).
371
-	 * @since 3.18.0
372
-	 */
373
-	public function get_country_code() {
374
-
375
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
376
-	}
377
-
378
-	/**
379
-	 * Set WordLift's country code.
380
-	 *
381
-	 * @param string $value WordLift's country code.
382
-	 *
383
-	 * @since 3.18.0
384
-	 */
385
-	public function set_country_code( $value ) {
386
-
387
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
388
-
389
-	}
390
-
391
-	/**
392
-	 * Get the alternateName.
393
-	 *
394
-	 * Website markup alternateName
395
-	 *
396
-	 * @return string|NULL alternateName or NULL if not set.
397
-	 * @since 3.38.6
398
-	 */
399
-	public function get_alternate_name() {
400
-		return $this->get( 'wl_general_settings', self::ALTERNATE_NAME );
401
-	}
402
-
403
-	/**
404
-	 * Set the alternateName.
405
-	 *
406
-	 * @param int $value The alternateName value.
407
-	 *
408
-	 * @since 3.38.6
409
-	 */
410
-	public function set_alternate_name( $value ) {
411
-
412
-		$this->set( 'wl_general_settings', self::ALTERNATE_NAME, wp_strip_all_tags( $value ) );
413
-
414
-	}
415
-
416
-	/**
417
-	 * Get the publisher entity post id.
418
-	 *
419
-	 * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
420
-	 * Article markup.
421
-	 *
422
-	 * @return int|NULL The publisher entity post id or NULL if not set.
423
-	 * @since 3.9.0
424
-	 */
425
-	public function get_publisher_id() {
426
-
427
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
428
-	}
429
-
430
-	/**
431
-	 * Set the publisher entity post id.
432
-	 *
433
-	 * @param int $value The publisher entity post id.
434
-	 *
435
-	 * @since 3.9.0
436
-	 */
437
-	public function set_publisher_id( $value ) {
438
-
439
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
440
-
441
-	}
442
-
443
-	/**
444
-	 * Get the dataset URI.
445
-	 *
446
-	 * @return string The dataset URI or an empty string if not set.
447
-	 * @since 3.10.0
448
-	 * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
449
-	 */
450
-	public function get_dataset_uri() {
451
-
452
-		if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
453
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
454
-		} else {
455
-			return null;
456
-		}
457
-	}
458
-
459
-	/**
460
-	 * Set the dataset URI.
461
-	 *
462
-	 * @param string $value The dataset URI.
463
-	 *
464
-	 * @since 3.10.0
465
-	 */
466
-	public function set_dataset_uri( $value ) {
467
-
468
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
469
-	}
470
-
471
-	/**
472
-	 * Get the package type.
473
-	 *
474
-	 * @return string The package type or an empty string if not set.
475
-	 * @since 3.20.0
476
-	 */
477
-	public function get_package_type() {
478
-
479
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
480
-	}
481
-
482
-	/**
483
-	 * Set the package type.
484
-	 *
485
-	 * @param string $value The package type.
486
-	 *
487
-	 * @since 3.20.0
488
-	 */
489
-	public function set_package_type( $value ) {
490
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
491
-	}
492
-
493
-	/**
494
-	 * Intercept the change of the WordLift key in order to set the dataset URI.
495
-	 *
496
-	 * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
497
-	 *               store the configuration parameters such as country or language.
498
-	 * @since 3.11.0
499
-	 *
500
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/761
501
-	 *
502
-	 * @param array $old_value The old settings.
503
-	 * @param array $new_value The new settings.
504
-	 */
505
-	public function update_key( $old_value, $new_value ) {
506
-
507
-		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
508
-		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
509
-		$new_key = isset( $new_value['key'] ) ? trim( $new_value['key'] ) : '';
510
-
511
-		// If the key hasn't changed, don't do anything.
512
-		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
513
-		// if ( $old_key === $new_key ) {
514
-		// return;
515
-		// }
516
-
517
-		// If the key is empty, empty the dataset URI.
518
-		if ( '' === $new_key ) {
519
-			$this->set_dataset_uri( '' );
520
-		}
521
-
522
-		// make the request to the remote server.
523
-		$this->get_remote_dataset_uri( $new_key );
524
-
525
-		do_action( 'wl_key_updated' );
526
-
527
-	}
528
-
529
-	/**
530
-	 * Handle retrieving the dataset uri from the remote server.
531
-	 *
532
-	 * If a valid dataset uri is returned it is stored in the appropriate option,
533
-	 * otherwise the option is set to empty string.
534
-	 *
535
-	 * @param string $key The key to be used.
536
-	 *
537
-	 * @since 3.12.0
538
-	 *
539
-	 * @since 3.17.0 send the site URL and get the dataset URI.
540
-	 */
541
-	public function get_remote_dataset_uri( $key ) {
542
-
543
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
544
-
545
-		if ( empty( $key ) ) {
546
-			$this->log->warn( 'Key set to empty value.' );
547
-
548
-			$this->set_dataset_uri( '' );
549
-			$this->set_package_type( null );
550
-
551
-			return;
552
-		}
553
-
554
-		/**
555
-		 * Allow 3rd parties to change the site_url.
556
-		 *
557
-		 * @param string $site_url The site url.
558
-		 *
559
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/850
560
-		 *
561
-		 * @since 3.20.0
562
-		 */
563
-		$home_url = get_option( 'home' );
564
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
565
-
566
-		// Build the URL.
567
-		$url = '/accounts'
568
-			   . '?key=' . rawurlencode( $key )
569
-			   . '&url=' . rawurlencode( $site_url )
570
-			   . '&country=' . $this->get_country_code()
571
-			   . '&language=' . $this->get_language_code();
572
-
573
-		$api_service = Default_Api_Service::get_instance();
574
-		/**
575
-		 * @since 3.27.7.1
576
-		 * The Key should be passed to headers, otherwise api would return null.
577
-		 */
578
-		$headers  = array(
579
-			'Authorization' => "Key $key",
580
-		);
581
-		$response = $api_service->request( 'PUT', $url, $headers )->get_response();
582
-
583
-		// The response is an error.
584
-		if ( is_wp_error( $response ) ) {
585
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
586
-
587
-			$this->set_dataset_uri( '' );
588
-			$this->set_package_type( null );
589
-
590
-			return;
591
-		}
592
-
593
-		// The response is not OK.
594
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
595
-			$base_url = $api_service->get_base_url();
596
-
597
-			if ( ! is_array( $response ) ) {
598
-				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
599
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
600
-			} else {
601
-				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
602
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
603
-			}
604
-
605
-			$this->set_dataset_uri( '' );
606
-			$this->set_package_type( null );
607
-
608
-			return;
609
-		}
610
-
611
-		/*
26
+    /**
27
+     * The entity base path option name.
28
+     *
29
+     * @since 3.6.0
30
+     */
31
+    const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
32
+
33
+    /**
34
+     * The skip wizard (admin installation wizard) option name.
35
+     *
36
+     * @since 3.9.0
37
+     */
38
+    const SKIP_WIZARD = 'wl_skip_wizard';
39
+
40
+    /**
41
+     * The skip installation notice.
42
+     *
43
+     * @since 3.40.3
44
+     */
45
+    const SKIP_INSTALLATION_NOTICE = 'wl_skip_installation_notice';
46
+
47
+    /**
48
+     * WordLift's key option name.
49
+     *
50
+     * @since 3.9.0
51
+     */
52
+    const KEY = 'key';
53
+
54
+    /**
55
+     * WordLift's configured language option name.
56
+     *
57
+     * @since 3.9.0
58
+     */
59
+    const LANGUAGE = 'site_language';
60
+
61
+    /**
62
+     * WordLift's configured country code.
63
+     *
64
+     * @since 3.18.0
65
+     */
66
+    const COUNTRY_CODE = 'country_code';
67
+
68
+    /**
69
+     * The alternateName option name.
70
+     *
71
+     * @since 3.38.6
72
+     */
73
+    const ALTERNATE_NAME = 'wl-alternate-name';
74
+
75
+    /**
76
+     * The publisher entity post ID option name.
77
+     *
78
+     * @since 3.9.0
79
+     */
80
+    const PUBLISHER_ID = 'publisher_id';
81
+
82
+    /**
83
+     * The dataset URI option name
84
+     *
85
+     * @since 3.10.0
86
+     */
87
+    const DATASET_URI = 'redlink_dataset_uri';
88
+
89
+    /**
90
+     * The link by default option name.
91
+     *
92
+     * @since 3.11.0
93
+     */
94
+    const LINK_BY_DEFAULT = 'link_by_default';
95
+
96
+    /**
97
+     * The analytics enable option.
98
+     *
99
+     * @since 3.21.0
100
+     */
101
+    const ANALYTICS_ENABLE = 'analytics_enable';
102
+
103
+    /**
104
+     * The analytics entity uri dimension option.
105
+     *
106
+     * @since 3.21.0
107
+     */
108
+    const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
109
+
110
+    /**
111
+     * The analytics entity type dimension option.
112
+     *
113
+     * @since 3.21.0
114
+     */
115
+    const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
116
+
117
+    /**
118
+     * The user preferences about sharing data option.
119
+     *
120
+     * @since 3.19.0
121
+     */
122
+    const SEND_DIAGNOSTIC = 'send_diagnostic';
123
+
124
+    /**
125
+     * The package type configuration key.
126
+     *
127
+     * @since 3.20.0
128
+     */
129
+    const PACKAGE_TYPE = 'package_type';
130
+    /**
131
+     * The dataset ids connected to the current key
132
+     *
133
+     * @since 3.38.5
134
+     */
135
+    const NETWORK_DATASET_IDS = 'network_dataset_ids';
136
+
137
+    /**
138
+     * The {@link Wordlift_Log_Service} instance.
139
+     *
140
+     * @since 3.16.0
141
+     *
142
+     * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
143
+     */
144
+    private $log;
145
+
146
+    /**
147
+     * Create a Wordlift_Configuration_Service's instance.
148
+     *
149
+     * @since 3.6.0
150
+     */
151
+    protected function __construct() {
152
+
153
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
154
+
155
+        // Sync some configuration properties when key is validated.
156
+        add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
157
+
158
+    }
159
+
160
+    /**
161
+     * @param $response \Wordlift\Api\Response
162
+     *
163
+     * @return void
164
+     */
165
+    public function sync( $response ) {
166
+        if ( ! $response->is_success() ) {
167
+            return;
168
+        }
169
+        $data = json_decode( $response->get_body(), true );
170
+        if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
171
+            return;
172
+        }
173
+        $this->set_network_dataset_ids( $data['networkDatasetId'] );
174
+    }
175
+
176
+    /**
177
+     * The Wordlift_Configuration_Service's singleton instance.
178
+     *
179
+     * @since  3.6.0
180
+     *
181
+     * @access private
182
+     * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
183
+     */
184
+    private static $instance = null;
185
+
186
+    /**
187
+     * Get the singleton instance.
188
+     *
189
+     * @return \Wordlift_Configuration_Service
190
+     * @since 3.6.0
191
+     */
192
+    public static function get_instance() {
193
+
194
+        if ( ! isset( self::$instance ) ) {
195
+            self::$instance = new self();
196
+        }
197
+
198
+        return self::$instance;
199
+    }
200
+
201
+    /**
202
+     * Get a configuration given the option name and a key. The option value is
203
+     * expected to be an array.
204
+     *
205
+     * @param string $option The option name.
206
+     * @param string $key A key in the option value array.
207
+     * @param string $default The default value in case the key is not found (by default an empty string).
208
+     *
209
+     * @return mixed The configuration value or the default value if not found.
210
+     * @since 3.6.0
211
+     */
212
+    private function get( $option, $key, $default = '' ) {
213
+
214
+        $options = get_option( $option, array() );
215
+
216
+        return isset( $options[ $key ] ) ? $options[ $key ] : $default;
217
+    }
218
+
219
+    /**
220
+     * Set a configuration parameter.
221
+     *
222
+     * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
223
+     * @param string $key The value key.
224
+     * @param mixed  $value The value.
225
+     *
226
+     * @since 3.9.0
227
+     */
228
+    private function set( $option, $key, $value ) {
229
+
230
+        $values         = get_option( $option );
231
+        $values         = isset( $values ) ? $values : array();
232
+        $values[ $key ] = $value;
233
+        update_option( $option, $values );
234
+
235
+    }
236
+
237
+    /**
238
+     * Get the entity base path, by default 'entity'.
239
+     *
240
+     * @return string The entity base path.
241
+     * @since 3.6.0
242
+     */
243
+    public function get_entity_base_path() {
244
+
245
+        return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
246
+    }
247
+
248
+    /**
249
+     * Get the entity base path.
250
+     *
251
+     * @param string $value The entity base path.
252
+     *
253
+     * @since 3.9.0
254
+     */
255
+    public function set_entity_base_path( $value ) {
256
+
257
+        $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
258
+
259
+    }
260
+
261
+    /**
262
+     * Whether the installation skip wizard should be skipped.
263
+     *
264
+     * @return bool True if it should be skipped otherwise false.
265
+     * @since 3.9.0
266
+     */
267
+    public function is_skip_wizard() {
268
+
269
+        return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
270
+    }
271
+
272
+    /**
273
+     * Set the skip wizard parameter.
274
+     *
275
+     * @param bool $value True to skip the wizard. We expect a boolean value.
276
+     *
277
+     * @since 3.9.0
278
+     */
279
+    public function set_skip_wizard( $value ) {
280
+
281
+        $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
282
+
283
+    }
284
+
285
+    /**
286
+     * Get WordLift's key.
287
+     *
288
+     * @return string WordLift's key or an empty string if not set.
289
+     * @since 3.9.0
290
+     */
291
+    public function get_key() {
292
+
293
+        return $this->get( 'wl_general_settings', self::KEY, '' );
294
+    }
295
+
296
+    /**
297
+     * Set WordLift's key.
298
+     *
299
+     * @param string $value WordLift's key.
300
+     *
301
+     * @since 3.9.0
302
+     */
303
+    public function set_key( $value ) {
304
+
305
+        $this->set( 'wl_general_settings', self::KEY, $value );
306
+    }
307
+
308
+    /**
309
+     * Get WordLift's configured language, by default 'en'.
310
+     *
311
+     * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
312
+     *
313
+     * @return string WordLift's configured language code ('en' by default).
314
+     * @since 3.9.0
315
+     */
316
+    public function get_language_code() {
317
+
318
+        $language = get_locale();
319
+        if ( ! $language ) {
320
+            return 'en';
321
+        }
322
+
323
+        return substr( $language, 0, 2 );
324
+    }
325
+
326
+    /**
327
+     * @param string $value WordLift's language code.
328
+     *
329
+     * @see https://github.com/insideout10/wordlift-plugin/issues/1466
330
+     *
331
+     * Set WordLift's language code, used when storing strings to the Linked Data dataset.
332
+     *
333
+     * @deprecated As of 3.32.7 this below method has no effect on setting the language, we use the
334
+     * language code form WordPress directly.
335
+     *
336
+     * @since 3.9.0
337
+     */
338
+    public function set_language_code( $value ) {
339
+
340
+        $this->set( 'wl_general_settings', self::LANGUAGE, $value );
341
+
342
+    }
343
+
344
+    /**
345
+     * Set the user preferences about sharing diagnostic with us.
346
+     *
347
+     * @param string $value The user preferences(yes/no).
348
+     *
349
+     * @since 3.19.0
350
+     */
351
+    public function set_diagnostic_preferences( $value ) {
352
+
353
+        $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
354
+
355
+    }
356
+
357
+    /**
358
+     * Get the user preferences about sharing diagnostic.
359
+     *
360
+     * @since 3.19.0
361
+     */
362
+    public function get_diagnostic_preferences() {
363
+
364
+        return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
365
+    }
366
+
367
+    /**
368
+     * Get WordLift's configured country code, by default 'us'.
369
+     *
370
+     * @return string WordLift's configured country code ('us' by default).
371
+     * @since 3.18.0
372
+     */
373
+    public function get_country_code() {
374
+
375
+        return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
376
+    }
377
+
378
+    /**
379
+     * Set WordLift's country code.
380
+     *
381
+     * @param string $value WordLift's country code.
382
+     *
383
+     * @since 3.18.0
384
+     */
385
+    public function set_country_code( $value ) {
386
+
387
+        $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
388
+
389
+    }
390
+
391
+    /**
392
+     * Get the alternateName.
393
+     *
394
+     * Website markup alternateName
395
+     *
396
+     * @return string|NULL alternateName or NULL if not set.
397
+     * @since 3.38.6
398
+     */
399
+    public function get_alternate_name() {
400
+        return $this->get( 'wl_general_settings', self::ALTERNATE_NAME );
401
+    }
402
+
403
+    /**
404
+     * Set the alternateName.
405
+     *
406
+     * @param int $value The alternateName value.
407
+     *
408
+     * @since 3.38.6
409
+     */
410
+    public function set_alternate_name( $value ) {
411
+
412
+        $this->set( 'wl_general_settings', self::ALTERNATE_NAME, wp_strip_all_tags( $value ) );
413
+
414
+    }
415
+
416
+    /**
417
+     * Get the publisher entity post id.
418
+     *
419
+     * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
420
+     * Article markup.
421
+     *
422
+     * @return int|NULL The publisher entity post id or NULL if not set.
423
+     * @since 3.9.0
424
+     */
425
+    public function get_publisher_id() {
426
+
427
+        return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
428
+    }
429
+
430
+    /**
431
+     * Set the publisher entity post id.
432
+     *
433
+     * @param int $value The publisher entity post id.
434
+     *
435
+     * @since 3.9.0
436
+     */
437
+    public function set_publisher_id( $value ) {
438
+
439
+        $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
440
+
441
+    }
442
+
443
+    /**
444
+     * Get the dataset URI.
445
+     *
446
+     * @return string The dataset URI or an empty string if not set.
447
+     * @since 3.10.0
448
+     * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
449
+     */
450
+    public function get_dataset_uri() {
451
+
452
+        if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
453
+            return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
454
+        } else {
455
+            return null;
456
+        }
457
+    }
458
+
459
+    /**
460
+     * Set the dataset URI.
461
+     *
462
+     * @param string $value The dataset URI.
463
+     *
464
+     * @since 3.10.0
465
+     */
466
+    public function set_dataset_uri( $value ) {
467
+
468
+        $this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
469
+    }
470
+
471
+    /**
472
+     * Get the package type.
473
+     *
474
+     * @return string The package type or an empty string if not set.
475
+     * @since 3.20.0
476
+     */
477
+    public function get_package_type() {
478
+
479
+        return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
480
+    }
481
+
482
+    /**
483
+     * Set the package type.
484
+     *
485
+     * @param string $value The package type.
486
+     *
487
+     * @since 3.20.0
488
+     */
489
+    public function set_package_type( $value ) {
490
+        $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
491
+    }
492
+
493
+    /**
494
+     * Intercept the change of the WordLift key in order to set the dataset URI.
495
+     *
496
+     * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
497
+     *               store the configuration parameters such as country or language.
498
+     * @since 3.11.0
499
+     *
500
+     * @see https://github.com/insideout10/wordlift-plugin/issues/761
501
+     *
502
+     * @param array $old_value The old settings.
503
+     * @param array $new_value The new settings.
504
+     */
505
+    public function update_key( $old_value, $new_value ) {
506
+
507
+        // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
508
+        // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
509
+        $new_key = isset( $new_value['key'] ) ? trim( $new_value['key'] ) : '';
510
+
511
+        // If the key hasn't changed, don't do anything.
512
+        // WARN The 'update_option' hook is fired only if the new and old value are not equal.
513
+        // if ( $old_key === $new_key ) {
514
+        // return;
515
+        // }
516
+
517
+        // If the key is empty, empty the dataset URI.
518
+        if ( '' === $new_key ) {
519
+            $this->set_dataset_uri( '' );
520
+        }
521
+
522
+        // make the request to the remote server.
523
+        $this->get_remote_dataset_uri( $new_key );
524
+
525
+        do_action( 'wl_key_updated' );
526
+
527
+    }
528
+
529
+    /**
530
+     * Handle retrieving the dataset uri from the remote server.
531
+     *
532
+     * If a valid dataset uri is returned it is stored in the appropriate option,
533
+     * otherwise the option is set to empty string.
534
+     *
535
+     * @param string $key The key to be used.
536
+     *
537
+     * @since 3.12.0
538
+     *
539
+     * @since 3.17.0 send the site URL and get the dataset URI.
540
+     */
541
+    public function get_remote_dataset_uri( $key ) {
542
+
543
+        $this->log->trace( 'Getting the remote dataset URI and package type...' );
544
+
545
+        if ( empty( $key ) ) {
546
+            $this->log->warn( 'Key set to empty value.' );
547
+
548
+            $this->set_dataset_uri( '' );
549
+            $this->set_package_type( null );
550
+
551
+            return;
552
+        }
553
+
554
+        /**
555
+         * Allow 3rd parties to change the site_url.
556
+         *
557
+         * @param string $site_url The site url.
558
+         *
559
+         * @see https://github.com/insideout10/wordlift-plugin/issues/850
560
+         *
561
+         * @since 3.20.0
562
+         */
563
+        $home_url = get_option( 'home' );
564
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
565
+
566
+        // Build the URL.
567
+        $url = '/accounts'
568
+                . '?key=' . rawurlencode( $key )
569
+                . '&url=' . rawurlencode( $site_url )
570
+                . '&country=' . $this->get_country_code()
571
+                . '&language=' . $this->get_language_code();
572
+
573
+        $api_service = Default_Api_Service::get_instance();
574
+        /**
575
+         * @since 3.27.7.1
576
+         * The Key should be passed to headers, otherwise api would return null.
577
+         */
578
+        $headers  = array(
579
+            'Authorization' => "Key $key",
580
+        );
581
+        $response = $api_service->request( 'PUT', $url, $headers )->get_response();
582
+
583
+        // The response is an error.
584
+        if ( is_wp_error( $response ) ) {
585
+            $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
586
+
587
+            $this->set_dataset_uri( '' );
588
+            $this->set_package_type( null );
589
+
590
+            return;
591
+        }
592
+
593
+        // The response is not OK.
594
+        if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
595
+            $base_url = $api_service->get_base_url();
596
+
597
+            if ( ! is_array( $response ) ) {
598
+                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
599
+                $this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
600
+            } else {
601
+                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
602
+                $this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
603
+            }
604
+
605
+            $this->set_dataset_uri( '' );
606
+            $this->set_package_type( null );
607
+
608
+            return;
609
+        }
610
+
611
+        /*
612 612
 		 * We also store the package type.
613 613
 		 *
614 614
 		 * @since 3.20.0
615 615
 		 */
616
-		$json = json_decode( $response['body'] );
617
-		/**
618
-		 * @since 3.27.7
619
-		 * Remove the trailing slash returned from the new platform api.
620
-		 */
621
-		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
622
-		$dataset_uri = untrailingslashit( $json->datasetURI );
623
-		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
624
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
625
-
626
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
627
-
628
-		$this->set_dataset_uri( $dataset_uri );
629
-		$this->set_package_type( $package_type );
630
-	}
631
-
632
-	/**
633
-	 * Handle the edge case where a user submits the same key again
634
-	 * when he does not have the dataset uri to regain it.
635
-	 *
636
-	 * This can not be handled in the normal option update hook because
637
-	 * it is not being triggered when the save value equals to the one already
638
-	 * in the DB.
639
-	 *
640
-	 * @param mixed $value The new, unserialized option value.
641
-	 * @param mixed $old_value The old option value.
642
-	 *
643
-	 * @return mixed The same value in the $value parameter
644
-	 * @since 3.12.0
645
-	 */
646
-	public function maybe_update_dataset_uri( $value, $old_value ) {
647
-
648
-		// Check the old key value and the new one. Here we're only handling the
649
-		// case where the key hasn't changed and the dataset URI isn't set. The
650
-		// other case, i.e. a new key is inserted, is handled at `update_key`.
651
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
652
-		$new_key = isset( $value['key'] ) ? trim( $value['key'] ) : '';
653
-
654
-		$dataset_uri = $this->get_dataset_uri();
655
-
656
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
657
-
658
-			// make the request to the remote server to try to get the dataset uri.
659
-			$this->get_remote_dataset_uri( $new_key );
660
-		}
661
-
662
-		return $value;
663
-	}
664
-
665
-	/**
666
-	 * Get the API URI to retrieve the dataset URI using the WordLift Key.
667
-	 *
668
-	 * @param string $key The WordLift key to use.
669
-	 *
670
-	 * @return string The API URI.
671
-	 * @since 3.11.0
672
-	 */
673
-	public function get_accounts_by_key_dataset_uri( $key ) {
674
-
675
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
676
-	}
677
-
678
-	/**
679
-	 * Get the `accounts` end point.
680
-	 *
681
-	 * @return string The `accounts` end point.
682
-	 * @since 3.16.0
683
-	 */
684
-	public function get_accounts() {
685
-
686
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
687
-	}
688
-
689
-	/**
690
-	 * Get the `link by default` option.
691
-	 *
692
-	 * @return bool True if entities must be linked by default otherwise false.
693
-	 * @since 3.13.0
694
-	 */
695
-	public function is_link_by_default() {
696
-
697
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
698
-	}
699
-
700
-	/**
701
-	 * Set the `link by default` option.
702
-	 *
703
-	 * @param bool $value True to enabling linking by default, otherwise false.
704
-	 *
705
-	 * @since 3.13.0
706
-	 */
707
-	public function set_link_by_default( $value ) {
708
-
709
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
710
-	}
711
-
712
-	/**
713
-	 * Get the 'analytics-enable' option.
714
-	 *
715
-	 * @return string 'no' or 'yes' representing bool.
716
-	 * @since 3.21.0
717
-	 */
718
-	public function is_analytics_enable() {
719
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
720
-	}
721
-
722
-	/**
723
-	 * Set the `analytics-enable` option.
724
-	 *
725
-	 * @param bool $value True to enabling analytics, otherwise false.
726
-	 *
727
-	 * @since 3.21.0
728
-	 */
729
-	public function set_is_analytics_enable( $value ) {
730
-
731
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
732
-	}
733
-
734
-	/**
735
-	 * Get the 'analytics-entity-uri-dimention' option.
736
-	 *
737
-	 * @return int
738
-	 * @since 3.21.0
739
-	 */
740
-	public function get_analytics_entity_uri_dimension() {
741
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
742
-	}
743
-
744
-	/**
745
-	 * Get the 'analytics-entity-type-dimension' option.
746
-	 *
747
-	 * @return int
748
-	 * @since 3.21.0
749
-	 */
750
-	public function get_analytics_entity_type_dimension() {
751
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
752
-	}
753
-
754
-	/**
755
-	 * Get the URL to perform autocomplete request.
756
-	 *
757
-	 * @return string The URL to call to perform the autocomplete request.
758
-	 * @since 3.15.0
759
-	 */
760
-	public function get_autocomplete_url() {
761
-
762
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
763
-
764
-	}
765
-
766
-	/**
767
-	 * Get the URL to perform feedback deactivation request.
768
-	 *
769
-	 * @return string The URL to call to perform the feedback deactivation request.
770
-	 * @since 3.19.0
771
-	 */
772
-	public function get_deactivation_feedback_url() {
773
-
774
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
775
-
776
-	}
777
-
778
-	/**
779
-	 * Get the base API URL.
780
-	 *
781
-	 * @return string The base API URL.
782
-	 * @since 3.20.0
783
-	 */
784
-	public function get_api_url() {
785
-
786
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
787
-	}
788
-
789
-	public function get_network_dataset_ids() {
790
-		return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
791
-	}
792
-
793
-	public function set_network_dataset_ids( $network_dataset_ids ) {
794
-		$this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
795
-	}
796
-
797
-	public function get_skip_installation_notice() {
798
-
799
-		return $this->get( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, false );
800
-	}
801
-
802
-	public function set_skip_installation_notice( $value ) {
803
-
804
-		$this->set( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, $value );
805
-
806
-	}
616
+        $json = json_decode( $response['body'] );
617
+        /**
618
+         * @since 3.27.7
619
+         * Remove the trailing slash returned from the new platform api.
620
+         */
621
+        // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
622
+        $dataset_uri = untrailingslashit( $json->datasetURI );
623
+        // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
624
+        $package_type = isset( $json->packageType ) ? $json->packageType : null;
625
+
626
+        $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
627
+
628
+        $this->set_dataset_uri( $dataset_uri );
629
+        $this->set_package_type( $package_type );
630
+    }
631
+
632
+    /**
633
+     * Handle the edge case where a user submits the same key again
634
+     * when he does not have the dataset uri to regain it.
635
+     *
636
+     * This can not be handled in the normal option update hook because
637
+     * it is not being triggered when the save value equals to the one already
638
+     * in the DB.
639
+     *
640
+     * @param mixed $value The new, unserialized option value.
641
+     * @param mixed $old_value The old option value.
642
+     *
643
+     * @return mixed The same value in the $value parameter
644
+     * @since 3.12.0
645
+     */
646
+    public function maybe_update_dataset_uri( $value, $old_value ) {
647
+
648
+        // Check the old key value and the new one. Here we're only handling the
649
+        // case where the key hasn't changed and the dataset URI isn't set. The
650
+        // other case, i.e. a new key is inserted, is handled at `update_key`.
651
+        $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
652
+        $new_key = isset( $value['key'] ) ? trim( $value['key'] ) : '';
653
+
654
+        $dataset_uri = $this->get_dataset_uri();
655
+
656
+        if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
657
+
658
+            // make the request to the remote server to try to get the dataset uri.
659
+            $this->get_remote_dataset_uri( $new_key );
660
+        }
661
+
662
+        return $value;
663
+    }
664
+
665
+    /**
666
+     * Get the API URI to retrieve the dataset URI using the WordLift Key.
667
+     *
668
+     * @param string $key The WordLift key to use.
669
+     *
670
+     * @return string The API URI.
671
+     * @since 3.11.0
672
+     */
673
+    public function get_accounts_by_key_dataset_uri( $key ) {
674
+
675
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
676
+    }
677
+
678
+    /**
679
+     * Get the `accounts` end point.
680
+     *
681
+     * @return string The `accounts` end point.
682
+     * @since 3.16.0
683
+     */
684
+    public function get_accounts() {
685
+
686
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
687
+    }
688
+
689
+    /**
690
+     * Get the `link by default` option.
691
+     *
692
+     * @return bool True if entities must be linked by default otherwise false.
693
+     * @since 3.13.0
694
+     */
695
+    public function is_link_by_default() {
696
+
697
+        return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
698
+    }
699
+
700
+    /**
701
+     * Set the `link by default` option.
702
+     *
703
+     * @param bool $value True to enabling linking by default, otherwise false.
704
+     *
705
+     * @since 3.13.0
706
+     */
707
+    public function set_link_by_default( $value ) {
708
+
709
+        $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
710
+    }
711
+
712
+    /**
713
+     * Get the 'analytics-enable' option.
714
+     *
715
+     * @return string 'no' or 'yes' representing bool.
716
+     * @since 3.21.0
717
+     */
718
+    public function is_analytics_enable() {
719
+        return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
720
+    }
721
+
722
+    /**
723
+     * Set the `analytics-enable` option.
724
+     *
725
+     * @param bool $value True to enabling analytics, otherwise false.
726
+     *
727
+     * @since 3.21.0
728
+     */
729
+    public function set_is_analytics_enable( $value ) {
730
+
731
+        $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
732
+    }
733
+
734
+    /**
735
+     * Get the 'analytics-entity-uri-dimention' option.
736
+     *
737
+     * @return int
738
+     * @since 3.21.0
739
+     */
740
+    public function get_analytics_entity_uri_dimension() {
741
+        return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
742
+    }
743
+
744
+    /**
745
+     * Get the 'analytics-entity-type-dimension' option.
746
+     *
747
+     * @return int
748
+     * @since 3.21.0
749
+     */
750
+    public function get_analytics_entity_type_dimension() {
751
+        return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
752
+    }
753
+
754
+    /**
755
+     * Get the URL to perform autocomplete request.
756
+     *
757
+     * @return string The URL to call to perform the autocomplete request.
758
+     * @since 3.15.0
759
+     */
760
+    public function get_autocomplete_url() {
761
+
762
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
763
+
764
+    }
765
+
766
+    /**
767
+     * Get the URL to perform feedback deactivation request.
768
+     *
769
+     * @return string The URL to call to perform the feedback deactivation request.
770
+     * @since 3.19.0
771
+     */
772
+    public function get_deactivation_feedback_url() {
773
+
774
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
775
+
776
+    }
777
+
778
+    /**
779
+     * Get the base API URL.
780
+     *
781
+     * @return string The base API URL.
782
+     * @since 3.20.0
783
+     */
784
+    public function get_api_url() {
785
+
786
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
787
+    }
788
+
789
+    public function get_network_dataset_ids() {
790
+        return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
791
+    }
792
+
793
+    public function set_network_dataset_ids( $network_dataset_ids ) {
794
+        $this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
795
+    }
796
+
797
+    public function get_skip_installation_notice() {
798
+
799
+        return $this->get( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, false );
800
+    }
801
+
802
+    public function set_skip_installation_notice( $value ) {
803
+
804
+        $this->set( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, $value );
805
+
806
+    }
807 807
 
808 808
 }
Please login to merge, or discard this patch.
Spacing   +108 added lines, -108 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
 
13 13
 use Wordlift\Api\Default_Api_Service;
14 14
 
15
-if ( ! defined( 'ABSPATH' ) ) {
15
+if ( ! defined('ABSPATH')) {
16 16
 	exit;
17 17
 }
18 18
 
@@ -150,10 +150,10 @@  discard block
 block discarded – undo
150 150
 	 */
151 151
 	protected function __construct() {
152 152
 
153
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
153
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
154 154
 
155 155
 		// Sync some configuration properties when key is validated.
156
-		add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
156
+		add_action('wl_key_validation_response', array($this, 'sync'));
157 157
 
158 158
 	}
159 159
 
@@ -162,15 +162,15 @@  discard block
 block discarded – undo
162 162
 	 *
163 163
 	 * @return void
164 164
 	 */
165
-	public function sync( $response ) {
166
-		if ( ! $response->is_success() ) {
165
+	public function sync($response) {
166
+		if ( ! $response->is_success()) {
167 167
 			return;
168 168
 		}
169
-		$data = json_decode( $response->get_body(), true );
170
-		if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
169
+		$data = json_decode($response->get_body(), true);
170
+		if ( ! is_array($data) || ! array_key_exists('networkDatasetId', $data)) {
171 171
 			return;
172 172
 		}
173
-		$this->set_network_dataset_ids( $data['networkDatasetId'] );
173
+		$this->set_network_dataset_ids($data['networkDatasetId']);
174 174
 	}
175 175
 
176 176
 	/**
@@ -191,7 +191,7 @@  discard block
 block discarded – undo
191 191
 	 */
192 192
 	public static function get_instance() {
193 193
 
194
-		if ( ! isset( self::$instance ) ) {
194
+		if ( ! isset(self::$instance)) {
195 195
 			self::$instance = new self();
196 196
 		}
197 197
 
@@ -209,11 +209,11 @@  discard block
 block discarded – undo
209 209
 	 * @return mixed The configuration value or the default value if not found.
210 210
 	 * @since 3.6.0
211 211
 	 */
212
-	private function get( $option, $key, $default = '' ) {
212
+	private function get($option, $key, $default = '') {
213 213
 
214
-		$options = get_option( $option, array() );
214
+		$options = get_option($option, array());
215 215
 
216
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
216
+		return isset($options[$key]) ? $options[$key] : $default;
217 217
 	}
218 218
 
219 219
 	/**
@@ -225,12 +225,12 @@  discard block
 block discarded – undo
225 225
 	 *
226 226
 	 * @since 3.9.0
227 227
 	 */
228
-	private function set( $option, $key, $value ) {
228
+	private function set($option, $key, $value) {
229 229
 
230
-		$values         = get_option( $option );
231
-		$values         = isset( $values ) ? $values : array();
232
-		$values[ $key ] = $value;
233
-		update_option( $option, $values );
230
+		$values         = get_option($option);
231
+		$values         = isset($values) ? $values : array();
232
+		$values[$key] = $value;
233
+		update_option($option, $values);
234 234
 
235 235
 	}
236 236
 
@@ -242,7 +242,7 @@  discard block
 block discarded – undo
242 242
 	 */
243 243
 	public function get_entity_base_path() {
244 244
 
245
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
245
+		return $this->get('wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity');
246 246
 	}
247 247
 
248 248
 	/**
@@ -252,9 +252,9 @@  discard block
 block discarded – undo
252 252
 	 *
253 253
 	 * @since 3.9.0
254 254
 	 */
255
-	public function set_entity_base_path( $value ) {
255
+	public function set_entity_base_path($value) {
256 256
 
257
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
257
+		$this->set('wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value);
258 258
 
259 259
 	}
260 260
 
@@ -266,7 +266,7 @@  discard block
 block discarded – undo
266 266
 	 */
267 267
 	public function is_skip_wizard() {
268 268
 
269
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
269
+		return $this->get('wl_general_settings', self::SKIP_WIZARD, false);
270 270
 	}
271 271
 
272 272
 	/**
@@ -276,9 +276,9 @@  discard block
 block discarded – undo
276 276
 	 *
277 277
 	 * @since 3.9.0
278 278
 	 */
279
-	public function set_skip_wizard( $value ) {
279
+	public function set_skip_wizard($value) {
280 280
 
281
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
281
+		$this->set('wl_general_settings', self::SKIP_WIZARD, true === $value);
282 282
 
283 283
 	}
284 284
 
@@ -290,7 +290,7 @@  discard block
 block discarded – undo
290 290
 	 */
291 291
 	public function get_key() {
292 292
 
293
-		return $this->get( 'wl_general_settings', self::KEY, '' );
293
+		return $this->get('wl_general_settings', self::KEY, '');
294 294
 	}
295 295
 
296 296
 	/**
@@ -300,9 +300,9 @@  discard block
 block discarded – undo
300 300
 	 *
301 301
 	 * @since 3.9.0
302 302
 	 */
303
-	public function set_key( $value ) {
303
+	public function set_key($value) {
304 304
 
305
-		$this->set( 'wl_general_settings', self::KEY, $value );
305
+		$this->set('wl_general_settings', self::KEY, $value);
306 306
 	}
307 307
 
308 308
 	/**
@@ -316,11 +316,11 @@  discard block
 block discarded – undo
316 316
 	public function get_language_code() {
317 317
 
318 318
 		$language = get_locale();
319
-		if ( ! $language ) {
319
+		if ( ! $language) {
320 320
 			return 'en';
321 321
 		}
322 322
 
323
-		return substr( $language, 0, 2 );
323
+		return substr($language, 0, 2);
324 324
 	}
325 325
 
326 326
 	/**
@@ -335,9 +335,9 @@  discard block
 block discarded – undo
335 335
 	 *
336 336
 	 * @since 3.9.0
337 337
 	 */
338
-	public function set_language_code( $value ) {
338
+	public function set_language_code($value) {
339 339
 
340
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
340
+		$this->set('wl_general_settings', self::LANGUAGE, $value);
341 341
 
342 342
 	}
343 343
 
@@ -348,9 +348,9 @@  discard block
 block discarded – undo
348 348
 	 *
349 349
 	 * @since 3.19.0
350 350
 	 */
351
-	public function set_diagnostic_preferences( $value ) {
351
+	public function set_diagnostic_preferences($value) {
352 352
 
353
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
353
+		$this->set('wl_general_settings', self::SEND_DIAGNOSTIC, $value);
354 354
 
355 355
 	}
356 356
 
@@ -361,7 +361,7 @@  discard block
 block discarded – undo
361 361
 	 */
362 362
 	public function get_diagnostic_preferences() {
363 363
 
364
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
364
+		return $this->get('wl_general_settings', self::SEND_DIAGNOSTIC, 'no');
365 365
 	}
366 366
 
367 367
 	/**
@@ -372,7 +372,7 @@  discard block
 block discarded – undo
372 372
 	 */
373 373
 	public function get_country_code() {
374 374
 
375
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
375
+		return $this->get('wl_general_settings', self::COUNTRY_CODE, 'us');
376 376
 	}
377 377
 
378 378
 	/**
@@ -382,9 +382,9 @@  discard block
 block discarded – undo
382 382
 	 *
383 383
 	 * @since 3.18.0
384 384
 	 */
385
-	public function set_country_code( $value ) {
385
+	public function set_country_code($value) {
386 386
 
387
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
387
+		$this->set('wl_general_settings', self::COUNTRY_CODE, $value);
388 388
 
389 389
 	}
390 390
 
@@ -397,7 +397,7 @@  discard block
 block discarded – undo
397 397
 	 * @since 3.38.6
398 398
 	 */
399 399
 	public function get_alternate_name() {
400
-		return $this->get( 'wl_general_settings', self::ALTERNATE_NAME );
400
+		return $this->get('wl_general_settings', self::ALTERNATE_NAME);
401 401
 	}
402 402
 
403 403
 	/**
@@ -407,9 +407,9 @@  discard block
 block discarded – undo
407 407
 	 *
408 408
 	 * @since 3.38.6
409 409
 	 */
410
-	public function set_alternate_name( $value ) {
410
+	public function set_alternate_name($value) {
411 411
 
412
-		$this->set( 'wl_general_settings', self::ALTERNATE_NAME, wp_strip_all_tags( $value ) );
412
+		$this->set('wl_general_settings', self::ALTERNATE_NAME, wp_strip_all_tags($value));
413 413
 
414 414
 	}
415 415
 
@@ -424,7 +424,7 @@  discard block
 block discarded – undo
424 424
 	 */
425 425
 	public function get_publisher_id() {
426 426
 
427
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
427
+		return $this->get('wl_general_settings', self::PUBLISHER_ID, null);
428 428
 	}
429 429
 
430 430
 	/**
@@ -434,9 +434,9 @@  discard block
 block discarded – undo
434 434
 	 *
435 435
 	 * @since 3.9.0
436 436
 	 */
437
-	public function set_publisher_id( $value ) {
437
+	public function set_publisher_id($value) {
438 438
 
439
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
439
+		$this->set('wl_general_settings', self::PUBLISHER_ID, $value);
440 440
 
441 441
 	}
442 442
 
@@ -449,8 +449,8 @@  discard block
 block discarded – undo
449 449
 	 */
450 450
 	public function get_dataset_uri() {
451 451
 
452
-		if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
453
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
452
+		if (apply_filters('wl_feature__enable__dataset', true)) {
453
+			return $this->get('wl_advanced_settings', self::DATASET_URI, null);
454 454
 		} else {
455 455
 			return null;
456 456
 		}
@@ -463,9 +463,9 @@  discard block
 block discarded – undo
463 463
 	 *
464 464
 	 * @since 3.10.0
465 465
 	 */
466
-	public function set_dataset_uri( $value ) {
466
+	public function set_dataset_uri($value) {
467 467
 
468
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
468
+		$this->set('wl_advanced_settings', self::DATASET_URI, $value);
469 469
 	}
470 470
 
471 471
 	/**
@@ -476,7 +476,7 @@  discard block
 block discarded – undo
476 476
 	 */
477 477
 	public function get_package_type() {
478 478
 
479
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
479
+		return $this->get('wl_advanced_settings', self::PACKAGE_TYPE, null);
480 480
 	}
481 481
 
482 482
 	/**
@@ -486,8 +486,8 @@  discard block
 block discarded – undo
486 486
 	 *
487 487
 	 * @since 3.20.0
488 488
 	 */
489
-	public function set_package_type( $value ) {
490
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
489
+	public function set_package_type($value) {
490
+		$this->set('wl_advanced_settings', self::PACKAGE_TYPE, $value);
491 491
 	}
492 492
 
493 493
 	/**
@@ -502,11 +502,11 @@  discard block
 block discarded – undo
502 502
 	 * @param array $old_value The old settings.
503 503
 	 * @param array $new_value The new settings.
504 504
 	 */
505
-	public function update_key( $old_value, $new_value ) {
505
+	public function update_key($old_value, $new_value) {
506 506
 
507 507
 		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
508 508
 		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
509
-		$new_key = isset( $new_value['key'] ) ? trim( $new_value['key'] ) : '';
509
+		$new_key = isset($new_value['key']) ? trim($new_value['key']) : '';
510 510
 
511 511
 		// If the key hasn't changed, don't do anything.
512 512
 		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
@@ -515,14 +515,14 @@  discard block
 block discarded – undo
515 515
 		// }
516 516
 
517 517
 		// If the key is empty, empty the dataset URI.
518
-		if ( '' === $new_key ) {
519
-			$this->set_dataset_uri( '' );
518
+		if ('' === $new_key) {
519
+			$this->set_dataset_uri('');
520 520
 		}
521 521
 
522 522
 		// make the request to the remote server.
523
-		$this->get_remote_dataset_uri( $new_key );
523
+		$this->get_remote_dataset_uri($new_key);
524 524
 
525
-		do_action( 'wl_key_updated' );
525
+		do_action('wl_key_updated');
526 526
 
527 527
 	}
528 528
 
@@ -538,15 +538,15 @@  discard block
 block discarded – undo
538 538
 	 *
539 539
 	 * @since 3.17.0 send the site URL and get the dataset URI.
540 540
 	 */
541
-	public function get_remote_dataset_uri( $key ) {
541
+	public function get_remote_dataset_uri($key) {
542 542
 
543
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
543
+		$this->log->trace('Getting the remote dataset URI and package type...');
544 544
 
545
-		if ( empty( $key ) ) {
546
-			$this->log->warn( 'Key set to empty value.' );
545
+		if (empty($key)) {
546
+			$this->log->warn('Key set to empty value.');
547 547
 
548
-			$this->set_dataset_uri( '' );
549
-			$this->set_package_type( null );
548
+			$this->set_dataset_uri('');
549
+			$this->set_package_type(null);
550 550
 
551 551
 			return;
552 552
 		}
@@ -560,15 +560,15 @@  discard block
 block discarded – undo
560 560
 		 *
561 561
 		 * @since 3.20.0
562 562
 		 */
563
-		$home_url = get_option( 'home' );
564
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
563
+		$home_url = get_option('home');
564
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url));
565 565
 
566 566
 		// Build the URL.
567 567
 		$url = '/accounts'
568
-			   . '?key=' . rawurlencode( $key )
569
-			   . '&url=' . rawurlencode( $site_url )
570
-			   . '&country=' . $this->get_country_code()
571
-			   . '&language=' . $this->get_language_code();
568
+			   . '?key='.rawurlencode($key)
569
+			   . '&url='.rawurlencode($site_url)
570
+			   . '&country='.$this->get_country_code()
571
+			   . '&language='.$this->get_language_code();
572 572
 
573 573
 		$api_service = Default_Api_Service::get_instance();
574 574
 		/**
@@ -578,32 +578,32 @@  discard block
 block discarded – undo
578 578
 		$headers  = array(
579 579
 			'Authorization' => "Key $key",
580 580
 		);
581
-		$response = $api_service->request( 'PUT', $url, $headers )->get_response();
581
+		$response = $api_service->request('PUT', $url, $headers)->get_response();
582 582
 
583 583
 		// The response is an error.
584
-		if ( is_wp_error( $response ) ) {
585
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
584
+		if (is_wp_error($response)) {
585
+			$this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message());
586 586
 
587
-			$this->set_dataset_uri( '' );
588
-			$this->set_package_type( null );
587
+			$this->set_dataset_uri('');
588
+			$this->set_package_type(null);
589 589
 
590 590
 			return;
591 591
 		}
592 592
 
593 593
 		// The response is not OK.
594
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
594
+		if ( ! is_array($response) || 200 !== (int) $response['response']['code']) {
595 595
 			$base_url = $api_service->get_base_url();
596 596
 
597
-			if ( ! is_array( $response ) ) {
597
+			if ( ! is_array($response)) {
598 598
 				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
599
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
599
+				$this->log->error("Unexpected response when opening URL $base_url$url: ".var_export($response, true));
600 600
 			} else {
601 601
 				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
602
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
602
+				$this->log->error("Unexpected status code when opening URL $base_url$url: ".$response['response']['code']."\n".var_export($response, true));
603 603
 			}
604 604
 
605
-			$this->set_dataset_uri( '' );
606
-			$this->set_package_type( null );
605
+			$this->set_dataset_uri('');
606
+			$this->set_package_type(null);
607 607
 
608 608
 			return;
609 609
 		}
@@ -613,20 +613,20 @@  discard block
 block discarded – undo
613 613
 		 *
614 614
 		 * @since 3.20.0
615 615
 		 */
616
-		$json = json_decode( $response['body'] );
616
+		$json = json_decode($response['body']);
617 617
 		/**
618 618
 		 * @since 3.27.7
619 619
 		 * Remove the trailing slash returned from the new platform api.
620 620
 		 */
621 621
 		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
622
-		$dataset_uri = untrailingslashit( $json->datasetURI );
622
+		$dataset_uri = untrailingslashit($json->datasetURI);
623 623
 		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
624
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
624
+		$package_type = isset($json->packageType) ? $json->packageType : null;
625 625
 
626
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
626
+		$this->log->info("Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]...");
627 627
 
628
-		$this->set_dataset_uri( $dataset_uri );
629
-		$this->set_package_type( $package_type );
628
+		$this->set_dataset_uri($dataset_uri);
629
+		$this->set_package_type($package_type);
630 630
 	}
631 631
 
632 632
 	/**
@@ -643,20 +643,20 @@  discard block
 block discarded – undo
643 643
 	 * @return mixed The same value in the $value parameter
644 644
 	 * @since 3.12.0
645 645
 	 */
646
-	public function maybe_update_dataset_uri( $value, $old_value ) {
646
+	public function maybe_update_dataset_uri($value, $old_value) {
647 647
 
648 648
 		// Check the old key value and the new one. Here we're only handling the
649 649
 		// case where the key hasn't changed and the dataset URI isn't set. The
650 650
 		// other case, i.e. a new key is inserted, is handled at `update_key`.
651
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
652
-		$new_key = isset( $value['key'] ) ? trim( $value['key'] ) : '';
651
+		$old_key = isset($old_value['key']) ? $old_value['key'] : '';
652
+		$new_key = isset($value['key']) ? trim($value['key']) : '';
653 653
 
654 654
 		$dataset_uri = $this->get_dataset_uri();
655 655
 
656
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
656
+		if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) {
657 657
 
658 658
 			// make the request to the remote server to try to get the dataset uri.
659
-			$this->get_remote_dataset_uri( $new_key );
659
+			$this->get_remote_dataset_uri($new_key);
660 660
 		}
661 661
 
662 662
 		return $value;
@@ -670,9 +670,9 @@  discard block
 block discarded – undo
670 670
 	 * @return string The API URI.
671 671
 	 * @since 3.11.0
672 672
 	 */
673
-	public function get_accounts_by_key_dataset_uri( $key ) {
673
+	public function get_accounts_by_key_dataset_uri($key) {
674 674
 
675
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
675
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri";
676 676
 	}
677 677
 
678 678
 	/**
@@ -683,7 +683,7 @@  discard block
 block discarded – undo
683 683
 	 */
684 684
 	public function get_accounts() {
685 685
 
686
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
686
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts';
687 687
 	}
688 688
 
689 689
 	/**
@@ -694,7 +694,7 @@  discard block
 block discarded – undo
694 694
 	 */
695 695
 	public function is_link_by_default() {
696 696
 
697
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
697
+		return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes');
698 698
 	}
699 699
 
700 700
 	/**
@@ -704,9 +704,9 @@  discard block
 block discarded – undo
704 704
 	 *
705 705
 	 * @since 3.13.0
706 706
 	 */
707
-	public function set_link_by_default( $value ) {
707
+	public function set_link_by_default($value) {
708 708
 
709
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
709
+		$this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no');
710 710
 	}
711 711
 
712 712
 	/**
@@ -716,7 +716,7 @@  discard block
 block discarded – undo
716 716
 	 * @since 3.21.0
717 717
 	 */
718 718
 	public function is_analytics_enable() {
719
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
719
+		return 'yes' === $this->get('wl_analytics_settings', self::ANALYTICS_ENABLE, 'no');
720 720
 	}
721 721
 
722 722
 	/**
@@ -726,9 +726,9 @@  discard block
 block discarded – undo
726 726
 	 *
727 727
 	 * @since 3.21.0
728 728
 	 */
729
-	public function set_is_analytics_enable( $value ) {
729
+	public function set_is_analytics_enable($value) {
730 730
 
731
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
731
+		$this->set('wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no');
732 732
 	}
733 733
 
734 734
 	/**
@@ -738,7 +738,7 @@  discard block
 block discarded – undo
738 738
 	 * @since 3.21.0
739 739
 	 */
740 740
 	public function get_analytics_entity_uri_dimension() {
741
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
741
+		return (int) $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1);
742 742
 	}
743 743
 
744 744
 	/**
@@ -748,7 +748,7 @@  discard block
 block discarded – undo
748 748
 	 * @since 3.21.0
749 749
 	 */
750 750
 	public function get_analytics_entity_type_dimension() {
751
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
751
+		return $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2);
752 752
 	}
753 753
 
754 754
 	/**
@@ -759,7 +759,7 @@  discard block
 block discarded – undo
759 759
 	 */
760 760
 	public function get_autocomplete_url() {
761 761
 
762
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
762
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete';
763 763
 
764 764
 	}
765 765
 
@@ -771,7 +771,7 @@  discard block
 block discarded – undo
771 771
 	 */
772 772
 	public function get_deactivation_feedback_url() {
773 773
 
774
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
774
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks';
775 775
 
776 776
 	}
777 777
 
@@ -787,21 +787,21 @@  discard block
 block discarded – undo
787 787
 	}
788 788
 
789 789
 	public function get_network_dataset_ids() {
790
-		return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
790
+		return $this->get('wl_advanced_settings', self::NETWORK_DATASET_IDS, array());
791 791
 	}
792 792
 
793
-	public function set_network_dataset_ids( $network_dataset_ids ) {
794
-		$this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
793
+	public function set_network_dataset_ids($network_dataset_ids) {
794
+		$this->set('wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids);
795 795
 	}
796 796
 
797 797
 	public function get_skip_installation_notice() {
798 798
 
799
-		return $this->get( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, false );
799
+		return $this->get('wl_general_settings', self::SKIP_INSTALLATION_NOTICE, false);
800 800
 	}
801 801
 
802
-	public function set_skip_installation_notice( $value ) {
802
+	public function set_skip_installation_notice($value) {
803 803
 
804
-		$this->set( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, $value );
804
+		$this->set('wl_general_settings', self::SKIP_INSTALLATION_NOTICE, $value);
805 805
 
806 806
 	}
807 807
 
Please login to merge, or discard this patch.
src/js/dist/no-editor-analysis.asset.php 2 patches
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@
 block discarded – undo
1 1
 <?php return array(
2
-	'dependencies' => array( 'react', 'react-dom', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-polyfill', 'wp-rich-text' ),
3
-	'version'      => '0ffbdec637035dcca8bbe5839f9b3cd3',
2
+    'dependencies' => array( 'react', 'react-dom', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-polyfill', 'wp-rich-text' ),
3
+    'version'      => '0ffbdec637035dcca8bbe5839f9b3cd3',
4 4
 );
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@
 block discarded – undo
1 1
 <?php return array(
2
-	'dependencies' => array( 'react', 'react-dom', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-polyfill', 'wp-rich-text' ),
2
+	'dependencies' => array('react', 'react-dom', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-polyfill', 'wp-rich-text'),
3 3
 	'version'      => '0ffbdec637035dcca8bbe5839f9b3cd3',
4 4
 );
Please login to merge, or discard this patch.
src/js/dist/block-editor.asset.php 2 patches
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@
 block discarded – undo
1 1
 <?php return array(
2
-	'dependencies' => array( 'react', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-polyfill', 'wp-rich-text' ),
3
-	'version'      => '9991f5bb31a907ef27b3f15491a97d8d',
2
+    'dependencies' => array( 'react', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-polyfill', 'wp-rich-text' ),
3
+    'version'      => '9991f5bb31a907ef27b3f15491a97d8d',
4 4
 );
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@
 block discarded – undo
1 1
 <?php return array(
2
-	'dependencies' => array( 'react', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-polyfill', 'wp-rich-text' ),
2
+	'dependencies' => array('react', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-polyfill', 'wp-rich-text'),
3 3
 	'version'      => '9991f5bb31a907ef27b3f15491a97d8d',
4 4
 );
Please login to merge, or discard this patch.
src/js/dist/edit.asset.php 2 patches
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@
 block discarded – undo
1 1
 <?php return array(
2
-	'dependencies' => array( 'react', 'react-dom', 'wp-hooks', 'wp-polyfill' ),
3
-	'version'      => 'e4663448def6a65455d4a6b394d6a89d',
2
+    'dependencies' => array( 'react', 'react-dom', 'wp-hooks', 'wp-polyfill' ),
3
+    'version'      => 'e4663448def6a65455d4a6b394d6a89d',
4 4
 );
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@
 block discarded – undo
1 1
 <?php return array(
2
-	'dependencies' => array( 'react', 'react-dom', 'wp-hooks', 'wp-polyfill' ),
2
+	'dependencies' => array('react', 'react-dom', 'wp-hooks', 'wp-polyfill'),
3 3
 	'version'      => 'e4663448def6a65455d4a6b394d6a89d',
4 4
 );
Please login to merge, or discard this patch.