Completed
Push — develop ( 0c26c3...8268ab )
by David
03:02
created
src/includes/class-wordlift-entity-service.php 2 patches
Indentation   +584 added lines, -584 removed lines patch added patch discarded remove patch
@@ -16,508 +16,508 @@  discard block
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Entity_Service {
18 18
 
19
-	/**
20
-	 * The Log service.
21
-	 *
22
-	 * @since  3.2.0
23
-	 * @access private
24
-	 * @var \Wordlift_Log_Service $log The Log service.
25
-	 */
26
-	private $log;
27
-
28
-	/**
29
-	 * The UI service.
30
-	 *
31
-	 * @since  3.2.0
32
-	 * @access private
33
-	 * @var \Wordlift_UI_Service $ui_service The UI service.
34
-	 */
35
-	private $ui_service;
36
-
37
-	/**
38
-	 * The {@link Wordlift_Relation_Service} instance.
39
-	 *
40
-	 * @since  3.15.0
41
-	 * @access private
42
-	 * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
43
-	 */
44
-	private $relation_service;
45
-
46
-	/**
47
-	 * The {@link Wordlift_Entity_Uri_Service} instance.
48
-	 *
49
-	 * @since  3.16.3
50
-	 * @access private
51
-	 * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
52
-	 */
53
-	private $entity_uri_service;
54
-
55
-	/**
56
-	 * The entity post type name.
57
-	 *
58
-	 * @since 3.1.0
59
-	 */
60
-	const TYPE_NAME = 'entity';
61
-
62
-	/**
63
-	 * The alternative label meta key.
64
-	 *
65
-	 * @since 3.2.0
66
-	 */
67
-	const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
68
-
69
-	/**
70
-	 * The alternative label input template.
71
-	 *
72
-	 * @since 3.2.0
73
-	 */
74
-	// TODO: this should be moved to a class that deals with HTML code.
75
-	const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
19
+    /**
20
+     * The Log service.
21
+     *
22
+     * @since  3.2.0
23
+     * @access private
24
+     * @var \Wordlift_Log_Service $log The Log service.
25
+     */
26
+    private $log;
27
+
28
+    /**
29
+     * The UI service.
30
+     *
31
+     * @since  3.2.0
32
+     * @access private
33
+     * @var \Wordlift_UI_Service $ui_service The UI service.
34
+     */
35
+    private $ui_service;
36
+
37
+    /**
38
+     * The {@link Wordlift_Relation_Service} instance.
39
+     *
40
+     * @since  3.15.0
41
+     * @access private
42
+     * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
43
+     */
44
+    private $relation_service;
45
+
46
+    /**
47
+     * The {@link Wordlift_Entity_Uri_Service} instance.
48
+     *
49
+     * @since  3.16.3
50
+     * @access private
51
+     * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
52
+     */
53
+    private $entity_uri_service;
54
+
55
+    /**
56
+     * The entity post type name.
57
+     *
58
+     * @since 3.1.0
59
+     */
60
+    const TYPE_NAME = 'entity';
61
+
62
+    /**
63
+     * The alternative label meta key.
64
+     *
65
+     * @since 3.2.0
66
+     */
67
+    const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
68
+
69
+    /**
70
+     * The alternative label input template.
71
+     *
72
+     * @since 3.2.0
73
+     */
74
+    // TODO: this should be moved to a class that deals with HTML code.
75
+    const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label">
76 76
                 <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label>
77 77
                 <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text">
78 78
                 <button class="button wl-delete-button">%s</button>
79 79
                 </div>';
80 80
 
81
-	/**
82
-	 * A singleton instance of the Entity service.
83
-	 *
84
-	 * @since  3.2.0
85
-	 * @access private
86
-	 * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
87
-	 */
88
-	private static $instance;
89
-
90
-	/**
91
-	 * Create a Wordlift_Entity_Service instance.
92
-	 *
93
-	 * @param \Wordlift_UI_Service $ui_service The UI service.
94
-	 * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
95
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
96
-	 *
97
-	 * @since 3.2.0
98
-	 *
99
-	 */
100
-	public function __construct( $ui_service, $relation_service, $entity_uri_service ) {
101
-
102
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
103
-
104
-		$this->ui_service         = $ui_service;
105
-		$this->relation_service   = $relation_service;
106
-		$this->entity_uri_service = $entity_uri_service;
107
-
108
-		// Set the singleton instance.
109
-		self::$instance = $this;
110
-	}
111
-
112
-	/**
113
-	 * Get the singleton instance of the Entity service.
114
-	 *
115
-	 * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
116
-	 * @since 3.2.0
117
-	 */
118
-	public static function get_instance() {
119
-
120
-		return self::$instance;
121
-	}
122
-
123
-	/**
124
-	 * Determines whether a post is an entity or not. Entity is in this context
125
-	 * something which is not an article.
126
-	 *
127
-	 * @param int $post_id A post id.
128
-	 *
129
-	 * @return bool Return true if the post is an entity otherwise false.
130
-	 * @since 3.1.0
131
-	 *
132
-	 */
133
-	public function is_entity( $post_id ) {
134
-
135
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
136
-
137
-		if ( is_wp_error( $terms ) ) {
138
-			$this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() );
139
-
140
-			return false;
141
-		}
142
-
143
-		if ( empty( $terms ) ) {
144
-			return false;
145
-		}
146
-
147
-		/*
81
+    /**
82
+     * A singleton instance of the Entity service.
83
+     *
84
+     * @since  3.2.0
85
+     * @access private
86
+     * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service.
87
+     */
88
+    private static $instance;
89
+
90
+    /**
91
+     * Create a Wordlift_Entity_Service instance.
92
+     *
93
+     * @param \Wordlift_UI_Service $ui_service The UI service.
94
+     * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance.
95
+     * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
96
+     *
97
+     * @since 3.2.0
98
+     *
99
+     */
100
+    public function __construct( $ui_service, $relation_service, $entity_uri_service ) {
101
+
102
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
103
+
104
+        $this->ui_service         = $ui_service;
105
+        $this->relation_service   = $relation_service;
106
+        $this->entity_uri_service = $entity_uri_service;
107
+
108
+        // Set the singleton instance.
109
+        self::$instance = $this;
110
+    }
111
+
112
+    /**
113
+     * Get the singleton instance of the Entity service.
114
+     *
115
+     * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
116
+     * @since 3.2.0
117
+     */
118
+    public static function get_instance() {
119
+
120
+        return self::$instance;
121
+    }
122
+
123
+    /**
124
+     * Determines whether a post is an entity or not. Entity is in this context
125
+     * something which is not an article.
126
+     *
127
+     * @param int $post_id A post id.
128
+     *
129
+     * @return bool Return true if the post is an entity otherwise false.
130
+     * @since 3.1.0
131
+     *
132
+     */
133
+    public function is_entity( $post_id ) {
134
+
135
+        $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
136
+
137
+        if ( is_wp_error( $terms ) ) {
138
+            $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() );
139
+
140
+            return false;
141
+        }
142
+
143
+        if ( empty( $terms ) ) {
144
+            return false;
145
+        }
146
+
147
+        /*
148 148
 		 * We don't consider an `article` to be an entity.
149 149
 		 *
150 150
 		 * @since 3.20.0 At least one associated mustn't be an `article`.
151 151
 		 *
152 152
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
153 153
 		 */
154
-		foreach ( $terms as $term ) {
155
-			if ( 'article' !== $term->slug ) {
156
-				return true;
157
-			}
158
-		}
159
-
160
-		return false;
161
-	}
162
-
163
-	/**
164
-	 * Get the proper classification scope for a given entity post
165
-	 *
166
-	 * @param integer $post_id An entity post id.
167
-	 *
168
-	 * @param string $default The default classification scope, `what` if not
169
-	 *                         provided.
170
-	 *
171
-	 * @return string Returns a classification scope (e.g. 'what').
172
-	 * @since 3.5.0
173
-	 *
174
-	 */
175
-	public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
176
-
177
-		if ( false === $this->is_entity( $post_id ) ) {
178
-			return $default;
179
-		}
180
-
181
-		// Retrieve the entity type
182
-		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
183
-		$entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
184
-		// Retrieve classification boxes configuration
185
-		$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
186
-		foreach ( $classification_boxes as $cb ) {
187
-			if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
188
-				return $cb['id'];
189
-			}
190
-		}
191
-
192
-		return $default;
193
-	}
194
-
195
-	/**
196
-	 * Check whether a {@link WP_Post} is used.
197
-	 *
198
-	 * @param int $post_id The {@link WP_Post}'s id.
199
-	 *
200
-	 * @return bool|null Null if it's not an entity, otherwise true if it's used.
201
-	 */
202
-	public function is_used( $post_id ) {
203
-
204
-		if ( false === $this->is_entity( $post_id ) ) {
205
-			return null;
206
-		}
207
-		// Retrieve the post
208
-		$entity = get_post( $post_id );
209
-
210
-		global $wpdb;
211
-		// Retrieve Wordlift relation instances table name
212
-		$table_name = wl_core_get_relation_instances_table_name();
213
-
214
-		// Check is it's referenced / related to another post / entity
215
-		$stmt = $wpdb->prepare(
216
-			"SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
217
-			$entity->ID
218
-		);
219
-
220
-		// Perform the query
221
-		$relation_instances = (int) $wpdb->get_var( $stmt );
222
-		// If there is at least one relation instance for the current entity, then it's used
223
-		if ( 0 < $relation_instances ) {
224
-			return true;
225
-		}
226
-
227
-		// Check if the entity uri is used as meta_value
228
-		$stmt = $wpdb->prepare(
229
-			"SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
230
-			$entity->ID,
231
-			wl_get_entity_uri( $entity->ID )
232
-		);
233
-		// Perform the query
234
-		$meta_instances = (int) $wpdb->get_var( $stmt );
235
-
236
-		// If there is at least one meta that refers the current entity uri, then current entity is used
237
-		if ( 0 < $meta_instances ) {
238
-			return true;
239
-		}
240
-
241
-		// If we are here, it means the current entity is not used at the moment
242
-		return false;
243
-	}
244
-
245
-	/**
246
-	 * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
247
-	 *
248
-	 * @param string $uri The entity URI.
249
-	 *
250
-	 * @return WP_Post|null A WP_Post instance or null if not found.
251
-	 * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
252
-	 *
253
-	 * @since      3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
254
-	 * @since      3.2.0
255
-	 *
256
-	 */
257
-	public function get_entity_post_by_uri( $uri ) {
258
-
259
-		return $this->entity_uri_service->get_entity( $uri );
260
-	}
261
-
262
-	/**
263
-	 * Fires once a post has been saved. This function uses the $_REQUEST, therefore
264
-	 * we check that the post we're saving is the current post.
265
-	 *
266
-	 * @see   https://github.com/insideout10/wordlift-plugin/issues/363
267
-	 *
268
-	 * @since 3.2.0
269
-	 *
270
-	 * @param int $post_id Post ID.
271
-	 * @param WP_Post $post Post object.
272
-	 * @param bool $update Whether this is an existing post being updated or not.
273
-	 */
274
-	public function save_post( $post_id, $post, $update ) {
275
-
276
-		// Avoid doing anything if post is autosave or a revision.
277
-		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
278
-			return;
279
-		}
280
-
281
-		// We're setting the alternative label that have been provided via the UI
282
-		// (in fact we're using $_REQUEST), while save_post may be also called
283
-		// programmatically by some other function: we need to check therefore if
284
-		// the $post_id in the save_post call matches the post id set in the request.
285
-		//
286
-		// If this is not the current post being saved or if it's not an entity, return.
287
-		if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
288
-			return;
289
-		}
290
-
291
-		// Get the alt labels from the request (or empty array).
292
-		$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
293
-
294
-		if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) {
295
-			// This is via classic editor, so set the alternative labels.
296
-			$this->set_alternative_labels( $post_id, $alt_labels );
297
-		}
298
-
299
-
300
-	}
301
-
302
-	/**
303
-	 * Set the alternative labels.
304
-	 *
305
-	 * @param int $post_id The post id.
306
-	 * @param array $alt_labels An array of labels.
307
-	 *
308
-	 * @since 3.2.0
309
-	 *
310
-	 */
311
-	public function set_alternative_labels( $post_id, $alt_labels ) {
312
-
313
-		// Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error
314
-		// instead of post id.
315
-		if ( ! is_numeric( $post_id ) ) {
316
-			return;
317
-		}
318
-
319
-		// Force $alt_labels to be an array
320
-		if ( ! is_array( $alt_labels ) ) {
321
-			$alt_labels = array( $alt_labels );
322
-		}
323
-
324
-		$this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
325
-
326
-		// Delete all the existing alternate labels.
327
-		delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
328
-
329
-		// Set the alternative labels.
330
-		foreach ( $alt_labels as $alt_label ) {
331
-			if ( ! empty( $alt_label ) ) {
332
-				add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
333
-			}
334
-		}
335
-
336
-	}
337
-
338
-	/**
339
-	 * Retrieve the alternate labels.
340
-	 *
341
-	 * @param int $post_id Post id.
342
-	 *
343
-	 * @return mixed An array  of alternative labels.
344
-	 * @since 3.2.0
345
-	 *
346
-	 */
347
-	public function get_alternative_labels( $post_id ) {
348
-
349
-		return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
350
-	}
351
-
352
-	/**
353
-	 * Retrieve the labels for an entity, i.e. the title + the synonyms.
354
-	 *
355
-	 * @param int $post_id The entity {@link WP_Post} id.
356
-	 *
357
-	 * @return array An array with the entity title and labels.
358
-	 * @since 3.12.0
359
-	 *
360
-	 */
361
-	public function get_labels( $post_id ) {
362
-
363
-		return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
364
-	}
365
-
366
-	/**
367
-	 * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
368
-	 *
369
-	 * @param WP_Post $post Post object.
370
-	 *
371
-	 * @since 3.2.0
372
-	 *
373
-	 */
374
-	public function edit_form_before_permalink( $post ) {
375
-
376
-		// If it's not an entity, return.
377
-		if ( ! $this->is_entity( $post->ID ) ) {
378
-			return;
379
-		}
380
-
381
-		// Print the input template.
382
-		$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
383
-
384
-		// Print all the currently set alternative labels.
385
-		foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
386
-
387
-			echo $this->get_alternative_label_input( $alt_label );
388
-
389
-		};
390
-
391
-		// Print the button.
392
-		$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
393
-
394
-	}
395
-
396
-	/**
397
-	 * Get the URI for the entity with the specified post id.
398
-	 *
399
-	 * @param int $post_id The entity post id.
400
-	 *
401
-	 * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
402
-	 * @since 3.6.0
403
-	 *
404
-	 */
405
-	public function get_uri( $post_id ) {
406
-
407
-		$log = Wordlift_Log_Service::get_logger( get_class() );
408
-
409
-		// If a null is given, nothing to do
410
-		if ( is_null( $post_id ) ) {
411
-			return null;
412
-		}
413
-
414
-		$dataset_uri = wl_configuration_get_redlink_dataset_uri();
415
-
416
-		if ( empty( $dataset_uri ) ) {
417
-			// Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri()
418
-			$log->debug( 'Continuing, dataset uri not configured...' );
419
-		}
420
-
421
-		$uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
422
-
423
-		/*
154
+        foreach ( $terms as $term ) {
155
+            if ( 'article' !== $term->slug ) {
156
+                return true;
157
+            }
158
+        }
159
+
160
+        return false;
161
+    }
162
+
163
+    /**
164
+     * Get the proper classification scope for a given entity post
165
+     *
166
+     * @param integer $post_id An entity post id.
167
+     *
168
+     * @param string $default The default classification scope, `what` if not
169
+     *                         provided.
170
+     *
171
+     * @return string Returns a classification scope (e.g. 'what').
172
+     * @since 3.5.0
173
+     *
174
+     */
175
+    public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
176
+
177
+        if ( false === $this->is_entity( $post_id ) ) {
178
+            return $default;
179
+        }
180
+
181
+        // Retrieve the entity type
182
+        $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
183
+        $entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
184
+        // Retrieve classification boxes configuration
185
+        $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
186
+        foreach ( $classification_boxes as $cb ) {
187
+            if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
188
+                return $cb['id'];
189
+            }
190
+        }
191
+
192
+        return $default;
193
+    }
194
+
195
+    /**
196
+     * Check whether a {@link WP_Post} is used.
197
+     *
198
+     * @param int $post_id The {@link WP_Post}'s id.
199
+     *
200
+     * @return bool|null Null if it's not an entity, otherwise true if it's used.
201
+     */
202
+    public function is_used( $post_id ) {
203
+
204
+        if ( false === $this->is_entity( $post_id ) ) {
205
+            return null;
206
+        }
207
+        // Retrieve the post
208
+        $entity = get_post( $post_id );
209
+
210
+        global $wpdb;
211
+        // Retrieve Wordlift relation instances table name
212
+        $table_name = wl_core_get_relation_instances_table_name();
213
+
214
+        // Check is it's referenced / related to another post / entity
215
+        $stmt = $wpdb->prepare(
216
+            "SELECT COUNT(*) FROM $table_name WHERE  object_id = %d",
217
+            $entity->ID
218
+        );
219
+
220
+        // Perform the query
221
+        $relation_instances = (int) $wpdb->get_var( $stmt );
222
+        // If there is at least one relation instance for the current entity, then it's used
223
+        if ( 0 < $relation_instances ) {
224
+            return true;
225
+        }
226
+
227
+        // Check if the entity uri is used as meta_value
228
+        $stmt = $wpdb->prepare(
229
+            "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
230
+            $entity->ID,
231
+            wl_get_entity_uri( $entity->ID )
232
+        );
233
+        // Perform the query
234
+        $meta_instances = (int) $wpdb->get_var( $stmt );
235
+
236
+        // If there is at least one meta that refers the current entity uri, then current entity is used
237
+        if ( 0 < $meta_instances ) {
238
+            return true;
239
+        }
240
+
241
+        // If we are here, it means the current entity is not used at the moment
242
+        return false;
243
+    }
244
+
245
+    /**
246
+     * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
247
+     *
248
+     * @param string $uri The entity URI.
249
+     *
250
+     * @return WP_Post|null A WP_Post instance or null if not found.
251
+     * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
252
+     *
253
+     * @since      3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );
254
+     * @since      3.2.0
255
+     *
256
+     */
257
+    public function get_entity_post_by_uri( $uri ) {
258
+
259
+        return $this->entity_uri_service->get_entity( $uri );
260
+    }
261
+
262
+    /**
263
+     * Fires once a post has been saved. This function uses the $_REQUEST, therefore
264
+     * we check that the post we're saving is the current post.
265
+     *
266
+     * @see   https://github.com/insideout10/wordlift-plugin/issues/363
267
+     *
268
+     * @since 3.2.0
269
+     *
270
+     * @param int $post_id Post ID.
271
+     * @param WP_Post $post Post object.
272
+     * @param bool $update Whether this is an existing post being updated or not.
273
+     */
274
+    public function save_post( $post_id, $post, $update ) {
275
+
276
+        // Avoid doing anything if post is autosave or a revision.
277
+        if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
278
+            return;
279
+        }
280
+
281
+        // We're setting the alternative label that have been provided via the UI
282
+        // (in fact we're using $_REQUEST), while save_post may be also called
283
+        // programmatically by some other function: we need to check therefore if
284
+        // the $post_id in the save_post call matches the post id set in the request.
285
+        //
286
+        // If this is not the current post being saved or if it's not an entity, return.
287
+        if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
288
+            return;
289
+        }
290
+
291
+        // Get the alt labels from the request (or empty array).
292
+        $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
293
+
294
+        if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) {
295
+            // This is via classic editor, so set the alternative labels.
296
+            $this->set_alternative_labels( $post_id, $alt_labels );
297
+        }
298
+
299
+
300
+    }
301
+
302
+    /**
303
+     * Set the alternative labels.
304
+     *
305
+     * @param int $post_id The post id.
306
+     * @param array $alt_labels An array of labels.
307
+     *
308
+     * @since 3.2.0
309
+     *
310
+     */
311
+    public function set_alternative_labels( $post_id, $alt_labels ) {
312
+
313
+        // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error
314
+        // instead of post id.
315
+        if ( ! is_numeric( $post_id ) ) {
316
+            return;
317
+        }
318
+
319
+        // Force $alt_labels to be an array
320
+        if ( ! is_array( $alt_labels ) ) {
321
+            $alt_labels = array( $alt_labels );
322
+        }
323
+
324
+        $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
325
+
326
+        // Delete all the existing alternate labels.
327
+        delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
328
+
329
+        // Set the alternative labels.
330
+        foreach ( $alt_labels as $alt_label ) {
331
+            if ( ! empty( $alt_label ) ) {
332
+                add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
333
+            }
334
+        }
335
+
336
+    }
337
+
338
+    /**
339
+     * Retrieve the alternate labels.
340
+     *
341
+     * @param int $post_id Post id.
342
+     *
343
+     * @return mixed An array  of alternative labels.
344
+     * @since 3.2.0
345
+     *
346
+     */
347
+    public function get_alternative_labels( $post_id ) {
348
+
349
+        return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
350
+    }
351
+
352
+    /**
353
+     * Retrieve the labels for an entity, i.e. the title + the synonyms.
354
+     *
355
+     * @param int $post_id The entity {@link WP_Post} id.
356
+     *
357
+     * @return array An array with the entity title and labels.
358
+     * @since 3.12.0
359
+     *
360
+     */
361
+    public function get_labels( $post_id ) {
362
+
363
+        return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
364
+    }
365
+
366
+    /**
367
+     * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0).
368
+     *
369
+     * @param WP_Post $post Post object.
370
+     *
371
+     * @since 3.2.0
372
+     *
373
+     */
374
+    public function edit_form_before_permalink( $post ) {
375
+
376
+        // If it's not an entity, return.
377
+        if ( ! $this->is_entity( $post->ID ) ) {
378
+            return;
379
+        }
380
+
381
+        // Print the input template.
382
+        $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
383
+
384
+        // Print all the currently set alternative labels.
385
+        foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
386
+
387
+            echo $this->get_alternative_label_input( $alt_label );
388
+
389
+        };
390
+
391
+        // Print the button.
392
+        $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
393
+
394
+    }
395
+
396
+    /**
397
+     * Get the URI for the entity with the specified post id.
398
+     *
399
+     * @param int $post_id The entity post id.
400
+     *
401
+     * @return null|string The entity URI or NULL if not found or the dataset URI is not configured.
402
+     * @since 3.6.0
403
+     *
404
+     */
405
+    public function get_uri( $post_id ) {
406
+
407
+        $log = Wordlift_Log_Service::get_logger( get_class() );
408
+
409
+        // If a null is given, nothing to do
410
+        if ( is_null( $post_id ) ) {
411
+            return null;
412
+        }
413
+
414
+        $dataset_uri = wl_configuration_get_redlink_dataset_uri();
415
+
416
+        if ( empty( $dataset_uri ) ) {
417
+            // Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri()
418
+            $log->debug( 'Continuing, dataset uri not configured...' );
419
+        }
420
+
421
+        $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
422
+
423
+        /*
424 424
 		 * Consider the URI invalid if it doesn't start with the dataset URI.
425 425
 		 *
426 426
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/996
427 427
 		 */
428
-		if ( null === $dataset_uri || 0 !== strpos( $uri, $dataset_uri ) ) {
429
-			$uri = null;
430
-		}
431
-
432
-		// Set the URI if it isn't set yet.
433
-		$post_status = get_post_status( $post_id );
434
-		if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
435
-			$uri = wl_build_entity_uri( $post_id );
436
-			wl_set_entity_uri( $post_id, $uri );
437
-		}
438
-
439
-		return $uri;
440
-	}
441
-
442
-
443
-	/**
444
-	 * Get the alternative label input HTML code.
445
-	 *
446
-	 * @param string $value The input value.
447
-	 *
448
-	 * @return string The input HTML code.
449
-	 * @since 3.2.0
450
-	 *
451
-	 */
452
-	private function get_alternative_label_input( $value = '' ) {
453
-
454
-		return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
455
-	}
456
-
457
-	/**
458
-	 * Get the number of entity posts published in this blog.
459
-	 *
460
-	 * @return int The number of published entity posts.
461
-	 * @since 3.6.0
462
-	 *
463
-	 */
464
-	public function count() {
465
-		global $wpdb;
466
-
467
-		// Try to get the count from the transient.
468
-		$count = get_transient( '_wl_entity_service__count' );
469
-		if ( false !== $count ) {
470
-			return $count;
471
-		}
472
-
473
-		// Query the count.
474
-		$count = $wpdb->get_var( $wpdb->prepare(
475
-			"SELECT COUNT( DISTINCT( tr.object_id ) )"
476
-			. " FROM {$wpdb->term_relationships} tr"
477
-			. " INNER JOIN {$wpdb->term_taxonomy} tt"
478
-			. "  ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id"
479
-			. " INNER JOIN {$wpdb->terms} t"
480
-			. "  ON t.term_id = tt.term_id AND t.name != %s",
481
-			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
482
-			'article'
483
-		) );
484
-
485
-		// Store the count in cache.
486
-		set_transient( '_wl_entity_service__count', $count, 900 );
487
-
488
-		return $count;
489
-	}
490
-
491
-	/**
492
-	 * Add the entity filtering criterias to the arguments for a `get_posts`
493
-	 * call.
494
-	 *
495
-	 * @param array $args The arguments for a `get_posts` call.
496
-	 *
497
-	 * @return array The arguments for a `get_posts` call.
498
-	 * @since 3.15.0
499
-	 *
500
-	 */
501
-	public static function add_criterias( $args ) {
502
-
503
-		// Build an optimal tax-query.
504
-		$tax_query = array(
505
-			'relation' => 'AND',
506
-			array(
507
-				'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
508
-				'operator' => 'EXISTS',
509
-			),
510
-			array(
511
-				'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
512
-				'field'    => 'slug',
513
-				'terms'    => 'article',
514
-				'operator' => 'NOT IN',
515
-			),
516
-		);
517
-
518
-		return $args + array(
519
-				'post_type' => Wordlift_Entity_Service::valid_entity_post_types(),
520
-				/*
428
+        if ( null === $dataset_uri || 0 !== strpos( $uri, $dataset_uri ) ) {
429
+            $uri = null;
430
+        }
431
+
432
+        // Set the URI if it isn't set yet.
433
+        $post_status = get_post_status( $post_id );
434
+        if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
435
+            $uri = wl_build_entity_uri( $post_id );
436
+            wl_set_entity_uri( $post_id, $uri );
437
+        }
438
+
439
+        return $uri;
440
+    }
441
+
442
+
443
+    /**
444
+     * Get the alternative label input HTML code.
445
+     *
446
+     * @param string $value The input value.
447
+     *
448
+     * @return string The input HTML code.
449
+     * @since 3.2.0
450
+     *
451
+     */
452
+    private function get_alternative_label_input( $value = '' ) {
453
+
454
+        return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
455
+    }
456
+
457
+    /**
458
+     * Get the number of entity posts published in this blog.
459
+     *
460
+     * @return int The number of published entity posts.
461
+     * @since 3.6.0
462
+     *
463
+     */
464
+    public function count() {
465
+        global $wpdb;
466
+
467
+        // Try to get the count from the transient.
468
+        $count = get_transient( '_wl_entity_service__count' );
469
+        if ( false !== $count ) {
470
+            return $count;
471
+        }
472
+
473
+        // Query the count.
474
+        $count = $wpdb->get_var( $wpdb->prepare(
475
+            "SELECT COUNT( DISTINCT( tr.object_id ) )"
476
+            . " FROM {$wpdb->term_relationships} tr"
477
+            . " INNER JOIN {$wpdb->term_taxonomy} tt"
478
+            . "  ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id"
479
+            . " INNER JOIN {$wpdb->terms} t"
480
+            . "  ON t.term_id = tt.term_id AND t.name != %s",
481
+            Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
482
+            'article'
483
+        ) );
484
+
485
+        // Store the count in cache.
486
+        set_transient( '_wl_entity_service__count', $count, 900 );
487
+
488
+        return $count;
489
+    }
490
+
491
+    /**
492
+     * Add the entity filtering criterias to the arguments for a `get_posts`
493
+     * call.
494
+     *
495
+     * @param array $args The arguments for a `get_posts` call.
496
+     *
497
+     * @return array The arguments for a `get_posts` call.
498
+     * @since 3.15.0
499
+     *
500
+     */
501
+    public static function add_criterias( $args ) {
502
+
503
+        // Build an optimal tax-query.
504
+        $tax_query = array(
505
+            'relation' => 'AND',
506
+            array(
507
+                'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
508
+                'operator' => 'EXISTS',
509
+            ),
510
+            array(
511
+                'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
512
+                'field'    => 'slug',
513
+                'terms'    => 'article',
514
+                'operator' => 'NOT IN',
515
+            ),
516
+        );
517
+
518
+        return $args + array(
519
+                'post_type' => Wordlift_Entity_Service::valid_entity_post_types(),
520
+                /*
521 521
 				 * Ensure compatibility with Polylang.
522 522
 				 *
523 523
 				 * @see https://github.com/insideout10/wordlift-plugin/issues/855.
@@ -525,102 +525,102 @@  discard block
 block discarded – undo
525 525
 				 *
526 526
 				 * @since 3.19.5
527 527
 				 */
528
-				'lang'      => '',
529
-				'tax_query' => $tax_query,
530
-			);
531
-	}
532
-
533
-	/**
534
-	 * Create a new entity.
535
-	 *
536
-	 * @param string $name The entity name.
537
-	 * @param string $type_uri The entity's type URI.
538
-	 * @param null $logo The entity logo id (or NULL if none).
539
-	 * @param string $status The post status, by default 'publish'.
540
-	 *
541
-	 * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
542
-	 * @since 3.9.0
543
-	 *
544
-	 */
545
-	public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
546
-
547
-		// Create an entity for the publisher.
548
-		$post_id = @wp_insert_post( array(
549
-			'post_type'    => self::TYPE_NAME,
550
-			'post_title'   => $name,
551
-			'post_status'  => $status,
552
-			'post_content' => '',
553
-		) );
554
-
555
-		// Return the error if any.
556
-		if ( is_wp_error( $post_id ) ) {
557
-			return $post_id;
558
-		}
559
-
560
-		// Set the entity logo.
561
-		if ( $logo && is_numeric( $logo ) ) {
562
-			set_post_thumbnail( $post_id, $logo );
563
-		}
564
-
565
-		// Set the entity type.
566
-		Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
567
-
568
-		return $post_id;
569
-	}
570
-
571
-	/**
572
-	 * Get the entities related to the one with the specified id. By default only
573
-	 * published entities will be returned.
574
-	 *
575
-	 * @param int $id The post id.
576
-	 * @param string $post_status The target post status (default = publish).
577
-	 *
578
-	 * @return array An array of post ids.
579
-	 * @since 3.10.0
580
-	 *
581
-	 */
582
-	public function get_related_entities( $id, $post_status = 'publish' ) {
583
-
584
-		return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
585
-	}
586
-
587
-	/**
588
-	 * Get the list of entities.
589
-	 *
590
-	 * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
591
-	 *
592
-	 * @return array An array of entity posts.
593
-	 * @since 3.12.2
594
-	 *
595
-	 */
596
-	public function get( $params = array() ) {
597
-
598
-		// Set the defaults.
599
-		$defaults = array( 'post_type' => 'entity' );
600
-
601
-		// Merge the defaults with the provided parameters.
602
-		$args = wp_parse_args( $params, $defaults );
603
-
604
-		// Call the `get_posts` function.
605
-		return get_posts( $args );
606
-	}
607
-
608
-	/**
609
-	 * The list of post type names which can be used for entities
610
-	 *
611
-	 * Criteria is that the post type is public. The list of valid post types
612
-	 * can be overridden with a filter.
613
-	 *
614
-	 * @return array Array containing the names of the valid post types.
615
-	 * @since 3.15.0
616
-	 *
617
-	 */
618
-	static function valid_entity_post_types() {
619
-
620
-		// Ignore builtins in the call to avoid getting attachments.
621
-		$post_types = array( 'post', 'page', self::TYPE_NAME, 'product' );
622
-
623
-		return apply_filters( 'wl_valid_entity_post_types', $post_types );
624
-	}
528
+                'lang'      => '',
529
+                'tax_query' => $tax_query,
530
+            );
531
+    }
532
+
533
+    /**
534
+     * Create a new entity.
535
+     *
536
+     * @param string $name The entity name.
537
+     * @param string $type_uri The entity's type URI.
538
+     * @param null $logo The entity logo id (or NULL if none).
539
+     * @param string $status The post status, by default 'publish'.
540
+     *
541
+     * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails.
542
+     * @since 3.9.0
543
+     *
544
+     */
545
+    public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
546
+
547
+        // Create an entity for the publisher.
548
+        $post_id = @wp_insert_post( array(
549
+            'post_type'    => self::TYPE_NAME,
550
+            'post_title'   => $name,
551
+            'post_status'  => $status,
552
+            'post_content' => '',
553
+        ) );
554
+
555
+        // Return the error if any.
556
+        if ( is_wp_error( $post_id ) ) {
557
+            return $post_id;
558
+        }
559
+
560
+        // Set the entity logo.
561
+        if ( $logo && is_numeric( $logo ) ) {
562
+            set_post_thumbnail( $post_id, $logo );
563
+        }
564
+
565
+        // Set the entity type.
566
+        Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
567
+
568
+        return $post_id;
569
+    }
570
+
571
+    /**
572
+     * Get the entities related to the one with the specified id. By default only
573
+     * published entities will be returned.
574
+     *
575
+     * @param int $id The post id.
576
+     * @param string $post_status The target post status (default = publish).
577
+     *
578
+     * @return array An array of post ids.
579
+     * @since 3.10.0
580
+     *
581
+     */
582
+    public function get_related_entities( $id, $post_status = 'publish' ) {
583
+
584
+        return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
585
+    }
586
+
587
+    /**
588
+     * Get the list of entities.
589
+     *
590
+     * @param array $params Custom parameters for WordPress' own {@link get_posts} function.
591
+     *
592
+     * @return array An array of entity posts.
593
+     * @since 3.12.2
594
+     *
595
+     */
596
+    public function get( $params = array() ) {
597
+
598
+        // Set the defaults.
599
+        $defaults = array( 'post_type' => 'entity' );
600
+
601
+        // Merge the defaults with the provided parameters.
602
+        $args = wp_parse_args( $params, $defaults );
603
+
604
+        // Call the `get_posts` function.
605
+        return get_posts( $args );
606
+    }
607
+
608
+    /**
609
+     * The list of post type names which can be used for entities
610
+     *
611
+     * Criteria is that the post type is public. The list of valid post types
612
+     * can be overridden with a filter.
613
+     *
614
+     * @return array Array containing the names of the valid post types.
615
+     * @since 3.15.0
616
+     *
617
+     */
618
+    static function valid_entity_post_types() {
619
+
620
+        // Ignore builtins in the call to avoid getting attachments.
621
+        $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' );
622
+
623
+        return apply_filters( 'wl_valid_entity_post_types', $post_types );
624
+    }
625 625
 
626 626
 }
Please login to merge, or discard this patch.
Spacing   +85 added lines, -85 removed lines patch added patch discarded remove patch
@@ -97,9 +97,9 @@  discard block
 block discarded – undo
97 97
 	 * @since 3.2.0
98 98
 	 *
99 99
 	 */
100
-	public function __construct( $ui_service, $relation_service, $entity_uri_service ) {
100
+	public function __construct($ui_service, $relation_service, $entity_uri_service) {
101 101
 
102
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
102
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service');
103 103
 
104 104
 		$this->ui_service         = $ui_service;
105 105
 		$this->relation_service   = $relation_service;
@@ -130,17 +130,17 @@  discard block
 block discarded – undo
130 130
 	 * @since 3.1.0
131 131
 	 *
132 132
 	 */
133
-	public function is_entity( $post_id ) {
133
+	public function is_entity($post_id) {
134 134
 
135
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
135
+		$terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
136 136
 
137
-		if ( is_wp_error( $terms ) ) {
138
-			$this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() );
137
+		if (is_wp_error($terms)) {
138
+			$this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message());
139 139
 
140 140
 			return false;
141 141
 		}
142 142
 
143
-		if ( empty( $terms ) ) {
143
+		if (empty($terms)) {
144 144
 			return false;
145 145
 		}
146 146
 
@@ -151,8 +151,8 @@  discard block
 block discarded – undo
151 151
 		 *
152 152
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
153 153
 		 */
154
-		foreach ( $terms as $term ) {
155
-			if ( 'article' !== $term->slug ) {
154
+		foreach ($terms as $term) {
155
+			if ('article' !== $term->slug) {
156 156
 				return true;
157 157
 			}
158 158
 		}
@@ -172,19 +172,19 @@  discard block
 block discarded – undo
172 172
 	 * @since 3.5.0
173 173
 	 *
174 174
 	 */
175
-	public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) {
175
+	public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) {
176 176
 
177
-		if ( false === $this->is_entity( $post_id ) ) {
177
+		if (false === $this->is_entity($post_id)) {
178 178
 			return $default;
179 179
 		}
180 180
 
181 181
 		// Retrieve the entity type
182
-		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
183
-		$entity_type     = str_replace( 'wl-', '', $entity_type_arr['css_class'] );
182
+		$entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id);
183
+		$entity_type     = str_replace('wl-', '', $entity_type_arr['css_class']);
184 184
 		// Retrieve classification boxes configuration
185
-		$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
186
-		foreach ( $classification_boxes as $cb ) {
187
-			if ( in_array( $entity_type, $cb['registeredTypes'] ) ) {
185
+		$classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES);
186
+		foreach ($classification_boxes as $cb) {
187
+			if (in_array($entity_type, $cb['registeredTypes'])) {
188 188
 				return $cb['id'];
189 189
 			}
190 190
 		}
@@ -199,13 +199,13 @@  discard block
 block discarded – undo
199 199
 	 *
200 200
 	 * @return bool|null Null if it's not an entity, otherwise true if it's used.
201 201
 	 */
202
-	public function is_used( $post_id ) {
202
+	public function is_used($post_id) {
203 203
 
204
-		if ( false === $this->is_entity( $post_id ) ) {
204
+		if (false === $this->is_entity($post_id)) {
205 205
 			return null;
206 206
 		}
207 207
 		// Retrieve the post
208
-		$entity = get_post( $post_id );
208
+		$entity = get_post($post_id);
209 209
 
210 210
 		global $wpdb;
211 211
 		// Retrieve Wordlift relation instances table name
@@ -218,9 +218,9 @@  discard block
 block discarded – undo
218 218
 		);
219 219
 
220 220
 		// Perform the query
221
-		$relation_instances = (int) $wpdb->get_var( $stmt );
221
+		$relation_instances = (int) $wpdb->get_var($stmt);
222 222
 		// If there is at least one relation instance for the current entity, then it's used
223
-		if ( 0 < $relation_instances ) {
223
+		if (0 < $relation_instances) {
224 224
 			return true;
225 225
 		}
226 226
 
@@ -228,13 +228,13 @@  discard block
 block discarded – undo
228 228
 		$stmt = $wpdb->prepare(
229 229
 			"SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s",
230 230
 			$entity->ID,
231
-			wl_get_entity_uri( $entity->ID )
231
+			wl_get_entity_uri($entity->ID)
232 232
 		);
233 233
 		// Perform the query
234
-		$meta_instances = (int) $wpdb->get_var( $stmt );
234
+		$meta_instances = (int) $wpdb->get_var($stmt);
235 235
 
236 236
 		// If there is at least one meta that refers the current entity uri, then current entity is used
237
-		if ( 0 < $meta_instances ) {
237
+		if (0 < $meta_instances) {
238 238
 			return true;
239 239
 		}
240 240
 
@@ -254,9 +254,9 @@  discard block
 block discarded – undo
254 254
 	 * @since      3.2.0
255 255
 	 *
256 256
 	 */
257
-	public function get_entity_post_by_uri( $uri ) {
257
+	public function get_entity_post_by_uri($uri) {
258 258
 
259
-		return $this->entity_uri_service->get_entity( $uri );
259
+		return $this->entity_uri_service->get_entity($uri);
260 260
 	}
261 261
 
262 262
 	/**
@@ -271,10 +271,10 @@  discard block
 block discarded – undo
271 271
 	 * @param WP_Post $post Post object.
272 272
 	 * @param bool $update Whether this is an existing post being updated or not.
273 273
 	 */
274
-	public function save_post( $post_id, $post, $update ) {
274
+	public function save_post($post_id, $post, $update) {
275 275
 
276 276
 		// Avoid doing anything if post is autosave or a revision.
277
-		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
277
+		if (wp_is_post_autosave($post) || wp_is_post_revision($post)) {
278 278
 			return;
279 279
 		}
280 280
 
@@ -284,16 +284,16 @@  discard block
 block discarded – undo
284 284
 		// the $post_id in the save_post call matches the post id set in the request.
285 285
 		//
286 286
 		// If this is not the current post being saved or if it's not an entity, return.
287
-		if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) {
287
+		if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) {
288 288
 			return;
289 289
 		}
290 290
 
291 291
 		// Get the alt labels from the request (or empty array).
292
-		$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array();
292
+		$alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array();
293 293
 
294
-		if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) {
294
+		if (( ! empty($_POST['content']) && ! empty($_POST['post_content'])) || isset($_REQUEST['wl_alternative_label'])) {
295 295
 			// This is via classic editor, so set the alternative labels.
296
-			$this->set_alternative_labels( $post_id, $alt_labels );
296
+			$this->set_alternative_labels($post_id, $alt_labels);
297 297
 		}
298 298
 
299 299
 
@@ -308,28 +308,28 @@  discard block
 block discarded – undo
308 308
 	 * @since 3.2.0
309 309
 	 *
310 310
 	 */
311
-	public function set_alternative_labels( $post_id, $alt_labels ) {
311
+	public function set_alternative_labels($post_id, $alt_labels) {
312 312
 
313 313
 		// Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error
314 314
 		// instead of post id.
315
-		if ( ! is_numeric( $post_id ) ) {
315
+		if ( ! is_numeric($post_id)) {
316 316
 			return;
317 317
 		}
318 318
 
319 319
 		// Force $alt_labels to be an array
320
-		if ( ! is_array( $alt_labels ) ) {
321
-			$alt_labels = array( $alt_labels );
320
+		if ( ! is_array($alt_labels)) {
321
+			$alt_labels = array($alt_labels);
322 322
 		}
323 323
 
324
-		$this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" );
324
+		$this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]");
325 325
 
326 326
 		// Delete all the existing alternate labels.
327
-		delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
327
+		delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY);
328 328
 
329 329
 		// Set the alternative labels.
330
-		foreach ( $alt_labels as $alt_label ) {
331
-			if ( ! empty( $alt_label ) ) {
332
-				add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
330
+		foreach ($alt_labels as $alt_label) {
331
+			if ( ! empty($alt_label)) {
332
+				add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label);
333 333
 			}
334 334
 		}
335 335
 
@@ -344,9 +344,9 @@  discard block
 block discarded – undo
344 344
 	 * @since 3.2.0
345 345
 	 *
346 346
 	 */
347
-	public function get_alternative_labels( $post_id ) {
347
+	public function get_alternative_labels($post_id) {
348 348
 
349
-		return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY );
349
+		return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY);
350 350
 	}
351 351
 
352 352
 	/**
@@ -358,9 +358,9 @@  discard block
 block discarded – undo
358 358
 	 * @since 3.12.0
359 359
 	 *
360 360
 	 */
361
-	public function get_labels( $post_id ) {
361
+	public function get_labels($post_id) {
362 362
 
363
-		return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) );
363
+		return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id));
364 364
 	}
365 365
 
366 366
 	/**
@@ -371,25 +371,25 @@  discard block
 block discarded – undo
371 371
 	 * @since 3.2.0
372 372
 	 *
373 373
 	 */
374
-	public function edit_form_before_permalink( $post ) {
374
+	public function edit_form_before_permalink($post) {
375 375
 
376 376
 		// If it's not an entity, return.
377
-		if ( ! $this->is_entity( $post->ID ) ) {
377
+		if ( ! $this->is_entity($post->ID)) {
378 378
 			return;
379 379
 		}
380 380
 
381 381
 		// Print the input template.
382
-		$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() );
382
+		$this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input());
383 383
 
384 384
 		// Print all the currently set alternative labels.
385
-		foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) {
385
+		foreach ($this->get_alternative_labels($post->ID) as $alt_label) {
386 386
 
387
-			echo $this->get_alternative_label_input( $alt_label );
387
+			echo $this->get_alternative_label_input($alt_label);
388 388
 
389 389
 		};
390 390
 
391 391
 		// Print the button.
392
-		$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) );
392
+		$this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift'));
393 393
 
394 394
 	}
395 395
 
@@ -402,38 +402,38 @@  discard block
 block discarded – undo
402 402
 	 * @since 3.6.0
403 403
 	 *
404 404
 	 */
405
-	public function get_uri( $post_id ) {
405
+	public function get_uri($post_id) {
406 406
 
407
-		$log = Wordlift_Log_Service::get_logger( get_class() );
407
+		$log = Wordlift_Log_Service::get_logger(get_class());
408 408
 
409 409
 		// If a null is given, nothing to do
410
-		if ( is_null( $post_id ) ) {
410
+		if (is_null($post_id)) {
411 411
 			return null;
412 412
 		}
413 413
 
414 414
 		$dataset_uri = wl_configuration_get_redlink_dataset_uri();
415 415
 
416
-		if ( empty( $dataset_uri ) ) {
416
+		if (empty($dataset_uri)) {
417 417
 			// Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri()
418
-			$log->debug( 'Continuing, dataset uri not configured...' );
418
+			$log->debug('Continuing, dataset uri not configured...');
419 419
 		}
420 420
 
421
-		$uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true );
421
+		$uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true);
422 422
 
423 423
 		/*
424 424
 		 * Consider the URI invalid if it doesn't start with the dataset URI.
425 425
 		 *
426 426
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/996
427 427
 		 */
428
-		if ( null === $dataset_uri || 0 !== strpos( $uri, $dataset_uri ) ) {
428
+		if (null === $dataset_uri || 0 !== strpos($uri, $dataset_uri)) {
429 429
 			$uri = null;
430 430
 		}
431 431
 
432 432
 		// Set the URI if it isn't set yet.
433
-		$post_status = get_post_status( $post_id );
434
-		if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) {
435
-			$uri = wl_build_entity_uri( $post_id );
436
-			wl_set_entity_uri( $post_id, $uri );
433
+		$post_status = get_post_status($post_id);
434
+		if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) {
435
+			$uri = wl_build_entity_uri($post_id);
436
+			wl_set_entity_uri($post_id, $uri);
437 437
 		}
438 438
 
439 439
 		return $uri;
@@ -449,9 +449,9 @@  discard block
 block discarded – undo
449 449
 	 * @since 3.2.0
450 450
 	 *
451 451
 	 */
452
-	private function get_alternative_label_input( $value = '' ) {
452
+	private function get_alternative_label_input($value = '') {
453 453
 
454
-		return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) );
454
+		return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift'));
455 455
 	}
456 456
 
457 457
 	/**
@@ -465,13 +465,13 @@  discard block
 block discarded – undo
465 465
 		global $wpdb;
466 466
 
467 467
 		// Try to get the count from the transient.
468
-		$count = get_transient( '_wl_entity_service__count' );
469
-		if ( false !== $count ) {
468
+		$count = get_transient('_wl_entity_service__count');
469
+		if (false !== $count) {
470 470
 			return $count;
471 471
 		}
472 472
 
473 473
 		// Query the count.
474
-		$count = $wpdb->get_var( $wpdb->prepare(
474
+		$count = $wpdb->get_var($wpdb->prepare(
475 475
 			"SELECT COUNT( DISTINCT( tr.object_id ) )"
476 476
 			. " FROM {$wpdb->term_relationships} tr"
477 477
 			. " INNER JOIN {$wpdb->term_taxonomy} tt"
@@ -480,10 +480,10 @@  discard block
 block discarded – undo
480 480
 			. "  ON t.term_id = tt.term_id AND t.name != %s",
481 481
 			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
482 482
 			'article'
483
-		) );
483
+		));
484 484
 
485 485
 		// Store the count in cache.
486
-		set_transient( '_wl_entity_service__count', $count, 900 );
486
+		set_transient('_wl_entity_service__count', $count, 900);
487 487
 
488 488
 		return $count;
489 489
 	}
@@ -498,7 +498,7 @@  discard block
 block discarded – undo
498 498
 	 * @since 3.15.0
499 499
 	 *
500 500
 	 */
501
-	public static function add_criterias( $args ) {
501
+	public static function add_criterias($args) {
502 502
 
503 503
 		// Build an optimal tax-query.
504 504
 		$tax_query = array(
@@ -542,28 +542,28 @@  discard block
 block discarded – undo
542 542
 	 * @since 3.9.0
543 543
 	 *
544 544
 	 */
545
-	public function create( $name, $type_uri, $logo = null, $status = 'publish' ) {
545
+	public function create($name, $type_uri, $logo = null, $status = 'publish') {
546 546
 
547 547
 		// Create an entity for the publisher.
548
-		$post_id = @wp_insert_post( array(
548
+		$post_id = @wp_insert_post(array(
549 549
 			'post_type'    => self::TYPE_NAME,
550 550
 			'post_title'   => $name,
551 551
 			'post_status'  => $status,
552 552
 			'post_content' => '',
553
-		) );
553
+		));
554 554
 
555 555
 		// Return the error if any.
556
-		if ( is_wp_error( $post_id ) ) {
556
+		if (is_wp_error($post_id)) {
557 557
 			return $post_id;
558 558
 		}
559 559
 
560 560
 		// Set the entity logo.
561
-		if ( $logo && is_numeric( $logo ) ) {
562
-			set_post_thumbnail( $post_id, $logo );
561
+		if ($logo && is_numeric($logo)) {
562
+			set_post_thumbnail($post_id, $logo);
563 563
 		}
564 564
 
565 565
 		// Set the entity type.
566
-		Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri );
566
+		Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri);
567 567
 
568 568
 		return $post_id;
569 569
 	}
@@ -579,9 +579,9 @@  discard block
 block discarded – undo
579 579
 	 * @since 3.10.0
580 580
 	 *
581 581
 	 */
582
-	public function get_related_entities( $id, $post_status = 'publish' ) {
582
+	public function get_related_entities($id, $post_status = 'publish') {
583 583
 
584
-		return $this->relation_service->get_objects( $id, 'ids', null, $post_status );
584
+		return $this->relation_service->get_objects($id, 'ids', null, $post_status);
585 585
 	}
586 586
 
587 587
 	/**
@@ -593,16 +593,16 @@  discard block
 block discarded – undo
593 593
 	 * @since 3.12.2
594 594
 	 *
595 595
 	 */
596
-	public function get( $params = array() ) {
596
+	public function get($params = array()) {
597 597
 
598 598
 		// Set the defaults.
599
-		$defaults = array( 'post_type' => 'entity' );
599
+		$defaults = array('post_type' => 'entity');
600 600
 
601 601
 		// Merge the defaults with the provided parameters.
602
-		$args = wp_parse_args( $params, $defaults );
602
+		$args = wp_parse_args($params, $defaults);
603 603
 
604 604
 		// Call the `get_posts` function.
605
-		return get_posts( $args );
605
+		return get_posts($args);
606 606
 	}
607 607
 
608 608
 	/**
@@ -618,9 +618,9 @@  discard block
 block discarded – undo
618 618
 	static function valid_entity_post_types() {
619 619
 
620 620
 		// Ignore builtins in the call to avoid getting attachments.
621
-		$post_types = array( 'post', 'page', self::TYPE_NAME, 'product' );
621
+		$post_types = array('post', 'page', self::TYPE_NAME, 'product');
622 622
 
623
-		return apply_filters( 'wl_valid_entity_post_types', $post_types );
623
+		return apply_filters('wl_valid_entity_post_types', $post_types);
624 624
 	}
625 625
 
626 626
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-configuration-service.php 2 patches
Indentation   +697 added lines, -697 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,705 +23,705 @@  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
-	 * WordLift's key option name.
42
-	 *
43
-	 * @since 3.9.0
44
-	 */
45
-	const KEY = 'key';
46
-
47
-	/**
48
-	 * WordLift's configured language option name.
49
-	 *
50
-	 * @since 3.9.0
51
-	 */
52
-	const LANGUAGE = 'site_language';
53
-
54
-	/**
55
-	 * WordLift's configured country code.
56
-	 *
57
-	 * @since 3.18.0
58
-	 */
59
-	const COUNTRY_CODE = 'country_code';
60
-
61
-	/**
62
-	 * The publisher entity post ID option name.
63
-	 *
64
-	 * @since 3.9.0
65
-	 */
66
-	const PUBLISHER_ID = 'publisher_id';
67
-
68
-	/**
69
-	 * The dataset URI option name
70
-	 *
71
-	 * @since 3.10.0
72
-	 */
73
-	const DATASET_URI = 'redlink_dataset_uri';
74
-
75
-	/**
76
-	 * The link by default option name.
77
-	 *
78
-	 * @since 3.11.0
79
-	 */
80
-	const LINK_BY_DEFAULT = 'link_by_default';
81
-
82
-	/**
83
-	 * The analytics enable option.
84
-	 *
85
-	 * @since 3.21.0
86
-	 */
87
-	const ANALYTICS_ENABLE = 'analytics_enable';
88
-
89
-	/**
90
-	 * The analytics entity uri dimension option.
91
-	 *
92
-	 * @since 3.21.0
93
-	 */
94
-	const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
95
-
96
-	/**
97
-	 * The analytics entity type dimension option.
98
-	 *
99
-	 * @since 3.21.0
100
-	 */
101
-	const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
102
-
103
-	/**
104
-	 * The user preferences about sharing data option.
105
-	 *
106
-	 * @since 3.19.0
107
-	 */
108
-	const SEND_DIAGNOSTIC = 'send_diagnostic';
109
-
110
-	/**
111
-	 * The package type configuration key.
112
-	 *
113
-	 * @since 3.20.0
114
-	 */
115
-	const PACKAGE_TYPE = 'package_type';
116
-
117
-	/**
118
-	 * The {@link Wordlift_Log_Service} instance.
119
-	 *
120
-	 * @since 3.16.0
121
-	 *
122
-	 * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
123
-	 */
124
-	private $log;
125
-
126
-	/**
127
-	 * The Wordlift_Configuration_Service's singleton instance.
128
-	 *
129
-	 * @since  3.6.0
130
-	 *
131
-	 * @access private
132
-	 * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
133
-	 */
134
-	private static $instance;
135
-
136
-	/**
137
-	 * Create a Wordlift_Configuration_Service's instance.
138
-	 *
139
-	 * @since 3.6.0
140
-	 */
141
-	public function __construct() {
142
-
143
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
144
-
145
-		self::$instance = $this;
146
-
147
-	}
148
-
149
-	/**
150
-	 * Get the singleton instance.
151
-	 *
152
-	 * @return \Wordlift_Configuration_Service
153
-	 * @since 3.6.0
154
-	 *
155
-	 */
156
-	public static function get_instance() {
157
-
158
-		return self::$instance;
159
-	}
160
-
161
-	/**
162
-	 * Get a configuration given the option name and a key. The option value is
163
-	 * expected to be an array.
164
-	 *
165
-	 * @param string $option The option name.
166
-	 * @param string $key A key in the option value array.
167
-	 * @param string $default The default value in case the key is not found (by default an empty string).
168
-	 *
169
-	 * @return mixed The configuration value or the default value if not found.
170
-	 * @since 3.6.0
171
-	 *
172
-	 */
173
-	private function get( $option, $key, $default = '' ) {
174
-
175
-		$options = get_option( $option, array() );
176
-
177
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
178
-	}
179
-
180
-	/**
181
-	 * Set a configuration parameter.
182
-	 *
183
-	 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
184
-	 * @param string $key The value key.
185
-	 * @param mixed $value The value.
186
-	 *
187
-	 * @since 3.9.0
188
-	 *
189
-	 */
190
-	private function set( $option, $key, $value ) {
191
-
192
-		$values         = get_option( $option );
193
-		$values         = isset( $values ) ? $values : array();
194
-		$values[ $key ] = $value;
195
-		update_option( $option, $values );
196
-
197
-	}
198
-
199
-	/**
200
-	 * Get the entity base path, by default 'entity'.
201
-	 *
202
-	 * @return string The entity base path.
203
-	 * @since 3.6.0
204
-	 *
205
-	 */
206
-	public function get_entity_base_path() {
207
-
208
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
209
-	}
210
-
211
-	/**
212
-	 * Get the entity base path.
213
-	 *
214
-	 * @param string $value The entity base path.
215
-	 *
216
-	 * @since 3.9.0
217
-	 *
218
-	 */
219
-	public function set_entity_base_path( $value ) {
220
-
221
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
222
-
223
-	}
224
-
225
-	/**
226
-	 * Whether the installation skip wizard should be skipped.
227
-	 *
228
-	 * @return bool True if it should be skipped otherwise false.
229
-	 * @since 3.9.0
230
-	 *
231
-	 */
232
-	public function is_skip_wizard() {
233
-
234
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
235
-	}
236
-
237
-	/**
238
-	 * Set the skip wizard parameter.
239
-	 *
240
-	 * @param bool $value True to skip the wizard. We expect a boolean value.
241
-	 *
242
-	 * @since 3.9.0
243
-	 *
244
-	 */
245
-	public function set_skip_wizard( $value ) {
246
-
247
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
248
-
249
-	}
250
-
251
-	/**
252
-	 * Get WordLift's key.
253
-	 *
254
-	 * @return string WordLift's key or an empty string if not set.
255
-	 * @since 3.9.0
256
-	 *
257
-	 */
258
-	public function get_key() {
259
-
260
-		return $this->get( 'wl_general_settings', self::KEY, '' );
261
-	}
262
-
263
-	/**
264
-	 * Set WordLift's key.
265
-	 *
266
-	 * @param string $value WordLift's key.
267
-	 *
268
-	 * @since 3.9.0
269
-	 *
270
-	 */
271
-	public function set_key( $value ) {
272
-
273
-		$this->set( 'wl_general_settings', self::KEY, $value );
274
-	}
275
-
276
-	/**
277
-	 * Get WordLift's configured language, by default 'en'.
278
-	 *
279
-	 * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
280
-	 *
281
-	 * @return string WordLift's configured language code ('en' by default).
282
-	 * @since 3.9.0
283
-	 *
284
-	 */
285
-	public function get_language_code() {
286
-
287
-		return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' );
288
-	}
289
-
290
-	/**
291
-	 * Set WordLift's language code, used when storing strings to the Linked Data dataset.
292
-	 *
293
-	 * @param string $value WordLift's language code.
294
-	 *
295
-	 * @since 3.9.0
296
-	 *
297
-	 */
298
-	public function set_language_code( $value ) {
299
-
300
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
301
-
302
-	}
303
-
304
-	/**
305
-	 * Set the user preferences about sharing diagnostic with us.
306
-	 *
307
-	 * @param string $value The user preferences(yes/no).
308
-	 *
309
-	 * @since 3.19.0
310
-	 *
311
-	 */
312
-	public function set_diagnostic_preferences( $value ) {
313
-
314
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
315
-
316
-	}
317
-
318
-	/**
319
-	 * Get the user preferences about sharing diagnostic.
320
-	 *
321
-	 * @since 3.19.0
322
-	 */
323
-	public function get_diagnostic_preferences() {
324
-
325
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
326
-	}
327
-
328
-	/**
329
-	 * Get WordLift's configured country code, by default 'us'.
330
-	 *
331
-	 * @return string WordLift's configured country code ('us' by default).
332
-	 * @since 3.18.0
333
-	 *
334
-	 */
335
-	public function get_country_code() {
336
-
337
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
338
-	}
339
-
340
-	/**
341
-	 * Set WordLift's country code.
342
-	 *
343
-	 * @param string $value WordLift's country code.
344
-	 *
345
-	 * @since 3.18.0
346
-	 *
347
-	 */
348
-	public function set_country_code( $value ) {
349
-
350
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
351
-
352
-	}
353
-
354
-	/**
355
-	 * Get the publisher entity post id.
356
-	 *
357
-	 * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
358
-	 * Article markup.
359
-	 *
360
-	 * @return int|NULL The publisher entity post id or NULL if not set.
361
-	 * @since 3.9.0
362
-	 *
363
-	 */
364
-	public function get_publisher_id() {
365
-
366
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
367
-	}
368
-
369
-	/**
370
-	 * Set the publisher entity post id.
371
-	 *
372
-	 * @param int $value The publisher entity post id.
373
-	 *
374
-	 * @since 3.9.0
375
-	 *
376
-	 */
377
-	public function set_publisher_id( $value ) {
378
-
379
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
380
-
381
-	}
382
-
383
-	/**
384
-	 * Get the dataset URI.
385
-	 *
386
-	 * @return string The dataset URI or an empty string if not set.
387
-	 * @since 3.10.0
388
-	 * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
389
-	 *
390
-	 */
391
-	public function get_dataset_uri() {
392
-
393
-		if ( apply_filters( 'wl_features__enable__dataset', true ) ) {
394
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
395
-		} else {
396
-			return null;
397
-		}
398
-	}
399
-
400
-	/**
401
-	 * Set the dataset URI.
402
-	 *
403
-	 * @param string $value The dataset URI.
404
-	 *
405
-	 * @since 3.10.0
406
-	 *
407
-	 */
408
-	public function set_dataset_uri( $value ) {
409
-
410
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
411
-	}
412
-
413
-	/**
414
-	 * Get the package type.
415
-	 *
416
-	 * @return string The package type or an empty string if not set.
417
-	 * @since 3.20.0
418
-	 *
419
-	 */
420
-	public function get_package_type() {
421
-
422
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
423
-	}
424
-
425
-	/**
426
-	 * Set the package type.
427
-	 *
428
-	 * @param string $value The package type.
429
-	 *
430
-	 * @since 3.20.0
431
-	 *
432
-	 */
433
-	public function set_package_type( $value ) {
434
-
435
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
436
-	}
437
-
438
-	/**
439
-	 * Intercept the change of the WordLift key in order to set the dataset URI.
440
-	 *
441
-	 *
442
-	 * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
443
-	 *               store the configuration parameters such as country or language.
444
-	 * @since 3.11.0
445
-	 *
446
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/761
447
-	 *
448
-	 * @param array $old_value The old settings.
449
-	 * @param array $new_value The new settings.
450
-	 */
451
-	public function update_key( $old_value, $new_value ) {
452
-
453
-		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
454
-		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
455
-		$new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
456
-
457
-		// If the key hasn't changed, don't do anything.
458
-		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
459
-		//		if ( $old_key === $new_key ) {
460
-		//			return;
461
-		//		}
462
-
463
-		// If the key is empty, empty the dataset URI.
464
-		if ( '' === $new_key ) {
465
-			$this->set_dataset_uri( '' );
466
-		}
467
-
468
-		// make the request to the remote server.
469
-		$this->get_remote_dataset_uri( $new_key );
470
-
471
-	}
472
-
473
-	/**
474
-	 * Handle retrieving the dataset uri from the remote server.
475
-	 *
476
-	 * If a valid dataset uri is returned it is stored in the appropriate option,
477
-	 * otherwise the option is set to empty string.
478
-	 *
479
-	 * @param string $key The key to be used.
480
-	 *
481
-	 * @since 3.12.0
482
-	 *
483
-	 * @since 3.17.0 send the site URL and get the dataset URI.
484
-	 */
485
-	public function get_remote_dataset_uri( $key ) {
486
-
487
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
488
-
489
-		if ( empty( $key ) ) {
490
-			$this->log->warn( 'Key set to empty value.' );
491
-
492
-			$this->set_dataset_uri( '' );
493
-			$this->set_package_type( null );
494
-
495
-			return;
496
-		}
497
-
498
-		/**
499
-		 * Allow 3rd parties to change the site_url.
500
-		 *
501
-		 * @param string $site_url The site url.
502
-		 *
503
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/850
504
-		 *
505
-		 * @since 3.20.0
506
-		 *
507
-		 */
508
-		$home_url = defined( 'WP_HOME' ) ? WP_HOME : get_option( 'home' );
509
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
510
-
511
-		// Build the URL.
512
-		$url = '/accounts'
513
-		       . '?key=' . rawurlencode( $key )
514
-		       . '&url=' . rawurlencode( $site_url )
515
-		       . '&country=' . $this->get_country_code()
516
-		       . '&language=' . $this->get_language_code();
517
-
518
-		$api_service = Default_Api_Service::get_instance();
519
-		$response    = $api_service->request( 'PUT', $url )->get_response();
520
-
521
-		// The response is an error.
522
-		if ( is_wp_error( $response ) ) {
523
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
524
-
525
-			$this->set_dataset_uri( '' );
526
-			$this->set_package_type( null );
527
-
528
-			return;
529
-		}
530
-
531
-		// The response is not OK.
532
-		if ( 200 !== (int) $response['response']['code'] ) {
533
-			$base_url = $api_service->get_base_url();
534
-			$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
535
-
536
-			$this->set_dataset_uri( '' );
537
-			$this->set_package_type( null );
538
-
539
-			return;
540
-		}
541
-
542
-		/*
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
+     * WordLift's key option name.
42
+     *
43
+     * @since 3.9.0
44
+     */
45
+    const KEY = 'key';
46
+
47
+    /**
48
+     * WordLift's configured language option name.
49
+     *
50
+     * @since 3.9.0
51
+     */
52
+    const LANGUAGE = 'site_language';
53
+
54
+    /**
55
+     * WordLift's configured country code.
56
+     *
57
+     * @since 3.18.0
58
+     */
59
+    const COUNTRY_CODE = 'country_code';
60
+
61
+    /**
62
+     * The publisher entity post ID option name.
63
+     *
64
+     * @since 3.9.0
65
+     */
66
+    const PUBLISHER_ID = 'publisher_id';
67
+
68
+    /**
69
+     * The dataset URI option name
70
+     *
71
+     * @since 3.10.0
72
+     */
73
+    const DATASET_URI = 'redlink_dataset_uri';
74
+
75
+    /**
76
+     * The link by default option name.
77
+     *
78
+     * @since 3.11.0
79
+     */
80
+    const LINK_BY_DEFAULT = 'link_by_default';
81
+
82
+    /**
83
+     * The analytics enable option.
84
+     *
85
+     * @since 3.21.0
86
+     */
87
+    const ANALYTICS_ENABLE = 'analytics_enable';
88
+
89
+    /**
90
+     * The analytics entity uri dimension option.
91
+     *
92
+     * @since 3.21.0
93
+     */
94
+    const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
95
+
96
+    /**
97
+     * The analytics entity type dimension option.
98
+     *
99
+     * @since 3.21.0
100
+     */
101
+    const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
102
+
103
+    /**
104
+     * The user preferences about sharing data option.
105
+     *
106
+     * @since 3.19.0
107
+     */
108
+    const SEND_DIAGNOSTIC = 'send_diagnostic';
109
+
110
+    /**
111
+     * The package type configuration key.
112
+     *
113
+     * @since 3.20.0
114
+     */
115
+    const PACKAGE_TYPE = 'package_type';
116
+
117
+    /**
118
+     * The {@link Wordlift_Log_Service} instance.
119
+     *
120
+     * @since 3.16.0
121
+     *
122
+     * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
123
+     */
124
+    private $log;
125
+
126
+    /**
127
+     * The Wordlift_Configuration_Service's singleton instance.
128
+     *
129
+     * @since  3.6.0
130
+     *
131
+     * @access private
132
+     * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
133
+     */
134
+    private static $instance;
135
+
136
+    /**
137
+     * Create a Wordlift_Configuration_Service's instance.
138
+     *
139
+     * @since 3.6.0
140
+     */
141
+    public function __construct() {
142
+
143
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
144
+
145
+        self::$instance = $this;
146
+
147
+    }
148
+
149
+    /**
150
+     * Get the singleton instance.
151
+     *
152
+     * @return \Wordlift_Configuration_Service
153
+     * @since 3.6.0
154
+     *
155
+     */
156
+    public static function get_instance() {
157
+
158
+        return self::$instance;
159
+    }
160
+
161
+    /**
162
+     * Get a configuration given the option name and a key. The option value is
163
+     * expected to be an array.
164
+     *
165
+     * @param string $option The option name.
166
+     * @param string $key A key in the option value array.
167
+     * @param string $default The default value in case the key is not found (by default an empty string).
168
+     *
169
+     * @return mixed The configuration value or the default value if not found.
170
+     * @since 3.6.0
171
+     *
172
+     */
173
+    private function get( $option, $key, $default = '' ) {
174
+
175
+        $options = get_option( $option, array() );
176
+
177
+        return isset( $options[ $key ] ) ? $options[ $key ] : $default;
178
+    }
179
+
180
+    /**
181
+     * Set a configuration parameter.
182
+     *
183
+     * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
184
+     * @param string $key The value key.
185
+     * @param mixed $value The value.
186
+     *
187
+     * @since 3.9.0
188
+     *
189
+     */
190
+    private function set( $option, $key, $value ) {
191
+
192
+        $values         = get_option( $option );
193
+        $values         = isset( $values ) ? $values : array();
194
+        $values[ $key ] = $value;
195
+        update_option( $option, $values );
196
+
197
+    }
198
+
199
+    /**
200
+     * Get the entity base path, by default 'entity'.
201
+     *
202
+     * @return string The entity base path.
203
+     * @since 3.6.0
204
+     *
205
+     */
206
+    public function get_entity_base_path() {
207
+
208
+        return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
209
+    }
210
+
211
+    /**
212
+     * Get the entity base path.
213
+     *
214
+     * @param string $value The entity base path.
215
+     *
216
+     * @since 3.9.0
217
+     *
218
+     */
219
+    public function set_entity_base_path( $value ) {
220
+
221
+        $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
222
+
223
+    }
224
+
225
+    /**
226
+     * Whether the installation skip wizard should be skipped.
227
+     *
228
+     * @return bool True if it should be skipped otherwise false.
229
+     * @since 3.9.0
230
+     *
231
+     */
232
+    public function is_skip_wizard() {
233
+
234
+        return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
235
+    }
236
+
237
+    /**
238
+     * Set the skip wizard parameter.
239
+     *
240
+     * @param bool $value True to skip the wizard. We expect a boolean value.
241
+     *
242
+     * @since 3.9.0
243
+     *
244
+     */
245
+    public function set_skip_wizard( $value ) {
246
+
247
+        $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
248
+
249
+    }
250
+
251
+    /**
252
+     * Get WordLift's key.
253
+     *
254
+     * @return string WordLift's key or an empty string if not set.
255
+     * @since 3.9.0
256
+     *
257
+     */
258
+    public function get_key() {
259
+
260
+        return $this->get( 'wl_general_settings', self::KEY, '' );
261
+    }
262
+
263
+    /**
264
+     * Set WordLift's key.
265
+     *
266
+     * @param string $value WordLift's key.
267
+     *
268
+     * @since 3.9.0
269
+     *
270
+     */
271
+    public function set_key( $value ) {
272
+
273
+        $this->set( 'wl_general_settings', self::KEY, $value );
274
+    }
275
+
276
+    /**
277
+     * Get WordLift's configured language, by default 'en'.
278
+     *
279
+     * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
280
+     *
281
+     * @return string WordLift's configured language code ('en' by default).
282
+     * @since 3.9.0
283
+     *
284
+     */
285
+    public function get_language_code() {
286
+
287
+        return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' );
288
+    }
289
+
290
+    /**
291
+     * Set WordLift's language code, used when storing strings to the Linked Data dataset.
292
+     *
293
+     * @param string $value WordLift's language code.
294
+     *
295
+     * @since 3.9.0
296
+     *
297
+     */
298
+    public function set_language_code( $value ) {
299
+
300
+        $this->set( 'wl_general_settings', self::LANGUAGE, $value );
301
+
302
+    }
303
+
304
+    /**
305
+     * Set the user preferences about sharing diagnostic with us.
306
+     *
307
+     * @param string $value The user preferences(yes/no).
308
+     *
309
+     * @since 3.19.0
310
+     *
311
+     */
312
+    public function set_diagnostic_preferences( $value ) {
313
+
314
+        $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
315
+
316
+    }
317
+
318
+    /**
319
+     * Get the user preferences about sharing diagnostic.
320
+     *
321
+     * @since 3.19.0
322
+     */
323
+    public function get_diagnostic_preferences() {
324
+
325
+        return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
326
+    }
327
+
328
+    /**
329
+     * Get WordLift's configured country code, by default 'us'.
330
+     *
331
+     * @return string WordLift's configured country code ('us' by default).
332
+     * @since 3.18.0
333
+     *
334
+     */
335
+    public function get_country_code() {
336
+
337
+        return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
338
+    }
339
+
340
+    /**
341
+     * Set WordLift's country code.
342
+     *
343
+     * @param string $value WordLift's country code.
344
+     *
345
+     * @since 3.18.0
346
+     *
347
+     */
348
+    public function set_country_code( $value ) {
349
+
350
+        $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
351
+
352
+    }
353
+
354
+    /**
355
+     * Get the publisher entity post id.
356
+     *
357
+     * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
358
+     * Article markup.
359
+     *
360
+     * @return int|NULL The publisher entity post id or NULL if not set.
361
+     * @since 3.9.0
362
+     *
363
+     */
364
+    public function get_publisher_id() {
365
+
366
+        return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
367
+    }
368
+
369
+    /**
370
+     * Set the publisher entity post id.
371
+     *
372
+     * @param int $value The publisher entity post id.
373
+     *
374
+     * @since 3.9.0
375
+     *
376
+     */
377
+    public function set_publisher_id( $value ) {
378
+
379
+        $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
380
+
381
+    }
382
+
383
+    /**
384
+     * Get the dataset URI.
385
+     *
386
+     * @return string The dataset URI or an empty string if not set.
387
+     * @since 3.10.0
388
+     * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
389
+     *
390
+     */
391
+    public function get_dataset_uri() {
392
+
393
+        if ( apply_filters( 'wl_features__enable__dataset', true ) ) {
394
+            return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
395
+        } else {
396
+            return null;
397
+        }
398
+    }
399
+
400
+    /**
401
+     * Set the dataset URI.
402
+     *
403
+     * @param string $value The dataset URI.
404
+     *
405
+     * @since 3.10.0
406
+     *
407
+     */
408
+    public function set_dataset_uri( $value ) {
409
+
410
+        $this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
411
+    }
412
+
413
+    /**
414
+     * Get the package type.
415
+     *
416
+     * @return string The package type or an empty string if not set.
417
+     * @since 3.20.0
418
+     *
419
+     */
420
+    public function get_package_type() {
421
+
422
+        return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
423
+    }
424
+
425
+    /**
426
+     * Set the package type.
427
+     *
428
+     * @param string $value The package type.
429
+     *
430
+     * @since 3.20.0
431
+     *
432
+     */
433
+    public function set_package_type( $value ) {
434
+
435
+        $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
436
+    }
437
+
438
+    /**
439
+     * Intercept the change of the WordLift key in order to set the dataset URI.
440
+     *
441
+     *
442
+     * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
443
+     *               store the configuration parameters such as country or language.
444
+     * @since 3.11.0
445
+     *
446
+     * @see https://github.com/insideout10/wordlift-plugin/issues/761
447
+     *
448
+     * @param array $old_value The old settings.
449
+     * @param array $new_value The new settings.
450
+     */
451
+    public function update_key( $old_value, $new_value ) {
452
+
453
+        // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
454
+        // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
455
+        $new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
456
+
457
+        // If the key hasn't changed, don't do anything.
458
+        // WARN The 'update_option' hook is fired only if the new and old value are not equal.
459
+        //		if ( $old_key === $new_key ) {
460
+        //			return;
461
+        //		}
462
+
463
+        // If the key is empty, empty the dataset URI.
464
+        if ( '' === $new_key ) {
465
+            $this->set_dataset_uri( '' );
466
+        }
467
+
468
+        // make the request to the remote server.
469
+        $this->get_remote_dataset_uri( $new_key );
470
+
471
+    }
472
+
473
+    /**
474
+     * Handle retrieving the dataset uri from the remote server.
475
+     *
476
+     * If a valid dataset uri is returned it is stored in the appropriate option,
477
+     * otherwise the option is set to empty string.
478
+     *
479
+     * @param string $key The key to be used.
480
+     *
481
+     * @since 3.12.0
482
+     *
483
+     * @since 3.17.0 send the site URL and get the dataset URI.
484
+     */
485
+    public function get_remote_dataset_uri( $key ) {
486
+
487
+        $this->log->trace( 'Getting the remote dataset URI and package type...' );
488
+
489
+        if ( empty( $key ) ) {
490
+            $this->log->warn( 'Key set to empty value.' );
491
+
492
+            $this->set_dataset_uri( '' );
493
+            $this->set_package_type( null );
494
+
495
+            return;
496
+        }
497
+
498
+        /**
499
+         * Allow 3rd parties to change the site_url.
500
+         *
501
+         * @param string $site_url The site url.
502
+         *
503
+         * @see https://github.com/insideout10/wordlift-plugin/issues/850
504
+         *
505
+         * @since 3.20.0
506
+         *
507
+         */
508
+        $home_url = defined( 'WP_HOME' ) ? WP_HOME : get_option( 'home' );
509
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
510
+
511
+        // Build the URL.
512
+        $url = '/accounts'
513
+                . '?key=' . rawurlencode( $key )
514
+                . '&url=' . rawurlencode( $site_url )
515
+                . '&country=' . $this->get_country_code()
516
+                . '&language=' . $this->get_language_code();
517
+
518
+        $api_service = Default_Api_Service::get_instance();
519
+        $response    = $api_service->request( 'PUT', $url )->get_response();
520
+
521
+        // The response is an error.
522
+        if ( is_wp_error( $response ) ) {
523
+            $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
524
+
525
+            $this->set_dataset_uri( '' );
526
+            $this->set_package_type( null );
527
+
528
+            return;
529
+        }
530
+
531
+        // The response is not OK.
532
+        if ( 200 !== (int) $response['response']['code'] ) {
533
+            $base_url = $api_service->get_base_url();
534
+            $this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
535
+
536
+            $this->set_dataset_uri( '' );
537
+            $this->set_package_type( null );
538
+
539
+            return;
540
+        }
541
+
542
+        /*
543 543
 		 * We also store the package type.
544 544
 		 *
545 545
 		 * @since 3.20.0
546 546
 		 */
547
-		$json         = json_decode( $response['body'] );
548
-		$dataset_uri  = $json->datasetURI;
549
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
550
-
551
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
552
-
553
-		$this->set_dataset_uri( $dataset_uri );
554
-		$this->set_package_type( $package_type );
555
-
556
-	}
557
-
558
-	/**
559
-	 * Handle the edge case where a user submits the same key again
560
-	 * when he does not have the dataset uri to regain it.
561
-	 *
562
-	 * This can not be handled in the normal option update hook because
563
-	 * it is not being triggered when the save value equals to the one already
564
-	 * in the DB.
565
-	 *
566
-	 * @param mixed $value The new, unserialized option value.
567
-	 * @param mixed $old_value The old option value.
568
-	 *
569
-	 * @return mixed The same value in the $value parameter
570
-	 * @since 3.12.0
571
-	 *
572
-	 */
573
-	function maybe_update_dataset_uri( $value, $old_value ) {
574
-
575
-		// Check the old key value and the new one. Here we're only handling the
576
-		// case where the key hasn't changed and the dataset URI isn't set. The
577
-		// other case, i.e. a new key is inserted, is handled at `update_key`.
578
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
579
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
580
-
581
-		$dataset_uri = $this->get_dataset_uri();
582
-
583
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
584
-
585
-			// make the request to the remote server to try to get the dataset uri.
586
-			$this->get_remote_dataset_uri( $new_key );
587
-		}
588
-
589
-		return $value;
590
-	}
591
-
592
-	/**
593
-	 * Get the API URI to retrieve the dataset URI using the WordLift Key.
594
-	 *
595
-	 * @param string $key The WordLift key to use.
596
-	 *
597
-	 * @return string The API URI.
598
-	 * @since 3.11.0
599
-	 *
600
-	 */
601
-	public function get_accounts_by_key_dataset_uri( $key ) {
602
-
603
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
604
-	}
605
-
606
-	/**
607
-	 * Get the `accounts` end point.
608
-	 *
609
-	 * @return string The `accounts` end point.
610
-	 * @since 3.16.0
611
-	 *
612
-	 */
613
-	public function get_accounts() {
614
-
615
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
616
-	}
617
-
618
-	/**
619
-	 * Get the `link by default` option.
620
-	 *
621
-	 * @return bool True if entities must be linked by default otherwise false.
622
-	 * @since 3.13.0
623
-	 *
624
-	 */
625
-	public function is_link_by_default() {
626
-
627
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
628
-	}
629
-
630
-	/**
631
-	 * Set the `link by default` option.
632
-	 *
633
-	 * @param bool $value True to enabling linking by default, otherwise false.
634
-	 *
635
-	 * @since 3.13.0
636
-	 *
637
-	 */
638
-	public function set_link_by_default( $value ) {
639
-
640
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
641
-	}
642
-
643
-	/**
644
-	 * Get the 'analytics-enable' option.
645
-	 *
646
-	 * @return string 'no' or 'yes' representing bool.
647
-	 * @since 3.21.0
648
-	 *
649
-	 */
650
-	public function is_analytics_enable() {
651
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
652
-	}
653
-
654
-	/**
655
-	 * Set the `analytics-enable` option.
656
-	 *
657
-	 * @param bool $value True to enabling analytics, otherwise false.
658
-	 *
659
-	 * @since 3.21.0
660
-	 *
661
-	 */
662
-	public function set_is_analytics_enable( $value ) {
663
-
664
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
665
-	}
666
-
667
-	/**
668
-	 * Get the 'analytics-entity-uri-dimention' option.
669
-	 *
670
-	 * @return int
671
-	 * @since 3.21.0
672
-	 *
673
-	 */
674
-	public function get_analytics_entity_uri_dimension() {
675
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
676
-	}
677
-
678
-	/**
679
-	 * Get the 'analytics-entity-type-dimension' option.
680
-	 *
681
-	 * @return int
682
-	 * @since 3.21.0
683
-	 *
684
-	 */
685
-	public function get_analytics_entity_type_dimension() {
686
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
687
-	}
688
-
689
-	/**
690
-	 * Get the URL to perform autocomplete request.
691
-	 *
692
-	 * @return string The URL to call to perform the autocomplete request.
693
-	 * @since 3.15.0
694
-	 *
695
-	 */
696
-	public function get_autocomplete_url() {
697
-
698
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
699
-
700
-	}
701
-
702
-	/**
703
-	 * Get the URL to perform feedback deactivation request.
704
-	 *
705
-	 * @return string The URL to call to perform the feedback deactivation request.
706
-	 * @since 3.19.0
707
-	 *
708
-	 */
709
-	public function get_deactivation_feedback_url() {
710
-
711
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
712
-
713
-	}
714
-
715
-	/**
716
-	 * Get the base API URL.
717
-	 *
718
-	 * @return string The base API URL.
719
-	 * @since 3.20.0
720
-	 *
721
-	 */
722
-	public function get_api_url() {
723
-
724
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
725
-	}
547
+        $json         = json_decode( $response['body'] );
548
+        $dataset_uri  = $json->datasetURI;
549
+        $package_type = isset( $json->packageType ) ? $json->packageType : null;
550
+
551
+        $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
552
+
553
+        $this->set_dataset_uri( $dataset_uri );
554
+        $this->set_package_type( $package_type );
555
+
556
+    }
557
+
558
+    /**
559
+     * Handle the edge case where a user submits the same key again
560
+     * when he does not have the dataset uri to regain it.
561
+     *
562
+     * This can not be handled in the normal option update hook because
563
+     * it is not being triggered when the save value equals to the one already
564
+     * in the DB.
565
+     *
566
+     * @param mixed $value The new, unserialized option value.
567
+     * @param mixed $old_value The old option value.
568
+     *
569
+     * @return mixed The same value in the $value parameter
570
+     * @since 3.12.0
571
+     *
572
+     */
573
+    function maybe_update_dataset_uri( $value, $old_value ) {
574
+
575
+        // Check the old key value and the new one. Here we're only handling the
576
+        // case where the key hasn't changed and the dataset URI isn't set. The
577
+        // other case, i.e. a new key is inserted, is handled at `update_key`.
578
+        $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
579
+        $new_key = isset( $value['key'] ) ? $value['key'] : '';
580
+
581
+        $dataset_uri = $this->get_dataset_uri();
582
+
583
+        if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
584
+
585
+            // make the request to the remote server to try to get the dataset uri.
586
+            $this->get_remote_dataset_uri( $new_key );
587
+        }
588
+
589
+        return $value;
590
+    }
591
+
592
+    /**
593
+     * Get the API URI to retrieve the dataset URI using the WordLift Key.
594
+     *
595
+     * @param string $key The WordLift key to use.
596
+     *
597
+     * @return string The API URI.
598
+     * @since 3.11.0
599
+     *
600
+     */
601
+    public function get_accounts_by_key_dataset_uri( $key ) {
602
+
603
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
604
+    }
605
+
606
+    /**
607
+     * Get the `accounts` end point.
608
+     *
609
+     * @return string The `accounts` end point.
610
+     * @since 3.16.0
611
+     *
612
+     */
613
+    public function get_accounts() {
614
+
615
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
616
+    }
617
+
618
+    /**
619
+     * Get the `link by default` option.
620
+     *
621
+     * @return bool True if entities must be linked by default otherwise false.
622
+     * @since 3.13.0
623
+     *
624
+     */
625
+    public function is_link_by_default() {
626
+
627
+        return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
628
+    }
629
+
630
+    /**
631
+     * Set the `link by default` option.
632
+     *
633
+     * @param bool $value True to enabling linking by default, otherwise false.
634
+     *
635
+     * @since 3.13.0
636
+     *
637
+     */
638
+    public function set_link_by_default( $value ) {
639
+
640
+        $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
641
+    }
642
+
643
+    /**
644
+     * Get the 'analytics-enable' option.
645
+     *
646
+     * @return string 'no' or 'yes' representing bool.
647
+     * @since 3.21.0
648
+     *
649
+     */
650
+    public function is_analytics_enable() {
651
+        return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
652
+    }
653
+
654
+    /**
655
+     * Set the `analytics-enable` option.
656
+     *
657
+     * @param bool $value True to enabling analytics, otherwise false.
658
+     *
659
+     * @since 3.21.0
660
+     *
661
+     */
662
+    public function set_is_analytics_enable( $value ) {
663
+
664
+        $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
665
+    }
666
+
667
+    /**
668
+     * Get the 'analytics-entity-uri-dimention' option.
669
+     *
670
+     * @return int
671
+     * @since 3.21.0
672
+     *
673
+     */
674
+    public function get_analytics_entity_uri_dimension() {
675
+        return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
676
+    }
677
+
678
+    /**
679
+     * Get the 'analytics-entity-type-dimension' option.
680
+     *
681
+     * @return int
682
+     * @since 3.21.0
683
+     *
684
+     */
685
+    public function get_analytics_entity_type_dimension() {
686
+        return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
687
+    }
688
+
689
+    /**
690
+     * Get the URL to perform autocomplete request.
691
+     *
692
+     * @return string The URL to call to perform the autocomplete request.
693
+     * @since 3.15.0
694
+     *
695
+     */
696
+    public function get_autocomplete_url() {
697
+
698
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
699
+
700
+    }
701
+
702
+    /**
703
+     * Get the URL to perform feedback deactivation request.
704
+     *
705
+     * @return string The URL to call to perform the feedback deactivation request.
706
+     * @since 3.19.0
707
+     *
708
+     */
709
+    public function get_deactivation_feedback_url() {
710
+
711
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
712
+
713
+    }
714
+
715
+    /**
716
+     * Get the base API URL.
717
+     *
718
+     * @return string The base API URL.
719
+     * @since 3.20.0
720
+     *
721
+     */
722
+    public function get_api_url() {
723
+
724
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
725
+    }
726 726
 
727 727
 }
Please login to merge, or discard this patch.
Spacing   +87 added lines, -87 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
 
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
 	 */
141 141
 	public function __construct() {
142 142
 
143
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
143
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
144 144
 
145 145
 		self::$instance = $this;
146 146
 
@@ -170,11 +170,11 @@  discard block
 block discarded – undo
170 170
 	 * @since 3.6.0
171 171
 	 *
172 172
 	 */
173
-	private function get( $option, $key, $default = '' ) {
173
+	private function get($option, $key, $default = '') {
174 174
 
175
-		$options = get_option( $option, array() );
175
+		$options = get_option($option, array());
176 176
 
177
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
177
+		return isset($options[$key]) ? $options[$key] : $default;
178 178
 	}
179 179
 
180 180
 	/**
@@ -187,12 +187,12 @@  discard block
 block discarded – undo
187 187
 	 * @since 3.9.0
188 188
 	 *
189 189
 	 */
190
-	private function set( $option, $key, $value ) {
190
+	private function set($option, $key, $value) {
191 191
 
192
-		$values         = get_option( $option );
193
-		$values         = isset( $values ) ? $values : array();
194
-		$values[ $key ] = $value;
195
-		update_option( $option, $values );
192
+		$values         = get_option($option);
193
+		$values         = isset($values) ? $values : array();
194
+		$values[$key] = $value;
195
+		update_option($option, $values);
196 196
 
197 197
 	}
198 198
 
@@ -205,7 +205,7 @@  discard block
 block discarded – undo
205 205
 	 */
206 206
 	public function get_entity_base_path() {
207 207
 
208
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
208
+		return $this->get('wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity');
209 209
 	}
210 210
 
211 211
 	/**
@@ -216,9 +216,9 @@  discard block
 block discarded – undo
216 216
 	 * @since 3.9.0
217 217
 	 *
218 218
 	 */
219
-	public function set_entity_base_path( $value ) {
219
+	public function set_entity_base_path($value) {
220 220
 
221
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
221
+		$this->set('wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value);
222 222
 
223 223
 	}
224 224
 
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
 	 */
232 232
 	public function is_skip_wizard() {
233 233
 
234
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
234
+		return $this->get('wl_general_settings', self::SKIP_WIZARD, false);
235 235
 	}
236 236
 
237 237
 	/**
@@ -242,9 +242,9 @@  discard block
 block discarded – undo
242 242
 	 * @since 3.9.0
243 243
 	 *
244 244
 	 */
245
-	public function set_skip_wizard( $value ) {
245
+	public function set_skip_wizard($value) {
246 246
 
247
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
247
+		$this->set('wl_general_settings', self::SKIP_WIZARD, true === $value);
248 248
 
249 249
 	}
250 250
 
@@ -257,7 +257,7 @@  discard block
 block discarded – undo
257 257
 	 */
258 258
 	public function get_key() {
259 259
 
260
-		return $this->get( 'wl_general_settings', self::KEY, '' );
260
+		return $this->get('wl_general_settings', self::KEY, '');
261 261
 	}
262 262
 
263 263
 	/**
@@ -268,9 +268,9 @@  discard block
 block discarded – undo
268 268
 	 * @since 3.9.0
269 269
 	 *
270 270
 	 */
271
-	public function set_key( $value ) {
271
+	public function set_key($value) {
272 272
 
273
-		$this->set( 'wl_general_settings', self::KEY, $value );
273
+		$this->set('wl_general_settings', self::KEY, $value);
274 274
 	}
275 275
 
276 276
 	/**
@@ -284,7 +284,7 @@  discard block
 block discarded – undo
284 284
 	 */
285 285
 	public function get_language_code() {
286 286
 
287
-		return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' );
287
+		return $this->get('wl_general_settings', self::LANGUAGE, 'en');
288 288
 	}
289 289
 
290 290
 	/**
@@ -295,9 +295,9 @@  discard block
 block discarded – undo
295 295
 	 * @since 3.9.0
296 296
 	 *
297 297
 	 */
298
-	public function set_language_code( $value ) {
298
+	public function set_language_code($value) {
299 299
 
300
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
300
+		$this->set('wl_general_settings', self::LANGUAGE, $value);
301 301
 
302 302
 	}
303 303
 
@@ -309,9 +309,9 @@  discard block
 block discarded – undo
309 309
 	 * @since 3.19.0
310 310
 	 *
311 311
 	 */
312
-	public function set_diagnostic_preferences( $value ) {
312
+	public function set_diagnostic_preferences($value) {
313 313
 
314
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
314
+		$this->set('wl_general_settings', self::SEND_DIAGNOSTIC, $value);
315 315
 
316 316
 	}
317 317
 
@@ -322,7 +322,7 @@  discard block
 block discarded – undo
322 322
 	 */
323 323
 	public function get_diagnostic_preferences() {
324 324
 
325
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
325
+		return $this->get('wl_general_settings', self::SEND_DIAGNOSTIC, 'no');
326 326
 	}
327 327
 
328 328
 	/**
@@ -334,7 +334,7 @@  discard block
 block discarded – undo
334 334
 	 */
335 335
 	public function get_country_code() {
336 336
 
337
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
337
+		return $this->get('wl_general_settings', self::COUNTRY_CODE, 'us');
338 338
 	}
339 339
 
340 340
 	/**
@@ -345,9 +345,9 @@  discard block
 block discarded – undo
345 345
 	 * @since 3.18.0
346 346
 	 *
347 347
 	 */
348
-	public function set_country_code( $value ) {
348
+	public function set_country_code($value) {
349 349
 
350
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
350
+		$this->set('wl_general_settings', self::COUNTRY_CODE, $value);
351 351
 
352 352
 	}
353 353
 
@@ -363,7 +363,7 @@  discard block
 block discarded – undo
363 363
 	 */
364 364
 	public function get_publisher_id() {
365 365
 
366
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
366
+		return $this->get('wl_general_settings', self::PUBLISHER_ID, null);
367 367
 	}
368 368
 
369 369
 	/**
@@ -374,9 +374,9 @@  discard block
 block discarded – undo
374 374
 	 * @since 3.9.0
375 375
 	 *
376 376
 	 */
377
-	public function set_publisher_id( $value ) {
377
+	public function set_publisher_id($value) {
378 378
 
379
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
379
+		$this->set('wl_general_settings', self::PUBLISHER_ID, $value);
380 380
 
381 381
 	}
382 382
 
@@ -390,8 +390,8 @@  discard block
 block discarded – undo
390 390
 	 */
391 391
 	public function get_dataset_uri() {
392 392
 
393
-		if ( apply_filters( 'wl_features__enable__dataset', true ) ) {
394
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
393
+		if (apply_filters('wl_features__enable__dataset', true)) {
394
+			return $this->get('wl_advanced_settings', self::DATASET_URI, null);
395 395
 		} else {
396 396
 			return null;
397 397
 		}
@@ -405,9 +405,9 @@  discard block
 block discarded – undo
405 405
 	 * @since 3.10.0
406 406
 	 *
407 407
 	 */
408
-	public function set_dataset_uri( $value ) {
408
+	public function set_dataset_uri($value) {
409 409
 
410
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
410
+		$this->set('wl_advanced_settings', self::DATASET_URI, $value);
411 411
 	}
412 412
 
413 413
 	/**
@@ -419,7 +419,7 @@  discard block
 block discarded – undo
419 419
 	 */
420 420
 	public function get_package_type() {
421 421
 
422
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
422
+		return $this->get('wl_advanced_settings', self::PACKAGE_TYPE, null);
423 423
 	}
424 424
 
425 425
 	/**
@@ -430,9 +430,9 @@  discard block
 block discarded – undo
430 430
 	 * @since 3.20.0
431 431
 	 *
432 432
 	 */
433
-	public function set_package_type( $value ) {
433
+	public function set_package_type($value) {
434 434
 
435
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
435
+		$this->set('wl_advanced_settings', self::PACKAGE_TYPE, $value);
436 436
 	}
437 437
 
438 438
 	/**
@@ -448,11 +448,11 @@  discard block
 block discarded – undo
448 448
 	 * @param array $old_value The old settings.
449 449
 	 * @param array $new_value The new settings.
450 450
 	 */
451
-	public function update_key( $old_value, $new_value ) {
451
+	public function update_key($old_value, $new_value) {
452 452
 
453 453
 		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
454 454
 		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
455
-		$new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
455
+		$new_key = isset($new_value['key']) ? $new_value['key'] : '';
456 456
 
457 457
 		// If the key hasn't changed, don't do anything.
458 458
 		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
@@ -461,12 +461,12 @@  discard block
 block discarded – undo
461 461
 		//		}
462 462
 
463 463
 		// If the key is empty, empty the dataset URI.
464
-		if ( '' === $new_key ) {
465
-			$this->set_dataset_uri( '' );
464
+		if ('' === $new_key) {
465
+			$this->set_dataset_uri('');
466 466
 		}
467 467
 
468 468
 		// make the request to the remote server.
469
-		$this->get_remote_dataset_uri( $new_key );
469
+		$this->get_remote_dataset_uri($new_key);
470 470
 
471 471
 	}
472 472
 
@@ -482,15 +482,15 @@  discard block
 block discarded – undo
482 482
 	 *
483 483
 	 * @since 3.17.0 send the site URL and get the dataset URI.
484 484
 	 */
485
-	public function get_remote_dataset_uri( $key ) {
485
+	public function get_remote_dataset_uri($key) {
486 486
 
487
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
487
+		$this->log->trace('Getting the remote dataset URI and package type...');
488 488
 
489
-		if ( empty( $key ) ) {
490
-			$this->log->warn( 'Key set to empty value.' );
489
+		if (empty($key)) {
490
+			$this->log->warn('Key set to empty value.');
491 491
 
492
-			$this->set_dataset_uri( '' );
493
-			$this->set_package_type( null );
492
+			$this->set_dataset_uri('');
493
+			$this->set_package_type(null);
494 494
 
495 495
 			return;
496 496
 		}
@@ -505,36 +505,36 @@  discard block
 block discarded – undo
505 505
 		 * @since 3.20.0
506 506
 		 *
507 507
 		 */
508
-		$home_url = defined( 'WP_HOME' ) ? WP_HOME : get_option( 'home' );
509
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
508
+		$home_url = defined('WP_HOME') ? WP_HOME : get_option('home');
509
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url));
510 510
 
511 511
 		// Build the URL.
512 512
 		$url = '/accounts'
513
-		       . '?key=' . rawurlencode( $key )
514
-		       . '&url=' . rawurlencode( $site_url )
515
-		       . '&country=' . $this->get_country_code()
516
-		       . '&language=' . $this->get_language_code();
513
+		       . '?key='.rawurlencode($key)
514
+		       . '&url='.rawurlencode($site_url)
515
+		       . '&country='.$this->get_country_code()
516
+		       . '&language='.$this->get_language_code();
517 517
 
518 518
 		$api_service = Default_Api_Service::get_instance();
519
-		$response    = $api_service->request( 'PUT', $url )->get_response();
519
+		$response    = $api_service->request('PUT', $url)->get_response();
520 520
 
521 521
 		// The response is an error.
522
-		if ( is_wp_error( $response ) ) {
523
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
522
+		if (is_wp_error($response)) {
523
+			$this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message());
524 524
 
525
-			$this->set_dataset_uri( '' );
526
-			$this->set_package_type( null );
525
+			$this->set_dataset_uri('');
526
+			$this->set_package_type(null);
527 527
 
528 528
 			return;
529 529
 		}
530 530
 
531 531
 		// The response is not OK.
532
-		if ( 200 !== (int) $response['response']['code'] ) {
532
+		if (200 !== (int) $response['response']['code']) {
533 533
 			$base_url = $api_service->get_base_url();
534
-			$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
534
+			$this->log->error("Unexpected status code when opening URL $base_url$url: ".$response['response']['code']."\n".var_export($response, true));
535 535
 
536
-			$this->set_dataset_uri( '' );
537
-			$this->set_package_type( null );
536
+			$this->set_dataset_uri('');
537
+			$this->set_package_type(null);
538 538
 
539 539
 			return;
540 540
 		}
@@ -544,14 +544,14 @@  discard block
 block discarded – undo
544 544
 		 *
545 545
 		 * @since 3.20.0
546 546
 		 */
547
-		$json         = json_decode( $response['body'] );
547
+		$json         = json_decode($response['body']);
548 548
 		$dataset_uri  = $json->datasetURI;
549
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
549
+		$package_type = isset($json->packageType) ? $json->packageType : null;
550 550
 
551
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
551
+		$this->log->info("Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]...");
552 552
 
553
-		$this->set_dataset_uri( $dataset_uri );
554
-		$this->set_package_type( $package_type );
553
+		$this->set_dataset_uri($dataset_uri);
554
+		$this->set_package_type($package_type);
555 555
 
556 556
 	}
557 557
 
@@ -570,20 +570,20 @@  discard block
 block discarded – undo
570 570
 	 * @since 3.12.0
571 571
 	 *
572 572
 	 */
573
-	function maybe_update_dataset_uri( $value, $old_value ) {
573
+	function maybe_update_dataset_uri($value, $old_value) {
574 574
 
575 575
 		// Check the old key value and the new one. Here we're only handling the
576 576
 		// case where the key hasn't changed and the dataset URI isn't set. The
577 577
 		// other case, i.e. a new key is inserted, is handled at `update_key`.
578
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
579
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
578
+		$old_key = isset($old_value['key']) ? $old_value['key'] : '';
579
+		$new_key = isset($value['key']) ? $value['key'] : '';
580 580
 
581 581
 		$dataset_uri = $this->get_dataset_uri();
582 582
 
583
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
583
+		if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) {
584 584
 
585 585
 			// make the request to the remote server to try to get the dataset uri.
586
-			$this->get_remote_dataset_uri( $new_key );
586
+			$this->get_remote_dataset_uri($new_key);
587 587
 		}
588 588
 
589 589
 		return $value;
@@ -598,9 +598,9 @@  discard block
 block discarded – undo
598 598
 	 * @since 3.11.0
599 599
 	 *
600 600
 	 */
601
-	public function get_accounts_by_key_dataset_uri( $key ) {
601
+	public function get_accounts_by_key_dataset_uri($key) {
602 602
 
603
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
603
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri";
604 604
 	}
605 605
 
606 606
 	/**
@@ -612,7 +612,7 @@  discard block
 block discarded – undo
612 612
 	 */
613 613
 	public function get_accounts() {
614 614
 
615
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
615
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts';
616 616
 	}
617 617
 
618 618
 	/**
@@ -624,7 +624,7 @@  discard block
 block discarded – undo
624 624
 	 */
625 625
 	public function is_link_by_default() {
626 626
 
627
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
627
+		return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes');
628 628
 	}
629 629
 
630 630
 	/**
@@ -635,9 +635,9 @@  discard block
 block discarded – undo
635 635
 	 * @since 3.13.0
636 636
 	 *
637 637
 	 */
638
-	public function set_link_by_default( $value ) {
638
+	public function set_link_by_default($value) {
639 639
 
640
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
640
+		$this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no');
641 641
 	}
642 642
 
643 643
 	/**
@@ -648,7 +648,7 @@  discard block
 block discarded – undo
648 648
 	 *
649 649
 	 */
650 650
 	public function is_analytics_enable() {
651
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
651
+		return 'yes' === $this->get('wl_analytics_settings', self::ANALYTICS_ENABLE, 'no');
652 652
 	}
653 653
 
654 654
 	/**
@@ -659,9 +659,9 @@  discard block
 block discarded – undo
659 659
 	 * @since 3.21.0
660 660
 	 *
661 661
 	 */
662
-	public function set_is_analytics_enable( $value ) {
662
+	public function set_is_analytics_enable($value) {
663 663
 
664
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
664
+		$this->set('wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no');
665 665
 	}
666 666
 
667 667
 	/**
@@ -672,7 +672,7 @@  discard block
 block discarded – undo
672 672
 	 *
673 673
 	 */
674 674
 	public function get_analytics_entity_uri_dimension() {
675
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
675
+		return (int) $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1);
676 676
 	}
677 677
 
678 678
 	/**
@@ -683,7 +683,7 @@  discard block
 block discarded – undo
683 683
 	 *
684 684
 	 */
685 685
 	public function get_analytics_entity_type_dimension() {
686
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
686
+		return $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2);
687 687
 	}
688 688
 
689 689
 	/**
@@ -695,7 +695,7 @@  discard block
 block discarded – undo
695 695
 	 */
696 696
 	public function get_autocomplete_url() {
697 697
 
698
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
698
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete';
699 699
 
700 700
 	}
701 701
 
@@ -708,7 +708,7 @@  discard block
 block discarded – undo
708 708
 	 */
709 709
 	public function get_deactivation_feedback_url() {
710 710
 
711
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
711
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks';
712 712
 
713 713
 	}
714 714
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-linked-data-service.php 2 patches
Indentation   +318 added lines, -318 removed lines patch added patch discarded remove patch
@@ -18,331 +18,331 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Linked_Data_Service {
20 20
 
21
-	//<editor-fold desc="## FIELDS">
22
-	/**
23
-	 * A {@link Wordlift_Log_Service} instance.
24
-	 *
25
-	 * @since  3.15.0
26
-	 * @access private
27
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
28
-	 */
29
-	private $log;
30
-
31
-	/**
32
-	 * The {@link Wordlift_Entity_Service} instance.
33
-	 *
34
-	 * @since  3.15.0
35
-	 * @access private
36
-	 * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
37
-	 */
38
-	private $entity_service;
39
-
40
-	/**
41
-	 * The {@link Wordlift_Entity_Type_Service} instance.
42
-	 *
43
-	 * @since  3.15.0
44
-	 * @access private
45
-	 * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
46
-	 */
47
-	private $entity_type_service;
48
-
49
-	/**
50
-	 * The {@link Wordlift_Schema_Service} instance.
51
-	 *
52
-	 * @since  3.15.0
53
-	 * @access private
54
-	 * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
55
-	 */
56
-	private $schema_service;
57
-
58
-	/**
59
-	 * The {@link Wordlift_Sparql_Service} instance.
60
-	 *
61
-	 * @since  3.15.0
62
-	 * @access private
63
-	 * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance.
64
-	 */
65
-	private $sparql_service;
66
-
67
-	/**
68
-	 * The {@link Wordlift_Linked_Data_Service} singleton instance.
69
-	 *
70
-	 * @since  3.15.0
71
-	 * @access private
72
-	 * @var \Wordlift_Linked_Data_Service $instance The {@link Wordlift_Linked_Data_Service} singleton instance.
73
-	 */
74
-	private static $instance;
75
-	//</editor-fold>
76
-
77
-	/**
78
-	 * Create a {@link Wordlift_Linked_Data_Service} instance.
79
-	 *
80
-	 * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
81
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
82
-	 * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
83
-	 * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance.
84
-	 *
85
-	 * @since 3.15.0
86
-	 *
87
-	 */
88
-	public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) {
89
-
90
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' );
91
-
92
-		$this->entity_service      = $entity_service;
93
-		$this->entity_type_service = $entity_type_service;
94
-		$this->schema_service      = $schema_service;
95
-		$this->sparql_service      = $sparql_service;
96
-
97
-		/*
21
+    //<editor-fold desc="## FIELDS">
22
+    /**
23
+     * A {@link Wordlift_Log_Service} instance.
24
+     *
25
+     * @since  3.15.0
26
+     * @access private
27
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
28
+     */
29
+    private $log;
30
+
31
+    /**
32
+     * The {@link Wordlift_Entity_Service} instance.
33
+     *
34
+     * @since  3.15.0
35
+     * @access private
36
+     * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
37
+     */
38
+    private $entity_service;
39
+
40
+    /**
41
+     * The {@link Wordlift_Entity_Type_Service} instance.
42
+     *
43
+     * @since  3.15.0
44
+     * @access private
45
+     * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
46
+     */
47
+    private $entity_type_service;
48
+
49
+    /**
50
+     * The {@link Wordlift_Schema_Service} instance.
51
+     *
52
+     * @since  3.15.0
53
+     * @access private
54
+     * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
55
+     */
56
+    private $schema_service;
57
+
58
+    /**
59
+     * The {@link Wordlift_Sparql_Service} instance.
60
+     *
61
+     * @since  3.15.0
62
+     * @access private
63
+     * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance.
64
+     */
65
+    private $sparql_service;
66
+
67
+    /**
68
+     * The {@link Wordlift_Linked_Data_Service} singleton instance.
69
+     *
70
+     * @since  3.15.0
71
+     * @access private
72
+     * @var \Wordlift_Linked_Data_Service $instance The {@link Wordlift_Linked_Data_Service} singleton instance.
73
+     */
74
+    private static $instance;
75
+    //</editor-fold>
76
+
77
+    /**
78
+     * Create a {@link Wordlift_Linked_Data_Service} instance.
79
+     *
80
+     * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
81
+     * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
82
+     * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
83
+     * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance.
84
+     *
85
+     * @since 3.15.0
86
+     *
87
+     */
88
+    public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) {
89
+
90
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' );
91
+
92
+        $this->entity_service      = $entity_service;
93
+        $this->entity_type_service = $entity_type_service;
94
+        $this->schema_service      = $schema_service;
95
+        $this->sparql_service      = $sparql_service;
96
+
97
+        /*
98 98
 		 * Allow callers to call the `push` and `remove` methods using WordPress' hooks.
99 99
 		 *
100 100
 		 * @since 3.27.6
101 101
 		 */
102
-		add_action( 'wl_legacy_linked_data__push', array( $this, 'push' ) );
103
-		add_action( 'wl_legacy_linked_data__remove', array( $this, 'remove' ) );
102
+        add_action( 'wl_legacy_linked_data__push', array( $this, 'push' ) );
103
+        add_action( 'wl_legacy_linked_data__remove', array( $this, 'remove' ) );
104 104
 
105
-		self::$instance = $this;
105
+        self::$instance = $this;
106 106
 
107
-	}
107
+    }
108 108
 
109
-	/**
110
-	 * Get the singleton instance of {@link Wordlift_Linked_Data_Service}.
111
-	 *
112
-	 * @return Wordlift_Linked_Data_Service The singleton instance of <a href='psi_element://Wordlift_Linked_Data_Service'>Wordlift_Linked_Data_Service</a>.
113
-	 * @since 3.15.0
114
-	 *
115
-	 */
116
-	public static function get_instance() {
117
-
118
-		return self::$instance;
119
-	}
109
+    /**
110
+     * Get the singleton instance of {@link Wordlift_Linked_Data_Service}.
111
+     *
112
+     * @return Wordlift_Linked_Data_Service The singleton instance of <a href='psi_element://Wordlift_Linked_Data_Service'>Wordlift_Linked_Data_Service</a>.
113
+     * @since 3.15.0
114
+     *
115
+     */
116
+    public static function get_instance() {
117
+
118
+        return self::$instance;
119
+    }
120 120
 
121
-	//<editor-fold desc="## FUNCTIONS">
122
-
123
-	/**
124
-	 * Push a {@link WP_Post} to the Linked Data store.
125
-	 *
126
-	 * If the {@link WP_Post} is an entity and it's not of the `Article` type,
127
-	 * then it is pushed to the remote Linked Data store.
128
-	 *
129
-	 * @param int $post_id The {@link WP_Post}'s id.
130
-	 *
131
-	 * @since 3.15.0
132
-	 *
133
-	 */
134
-	public function push( $post_id ) {
135
-
136
-		if ( null === Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) {
137
-			$this->log->debug( "Post $post_id won't be published because the dataset URI isn't set." );
138
-
139
-			return;
140
-		}
141
-
142
-		$this->log->debug( "Pushing post $post_id..." );
143
-
144
-		// @since 3.18.0 we don't check anymore if the post is an entity, i.e.
145
-		// we removed the following:
146
-		//		if ( ! $this->entity_service->is_entity( $post_id ) ) {
147
-		//			$this->log->debug( "Post $post_id is not an entity." );
148
-		//
149
-		//			return;
150
-		//		}
151
-
152
-		// Get the post and push it to the Linked Data store.
153
-
154
-		$this->log->debug( "Doing post $post_id push..." );
155
-
156
-		// Get the post.
157
-		$post = get_post( $post_id );
158
-
159
-		// Bail out if the post isn't found.
160
-		if ( null === $post ) {
161
-			$this->log->warn( "Post $post_id not found." );
162
-
163
-			return;
164
-		}
165
-
166
-		// Bail out if the post isn't published.
167
-		if ( 'publish' !== $post->post_status ) {
168
-			$this->log->info( "Post $post_id not published." );
169
-
170
-			return;
171
-		}
172
-
173
-		// Bail out if the URI isn't valid.
174
-		if ( ! $this->has_valid_uri( $post_id ) ) {
175
-			$this->log->warn( "Post $post_id URI invalid." );
176
-
177
-			return;
178
-		}
179
-
180
-		// First remove the post data.
181
-		$this->remove( $post_id );
182
-
183
-		// Then execute the insert query.
184
-		$this->insert( $post_id );
185
-
186
-		// Reindex the triple store if buffering is turned off.
187
-		if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) {
188
-			wordlift_reindex_triple_store();
189
-		}
190
-
191
-	}
192
-
193
-	/**
194
-	 * Check if an entity's {@link WP_Post} has a valid URI.
195
-	 *
196
-	 * @param int $post_id The entity's {@link WP_Post}'s id.
197
-	 *
198
-	 * @return bool True if the URI is valid otherwise false.
199
-	 * @since 3.15.0
200
-	 *
201
-	 */
202
-	private function has_valid_uri( $post_id ) {
203
-
204
-		// Get the entity's URI.
205
-		$uri = $this->entity_service->get_uri( $post_id );
206
-
207
-		// If the URI isn't found, return false.
208
-		if ( null === $uri ) {
209
-			return false;
210
-		}
211
-
212
-		// If the URI ends with a trailing slash, return false.
213
-		if ( '/' === substr( $uri, - 1 ) ) {
214
-			return false;
215
-		}
216
-
217
-		// URI is valid.
218
-		return true;
219
-	}
220
-
221
-	/**
222
-	 * Remove the specified {@link WP_Post} from the Linked Data.
223
-	 *
224
-	 * @param int $post_id The {@link WP_Post}'s id.
225
-	 *
226
-	 * @since 3.18.0
227
-	 *
228
-	 */
229
-	public function remove( $post_id ) {
230
-		$delete_query = '';
231
-
232
-		// Get the delete statements.
233
-		$triples = $this->get_delete_triples( $post_id );
234
-
235
-		// Loop through all triples and add the statement to delete query.
236
-		foreach ( $triples as $item ) {
237
-			$delete_query .= "DELETE { $item } WHERE { $item }; \n";
238
-		}
239
-
240
-		$this->log->trace( "Delete Query generated [ $delete_query ]." );
241
-
242
-		$this->sparql_service->execute( $delete_query );
243
-	}
244
-
245
-	/**
246
-	 * Insert the specific {@link WP_Post} to Linked Data store.
247
-	 *
248
-	 * @param int $post_id The {@link WP_Post}'s id.
249
-	 *
250
-	 * @since 3.18.0
251
-	 *
252
-	 */
253
-	private function insert( $post_id ) {
254
-		// Get the insert statements.
255
-		$insert_triples = $this->get_insert_triples( $post_id );
256
-
257
-		// Convert all statements to single string.
258
-		$insert_query_body = implode( "\n", $insert_triples );
259
-
260
-		// Build the insert query.
261
-		$insert_query = "INSERT DATA { $insert_query_body };";
262
-
263
-		$this->log->trace( "Insert Query generated [ $insert_query ]." );
264
-
265
-		$this->sparql_service->execute( $insert_query );
266
-	}
267
-
268
-	/**
269
-	 * Get the delete statements.
270
-	 *
271
-	 * @param int $post_id The {@link WP_Post}'s id.
272
-	 *
273
-	 * @return array An array of delete statements.
274
-	 * @since 3.18.0
275
-	 *
276
-	 */
277
-	private function get_delete_triples( $post_id ) {
278
-		$delete_triples = array();
279
-
280
-		// Loop through all renditions and get the triples.
281
-		foreach ( $this->schema_service->get_renditions() as $rendition ) {
282
-			// Push the rendition delete triple to $delete_triples.
283
-			$delete_triples = array_merge(
284
-				$delete_triples,
285
-				(array) $rendition->get_delete_triples( $post_id )
286
-			);
287
-		}
288
-
289
-		/**
290
-		 * Filter: 'wl_delete_triples' - Allow third parties to hook and add additional delete triples.
291
-		 *
292
-		 * @param array $delete_triples Delete triples.
293
-		 * @param int $post_id The current post ID.
294
-		 *
295
-		 * @since 3.18.0
296
-		 *
297
-		 */
298
-		return apply_filters( 'wl_delete_triples', array_unique( $delete_triples ), $post_id );
299
-	}
300
-
301
-	/**
302
-	 * Get the SPARQL insert triples ( ?s ?p ?o ) for the specified {@link WP_Post}.
303
-	 *
304
-	 * @param int $post_id The {@link WP_Post}'s id.
305
-	 *
306
-	 * @return array An array of insert triples.
307
-	 * @since 3.15.0
308
-	 *
309
-	 */
310
-	private function get_insert_triples( $post_id ) {
311
-
312
-		// Get the entity type.
313
-		$type = $this->entity_type_service->get( $post_id );
314
-
315
-		// Get the `linked_data` parameter.
316
-		$properties = $type['linked_data'];
317
-
318
-		// Accumulate the triples.
319
-		$triples = array();
320
-
321
-		/** @var Wordlift_Default_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */
322
-		foreach ( $properties as $property ) {
323
-			foreach ( $property->get_insert_triples( $post_id ) as $triple ) {
324
-				$triples[] = $triple;
325
-			}
326
-		}
327
-
328
-		$this->log->trace( count( $properties ) . ' properties and ' . count( $triples ) . " triples found for post $post_id." );
329
-
330
-		/**
331
-		 * Get the INSERT triples properties.
332
-		 *
333
-		 * The `wl_insert_triples` filter allows 3rd parties to extend
334
-		 * the list of triples for SPARQL INSERT statements.
335
-		 *
336
-		 * @param array $linked_data A {@link Wordlift_Sparql_Tuple_Rendition} instances.
337
-		 * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
338
-		 * @param int $post_id The {@link WP_Post}'s id.
339
-		 *
340
-		 * @since 3.18.0 The hook has been renamed from `wl_insert_tuples_properties` to `wl_insert_triples`.
341
-		 *
342
-		 * @since 3.17.0
343
-		 */
344
-		return apply_filters( 'wl_insert_triples', $triples, $this->entity_service, $post_id );
345
-	}
346
-	//</editor-fold>
121
+    //<editor-fold desc="## FUNCTIONS">
122
+
123
+    /**
124
+     * Push a {@link WP_Post} to the Linked Data store.
125
+     *
126
+     * If the {@link WP_Post} is an entity and it's not of the `Article` type,
127
+     * then it is pushed to the remote Linked Data store.
128
+     *
129
+     * @param int $post_id The {@link WP_Post}'s id.
130
+     *
131
+     * @since 3.15.0
132
+     *
133
+     */
134
+    public function push( $post_id ) {
135
+
136
+        if ( null === Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) {
137
+            $this->log->debug( "Post $post_id won't be published because the dataset URI isn't set." );
138
+
139
+            return;
140
+        }
141
+
142
+        $this->log->debug( "Pushing post $post_id..." );
143
+
144
+        // @since 3.18.0 we don't check anymore if the post is an entity, i.e.
145
+        // we removed the following:
146
+        //		if ( ! $this->entity_service->is_entity( $post_id ) ) {
147
+        //			$this->log->debug( "Post $post_id is not an entity." );
148
+        //
149
+        //			return;
150
+        //		}
151
+
152
+        // Get the post and push it to the Linked Data store.
153
+
154
+        $this->log->debug( "Doing post $post_id push..." );
155
+
156
+        // Get the post.
157
+        $post = get_post( $post_id );
158
+
159
+        // Bail out if the post isn't found.
160
+        if ( null === $post ) {
161
+            $this->log->warn( "Post $post_id not found." );
162
+
163
+            return;
164
+        }
165
+
166
+        // Bail out if the post isn't published.
167
+        if ( 'publish' !== $post->post_status ) {
168
+            $this->log->info( "Post $post_id not published." );
169
+
170
+            return;
171
+        }
172
+
173
+        // Bail out if the URI isn't valid.
174
+        if ( ! $this->has_valid_uri( $post_id ) ) {
175
+            $this->log->warn( "Post $post_id URI invalid." );
176
+
177
+            return;
178
+        }
179
+
180
+        // First remove the post data.
181
+        $this->remove( $post_id );
182
+
183
+        // Then execute the insert query.
184
+        $this->insert( $post_id );
185
+
186
+        // Reindex the triple store if buffering is turned off.
187
+        if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) {
188
+            wordlift_reindex_triple_store();
189
+        }
190
+
191
+    }
192
+
193
+    /**
194
+     * Check if an entity's {@link WP_Post} has a valid URI.
195
+     *
196
+     * @param int $post_id The entity's {@link WP_Post}'s id.
197
+     *
198
+     * @return bool True if the URI is valid otherwise false.
199
+     * @since 3.15.0
200
+     *
201
+     */
202
+    private function has_valid_uri( $post_id ) {
203
+
204
+        // Get the entity's URI.
205
+        $uri = $this->entity_service->get_uri( $post_id );
206
+
207
+        // If the URI isn't found, return false.
208
+        if ( null === $uri ) {
209
+            return false;
210
+        }
211
+
212
+        // If the URI ends with a trailing slash, return false.
213
+        if ( '/' === substr( $uri, - 1 ) ) {
214
+            return false;
215
+        }
216
+
217
+        // URI is valid.
218
+        return true;
219
+    }
220
+
221
+    /**
222
+     * Remove the specified {@link WP_Post} from the Linked Data.
223
+     *
224
+     * @param int $post_id The {@link WP_Post}'s id.
225
+     *
226
+     * @since 3.18.0
227
+     *
228
+     */
229
+    public function remove( $post_id ) {
230
+        $delete_query = '';
231
+
232
+        // Get the delete statements.
233
+        $triples = $this->get_delete_triples( $post_id );
234
+
235
+        // Loop through all triples and add the statement to delete query.
236
+        foreach ( $triples as $item ) {
237
+            $delete_query .= "DELETE { $item } WHERE { $item }; \n";
238
+        }
239
+
240
+        $this->log->trace( "Delete Query generated [ $delete_query ]." );
241
+
242
+        $this->sparql_service->execute( $delete_query );
243
+    }
244
+
245
+    /**
246
+     * Insert the specific {@link WP_Post} to Linked Data store.
247
+     *
248
+     * @param int $post_id The {@link WP_Post}'s id.
249
+     *
250
+     * @since 3.18.0
251
+     *
252
+     */
253
+    private function insert( $post_id ) {
254
+        // Get the insert statements.
255
+        $insert_triples = $this->get_insert_triples( $post_id );
256
+
257
+        // Convert all statements to single string.
258
+        $insert_query_body = implode( "\n", $insert_triples );
259
+
260
+        // Build the insert query.
261
+        $insert_query = "INSERT DATA { $insert_query_body };";
262
+
263
+        $this->log->trace( "Insert Query generated [ $insert_query ]." );
264
+
265
+        $this->sparql_service->execute( $insert_query );
266
+    }
267
+
268
+    /**
269
+     * Get the delete statements.
270
+     *
271
+     * @param int $post_id The {@link WP_Post}'s id.
272
+     *
273
+     * @return array An array of delete statements.
274
+     * @since 3.18.0
275
+     *
276
+     */
277
+    private function get_delete_triples( $post_id ) {
278
+        $delete_triples = array();
279
+
280
+        // Loop through all renditions and get the triples.
281
+        foreach ( $this->schema_service->get_renditions() as $rendition ) {
282
+            // Push the rendition delete triple to $delete_triples.
283
+            $delete_triples = array_merge(
284
+                $delete_triples,
285
+                (array) $rendition->get_delete_triples( $post_id )
286
+            );
287
+        }
288
+
289
+        /**
290
+         * Filter: 'wl_delete_triples' - Allow third parties to hook and add additional delete triples.
291
+         *
292
+         * @param array $delete_triples Delete triples.
293
+         * @param int $post_id The current post ID.
294
+         *
295
+         * @since 3.18.0
296
+         *
297
+         */
298
+        return apply_filters( 'wl_delete_triples', array_unique( $delete_triples ), $post_id );
299
+    }
300
+
301
+    /**
302
+     * Get the SPARQL insert triples ( ?s ?p ?o ) for the specified {@link WP_Post}.
303
+     *
304
+     * @param int $post_id The {@link WP_Post}'s id.
305
+     *
306
+     * @return array An array of insert triples.
307
+     * @since 3.15.0
308
+     *
309
+     */
310
+    private function get_insert_triples( $post_id ) {
311
+
312
+        // Get the entity type.
313
+        $type = $this->entity_type_service->get( $post_id );
314
+
315
+        // Get the `linked_data` parameter.
316
+        $properties = $type['linked_data'];
317
+
318
+        // Accumulate the triples.
319
+        $triples = array();
320
+
321
+        /** @var Wordlift_Default_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */
322
+        foreach ( $properties as $property ) {
323
+            foreach ( $property->get_insert_triples( $post_id ) as $triple ) {
324
+                $triples[] = $triple;
325
+            }
326
+        }
327
+
328
+        $this->log->trace( count( $properties ) . ' properties and ' . count( $triples ) . " triples found for post $post_id." );
329
+
330
+        /**
331
+         * Get the INSERT triples properties.
332
+         *
333
+         * The `wl_insert_triples` filter allows 3rd parties to extend
334
+         * the list of triples for SPARQL INSERT statements.
335
+         *
336
+         * @param array $linked_data A {@link Wordlift_Sparql_Tuple_Rendition} instances.
337
+         * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
338
+         * @param int $post_id The {@link WP_Post}'s id.
339
+         *
340
+         * @since 3.18.0 The hook has been renamed from `wl_insert_tuples_properties` to `wl_insert_triples`.
341
+         *
342
+         * @since 3.17.0
343
+         */
344
+        return apply_filters( 'wl_insert_triples', $triples, $this->entity_service, $post_id );
345
+    }
346
+    //</editor-fold>
347 347
 
348 348
 }
Please login to merge, or discard this patch.
Spacing   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -85,9 +85,9 @@  discard block
 block discarded – undo
85 85
 	 * @since 3.15.0
86 86
 	 *
87 87
 	 */
88
-	public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) {
88
+	public function __construct($entity_service, $entity_type_service, $schema_service, $sparql_service) {
89 89
 
90
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' );
90
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Linked_Data_Service');
91 91
 
92 92
 		$this->entity_service      = $entity_service;
93 93
 		$this->entity_type_service = $entity_type_service;
@@ -99,8 +99,8 @@  discard block
 block discarded – undo
99 99
 		 *
100 100
 		 * @since 3.27.6
101 101
 		 */
102
-		add_action( 'wl_legacy_linked_data__push', array( $this, 'push' ) );
103
-		add_action( 'wl_legacy_linked_data__remove', array( $this, 'remove' ) );
102
+		add_action('wl_legacy_linked_data__push', array($this, 'push'));
103
+		add_action('wl_legacy_linked_data__remove', array($this, 'remove'));
104 104
 
105 105
 		self::$instance = $this;
106 106
 
@@ -131,15 +131,15 @@  discard block
 block discarded – undo
131 131
 	 * @since 3.15.0
132 132
 	 *
133 133
 	 */
134
-	public function push( $post_id ) {
134
+	public function push($post_id) {
135 135
 
136
-		if ( null === Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) {
137
-			$this->log->debug( "Post $post_id won't be published because the dataset URI isn't set." );
136
+		if (null === Wordlift_Configuration_Service::get_instance()->get_dataset_uri()) {
137
+			$this->log->debug("Post $post_id won't be published because the dataset URI isn't set.");
138 138
 
139 139
 			return;
140 140
 		}
141 141
 
142
-		$this->log->debug( "Pushing post $post_id..." );
142
+		$this->log->debug("Pushing post $post_id...");
143 143
 
144 144
 		// @since 3.18.0 we don't check anymore if the post is an entity, i.e.
145 145
 		// we removed the following:
@@ -151,40 +151,40 @@  discard block
 block discarded – undo
151 151
 
152 152
 		// Get the post and push it to the Linked Data store.
153 153
 
154
-		$this->log->debug( "Doing post $post_id push..." );
154
+		$this->log->debug("Doing post $post_id push...");
155 155
 
156 156
 		// Get the post.
157
-		$post = get_post( $post_id );
157
+		$post = get_post($post_id);
158 158
 
159 159
 		// Bail out if the post isn't found.
160
-		if ( null === $post ) {
161
-			$this->log->warn( "Post $post_id not found." );
160
+		if (null === $post) {
161
+			$this->log->warn("Post $post_id not found.");
162 162
 
163 163
 			return;
164 164
 		}
165 165
 
166 166
 		// Bail out if the post isn't published.
167
-		if ( 'publish' !== $post->post_status ) {
168
-			$this->log->info( "Post $post_id not published." );
167
+		if ('publish' !== $post->post_status) {
168
+			$this->log->info("Post $post_id not published.");
169 169
 
170 170
 			return;
171 171
 		}
172 172
 
173 173
 		// Bail out if the URI isn't valid.
174
-		if ( ! $this->has_valid_uri( $post_id ) ) {
175
-			$this->log->warn( "Post $post_id URI invalid." );
174
+		if ( ! $this->has_valid_uri($post_id)) {
175
+			$this->log->warn("Post $post_id URI invalid.");
176 176
 
177 177
 			return;
178 178
 		}
179 179
 
180 180
 		// First remove the post data.
181
-		$this->remove( $post_id );
181
+		$this->remove($post_id);
182 182
 
183 183
 		// Then execute the insert query.
184
-		$this->insert( $post_id );
184
+		$this->insert($post_id);
185 185
 
186 186
 		// Reindex the triple store if buffering is turned off.
187
-		if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) {
187
+		if (false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING) {
188 188
 			wordlift_reindex_triple_store();
189 189
 		}
190 190
 
@@ -199,18 +199,18 @@  discard block
 block discarded – undo
199 199
 	 * @since 3.15.0
200 200
 	 *
201 201
 	 */
202
-	private function has_valid_uri( $post_id ) {
202
+	private function has_valid_uri($post_id) {
203 203
 
204 204
 		// Get the entity's URI.
205
-		$uri = $this->entity_service->get_uri( $post_id );
205
+		$uri = $this->entity_service->get_uri($post_id);
206 206
 
207 207
 		// If the URI isn't found, return false.
208
-		if ( null === $uri ) {
208
+		if (null === $uri) {
209 209
 			return false;
210 210
 		}
211 211
 
212 212
 		// If the URI ends with a trailing slash, return false.
213
-		if ( '/' === substr( $uri, - 1 ) ) {
213
+		if ('/' === substr($uri, - 1)) {
214 214
 			return false;
215 215
 		}
216 216
 
@@ -226,20 +226,20 @@  discard block
 block discarded – undo
226 226
 	 * @since 3.18.0
227 227
 	 *
228 228
 	 */
229
-	public function remove( $post_id ) {
229
+	public function remove($post_id) {
230 230
 		$delete_query = '';
231 231
 
232 232
 		// Get the delete statements.
233
-		$triples = $this->get_delete_triples( $post_id );
233
+		$triples = $this->get_delete_triples($post_id);
234 234
 
235 235
 		// Loop through all triples and add the statement to delete query.
236
-		foreach ( $triples as $item ) {
236
+		foreach ($triples as $item) {
237 237
 			$delete_query .= "DELETE { $item } WHERE { $item }; \n";
238 238
 		}
239 239
 
240
-		$this->log->trace( "Delete Query generated [ $delete_query ]." );
240
+		$this->log->trace("Delete Query generated [ $delete_query ].");
241 241
 
242
-		$this->sparql_service->execute( $delete_query );
242
+		$this->sparql_service->execute($delete_query);
243 243
 	}
244 244
 
245 245
 	/**
@@ -250,19 +250,19 @@  discard block
 block discarded – undo
250 250
 	 * @since 3.18.0
251 251
 	 *
252 252
 	 */
253
-	private function insert( $post_id ) {
253
+	private function insert($post_id) {
254 254
 		// Get the insert statements.
255
-		$insert_triples = $this->get_insert_triples( $post_id );
255
+		$insert_triples = $this->get_insert_triples($post_id);
256 256
 
257 257
 		// Convert all statements to single string.
258
-		$insert_query_body = implode( "\n", $insert_triples );
258
+		$insert_query_body = implode("\n", $insert_triples);
259 259
 
260 260
 		// Build the insert query.
261 261
 		$insert_query = "INSERT DATA { $insert_query_body };";
262 262
 
263
-		$this->log->trace( "Insert Query generated [ $insert_query ]." );
263
+		$this->log->trace("Insert Query generated [ $insert_query ].");
264 264
 
265
-		$this->sparql_service->execute( $insert_query );
265
+		$this->sparql_service->execute($insert_query);
266 266
 	}
267 267
 
268 268
 	/**
@@ -274,15 +274,15 @@  discard block
 block discarded – undo
274 274
 	 * @since 3.18.0
275 275
 	 *
276 276
 	 */
277
-	private function get_delete_triples( $post_id ) {
277
+	private function get_delete_triples($post_id) {
278 278
 		$delete_triples = array();
279 279
 
280 280
 		// Loop through all renditions and get the triples.
281
-		foreach ( $this->schema_service->get_renditions() as $rendition ) {
281
+		foreach ($this->schema_service->get_renditions() as $rendition) {
282 282
 			// Push the rendition delete triple to $delete_triples.
283 283
 			$delete_triples = array_merge(
284 284
 				$delete_triples,
285
-				(array) $rendition->get_delete_triples( $post_id )
285
+				(array) $rendition->get_delete_triples($post_id)
286 286
 			);
287 287
 		}
288 288
 
@@ -295,7 +295,7 @@  discard block
 block discarded – undo
295 295
 		 * @since 3.18.0
296 296
 		 *
297 297
 		 */
298
-		return apply_filters( 'wl_delete_triples', array_unique( $delete_triples ), $post_id );
298
+		return apply_filters('wl_delete_triples', array_unique($delete_triples), $post_id);
299 299
 	}
300 300
 
301 301
 	/**
@@ -307,10 +307,10 @@  discard block
 block discarded – undo
307 307
 	 * @since 3.15.0
308 308
 	 *
309 309
 	 */
310
-	private function get_insert_triples( $post_id ) {
310
+	private function get_insert_triples($post_id) {
311 311
 
312 312
 		// Get the entity type.
313
-		$type = $this->entity_type_service->get( $post_id );
313
+		$type = $this->entity_type_service->get($post_id);
314 314
 
315 315
 		// Get the `linked_data` parameter.
316 316
 		$properties = $type['linked_data'];
@@ -319,13 +319,13 @@  discard block
 block discarded – undo
319 319
 		$triples = array();
320 320
 
321 321
 		/** @var Wordlift_Default_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */
322
-		foreach ( $properties as $property ) {
323
-			foreach ( $property->get_insert_triples( $post_id ) as $triple ) {
322
+		foreach ($properties as $property) {
323
+			foreach ($property->get_insert_triples($post_id) as $triple) {
324 324
 				$triples[] = $triple;
325 325
 			}
326 326
 		}
327 327
 
328
-		$this->log->trace( count( $properties ) . ' properties and ' . count( $triples ) . " triples found for post $post_id." );
328
+		$this->log->trace(count($properties).' properties and '.count($triples)." triples found for post $post_id.");
329 329
 
330 330
 		/**
331 331
 		 * Get the INSERT triples properties.
@@ -341,7 +341,7 @@  discard block
 block discarded – undo
341 341
 		 *
342 342
 		 * @since 3.17.0
343 343
 		 */
344
-		return apply_filters( 'wl_insert_triples', $triples, $this->entity_service, $post_id );
344
+		return apply_filters('wl_insert_triples', $triples, $this->entity_service, $post_id);
345 345
 	}
346 346
 	//</editor-fold>
347 347
 
Please login to merge, or discard this patch.