Completed
Push — develop ( 492976...e2e400 )
by David
02:54
created
src/includes/class-wordlift-relation-service.php 1 patch
Indentation   +376 added lines, -376 removed lines patch added patch discarded remove patch
@@ -18,376 +18,376 @@  discard block
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Relation_Service {
20 20
 
21
-	/**
22
-	 * The singleton instance.
23
-	 *
24
-	 * @since  3.15.0
25
-	 * @access private
26
-	 * @var \Wordlift_Relation_Service $instance The singleton instance.
27
-	 */
28
-	private static $instance;
29
-
30
-	/**
31
-	 * The relation table name in MySQL, set during instantiation.
32
-	 *
33
-	 * @since  3.15.0
34
-	 * @access private
35
-	 * @var string $relation_table The relation table name.
36
-	 */
37
-	private $relation_table;
38
-
39
-	/**
40
-	 * A {@link Wordlift_Log_Service} instance.
41
-	 *
42
-	 * @since 3.15.3
43
-	 *
44
-	 * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
45
-	 */
46
-	private static $log;
47
-
48
-	/**
49
-	 * Create a {@link Wordlift_Relation_Service} instance.
50
-	 *
51
-	 * @since 3.15.0
52
-	 */
53
-	public function __construct() {
54
-
55
-		self::$log = Wordlift_Log_Service::get_logger( get_class() );
56
-
57
-		global $wpdb;
58
-
59
-		// The relations table.
60
-		$this->relation_table = "{$wpdb->prefix}wl_relation_instances";
61
-
62
-		self::$instance = $this;
63
-
64
-	}
65
-
66
-	/**
67
-	 * Get the singleton instance.
68
-	 *
69
-	 * @since  3.15.0
70
-	 * @access public
71
-	 * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service}
72
-	 *                                    singleton instance.
73
-	 */
74
-	public static function get_instance() {
75
-
76
-		return self::$instance;
77
-	}
78
-
79
-	/**
80
-	 * Get the articles referencing the specified entity {@link WP_Post}.
81
-	 *
82
-	 * @since 3.15.0
83
-	 *
84
-	 * @param int|array   $object_id The entity {@link WP_Post}'s id.
85
-	 * @param string      $fields    The fields to return, 'ids' to only return ids or
86
-	 *                               '*' to return all fields, by default '*'.
87
-	 * @param null|string $predicate The predicate (who|what|...), by default all.
88
-	 * @param null|string $status    The status, by default all.
89
-	 * @param array       $excludes  An array of ids to exclude from the results.
90
-	 * @param null|int    $limit     The maximum number of results, by default
91
-	 *                               no limit.
92
-	 * @param null|array  $include   The {@link WP_Post}s' ids to include.
93
-	 *
94
-	 * @return array|object|null Database query results
95
-	 */
96
-	public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null ) {
97
-		global $wpdb;
98
-
99
-		// The output fields.
100
-		$actual_fields = self::fields( $fields );
101
-
102
-		self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' );
103
-
104
-		$objects = $this->article_id_to_entity_id( $object_id );
105
-
106
-		// If there are no related objects, return an empty array.
107
-		if ( empty( $objects ) ) {
108
-			self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' );
109
-
110
-			return array();
111
-		}
112
-
113
-		self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' );
114
-
115
-		$sql =
116
-			"
21
+    /**
22
+     * The singleton instance.
23
+     *
24
+     * @since  3.15.0
25
+     * @access private
26
+     * @var \Wordlift_Relation_Service $instance The singleton instance.
27
+     */
28
+    private static $instance;
29
+
30
+    /**
31
+     * The relation table name in MySQL, set during instantiation.
32
+     *
33
+     * @since  3.15.0
34
+     * @access private
35
+     * @var string $relation_table The relation table name.
36
+     */
37
+    private $relation_table;
38
+
39
+    /**
40
+     * A {@link Wordlift_Log_Service} instance.
41
+     *
42
+     * @since 3.15.3
43
+     *
44
+     * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
45
+     */
46
+    private static $log;
47
+
48
+    /**
49
+     * Create a {@link Wordlift_Relation_Service} instance.
50
+     *
51
+     * @since 3.15.0
52
+     */
53
+    public function __construct() {
54
+
55
+        self::$log = Wordlift_Log_Service::get_logger( get_class() );
56
+
57
+        global $wpdb;
58
+
59
+        // The relations table.
60
+        $this->relation_table = "{$wpdb->prefix}wl_relation_instances";
61
+
62
+        self::$instance = $this;
63
+
64
+    }
65
+
66
+    /**
67
+     * Get the singleton instance.
68
+     *
69
+     * @since  3.15.0
70
+     * @access public
71
+     * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service}
72
+     *                                    singleton instance.
73
+     */
74
+    public static function get_instance() {
75
+
76
+        return self::$instance;
77
+    }
78
+
79
+    /**
80
+     * Get the articles referencing the specified entity {@link WP_Post}.
81
+     *
82
+     * @since 3.15.0
83
+     *
84
+     * @param int|array   $object_id The entity {@link WP_Post}'s id.
85
+     * @param string      $fields    The fields to return, 'ids' to only return ids or
86
+     *                               '*' to return all fields, by default '*'.
87
+     * @param null|string $predicate The predicate (who|what|...), by default all.
88
+     * @param null|string $status    The status, by default all.
89
+     * @param array       $excludes  An array of ids to exclude from the results.
90
+     * @param null|int    $limit     The maximum number of results, by default
91
+     *                               no limit.
92
+     * @param null|array  $include   The {@link WP_Post}s' ids to include.
93
+     *
94
+     * @return array|object|null Database query results
95
+     */
96
+    public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null ) {
97
+        global $wpdb;
98
+
99
+        // The output fields.
100
+        $actual_fields = self::fields( $fields );
101
+
102
+        self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' );
103
+
104
+        $objects = $this->article_id_to_entity_id( $object_id );
105
+
106
+        // If there are no related objects, return an empty array.
107
+        if ( empty( $objects ) ) {
108
+            self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' );
109
+
110
+            return array();
111
+        }
112
+
113
+        self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' );
114
+
115
+        $sql =
116
+            "
117 117
 			SELECT DISTINCT p.$actual_fields
118 118
 			FROM {$this->relation_table} r
119 119
 			INNER JOIN $wpdb->posts p
120 120
 				ON p.id = r.subject_id
121 121
 			"
122
-			// Add the status clause.
123
-			. self::and_status( $status )
124
-			. self::inner_join_is_article()
125
-			. self::where_object_id( $objects )
126
-			// Since `object_id` can be an article ID we need to exclude it from
127
-			// the results.
128
-			. self::and_article_not_in( array_merge( $excludes, (array) $object_id ) )
129
-			. self::and_article_in( $include )
130
-			. self::and_post_type_in()
131
-			. self::and_predicate( $predicate )
132
-			. self::limit( $limit );
133
-
134
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
135
-	}
136
-
137
-	/**
138
-	 * The `post_type IN` clause.
139
-	 *
140
-	 * @since 3.15.3
141
-	 *
142
-	 * @return string The `post_type IN` clause.
143
-	 */
144
-	private static function and_post_type_in() {
145
-
146
-		return " AND p.post_type IN ( '"
147
-			   . implode(
148
-				   "','",
149
-				   array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() )
150
-			   )
151
-			   . "' )";
152
-	}
153
-
154
-	/**
155
-	 * Add the limit clause if specified.
156
-	 *
157
-	 * @since 3.15.0
158
-	 *
159
-	 * @param null|int $limit The maximum number of results.
160
-	 *
161
-	 * @return string The limit clause (empty if no limit has been specified).
162
-	 */
163
-	private static function limit( $limit = null ) {
164
-
165
-		if ( null === $limit ) {
166
-			return '';
167
-		}
168
-
169
-		return "LIMIT $limit";
170
-	}
171
-
172
-	/**
173
-	 * Map the provided ids into entities (i.e. return the id if it's an entity
174
-	 * or get the entities if it's a post).
175
-	 *
176
-	 * @since 3.15.0
177
-	 *
178
-	 * @param int|array $object_id An array of posts/entities' ids.
179
-	 *
180
-	 * @return array An array of entities' ids.
181
-	 */
182
-	private function article_id_to_entity_id( $object_id ) {
183
-
184
-		$entity_service = Wordlift_Entity_Service::get_instance();
185
-
186
-		$relation_service = $this;
187
-
188
-		return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) {
189
-			if ( $entity_service->is_entity( $item ) ) {
190
-				return array_merge( $carry, (array) $item );
191
-			}
192
-
193
-			return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) );
194
-		}, array() );
195
-
196
-	}
197
-
198
-	/**
199
-	 * Add the WHERE clause.
200
-	 *
201
-	 * @since 3.15.0
202
-	 *
203
-	 * @param int|array $object_id An array of {@link WP_Post}s' ids.
204
-	 *
205
-	 * @return string The WHERE clause.
206
-	 */
207
-	private static function where_object_id( $object_id ) {
208
-
209
-		if ( empty( $object_id ) ) {
210
-			self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) );
211
-
212
-			return ' WHERE 1 = 1';
213
-		}
214
-
215
-		return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )';
216
-	}
217
-
218
-	/**
219
-	 * Add the exclude clause.
220
-	 *
221
-	 * @since 3.15.0
222
-	 *
223
-	 * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude.
224
-	 *
225
-	 * @return string The exclude clause.
226
-	 */
227
-	private static function and_article_not_in( $exclude ) {
228
-
229
-		return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )';
230
-	}
231
-
232
-	/**
233
-	 * Add the include clause.
234
-	 *
235
-	 * @since 3.15.0
236
-	 *
237
-	 * @param null|int|array $include An array of {@link WP_Post}s' ids.
238
-	 *
239
-	 * @return string An empty string if $include is null otherwise the include
240
-	 *                clause.
241
-	 */
242
-	private static function and_article_in( $include = null ) {
243
-
244
-		if ( null === $include ) {
245
-			return '';
246
-		}
247
-
248
-		return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )';
249
-	}
250
-
251
-	/**
252
-	 * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}.
253
-	 *
254
-	 * @since 3.15.0
255
-	 *
256
-	 * @param int         $object_id The object {@link WP_Post}'s id.
257
-	 * @param string      $fields    The fields to return, 'ids' to only return ids or
258
-	 *                               '*' to return all fields, by default '*'.
259
-	 * @param null|string $status    The status, by default all.
260
-	 *
261
-	 * @return array|object|null Database query results
262
-	 */
263
-	public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) {
264
-		global $wpdb;
265
-
266
-		// The output fields.
267
-		$actual_fields = self::fields( $fields );
268
-
269
-		$sql = $wpdb->prepare(
270
-			"
122
+            // Add the status clause.
123
+            . self::and_status( $status )
124
+            . self::inner_join_is_article()
125
+            . self::where_object_id( $objects )
126
+            // Since `object_id` can be an article ID we need to exclude it from
127
+            // the results.
128
+            . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) )
129
+            . self::and_article_in( $include )
130
+            . self::and_post_type_in()
131
+            . self::and_predicate( $predicate )
132
+            . self::limit( $limit );
133
+
134
+        return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
135
+    }
136
+
137
+    /**
138
+     * The `post_type IN` clause.
139
+     *
140
+     * @since 3.15.3
141
+     *
142
+     * @return string The `post_type IN` clause.
143
+     */
144
+    private static function and_post_type_in() {
145
+
146
+        return " AND p.post_type IN ( '"
147
+                . implode(
148
+                    "','",
149
+                    array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() )
150
+                )
151
+                . "' )";
152
+    }
153
+
154
+    /**
155
+     * Add the limit clause if specified.
156
+     *
157
+     * @since 3.15.0
158
+     *
159
+     * @param null|int $limit The maximum number of results.
160
+     *
161
+     * @return string The limit clause (empty if no limit has been specified).
162
+     */
163
+    private static function limit( $limit = null ) {
164
+
165
+        if ( null === $limit ) {
166
+            return '';
167
+        }
168
+
169
+        return "LIMIT $limit";
170
+    }
171
+
172
+    /**
173
+     * Map the provided ids into entities (i.e. return the id if it's an entity
174
+     * or get the entities if it's a post).
175
+     *
176
+     * @since 3.15.0
177
+     *
178
+     * @param int|array $object_id An array of posts/entities' ids.
179
+     *
180
+     * @return array An array of entities' ids.
181
+     */
182
+    private function article_id_to_entity_id( $object_id ) {
183
+
184
+        $entity_service = Wordlift_Entity_Service::get_instance();
185
+
186
+        $relation_service = $this;
187
+
188
+        return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) {
189
+            if ( $entity_service->is_entity( $item ) ) {
190
+                return array_merge( $carry, (array) $item );
191
+            }
192
+
193
+            return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) );
194
+        }, array() );
195
+
196
+    }
197
+
198
+    /**
199
+     * Add the WHERE clause.
200
+     *
201
+     * @since 3.15.0
202
+     *
203
+     * @param int|array $object_id An array of {@link WP_Post}s' ids.
204
+     *
205
+     * @return string The WHERE clause.
206
+     */
207
+    private static function where_object_id( $object_id ) {
208
+
209
+        if ( empty( $object_id ) ) {
210
+            self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) );
211
+
212
+            return ' WHERE 1 = 1';
213
+        }
214
+
215
+        return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )';
216
+    }
217
+
218
+    /**
219
+     * Add the exclude clause.
220
+     *
221
+     * @since 3.15.0
222
+     *
223
+     * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude.
224
+     *
225
+     * @return string The exclude clause.
226
+     */
227
+    private static function and_article_not_in( $exclude ) {
228
+
229
+        return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )';
230
+    }
231
+
232
+    /**
233
+     * Add the include clause.
234
+     *
235
+     * @since 3.15.0
236
+     *
237
+     * @param null|int|array $include An array of {@link WP_Post}s' ids.
238
+     *
239
+     * @return string An empty string if $include is null otherwise the include
240
+     *                clause.
241
+     */
242
+    private static function and_article_in( $include = null ) {
243
+
244
+        if ( null === $include ) {
245
+            return '';
246
+        }
247
+
248
+        return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )';
249
+    }
250
+
251
+    /**
252
+     * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}.
253
+     *
254
+     * @since 3.15.0
255
+     *
256
+     * @param int         $object_id The object {@link WP_Post}'s id.
257
+     * @param string      $fields    The fields to return, 'ids' to only return ids or
258
+     *                               '*' to return all fields, by default '*'.
259
+     * @param null|string $status    The status, by default all.
260
+     *
261
+     * @return array|object|null Database query results
262
+     */
263
+    public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) {
264
+        global $wpdb;
265
+
266
+        // The output fields.
267
+        $actual_fields = self::fields( $fields );
268
+
269
+        $sql = $wpdb->prepare(
270
+            "
271 271
 			SELECT p.$actual_fields
272 272
 			FROM {$this->relation_table} r
273 273
 			INNER JOIN $wpdb->posts p
274 274
 				ON p.id = r.subject_id
275 275
 			"
276
-			// Add the status clause.
277
-			. self::and_status( $status )
278
-			. self::inner_join_is_not_article()
279
-			. " WHERE r.object_id = %d "
280
-			. self::and_post_type_in()
281
-			,
282
-			$object_id
283
-		);
284
-
285
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
286
-	}
287
-
288
-	/**
289
-	 * Get the entities referenced by the specified {@link WP_Post}.
290
-	 *
291
-	 * @since 3.15.0
292
-	 *
293
-	 * @param int         $subject_id The {@link WP_Post}'s id.
294
-	 * @param string      $fields     The fields to return, 'ids' to only return ids or
295
-	 *                                '*' to return all fields, by default '*'.
296
-	 * @param null|string $predicate  The predicate (who|what|...), by default all.
297
-	 * @param null|string $status     The status, by default all.
298
-	 *
299
-	 * @return array|object|null Database query results
300
-	 */
301
-	public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) {
302
-		global $wpdb;
303
-
304
-		// The output fields.
305
-		$actual_fields = self::fields( $fields );
306
-
307
-		$sql = $wpdb->prepare(
308
-			"
276
+            // Add the status clause.
277
+            . self::and_status( $status )
278
+            . self::inner_join_is_not_article()
279
+            . " WHERE r.object_id = %d "
280
+            . self::and_post_type_in()
281
+            ,
282
+            $object_id
283
+        );
284
+
285
+        return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
286
+    }
287
+
288
+    /**
289
+     * Get the entities referenced by the specified {@link WP_Post}.
290
+     *
291
+     * @since 3.15.0
292
+     *
293
+     * @param int         $subject_id The {@link WP_Post}'s id.
294
+     * @param string      $fields     The fields to return, 'ids' to only return ids or
295
+     *                                '*' to return all fields, by default '*'.
296
+     * @param null|string $predicate  The predicate (who|what|...), by default all.
297
+     * @param null|string $status     The status, by default all.
298
+     *
299
+     * @return array|object|null Database query results
300
+     */
301
+    public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) {
302
+        global $wpdb;
303
+
304
+        // The output fields.
305
+        $actual_fields = self::fields( $fields );
306
+
307
+        $sql = $wpdb->prepare(
308
+            "
309 309
 			SELECT p.$actual_fields
310 310
 			FROM {$this->relation_table} r
311 311
 			INNER JOIN $wpdb->posts p
312 312
 				ON p.id = r.object_id
313 313
 			"
314
-			// Add the status clause.
315
-			. self::and_status( $status )
316
-			. self::inner_join_is_not_article()
317
-			. " WHERE r.subject_id = %d "
318
-			. self::and_post_type_in()
319
-			. self::and_predicate( $predicate )
320
-			,
321
-			$subject_id
322
-		);
323
-
324
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
325
-	}
326
-
327
-	/**
328
-	 * Add the `post_status` clause.
329
-	 *
330
-	 * @since 3.15.0
331
-	 *
332
-	 * @param null|string|array $status The status values.
333
-	 *
334
-	 * @return string An empty string if $status is null, otherwise the status clause.
335
-	 */
336
-	private static function and_status( $status = null ) {
337
-
338
-		if ( null === $status ) {
339
-			return '';
340
-		}
341
-
342
-		return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')";
343
-	}
344
-
345
-	/**
346
-	 * Add the `predicate` clause.
347
-	 *
348
-	 * @since 3.15.0
349
-	 *
350
-	 * @param null|string|array $predicate An array of predicates.
351
-	 *
352
-	 * @return string An empty string if $predicate is null otherwise the predicate
353
-	 *                clause.
354
-	 */
355
-	private static function and_predicate( $predicate = null ) {
356
-
357
-		if ( null === $predicate ) {
358
-			return '';
359
-		}
360
-
361
-		return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')";
362
-	}
363
-
364
-	/**
365
-	 * The select fields.
366
-	 *
367
-	 * @since 3.15.0
368
-	 *
369
-	 * @param string $fields Either 'ids' or '*', by default '*'.
370
-	 *
371
-	 * @return string The `id` field if `ids` otherwise `*`.
372
-	 */
373
-	private static function fields( $fields = '*' ) {
374
-
375
-		// The output fields.
376
-		return 'ids' === $fields ? 'id' : '*';
377
-	}
378
-
379
-	/**
380
-	 * The inner join clause for articles.
381
-	 *
382
-	 * @since 3.15.0
383
-	 *
384
-	 * @return string The articles inner join clause.
385
-	 */
386
-	private static function inner_join_is_article() {
387
-		global $wpdb;
388
-
389
-		return $wpdb->prepare(
390
-			"
314
+            // Add the status clause.
315
+            . self::and_status( $status )
316
+            . self::inner_join_is_not_article()
317
+            . " WHERE r.subject_id = %d "
318
+            . self::and_post_type_in()
319
+            . self::and_predicate( $predicate )
320
+            ,
321
+            $subject_id
322
+        );
323
+
324
+        return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
325
+    }
326
+
327
+    /**
328
+     * Add the `post_status` clause.
329
+     *
330
+     * @since 3.15.0
331
+     *
332
+     * @param null|string|array $status The status values.
333
+     *
334
+     * @return string An empty string if $status is null, otherwise the status clause.
335
+     */
336
+    private static function and_status( $status = null ) {
337
+
338
+        if ( null === $status ) {
339
+            return '';
340
+        }
341
+
342
+        return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')";
343
+    }
344
+
345
+    /**
346
+     * Add the `predicate` clause.
347
+     *
348
+     * @since 3.15.0
349
+     *
350
+     * @param null|string|array $predicate An array of predicates.
351
+     *
352
+     * @return string An empty string if $predicate is null otherwise the predicate
353
+     *                clause.
354
+     */
355
+    private static function and_predicate( $predicate = null ) {
356
+
357
+        if ( null === $predicate ) {
358
+            return '';
359
+        }
360
+
361
+        return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')";
362
+    }
363
+
364
+    /**
365
+     * The select fields.
366
+     *
367
+     * @since 3.15.0
368
+     *
369
+     * @param string $fields Either 'ids' or '*', by default '*'.
370
+     *
371
+     * @return string The `id` field if `ids` otherwise `*`.
372
+     */
373
+    private static function fields( $fields = '*' ) {
374
+
375
+        // The output fields.
376
+        return 'ids' === $fields ? 'id' : '*';
377
+    }
378
+
379
+    /**
380
+     * The inner join clause for articles.
381
+     *
382
+     * @since 3.15.0
383
+     *
384
+     * @return string The articles inner join clause.
385
+     */
386
+    private static function inner_join_is_article() {
387
+        global $wpdb;
388
+
389
+        return $wpdb->prepare(
390
+            "
391 391
 			INNER JOIN $wpdb->term_relationships tr
392 392
 			 ON p.id = tr.object_id
393 393
 			INNER JOIN $wpdb->term_taxonomy tt
@@ -397,23 +397,23 @@  discard block
 block discarded – undo
397 397
 			 ON t.term_id = tt.term_id
398 398
 			  AND t.slug = %s
399 399
 			",
400
-			'wl_entity_type',
401
-			'article'
402
-		);
403
-	}
404
-
405
-	/**
406
-	 * The inner join clause for non-articles.
407
-	 *
408
-	 * @since 3.15.0
409
-	 *
410
-	 * @return string The non-articles inner join clause.
411
-	 */
412
-	private static function inner_join_is_not_article() {
413
-		global $wpdb;
414
-
415
-		return $wpdb->prepare(
416
-			"
400
+            'wl_entity_type',
401
+            'article'
402
+        );
403
+    }
404
+
405
+    /**
406
+     * The inner join clause for non-articles.
407
+     *
408
+     * @since 3.15.0
409
+     *
410
+     * @return string The non-articles inner join clause.
411
+     */
412
+    private static function inner_join_is_not_article() {
413
+        global $wpdb;
414
+
415
+        return $wpdb->prepare(
416
+            "
417 417
 			INNER JOIN $wpdb->term_relationships tr
418 418
 			 ON p.id = tr.object_id
419 419
 			INNER JOIN $wpdb->term_taxonomy tt
@@ -423,9 +423,9 @@  discard block
 block discarded – undo
423 423
 			 ON t.term_id = tt.term_id
424 424
 			  AND NOT t.slug = %s
425 425
 			",
426
-			'wl_entity_type',
427
-			'article'
428
-		);
429
-	}
426
+            'wl_entity_type',
427
+            'article'
428
+        );
429
+    }
430 430
 
431 431
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-configuration-service.php 2 patches
Indentation   +464 added lines, -464 removed lines patch added patch discarded remove patch
@@ -11,7 +11,7 @@  discard block
 block discarded – undo
11 11
  */
12 12
 
13 13
 if ( ! defined( 'ABSPATH' ) ) {
14
-	exit;
14
+    exit;
15 15
 }
16 16
 
17 17
 /**
@@ -21,468 +21,468 @@  discard block
 block discarded – undo
21 21
  */
22 22
 class Wordlift_Configuration_Service {
23 23
 
24
-	/**
25
-	 * The entity base path option name.
26
-	 *
27
-	 * @since 3.6.0
28
-	 */
29
-	const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
30
-
31
-	/**
32
-	 * The skip wizard (admin installation wizard) option name.
33
-	 *
34
-	 * @since 3.9.0
35
-	 */
36
-	const SKIP_WIZARD = 'wl_skip_wizard';
37
-
38
-	/**
39
-	 * WordLift's key option name.
40
-	 *
41
-	 * @since 3.9.0
42
-	 */
43
-	const KEY = 'key';
44
-
45
-	/**
46
-	 * WordLift's configured language option name.
47
-	 *
48
-	 * @since 3.9.0
49
-	 */
50
-	const LANGUAGE = 'site_language';
51
-
52
-	/**
53
-	 * The publisher entity post ID option name.
54
-	 *
55
-	 * @since 3.9.0
56
-	 */
57
-	const PUBLISHER_ID = 'publisher_id';
58
-
59
-	/**
60
-	 * The dataset URI option name
61
-	 *
62
-	 * @since 3.10.0
63
-	 */
64
-	const DATASET_URI = 'redlink_dataset_uri';
65
-
66
-	/**
67
-	 * The link by default option name.
68
-	 *
69
-	 * @since 3.11.0
70
-	 */
71
-	const LINK_BY_DEFAULT = 'link_by_default';
72
-
73
-	/**
74
-	 * The {@link Wordlift_Log_Service} instance.
75
-	 *
76
-	 * @since 3.16.0
77
-	 *
78
-	 * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
79
-	 */
80
-	private $log;
81
-
82
-	/**
83
-	 * The Wordlift_Configuration_Service's singleton instance.
84
-	 *
85
-	 * @since  3.6.0
86
-	 *
87
-	 * @access private
88
-	 * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
89
-	 */
90
-	private static $instance;
91
-
92
-	/**
93
-	 * Create a Wordlift_Configuration_Service's instance.
94
-	 *
95
-	 * @since 3.6.0
96
-	 */
97
-	public function __construct() {
98
-
99
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
100
-
101
-		self::$instance = $this;
102
-
103
-	}
104
-
105
-	/**
106
-	 * Get the singleton instance.
107
-	 *
108
-	 * @since 3.6.0
109
-	 *
110
-	 * @return \Wordlift_Configuration_Service
111
-	 */
112
-	public static function get_instance() {
113
-
114
-		return self::$instance;
115
-	}
116
-
117
-	/**
118
-	 * Get a configuration given the option name and a key. The option value is
119
-	 * expected to be an array.
120
-	 *
121
-	 * @since 3.6.0
122
-	 *
123
-	 * @param string $option  The option name.
124
-	 * @param string $key     A key in the option value array.
125
-	 * @param string $default The default value in case the key is not found (by default an empty string).
126
-	 *
127
-	 * @return mixed The configuration value or the default value if not found.
128
-	 */
129
-	private function get( $option, $key, $default = '' ) {
130
-
131
-		$options = get_option( $option, array() );
132
-
133
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
134
-	}
135
-
136
-	/**
137
-	 * Set a configuration parameter.
138
-	 *
139
-	 * @since 3.9.0
140
-	 *
141
-	 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
142
-	 * @param string $key    The value key.
143
-	 * @param mixed  $value  The value.
144
-	 */
145
-	private function set( $option, $key, $value ) {
146
-
147
-		$values         = get_option( $option );
148
-		$values         = isset( $values ) ? $values : array();
149
-		$values[ $key ] = $value;
150
-		update_option( $option, $values );
151
-
152
-	}
153
-
154
-	/**
155
-	 * Get the entity base path, by default 'entity'.
156
-	 *
157
-	 * @since 3.6.0
158
-	 *
159
-	 * @return string The entity base path.
160
-	 */
161
-	public function get_entity_base_path() {
162
-
163
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
164
-	}
165
-
166
-	/**
167
-	 * Get the entity base path.
168
-	 *
169
-	 * @since 3.9.0
170
-	 *
171
-	 * @param string $value The entity base path.
172
-	 */
173
-	public function set_entity_base_path( $value ) {
174
-
175
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
176
-
177
-	}
178
-
179
-	/**
180
-	 * Whether the installation skip wizard should be skipped.
181
-	 *
182
-	 * @since 3.9.0
183
-	 *
184
-	 * @return bool True if it should be skipped otherwise false.
185
-	 */
186
-	public function is_skip_wizard() {
187
-
188
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
189
-	}
190
-
191
-	/**
192
-	 * Set the skip wizard parameter.
193
-	 *
194
-	 * @since 3.9.0
195
-	 *
196
-	 * @param bool $value True to skip the wizard. We expect a boolean value.
197
-	 */
198
-	public function set_skip_wizard( $value ) {
199
-
200
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
201
-
202
-	}
203
-
204
-	/**
205
-	 * Get WordLift's key.
206
-	 *
207
-	 * @since 3.9.0
208
-	 *
209
-	 * @return string WordLift's key or an empty string if not set.
210
-	 */
211
-	public function get_key() {
212
-
213
-		return $this->get( 'wl_general_settings', self::KEY, '' );
214
-	}
215
-
216
-	/**
217
-	 * Set WordLift's key.
218
-	 *
219
-	 * @since 3.9.0
220
-	 *
221
-	 * @param string $value WordLift's key.
222
-	 */
223
-	public function set_key( $value ) {
224
-
225
-		$this->set( 'wl_general_settings', self::KEY, $value );
226
-	}
227
-
228
-	/**
229
-	 * Get WordLift's configured language, by default 'en'.
230
-	 *
231
-	 * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
232
-	 *
233
-	 * @since 3.9.0
234
-	 *
235
-	 * @return string WordLift's configured language code ('en' by default).
236
-	 */
237
-	public function get_language_code() {
238
-
239
-		return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' );
240
-	}
241
-
242
-	/**
243
-	 * Set WordLift's language code, used when storing strings to the Linked Data dataset.
244
-	 *
245
-	 * @since 3.9.0
246
-	 *
247
-	 * @param string $value WordLift's language code.
248
-	 */
249
-	public function set_language_code( $value ) {
250
-
251
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
252
-
253
-	}
254
-
255
-	/**
256
-	 * Get the publisher entity post id.
257
-	 *
258
-	 * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
259
-	 * Article markup.
260
-	 *
261
-	 * @since 3.9.0
262
-	 *
263
-	 * @return int|NULL The publisher entity post id or NULL if not set.
264
-	 */
265
-	public function get_publisher_id() {
266
-
267
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
268
-	}
269
-
270
-	/**
271
-	 * Set the publisher entity post id.
272
-	 *
273
-	 * @since 3.9.0
274
-	 *
275
-	 * @param int $value The publisher entity post id.
276
-	 */
277
-	public function set_publisher_id( $value ) {
278
-
279
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
280
-
281
-	}
282
-
283
-	/**
284
-	 * Get the dataset URI.
285
-	 *
286
-	 * @since 3.10.0
287
-	 *
288
-	 * @return string The dataset URI or an empty string if not set.
289
-	 */
290
-	public function get_dataset_uri() {
291
-
292
-		return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
293
-	}
294
-
295
-	/**
296
-	 * Set the dataset URI.
297
-	 *
298
-	 * @since 3.10.0
299
-	 *
300
-	 * @param string $value The dataset URI.
301
-	 */
302
-	public function set_dataset_uri( $value ) {
303
-
304
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
305
-	}
306
-
307
-	/**
308
-	 * Intercept the change of the WordLift key in order to set the dataset URI.
309
-	 *
310
-	 * @since 3.11.0
311
-	 *
312
-	 * @param array $old_value The old settings.
313
-	 * @param array $new_value The new settings.
314
-	 */
315
-	public function update_key( $old_value, $new_value ) {
316
-
317
-		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
318
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
319
-		$new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
320
-
321
-		// If the key hasn't changed, don't do anything.
322
-		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
323
-		if ( $old_key === $new_key ) {
324
-			return;
325
-		}
326
-
327
-		// If the key is empty, empty the dataset URI.
328
-		if ( '' === $new_key ) {
329
-			$this->set_dataset_uri( '' );
330
-		}
331
-
332
-		// make the request to the remote server.
333
-		$this->get_remote_dataset_uri( $new_key );
334
-	}
335
-
336
-	/**
337
-	 * Handle retrieving the dataset uri from the remote server.
338
-	 *
339
-	 * If a valid dataset uri is returned it is stored in the appropriate option,
340
-	 * otherwise the option is set to empty string.
341
-	 *
342
-	 * @since 3.17.0 send the site URL and get the dataset URI.
343
-	 * @since 3.12.0
344
-	 *
345
-	 * @param string $key The key to be used.
346
-	 */
347
-	public function get_remote_dataset_uri( $key ) {
348
-
349
-		$this->log->trace( 'Getting the remote dataset URI...' );
350
-
351
-		// Build the URL.
352
-		$url = $this->get_accounts()
353
-			   . '?key=' . rawurlencode( $key )
354
-			   . '&url=' . rawurlencode( site_url() );
355
-
356
-		$args     = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array(
357
-			'method' => 'PUT',
358
-		) );
359
-		$response = wp_remote_request( $url, $args );
360
-
361
-		// Request the dataset URI.
362
-		// $response = wp_remote_get( $this->get_accounts_by_key_dataset_uri( $key ), unserialize( WL_REDLINK_API_HTTP_OPTIONS ) );
363
-
364
-		// If the response is valid, then set the value.
365
-		if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) {
366
-
367
-			$json        = json_decode( $response['body'] );
368
-			$dataset_uri = $json->datasetURI;
369
-
370
-			$this->log->info( "Setting the dataset URI to $dataset_uri..." );
371
-
372
-			$this->set_dataset_uri( $dataset_uri );
373
-		} else {
374
-			$this->set_dataset_uri( '' );
375
-		}
376
-	}
377
-
378
-	/**
379
-	 * Handle the edge case where a user submits the same key again
380
-	 * when he does not have the dataset uri to regain it.
381
-	 *
382
-	 * This can not be handled in the normal option update hook because
383
-	 * it is not being triggered when the save value equals to the one already
384
-	 * in the DB.
385
-	 *
386
-	 * @since 3.12.0
387
-	 *
388
-	 * @param mixed $value     The new, unserialized option value.
389
-	 * @param mixed $old_value The old option value.
390
-	 *
391
-	 * @return mixed The same value in the $value parameter
392
-	 */
393
-	function maybe_update_dataset_uri( $value, $old_value ) {
394
-
395
-		// Check the old key value and the new one. Here we're only handling the
396
-		// case where the key hasn't changed and the dataset URI isn't set. The
397
-		// other case, i.e. a new key is inserted, is handled at `update_key`.
398
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
399
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
400
-
401
-		$dataset_uri = $this->get_dataset_uri();
402
-
403
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
404
-
405
-			// make the request to the remote server to try to get the dataset uri.
406
-			$this->get_remote_dataset_uri( $new_key );
407
-		}
408
-
409
-		return $value;
410
-	}
411
-
412
-	/**
413
-	 * Get the API URI to retrieve the dataset URI using the WordLift Key.
414
-	 *
415
-	 * @since 3.11.0
416
-	 *
417
-	 * @param string $key The WordLift key to use.
418
-	 *
419
-	 * @return string The API URI.
420
-	 */
421
-	public function get_accounts_by_key_dataset_uri( $key ) {
422
-
423
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
424
-	}
425
-
426
-	/**
427
-	 * Get the `accounts` end point.
428
-	 *
429
-	 * @since 3.16.0
430
-	 *
431
-	 * @return string The `accounts` end point.
432
-	 */
433
-	public function get_accounts() {
434
-
435
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
436
-	}
437
-
438
-	/**
439
-	 * Get the `link by default` option.
440
-	 *
441
-	 * @since 3.13.0
442
-	 *
443
-	 * @return bool True if entities must be linked by default otherwise false.
444
-	 */
445
-	public function is_link_by_default() {
446
-
447
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
448
-	}
449
-
450
-	/**
451
-	 * Set the `link by default` option.
452
-	 *
453
-	 * @since 3.13.0
454
-	 *
455
-	 * @param bool $value True to enabling linking by default, otherwise false.
456
-	 */
457
-	public function set_link_by_default( $value ) {
458
-
459
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
460
-	}
461
-
462
-	/**
463
-	 * Get the URL to perform batch analyses.
464
-	 *
465
-	 * @since 3.14.0
466
-	 *
467
-	 * @return string The URL to call to perform the batch analyzes.
468
-	 */
469
-	public function get_batch_analysis_url() {
470
-
471
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'batch-analyses';
472
-
473
-	}
474
-
475
-	/**
476
-	 * Get the URL to perform autocomplete request.
477
-	 *
478
-	 * @since 3.15.0
479
-	 *
480
-	 * @return string The URL to call to perform the batch analyzes.
481
-	 */
482
-	public function get_autocomplete_url() {
483
-
484
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
485
-
486
-	}
24
+    /**
25
+     * The entity base path option name.
26
+     *
27
+     * @since 3.6.0
28
+     */
29
+    const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
30
+
31
+    /**
32
+     * The skip wizard (admin installation wizard) option name.
33
+     *
34
+     * @since 3.9.0
35
+     */
36
+    const SKIP_WIZARD = 'wl_skip_wizard';
37
+
38
+    /**
39
+     * WordLift's key option name.
40
+     *
41
+     * @since 3.9.0
42
+     */
43
+    const KEY = 'key';
44
+
45
+    /**
46
+     * WordLift's configured language option name.
47
+     *
48
+     * @since 3.9.0
49
+     */
50
+    const LANGUAGE = 'site_language';
51
+
52
+    /**
53
+     * The publisher entity post ID option name.
54
+     *
55
+     * @since 3.9.0
56
+     */
57
+    const PUBLISHER_ID = 'publisher_id';
58
+
59
+    /**
60
+     * The dataset URI option name
61
+     *
62
+     * @since 3.10.0
63
+     */
64
+    const DATASET_URI = 'redlink_dataset_uri';
65
+
66
+    /**
67
+     * The link by default option name.
68
+     *
69
+     * @since 3.11.0
70
+     */
71
+    const LINK_BY_DEFAULT = 'link_by_default';
72
+
73
+    /**
74
+     * The {@link Wordlift_Log_Service} instance.
75
+     *
76
+     * @since 3.16.0
77
+     *
78
+     * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
79
+     */
80
+    private $log;
81
+
82
+    /**
83
+     * The Wordlift_Configuration_Service's singleton instance.
84
+     *
85
+     * @since  3.6.0
86
+     *
87
+     * @access private
88
+     * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
89
+     */
90
+    private static $instance;
91
+
92
+    /**
93
+     * Create a Wordlift_Configuration_Service's instance.
94
+     *
95
+     * @since 3.6.0
96
+     */
97
+    public function __construct() {
98
+
99
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
100
+
101
+        self::$instance = $this;
102
+
103
+    }
104
+
105
+    /**
106
+     * Get the singleton instance.
107
+     *
108
+     * @since 3.6.0
109
+     *
110
+     * @return \Wordlift_Configuration_Service
111
+     */
112
+    public static function get_instance() {
113
+
114
+        return self::$instance;
115
+    }
116
+
117
+    /**
118
+     * Get a configuration given the option name and a key. The option value is
119
+     * expected to be an array.
120
+     *
121
+     * @since 3.6.0
122
+     *
123
+     * @param string $option  The option name.
124
+     * @param string $key     A key in the option value array.
125
+     * @param string $default The default value in case the key is not found (by default an empty string).
126
+     *
127
+     * @return mixed The configuration value or the default value if not found.
128
+     */
129
+    private function get( $option, $key, $default = '' ) {
130
+
131
+        $options = get_option( $option, array() );
132
+
133
+        return isset( $options[ $key ] ) ? $options[ $key ] : $default;
134
+    }
135
+
136
+    /**
137
+     * Set a configuration parameter.
138
+     *
139
+     * @since 3.9.0
140
+     *
141
+     * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
142
+     * @param string $key    The value key.
143
+     * @param mixed  $value  The value.
144
+     */
145
+    private function set( $option, $key, $value ) {
146
+
147
+        $values         = get_option( $option );
148
+        $values         = isset( $values ) ? $values : array();
149
+        $values[ $key ] = $value;
150
+        update_option( $option, $values );
151
+
152
+    }
153
+
154
+    /**
155
+     * Get the entity base path, by default 'entity'.
156
+     *
157
+     * @since 3.6.0
158
+     *
159
+     * @return string The entity base path.
160
+     */
161
+    public function get_entity_base_path() {
162
+
163
+        return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
164
+    }
165
+
166
+    /**
167
+     * Get the entity base path.
168
+     *
169
+     * @since 3.9.0
170
+     *
171
+     * @param string $value The entity base path.
172
+     */
173
+    public function set_entity_base_path( $value ) {
174
+
175
+        $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
176
+
177
+    }
178
+
179
+    /**
180
+     * Whether the installation skip wizard should be skipped.
181
+     *
182
+     * @since 3.9.0
183
+     *
184
+     * @return bool True if it should be skipped otherwise false.
185
+     */
186
+    public function is_skip_wizard() {
187
+
188
+        return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
189
+    }
190
+
191
+    /**
192
+     * Set the skip wizard parameter.
193
+     *
194
+     * @since 3.9.0
195
+     *
196
+     * @param bool $value True to skip the wizard. We expect a boolean value.
197
+     */
198
+    public function set_skip_wizard( $value ) {
199
+
200
+        $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
201
+
202
+    }
203
+
204
+    /**
205
+     * Get WordLift's key.
206
+     *
207
+     * @since 3.9.0
208
+     *
209
+     * @return string WordLift's key or an empty string if not set.
210
+     */
211
+    public function get_key() {
212
+
213
+        return $this->get( 'wl_general_settings', self::KEY, '' );
214
+    }
215
+
216
+    /**
217
+     * Set WordLift's key.
218
+     *
219
+     * @since 3.9.0
220
+     *
221
+     * @param string $value WordLift's key.
222
+     */
223
+    public function set_key( $value ) {
224
+
225
+        $this->set( 'wl_general_settings', self::KEY, $value );
226
+    }
227
+
228
+    /**
229
+     * Get WordLift's configured language, by default 'en'.
230
+     *
231
+     * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
232
+     *
233
+     * @since 3.9.0
234
+     *
235
+     * @return string WordLift's configured language code ('en' by default).
236
+     */
237
+    public function get_language_code() {
238
+
239
+        return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' );
240
+    }
241
+
242
+    /**
243
+     * Set WordLift's language code, used when storing strings to the Linked Data dataset.
244
+     *
245
+     * @since 3.9.0
246
+     *
247
+     * @param string $value WordLift's language code.
248
+     */
249
+    public function set_language_code( $value ) {
250
+
251
+        $this->set( 'wl_general_settings', self::LANGUAGE, $value );
252
+
253
+    }
254
+
255
+    /**
256
+     * Get the publisher entity post id.
257
+     *
258
+     * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
259
+     * Article markup.
260
+     *
261
+     * @since 3.9.0
262
+     *
263
+     * @return int|NULL The publisher entity post id or NULL if not set.
264
+     */
265
+    public function get_publisher_id() {
266
+
267
+        return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
268
+    }
269
+
270
+    /**
271
+     * Set the publisher entity post id.
272
+     *
273
+     * @since 3.9.0
274
+     *
275
+     * @param int $value The publisher entity post id.
276
+     */
277
+    public function set_publisher_id( $value ) {
278
+
279
+        $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
280
+
281
+    }
282
+
283
+    /**
284
+     * Get the dataset URI.
285
+     *
286
+     * @since 3.10.0
287
+     *
288
+     * @return string The dataset URI or an empty string if not set.
289
+     */
290
+    public function get_dataset_uri() {
291
+
292
+        return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
293
+    }
294
+
295
+    /**
296
+     * Set the dataset URI.
297
+     *
298
+     * @since 3.10.0
299
+     *
300
+     * @param string $value The dataset URI.
301
+     */
302
+    public function set_dataset_uri( $value ) {
303
+
304
+        $this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
305
+    }
306
+
307
+    /**
308
+     * Intercept the change of the WordLift key in order to set the dataset URI.
309
+     *
310
+     * @since 3.11.0
311
+     *
312
+     * @param array $old_value The old settings.
313
+     * @param array $new_value The new settings.
314
+     */
315
+    public function update_key( $old_value, $new_value ) {
316
+
317
+        // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
318
+        $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
319
+        $new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
320
+
321
+        // If the key hasn't changed, don't do anything.
322
+        // WARN The 'update_option' hook is fired only if the new and old value are not equal.
323
+        if ( $old_key === $new_key ) {
324
+            return;
325
+        }
326
+
327
+        // If the key is empty, empty the dataset URI.
328
+        if ( '' === $new_key ) {
329
+            $this->set_dataset_uri( '' );
330
+        }
331
+
332
+        // make the request to the remote server.
333
+        $this->get_remote_dataset_uri( $new_key );
334
+    }
335
+
336
+    /**
337
+     * Handle retrieving the dataset uri from the remote server.
338
+     *
339
+     * If a valid dataset uri is returned it is stored in the appropriate option,
340
+     * otherwise the option is set to empty string.
341
+     *
342
+     * @since 3.17.0 send the site URL and get the dataset URI.
343
+     * @since 3.12.0
344
+     *
345
+     * @param string $key The key to be used.
346
+     */
347
+    public function get_remote_dataset_uri( $key ) {
348
+
349
+        $this->log->trace( 'Getting the remote dataset URI...' );
350
+
351
+        // Build the URL.
352
+        $url = $this->get_accounts()
353
+                . '?key=' . rawurlencode( $key )
354
+                . '&url=' . rawurlencode( site_url() );
355
+
356
+        $args     = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array(
357
+            'method' => 'PUT',
358
+        ) );
359
+        $response = wp_remote_request( $url, $args );
360
+
361
+        // Request the dataset URI.
362
+        // $response = wp_remote_get( $this->get_accounts_by_key_dataset_uri( $key ), unserialize( WL_REDLINK_API_HTTP_OPTIONS ) );
363
+
364
+        // If the response is valid, then set the value.
365
+        if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) {
366
+
367
+            $json        = json_decode( $response['body'] );
368
+            $dataset_uri = $json->datasetURI;
369
+
370
+            $this->log->info( "Setting the dataset URI to $dataset_uri..." );
371
+
372
+            $this->set_dataset_uri( $dataset_uri );
373
+        } else {
374
+            $this->set_dataset_uri( '' );
375
+        }
376
+    }
377
+
378
+    /**
379
+     * Handle the edge case where a user submits the same key again
380
+     * when he does not have the dataset uri to regain it.
381
+     *
382
+     * This can not be handled in the normal option update hook because
383
+     * it is not being triggered when the save value equals to the one already
384
+     * in the DB.
385
+     *
386
+     * @since 3.12.0
387
+     *
388
+     * @param mixed $value     The new, unserialized option value.
389
+     * @param mixed $old_value The old option value.
390
+     *
391
+     * @return mixed The same value in the $value parameter
392
+     */
393
+    function maybe_update_dataset_uri( $value, $old_value ) {
394
+
395
+        // Check the old key value and the new one. Here we're only handling the
396
+        // case where the key hasn't changed and the dataset URI isn't set. The
397
+        // other case, i.e. a new key is inserted, is handled at `update_key`.
398
+        $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
399
+        $new_key = isset( $value['key'] ) ? $value['key'] : '';
400
+
401
+        $dataset_uri = $this->get_dataset_uri();
402
+
403
+        if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
404
+
405
+            // make the request to the remote server to try to get the dataset uri.
406
+            $this->get_remote_dataset_uri( $new_key );
407
+        }
408
+
409
+        return $value;
410
+    }
411
+
412
+    /**
413
+     * Get the API URI to retrieve the dataset URI using the WordLift Key.
414
+     *
415
+     * @since 3.11.0
416
+     *
417
+     * @param string $key The WordLift key to use.
418
+     *
419
+     * @return string The API URI.
420
+     */
421
+    public function get_accounts_by_key_dataset_uri( $key ) {
422
+
423
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
424
+    }
425
+
426
+    /**
427
+     * Get the `accounts` end point.
428
+     *
429
+     * @since 3.16.0
430
+     *
431
+     * @return string The `accounts` end point.
432
+     */
433
+    public function get_accounts() {
434
+
435
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
436
+    }
437
+
438
+    /**
439
+     * Get the `link by default` option.
440
+     *
441
+     * @since 3.13.0
442
+     *
443
+     * @return bool True if entities must be linked by default otherwise false.
444
+     */
445
+    public function is_link_by_default() {
446
+
447
+        return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
448
+    }
449
+
450
+    /**
451
+     * Set the `link by default` option.
452
+     *
453
+     * @since 3.13.0
454
+     *
455
+     * @param bool $value True to enabling linking by default, otherwise false.
456
+     */
457
+    public function set_link_by_default( $value ) {
458
+
459
+        $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
460
+    }
461
+
462
+    /**
463
+     * Get the URL to perform batch analyses.
464
+     *
465
+     * @since 3.14.0
466
+     *
467
+     * @return string The URL to call to perform the batch analyzes.
468
+     */
469
+    public function get_batch_analysis_url() {
470
+
471
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'batch-analyses';
472
+
473
+    }
474
+
475
+    /**
476
+     * Get the URL to perform autocomplete request.
477
+     *
478
+     * @since 3.15.0
479
+     *
480
+     * @return string The URL to call to perform the batch analyzes.
481
+     */
482
+    public function get_autocomplete_url() {
483
+
484
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
485
+
486
+    }
487 487
 
488 488
 }
Please login to merge, or discard this patch.
Spacing   +60 added lines, -60 removed lines patch added patch discarded remove patch
@@ -10,7 +10,7 @@  discard block
 block discarded – undo
10 10
  * @since      3.6.0
11 11
  */
12 12
 
13
-if ( ! defined( 'ABSPATH' ) ) {
13
+if ( ! defined('ABSPATH')) {
14 14
 	exit;
15 15
 }
16 16
 
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
 	 */
97 97
 	public function __construct() {
98 98
 
99
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
99
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
100 100
 
101 101
 		self::$instance = $this;
102 102
 
@@ -126,11 +126,11 @@  discard block
 block discarded – undo
126 126
 	 *
127 127
 	 * @return mixed The configuration value or the default value if not found.
128 128
 	 */
129
-	private function get( $option, $key, $default = '' ) {
129
+	private function get($option, $key, $default = '') {
130 130
 
131
-		$options = get_option( $option, array() );
131
+		$options = get_option($option, array());
132 132
 
133
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
133
+		return isset($options[$key]) ? $options[$key] : $default;
134 134
 	}
135 135
 
136 136
 	/**
@@ -142,12 +142,12 @@  discard block
 block discarded – undo
142 142
 	 * @param string $key    The value key.
143 143
 	 * @param mixed  $value  The value.
144 144
 	 */
145
-	private function set( $option, $key, $value ) {
145
+	private function set($option, $key, $value) {
146 146
 
147
-		$values         = get_option( $option );
148
-		$values         = isset( $values ) ? $values : array();
149
-		$values[ $key ] = $value;
150
-		update_option( $option, $values );
147
+		$values         = get_option($option);
148
+		$values         = isset($values) ? $values : array();
149
+		$values[$key] = $value;
150
+		update_option($option, $values);
151 151
 
152 152
 	}
153 153
 
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
 	 */
161 161
 	public function get_entity_base_path() {
162 162
 
163
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
163
+		return $this->get('wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity');
164 164
 	}
165 165
 
166 166
 	/**
@@ -170,9 +170,9 @@  discard block
 block discarded – undo
170 170
 	 *
171 171
 	 * @param string $value The entity base path.
172 172
 	 */
173
-	public function set_entity_base_path( $value ) {
173
+	public function set_entity_base_path($value) {
174 174
 
175
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
175
+		$this->set('wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value);
176 176
 
177 177
 	}
178 178
 
@@ -185,7 +185,7 @@  discard block
 block discarded – undo
185 185
 	 */
186 186
 	public function is_skip_wizard() {
187 187
 
188
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
188
+		return $this->get('wl_general_settings', self::SKIP_WIZARD, false);
189 189
 	}
190 190
 
191 191
 	/**
@@ -195,9 +195,9 @@  discard block
 block discarded – undo
195 195
 	 *
196 196
 	 * @param bool $value True to skip the wizard. We expect a boolean value.
197 197
 	 */
198
-	public function set_skip_wizard( $value ) {
198
+	public function set_skip_wizard($value) {
199 199
 
200
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
200
+		$this->set('wl_general_settings', self::SKIP_WIZARD, true === $value);
201 201
 
202 202
 	}
203 203
 
@@ -210,7 +210,7 @@  discard block
 block discarded – undo
210 210
 	 */
211 211
 	public function get_key() {
212 212
 
213
-		return $this->get( 'wl_general_settings', self::KEY, '' );
213
+		return $this->get('wl_general_settings', self::KEY, '');
214 214
 	}
215 215
 
216 216
 	/**
@@ -220,9 +220,9 @@  discard block
 block discarded – undo
220 220
 	 *
221 221
 	 * @param string $value WordLift's key.
222 222
 	 */
223
-	public function set_key( $value ) {
223
+	public function set_key($value) {
224 224
 
225
-		$this->set( 'wl_general_settings', self::KEY, $value );
225
+		$this->set('wl_general_settings', self::KEY, $value);
226 226
 	}
227 227
 
228 228
 	/**
@@ -236,7 +236,7 @@  discard block
 block discarded – undo
236 236
 	 */
237 237
 	public function get_language_code() {
238 238
 
239
-		return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' );
239
+		return $this->get('wl_general_settings', self::LANGUAGE, 'en');
240 240
 	}
241 241
 
242 242
 	/**
@@ -246,9 +246,9 @@  discard block
 block discarded – undo
246 246
 	 *
247 247
 	 * @param string $value WordLift's language code.
248 248
 	 */
249
-	public function set_language_code( $value ) {
249
+	public function set_language_code($value) {
250 250
 
251
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
251
+		$this->set('wl_general_settings', self::LANGUAGE, $value);
252 252
 
253 253
 	}
254 254
 
@@ -264,7 +264,7 @@  discard block
 block discarded – undo
264 264
 	 */
265 265
 	public function get_publisher_id() {
266 266
 
267
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
267
+		return $this->get('wl_general_settings', self::PUBLISHER_ID, null);
268 268
 	}
269 269
 
270 270
 	/**
@@ -274,9 +274,9 @@  discard block
 block discarded – undo
274 274
 	 *
275 275
 	 * @param int $value The publisher entity post id.
276 276
 	 */
277
-	public function set_publisher_id( $value ) {
277
+	public function set_publisher_id($value) {
278 278
 
279
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
279
+		$this->set('wl_general_settings', self::PUBLISHER_ID, $value);
280 280
 
281 281
 	}
282 282
 
@@ -289,7 +289,7 @@  discard block
 block discarded – undo
289 289
 	 */
290 290
 	public function get_dataset_uri() {
291 291
 
292
-		return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
292
+		return $this->get('wl_advanced_settings', self::DATASET_URI, null);
293 293
 	}
294 294
 
295 295
 	/**
@@ -299,9 +299,9 @@  discard block
 block discarded – undo
299 299
 	 *
300 300
 	 * @param string $value The dataset URI.
301 301
 	 */
302
-	public function set_dataset_uri( $value ) {
302
+	public function set_dataset_uri($value) {
303 303
 
304
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
304
+		$this->set('wl_advanced_settings', self::DATASET_URI, $value);
305 305
 	}
306 306
 
307 307
 	/**
@@ -312,25 +312,25 @@  discard block
 block discarded – undo
312 312
 	 * @param array $old_value The old settings.
313 313
 	 * @param array $new_value The new settings.
314 314
 	 */
315
-	public function update_key( $old_value, $new_value ) {
315
+	public function update_key($old_value, $new_value) {
316 316
 
317 317
 		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
318
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
319
-		$new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
318
+		$old_key = isset($old_value['key']) ? $old_value['key'] : '';
319
+		$new_key = isset($new_value['key']) ? $new_value['key'] : '';
320 320
 
321 321
 		// If the key hasn't changed, don't do anything.
322 322
 		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
323
-		if ( $old_key === $new_key ) {
323
+		if ($old_key === $new_key) {
324 324
 			return;
325 325
 		}
326 326
 
327 327
 		// If the key is empty, empty the dataset URI.
328
-		if ( '' === $new_key ) {
329
-			$this->set_dataset_uri( '' );
328
+		if ('' === $new_key) {
329
+			$this->set_dataset_uri('');
330 330
 		}
331 331
 
332 332
 		// make the request to the remote server.
333
-		$this->get_remote_dataset_uri( $new_key );
333
+		$this->get_remote_dataset_uri($new_key);
334 334
 	}
335 335
 
336 336
 	/**
@@ -344,34 +344,34 @@  discard block
 block discarded – undo
344 344
 	 *
345 345
 	 * @param string $key The key to be used.
346 346
 	 */
347
-	public function get_remote_dataset_uri( $key ) {
347
+	public function get_remote_dataset_uri($key) {
348 348
 
349
-		$this->log->trace( 'Getting the remote dataset URI...' );
349
+		$this->log->trace('Getting the remote dataset URI...');
350 350
 
351 351
 		// Build the URL.
352 352
 		$url = $this->get_accounts()
353
-			   . '?key=' . rawurlencode( $key )
354
-			   . '&url=' . rawurlencode( site_url() );
353
+			   . '?key='.rawurlencode($key)
354
+			   . '&url='.rawurlencode(site_url());
355 355
 
356
-		$args     = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array(
356
+		$args     = wp_parse_args(unserialize(WL_REDLINK_API_HTTP_OPTIONS), array(
357 357
 			'method' => 'PUT',
358
-		) );
359
-		$response = wp_remote_request( $url, $args );
358
+		));
359
+		$response = wp_remote_request($url, $args);
360 360
 
361 361
 		// Request the dataset URI.
362 362
 		// $response = wp_remote_get( $this->get_accounts_by_key_dataset_uri( $key ), unserialize( WL_REDLINK_API_HTTP_OPTIONS ) );
363 363
 
364 364
 		// If the response is valid, then set the value.
365
-		if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) {
365
+		if ( ! is_wp_error($response) && 200 === (int) $response['response']['code']) {
366 366
 
367
-			$json        = json_decode( $response['body'] );
367
+			$json        = json_decode($response['body']);
368 368
 			$dataset_uri = $json->datasetURI;
369 369
 
370
-			$this->log->info( "Setting the dataset URI to $dataset_uri..." );
370
+			$this->log->info("Setting the dataset URI to $dataset_uri...");
371 371
 
372
-			$this->set_dataset_uri( $dataset_uri );
372
+			$this->set_dataset_uri($dataset_uri);
373 373
 		} else {
374
-			$this->set_dataset_uri( '' );
374
+			$this->set_dataset_uri('');
375 375
 		}
376 376
 	}
377 377
 
@@ -390,20 +390,20 @@  discard block
 block discarded – undo
390 390
 	 *
391 391
 	 * @return mixed The same value in the $value parameter
392 392
 	 */
393
-	function maybe_update_dataset_uri( $value, $old_value ) {
393
+	function maybe_update_dataset_uri($value, $old_value) {
394 394
 
395 395
 		// Check the old key value and the new one. Here we're only handling the
396 396
 		// case where the key hasn't changed and the dataset URI isn't set. The
397 397
 		// other case, i.e. a new key is inserted, is handled at `update_key`.
398
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
399
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
398
+		$old_key = isset($old_value['key']) ? $old_value['key'] : '';
399
+		$new_key = isset($value['key']) ? $value['key'] : '';
400 400
 
401 401
 		$dataset_uri = $this->get_dataset_uri();
402 402
 
403
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
403
+		if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) {
404 404
 
405 405
 			// make the request to the remote server to try to get the dataset uri.
406
-			$this->get_remote_dataset_uri( $new_key );
406
+			$this->get_remote_dataset_uri($new_key);
407 407
 		}
408 408
 
409 409
 		return $value;
@@ -418,9 +418,9 @@  discard block
 block discarded – undo
418 418
 	 *
419 419
 	 * @return string The API URI.
420 420
 	 */
421
-	public function get_accounts_by_key_dataset_uri( $key ) {
421
+	public function get_accounts_by_key_dataset_uri($key) {
422 422
 
423
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
423
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri";
424 424
 	}
425 425
 
426 426
 	/**
@@ -432,7 +432,7 @@  discard block
 block discarded – undo
432 432
 	 */
433 433
 	public function get_accounts() {
434 434
 
435
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
435
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts';
436 436
 	}
437 437
 
438 438
 	/**
@@ -444,7 +444,7 @@  discard block
 block discarded – undo
444 444
 	 */
445 445
 	public function is_link_by_default() {
446 446
 
447
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
447
+		return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes');
448 448
 	}
449 449
 
450 450
 	/**
@@ -454,9 +454,9 @@  discard block
 block discarded – undo
454 454
 	 *
455 455
 	 * @param bool $value True to enabling linking by default, otherwise false.
456 456
 	 */
457
-	public function set_link_by_default( $value ) {
457
+	public function set_link_by_default($value) {
458 458
 
459
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
459
+		$this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no');
460 460
 	}
461 461
 
462 462
 	/**
@@ -468,7 +468,7 @@  discard block
 block discarded – undo
468 468
 	 */
469 469
 	public function get_batch_analysis_url() {
470 470
 
471
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'batch-analyses';
471
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'batch-analyses';
472 472
 
473 473
 	}
474 474
 
@@ -481,7 +481,7 @@  discard block
 block discarded – undo
481 481
 	 */
482 482
 	public function get_autocomplete_url() {
483 483
 
484
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
484
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete';
485 485
 
486 486
 	}
487 487
 
Please login to merge, or discard this patch.