Completed
Push — master ( 289c58...3674d5 )
by David
16:00
created
src/includes/class-wordlift-relation-service.php 2 patches
Indentation   +391 added lines, -391 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,29 +423,29 @@  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
-	}
430
-
431
-	/**
432
-	 * Find all the subject IDs and their referenced/related object IDs. The
433
-	 * object IDs are returned as comma separated IDs in the `object_ids` key.
434
-	 *
435
-	 * @since 3.18.0
436
-	 * @return mixed Database query results
437
-	 */
438
-	public function find_all_grouped_by_subject_id() {
439
-		global $wpdb;
440
-
441
-		return $wpdb->get_results(
442
-			"
426
+            'wl_entity_type',
427
+            'article'
428
+        );
429
+    }
430
+
431
+    /**
432
+     * Find all the subject IDs and their referenced/related object IDs. The
433
+     * object IDs are returned as comma separated IDs in the `object_ids` key.
434
+     *
435
+     * @since 3.18.0
436
+     * @return mixed Database query results
437
+     */
438
+    public function find_all_grouped_by_subject_id() {
439
+        global $wpdb;
440
+
441
+        return $wpdb->get_results(
442
+            "
443 443
 			SELECT subject_id, GROUP_CONCAT( DISTINCT object_id ORDER BY object_id SEPARATOR ',' ) AS object_ids
444 444
 			FROM $this->relation_table
445 445
 			GROUP BY subject_id
446 446
 			"
447
-		);
447
+        );
448 448
 
449
-	}
449
+    }
450 450
 
451 451
 }
Please login to merge, or discard this patch.
Spacing   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
 	 */
53 53
 	public function __construct() {
54 54
 
55
-		self::$log = Wordlift_Log_Service::get_logger( get_class() );
55
+		self::$log = Wordlift_Log_Service::get_logger(get_class());
56 56
 
57 57
 		global $wpdb;
58 58
 
@@ -93,24 +93,24 @@  discard block
 block discarded – undo
93 93
 	 *
94 94
 	 * @return array|object|null Database query results
95 95
 	 */
96
-	public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null ) {
96
+	public function get_article_subjects($object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null) {
97 97
 		global $wpdb;
98 98
 
99 99
 		// The output fields.
100
-		$actual_fields = self::fields( $fields );
100
+		$actual_fields = self::fields($fields);
101 101
 
102
-		self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' );
102
+		self::$log->trace('Getting article subjects for object '.implode(', ', (array) $object_id).'...');
103 103
 
104
-		$objects = $this->article_id_to_entity_id( $object_id );
104
+		$objects = $this->article_id_to_entity_id($object_id);
105 105
 
106 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 ) . '.' );
107
+		if (empty($objects)) {
108
+			self::$log->debug('No entities found for object '.implode(', ', (array) $object_id).'.');
109 109
 
110 110
 			return array();
111 111
 		}
112 112
 
113
-		self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' );
113
+		self::$log->debug(count($objects).' entity id(s) found for object '.implode(', ', (array) $object_id).'.');
114 114
 
115 115
 		$sql =
116 116
 			"
@@ -120,18 +120,18 @@  discard block
 block discarded – undo
120 120
 				ON p.id = r.subject_id
121 121
 			"
122 122
 			// Add the status clause.
123
-			. self::and_status( $status )
123
+			. self::and_status($status)
124 124
 			. self::inner_join_is_article()
125
-			. self::where_object_id( $objects )
125
+			. self::where_object_id($objects)
126 126
 			// Since `object_id` can be an article ID we need to exclude it from
127 127
 			// the results.
128
-			. self::and_article_not_in( array_merge( $excludes, (array) $object_id ) )
129
-			. self::and_article_in( $include )
128
+			. self::and_article_not_in(array_merge($excludes, (array) $object_id))
129
+			. self::and_article_in($include)
130 130
 			. self::and_post_type_in()
131
-			. self::and_predicate( $predicate )
132
-			. self::limit( $limit );
131
+			. self::and_predicate($predicate)
132
+			. self::limit($limit);
133 133
 
134
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
134
+		return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql);
135 135
 	}
136 136
 
137 137
 	/**
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
 		return " AND p.post_type IN ( '"
147 147
 			   . implode(
148 148
 				   "','",
149
-				   array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() )
149
+				   array_map('esc_sql', Wordlift_Entity_Service::valid_entity_post_types())
150 150
 			   )
151 151
 			   . "' )";
152 152
 	}
@@ -160,9 +160,9 @@  discard block
 block discarded – undo
160 160
 	 *
161 161
 	 * @return string The limit clause (empty if no limit has been specified).
162 162
 	 */
163
-	private static function limit( $limit = null ) {
163
+	private static function limit($limit = null) {
164 164
 
165
-		if ( null === $limit ) {
165
+		if (null === $limit) {
166 166
 			return '';
167 167
 		}
168 168
 
@@ -179,19 +179,19 @@  discard block
 block discarded – undo
179 179
 	 *
180 180
 	 * @return array An array of entities' ids.
181 181
 	 */
182
-	private function article_id_to_entity_id( $object_id ) {
182
+	private function article_id_to_entity_id($object_id) {
183 183
 
184 184
 		$entity_service = Wordlift_Entity_Service::get_instance();
185 185
 
186 186
 		$relation_service = $this;
187 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 );
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 191
 			}
192 192
 
193
-			return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) );
194
-		}, array() );
193
+			return array_merge($carry, $relation_service->get_objects($item, 'ids'));
194
+		}, array());
195 195
 
196 196
 	}
197 197
 
@@ -204,15 +204,15 @@  discard block
 block discarded – undo
204 204
 	 *
205 205
 	 * @return string The WHERE clause.
206 206
 	 */
207
-	private static function where_object_id( $object_id ) {
207
+	private static function where_object_id($object_id) {
208 208
 
209
-		if ( empty( $object_id ) ) {
209
+		if (empty($object_id)) {
210 210
 			// self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) );
211 211
 
212 212
 			return ' WHERE 1 = 1';
213 213
 		}
214 214
 
215
-		return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )';
215
+		return ' WHERE r.object_id IN ( '.implode(',', wp_parse_id_list((array) $object_id)).' )';
216 216
 	}
217 217
 
218 218
 	/**
@@ -224,9 +224,9 @@  discard block
 block discarded – undo
224 224
 	 *
225 225
 	 * @return string The exclude clause.
226 226
 	 */
227
-	private static function and_article_not_in( $exclude ) {
227
+	private static function and_article_not_in($exclude) {
228 228
 
229
-		return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )';
229
+		return ' AND NOT p.ID IN ( '.implode(',', wp_parse_id_list((array) $exclude)).' )';
230 230
 	}
231 231
 
232 232
 	/**
@@ -239,13 +239,13 @@  discard block
 block discarded – undo
239 239
 	 * @return string An empty string if $include is null otherwise the include
240 240
 	 *                clause.
241 241
 	 */
242
-	private static function and_article_in( $include = null ) {
242
+	private static function and_article_in($include = null) {
243 243
 
244
-		if ( null === $include ) {
244
+		if (null === $include) {
245 245
 			return '';
246 246
 		}
247 247
 
248
-		return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )';
248
+		return ' AND p.ID IN ( '.implode(',', wp_parse_id_list((array) $include)).' )';
249 249
 	}
250 250
 
251 251
 	/**
@@ -260,11 +260,11 @@  discard block
 block discarded – undo
260 260
 	 *
261 261
 	 * @return array|object|null Database query results
262 262
 	 */
263
-	public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) {
263
+	public function get_non_article_subjects($object_id, $fields = '*', $status = null) {
264 264
 		global $wpdb;
265 265
 
266 266
 		// The output fields.
267
-		$actual_fields = self::fields( $fields );
267
+		$actual_fields = self::fields($fields);
268 268
 
269 269
 		$sql = $wpdb->prepare(
270 270
 			"
@@ -274,7 +274,7 @@  discard block
 block discarded – undo
274 274
 				ON p.id = r.subject_id
275 275
 			"
276 276
 			// Add the status clause.
277
-			. self::and_status( $status )
277
+			. self::and_status($status)
278 278
 			. self::inner_join_is_not_article()
279 279
 			. " WHERE r.object_id = %d "
280 280
 			. self::and_post_type_in()
@@ -282,7 +282,7 @@  discard block
 block discarded – undo
282 282
 			$object_id
283 283
 		);
284 284
 
285
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
285
+		return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql);
286 286
 	}
287 287
 
288 288
 	/**
@@ -298,11 +298,11 @@  discard block
 block discarded – undo
298 298
 	 *
299 299
 	 * @return array|object|null Database query results
300 300
 	 */
301
-	public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) {
301
+	public function get_objects($subject_id, $fields = '*', $predicate = null, $status = null) {
302 302
 		global $wpdb;
303 303
 
304 304
 		// The output fields.
305
-		$actual_fields = self::fields( $fields );
305
+		$actual_fields = self::fields($fields);
306 306
 
307 307
 		$sql = $wpdb->prepare(
308 308
 			"
@@ -312,16 +312,16 @@  discard block
 block discarded – undo
312 312
 				ON p.id = r.object_id
313 313
 			"
314 314
 			// Add the status clause.
315
-			. self::and_status( $status )
315
+			. self::and_status($status)
316 316
 			. self::inner_join_is_not_article()
317 317
 			. " WHERE r.subject_id = %d "
318 318
 			. self::and_post_type_in()
319
-			. self::and_predicate( $predicate )
319
+			. self::and_predicate($predicate)
320 320
 			,
321 321
 			$subject_id
322 322
 		);
323 323
 
324
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
324
+		return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql);
325 325
 	}
326 326
 
327 327
 	/**
@@ -333,13 +333,13 @@  discard block
 block discarded – undo
333 333
 	 *
334 334
 	 * @return string An empty string if $status is null, otherwise the status clause.
335 335
 	 */
336
-	private static function and_status( $status = null ) {
336
+	private static function and_status($status = null) {
337 337
 
338
-		if ( null === $status ) {
338
+		if (null === $status) {
339 339
 			return '';
340 340
 		}
341 341
 
342
-		return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')";
342
+		return " AND p.post_status IN ('".implode("', '", array_map('esc_sql', (array) $status))."')";
343 343
 	}
344 344
 
345 345
 	/**
@@ -352,13 +352,13 @@  discard block
 block discarded – undo
352 352
 	 * @return string An empty string if $predicate is null otherwise the predicate
353 353
 	 *                clause.
354 354
 	 */
355
-	private static function and_predicate( $predicate = null ) {
355
+	private static function and_predicate($predicate = null) {
356 356
 
357
-		if ( null === $predicate ) {
357
+		if (null === $predicate) {
358 358
 			return '';
359 359
 		}
360 360
 
361
-		return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')";
361
+		return " AND r.predicate IN ('".implode("', '", array_map('esc_sql', (array) $predicate))."')";
362 362
 	}
363 363
 
364 364
 	/**
@@ -370,7 +370,7 @@  discard block
 block discarded – undo
370 370
 	 *
371 371
 	 * @return string The `id` field if `ids` otherwise `*`.
372 372
 	 */
373
-	private static function fields( $fields = '*' ) {
373
+	private static function fields($fields = '*') {
374 374
 
375 375
 		// The output fields.
376 376
 		return 'ids' === $fields ? 'id' : '*';
Please login to merge, or discard this patch.
src/includes/linked-data/storage/class-wordlift-post-meta-uri-storage.php 2 patches
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -18,50 +18,50 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Post_Meta_Uri_Storage extends Wordlift_Post_Meta_Storage {
20 20
 
21
-	/**
22
-	 * The {@link Wordlift_Entity_Service} instance.
23
-	 *
24
-	 * @since  3.15.0
25
-	 * @access private
26
-	 * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
27
-	 */
28
-	private $entity_service;
21
+    /**
22
+     * The {@link Wordlift_Entity_Service} instance.
23
+     *
24
+     * @since  3.15.0
25
+     * @access private
26
+     * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
27
+     */
28
+    private $entity_service;
29 29
 
30
-	/**
31
-	 * Create a {@link Wordlift_Post_Meta_Uri_Storage} instance.
32
-	 *
33
-	 * @since 3.15.0
34
-	 *
35
-	 * @param string                   $meta_key       The meta key to read data from.
36
-	 * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
37
-	 */
38
-	public function __construct( $meta_key, $entity_service ) {
39
-		parent::__construct( $meta_key );
30
+    /**
31
+     * Create a {@link Wordlift_Post_Meta_Uri_Storage} instance.
32
+     *
33
+     * @since 3.15.0
34
+     *
35
+     * @param string                   $meta_key       The meta key to read data from.
36
+     * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
37
+     */
38
+    public function __construct( $meta_key, $entity_service ) {
39
+        parent::__construct( $meta_key );
40 40
 
41
-		$this->entity_service = $entity_service;
41
+        $this->entity_service = $entity_service;
42 42
 
43
-	}
43
+    }
44 44
 
45
-	/**
46
-	 * Get the value for the specified meta key.
47
-	 *
48
-	 * The value is expected to be an entity post, for which the URI is loaded
49
-	 * and returned.
50
-	 *
51
-	 * @since 3.15.0
52
-	 *
53
-	 * @param int $post_id The {@link WP_Post}'s id.
54
-	 *
55
-	 * @return array An array of URIs (or an empty array if nothing is set).
56
-	 */
57
-	public function get( $post_id ) {
58
-		$values = parent::get( $post_id );
45
+    /**
46
+     * Get the value for the specified meta key.
47
+     *
48
+     * The value is expected to be an entity post, for which the URI is loaded
49
+     * and returned.
50
+     *
51
+     * @since 3.15.0
52
+     *
53
+     * @param int $post_id The {@link WP_Post}'s id.
54
+     *
55
+     * @return array An array of URIs (or an empty array if nothing is set).
56
+     */
57
+    public function get( $post_id ) {
58
+        $values = parent::get( $post_id );
59 59
 
60
-		$entity_service = $this->entity_service;
60
+        $entity_service = $this->entity_service;
61 61
 
62
-		return array_map( function ( $item ) use ( $entity_service ) {
63
-			return $entity_service->get_uri( $item );
64
-		}, $values );
65
-	}
62
+        return array_map( function ( $item ) use ( $entity_service ) {
63
+            return $entity_service->get_uri( $item );
64
+        }, $values );
65
+    }
66 66
 
67 67
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -35,8 +35,8 @@  discard block
 block discarded – undo
35 35
 	 * @param string                   $meta_key       The meta key to read data from.
36 36
 	 * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
37 37
 	 */
38
-	public function __construct( $meta_key, $entity_service ) {
39
-		parent::__construct( $meta_key );
38
+	public function __construct($meta_key, $entity_service) {
39
+		parent::__construct($meta_key);
40 40
 
41 41
 		$this->entity_service = $entity_service;
42 42
 
@@ -54,14 +54,14 @@  discard block
 block discarded – undo
54 54
 	 *
55 55
 	 * @return array An array of URIs (or an empty array if nothing is set).
56 56
 	 */
57
-	public function get( $post_id ) {
58
-		$values = parent::get( $post_id );
57
+	public function get($post_id) {
58
+		$values = parent::get($post_id);
59 59
 
60 60
 		$entity_service = $this->entity_service;
61 61
 
62
-		return array_map( function ( $item ) use ( $entity_service ) {
63
-			return $entity_service->get_uri( $item );
64
-		}, $values );
62
+		return array_map(function($item) use ($entity_service) {
63
+			return $entity_service->get_uri($item);
64
+		}, $values);
65 65
 	}
66 66
 
67 67
 }
Please login to merge, or discard this patch.