Completed
Pull Request — develop (#1567)
by
unknown
01:01
created
src/includes/class-wordlift-relation-service.php 2 patches
Indentation   +439 added lines, -439 removed lines patch added patch discarded remove patch
@@ -18,424 +18,424 @@  discard block
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Relation_Service {
20 20
 
21
-	/**
22
-	 * The relation table name in MySQL, set during instantiation.
23
-	 *
24
-	 * @since  3.15.0
25
-	 * @access private
26
-	 * @var string $relation_table The relation table name.
27
-	 */
28
-	private $relation_table;
29
-
30
-	/**
31
-	 * A {@link Wordlift_Log_Service} instance.
32
-	 *
33
-	 * @since 3.15.3
34
-	 *
35
-	 * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
36
-	 */
37
-	private static $log;
38
-
39
-	/**
40
-	 * Create a {@link Wordlift_Relation_Service} instance.
41
-	 *
42
-	 * @since 3.15.0
43
-	 */
44
-	protected function __construct() {
45
-		global $wpdb;
46
-
47
-		self::$log = Wordlift_Log_Service::get_logger( get_class() );
48
-
49
-		// The relations table.
50
-		$this->relation_table = "{$wpdb->prefix}wl_relation_instances";
51
-
52
-	}
53
-
54
-	/**
55
-	 * The singleton instance.
56
-	 *
57
-	 * @since  3.15.0
58
-	 * @access private
59
-	 * @var Wordlift_Relation_Service $instance The singleton instance.
60
-	 */
61
-	private static $instance = null;
62
-
63
-	/**
64
-	 * Get the singleton instance.
65
-	 *
66
-	 * @return Wordlift_Relation_Service The {@link Wordlift_Relation_Service} singleton instance.
67
-	 * @since  3.15.0
68
-	 * @access public
69
-	 */
70
-	public static function get_instance() {
71
-
72
-		if ( ! isset( self::$instance ) ) {
73
-			self::$instance = new self();
74
-		}
75
-
76
-		return self::$instance;
77
-	}
78
-
79
-	/**
80
-	 * Get the articles referencing the specified entity {@link WP_Post}.
81
-	 *
82
-	 * @param int|array $object_id The entity {@link WP_Post}'s id.
83
-	 * @param string $fields The fields to return, 'ids' to only return ids or
84
-	 *                               '*' to return all fields, by default '*'.
85
-	 * @param null|string $predicate The predicate (who|what|...), by default all.
86
-	 * @param null|string $status The status, by default all.
87
-	 * @param array $excludes An array of ids to exclude from the results.
88
-	 * @param null|int $limit The maximum number of results, by default
89
-	 *                               no limit.
90
-	 * @param null|array $include The {@link WP_Post}s' ids to include.
91
-	 *
92
-	 * @param null | string $order_by
93
-	 *
94
-	 * @param array $post_types
95
-	 *
96
-	 * @return array|object|null Database query results
97
-	 * @since 3.15.0
98
-	 */
99
-	public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array(), $offset = null ) {
100
-		global $wpdb;
101
-
102
-		// The output fields.
103
-		$actual_fields = self::fields( $fields );
104
-
105
-		self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' );
106
-
107
-		$objects = $this->article_id_to_entity_id( $object_id );
108
-
109
-		// If there are no related objects, return an empty array.
110
-		if ( empty( $objects ) ) {
111
-			self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' );
112
-
113
-			return array();
114
-		}
115
-
116
-		self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' );
117
-
118
-		$sql =
119
-			"
21
+    /**
22
+     * The relation table name in MySQL, set during instantiation.
23
+     *
24
+     * @since  3.15.0
25
+     * @access private
26
+     * @var string $relation_table The relation table name.
27
+     */
28
+    private $relation_table;
29
+
30
+    /**
31
+     * A {@link Wordlift_Log_Service} instance.
32
+     *
33
+     * @since 3.15.3
34
+     *
35
+     * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
36
+     */
37
+    private static $log;
38
+
39
+    /**
40
+     * Create a {@link Wordlift_Relation_Service} instance.
41
+     *
42
+     * @since 3.15.0
43
+     */
44
+    protected function __construct() {
45
+        global $wpdb;
46
+
47
+        self::$log = Wordlift_Log_Service::get_logger( get_class() );
48
+
49
+        // The relations table.
50
+        $this->relation_table = "{$wpdb->prefix}wl_relation_instances";
51
+
52
+    }
53
+
54
+    /**
55
+     * The singleton instance.
56
+     *
57
+     * @since  3.15.0
58
+     * @access private
59
+     * @var Wordlift_Relation_Service $instance The singleton instance.
60
+     */
61
+    private static $instance = null;
62
+
63
+    /**
64
+     * Get the singleton instance.
65
+     *
66
+     * @return Wordlift_Relation_Service The {@link Wordlift_Relation_Service} singleton instance.
67
+     * @since  3.15.0
68
+     * @access public
69
+     */
70
+    public static function get_instance() {
71
+
72
+        if ( ! isset( self::$instance ) ) {
73
+            self::$instance = new self();
74
+        }
75
+
76
+        return self::$instance;
77
+    }
78
+
79
+    /**
80
+     * Get the articles referencing the specified entity {@link WP_Post}.
81
+     *
82
+     * @param int|array $object_id The entity {@link WP_Post}'s id.
83
+     * @param string $fields The fields to return, 'ids' to only return ids or
84
+     *                               '*' to return all fields, by default '*'.
85
+     * @param null|string $predicate The predicate (who|what|...), by default all.
86
+     * @param null|string $status The status, by default all.
87
+     * @param array $excludes An array of ids to exclude from the results.
88
+     * @param null|int $limit The maximum number of results, by default
89
+     *                               no limit.
90
+     * @param null|array $include The {@link WP_Post}s' ids to include.
91
+     *
92
+     * @param null | string $order_by
93
+     *
94
+     * @param array $post_types
95
+     *
96
+     * @return array|object|null Database query results
97
+     * @since 3.15.0
98
+     */
99
+    public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array(), $offset = null ) {
100
+        global $wpdb;
101
+
102
+        // The output fields.
103
+        $actual_fields = self::fields( $fields );
104
+
105
+        self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' );
106
+
107
+        $objects = $this->article_id_to_entity_id( $object_id );
108
+
109
+        // If there are no related objects, return an empty array.
110
+        if ( empty( $objects ) ) {
111
+            self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' );
112
+
113
+            return array();
114
+        }
115
+
116
+        self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' );
117
+
118
+        $sql =
119
+            "
120 120
 			SELECT DISTINCT p.$actual_fields
121 121
 			FROM {$this->relation_table} r
122 122
 			INNER JOIN $wpdb->posts p
123 123
 				ON p.id = r.subject_id
124 124
 			"
125
-			// Add the status clause.
126
-			. self::and_status( $status )
127
-			. self::inner_join_is_article()
128
-			. self::where_object_id( $objects )
129
-			// Since `object_id` can be an article ID we need to exclude it from
130
-			// the results.
131
-			. self::and_article_not_in( array_merge( $excludes, (array) $object_id ) )
132
-			. self::and_article_in( $include )
133
-			. self::and_post_type_in( $post_types )
134
-			. self::and_predicate( $predicate )
135
-			. self::order_by( $order_by )
136
-			. self::limit( $limit )
137
-			. self::offset( $offset );
138
-
139
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
140
-	}
141
-
142
-	/**
143
-	 * The `post_type IN` clause.
144
-	 *
145
-	 * @param array $post_types If the post type is not provided then the valid
146
-	 * entity post types are used.
147
-	 *
148
-	 * @return string The `post_type IN` clause.
149
-	 * @since 3.15.3
150
-	 */
151
-	private static function and_post_type_in( $post_types = array() ) {
152
-
153
-		if ( $post_types === array() ) {
154
-			$post_types = Wordlift_Entity_Service::valid_entity_post_types();
155
-		}
156
-
157
-		return " AND p.post_type IN ( '"
158
-		       . implode(
159
-			       "','",
160
-			       array_map( 'esc_sql', $post_types )
161
-		       )
162
-		       . "' )";
163
-	}
164
-
165
-	/**
166
-	 * Add the limit clause if specified.
167
-	 *
168
-	 * @param null|int $limit The maximum number of results.
169
-	 *
170
-	 * @return string The limit clause (empty if no limit has been specified).
171
-	 * @since 3.15.0
172
-	 *
173
-	 */
174
-	private static function limit( $limit = null ) {
175
-
176
-		if ( null === $limit ) {
177
-			return '';
178
-		}
179
-
180
-		return "LIMIT $limit";
181
-	}
182
-
183
-	/**
184
-	 * Add the OFFSET clause if specified.
185
-	 *
186
-	 * @param null|int $offset The number of results to skip.
187
-	 *
188
-	 * @return string The offset clause (empty if no offset has been specified).
189
-	 * @since 3.35.11
190
-	 *
191
-	 */
192
-	private static function offset( $offset = null ) {
193
-
194
-		if ( null === $offset ) {
195
-			return '';
196
-		}
197
-
198
-		return " OFFSET $offset";
199
-	}
200
-
201
-	/**
202
-	 * @param $order_by string | null
203
-	 *
204
-	 * @return string
205
-	 */
206
-	private static function order_by( $order_by ) {
207
-		if ( ! $order_by ) {
208
-			return '';
209
-		}
210
-		$order_by         = (string) $order_by;
211
-		$order_by_clauses = array( 'DESC', 'ASC' );
212
-
213
-		if ( in_array( $order_by, $order_by_clauses, true ) ) {
214
-			return " ORDER BY p.post_modified ${order_by} ";
215
-		} else {
216
-			return ' ORDER BY p.post_modified DESC ';
217
-		}
218
-	}
219
-
220
-	/**
221
-	 * Map the provided ids into entities (i.e. return the id if it's an entity
222
-	 * or get the entities if it's a post).
223
-	 *
224
-	 * @param int|array $object_id An array of posts/entities' ids.
225
-	 *
226
-	 * @return array An array of entities' ids.
227
-	 * @since 3.15.0
228
-	 *
229
-	 */
230
-	private function article_id_to_entity_id( $object_id ) {
231
-
232
-		$entity_service = Wordlift_Entity_Service::get_instance();
233
-
234
-		$relation_service = $this;
235
-
236
-		return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) {
237
-			if ( $entity_service->is_entity( $item ) ) {
238
-				return array_merge( $carry, (array) $item );
239
-			}
240
-
241
-			return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) );
242
-		}, array() );
243
-
244
-	}
245
-
246
-	/**
247
-	 * Add the WHERE clause.
248
-	 *
249
-	 * @param int|array $object_id An array of {@link WP_Post}s' ids.
250
-	 *
251
-	 * @return string The WHERE clause.
252
-	 * @since 3.15.0
253
-	 *
254
-	 */
255
-	private static function where_object_id( $object_id ) {
256
-
257
-		if ( empty( $object_id ) ) {
258
-			// self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) );
259
-
260
-			return ' WHERE 1 = 1';
261
-		}
262
-
263
-		return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )';
264
-	}
265
-
266
-	/**
267
-	 * Add the exclude clause.
268
-	 *
269
-	 * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude.
270
-	 *
271
-	 * @return string The exclude clause.
272
-	 * @since 3.15.0
273
-	 *
274
-	 */
275
-	private static function and_article_not_in( $exclude ) {
276
-
277
-		return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )';
278
-	}
279
-
280
-	/**
281
-	 * Add the include clause.
282
-	 *
283
-	 * @param null|int|array $include An array of {@link WP_Post}s' ids.
284
-	 *
285
-	 * @return string An empty string if $include is null otherwise the include
286
-	 *                clause.
287
-	 * @since 3.15.0
288
-	 *
289
-	 */
290
-	private static function and_article_in( $include = null ) {
291
-
292
-		if ( null === $include ) {
293
-			return '';
294
-		}
295
-
296
-		return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )';
297
-	}
298
-
299
-	/**
300
-	 * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}.
301
-	 *
302
-	 * @param int $object_id The object {@link WP_Post}'s id.
303
-	 * @param string $fields The fields to return, 'ids' to only return ids or
304
-	 *                               '*' to return all fields, by default '*'.
305
-	 * @param null|string $status The status, by default all.
306
-	 *
307
-	 * @return array|object|null Database query results
308
-	 * @since 3.15.0
309
-	 *
310
-	 */
311
-	public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) {
312
-		global $wpdb;
313
-
314
-		// The output fields.
315
-		$actual_fields = self::fields( $fields );
316
-
317
-		$sql = $wpdb->prepare(
318
-			"
125
+            // Add the status clause.
126
+            . self::and_status( $status )
127
+            . self::inner_join_is_article()
128
+            . self::where_object_id( $objects )
129
+            // Since `object_id` can be an article ID we need to exclude it from
130
+            // the results.
131
+            . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) )
132
+            . self::and_article_in( $include )
133
+            . self::and_post_type_in( $post_types )
134
+            . self::and_predicate( $predicate )
135
+            . self::order_by( $order_by )
136
+            . self::limit( $limit )
137
+            . self::offset( $offset );
138
+
139
+        return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
140
+    }
141
+
142
+    /**
143
+     * The `post_type IN` clause.
144
+     *
145
+     * @param array $post_types If the post type is not provided then the valid
146
+     * entity post types are used.
147
+     *
148
+     * @return string The `post_type IN` clause.
149
+     * @since 3.15.3
150
+     */
151
+    private static function and_post_type_in( $post_types = array() ) {
152
+
153
+        if ( $post_types === array() ) {
154
+            $post_types = Wordlift_Entity_Service::valid_entity_post_types();
155
+        }
156
+
157
+        return " AND p.post_type IN ( '"
158
+                . implode(
159
+                    "','",
160
+                    array_map( 'esc_sql', $post_types )
161
+                )
162
+                . "' )";
163
+    }
164
+
165
+    /**
166
+     * Add the limit clause if specified.
167
+     *
168
+     * @param null|int $limit The maximum number of results.
169
+     *
170
+     * @return string The limit clause (empty if no limit has been specified).
171
+     * @since 3.15.0
172
+     *
173
+     */
174
+    private static function limit( $limit = null ) {
175
+
176
+        if ( null === $limit ) {
177
+            return '';
178
+        }
179
+
180
+        return "LIMIT $limit";
181
+    }
182
+
183
+    /**
184
+     * Add the OFFSET clause if specified.
185
+     *
186
+     * @param null|int $offset The number of results to skip.
187
+     *
188
+     * @return string The offset clause (empty if no offset has been specified).
189
+     * @since 3.35.11
190
+     *
191
+     */
192
+    private static function offset( $offset = null ) {
193
+
194
+        if ( null === $offset ) {
195
+            return '';
196
+        }
197
+
198
+        return " OFFSET $offset";
199
+    }
200
+
201
+    /**
202
+     * @param $order_by string | null
203
+     *
204
+     * @return string
205
+     */
206
+    private static function order_by( $order_by ) {
207
+        if ( ! $order_by ) {
208
+            return '';
209
+        }
210
+        $order_by         = (string) $order_by;
211
+        $order_by_clauses = array( 'DESC', 'ASC' );
212
+
213
+        if ( in_array( $order_by, $order_by_clauses, true ) ) {
214
+            return " ORDER BY p.post_modified ${order_by} ";
215
+        } else {
216
+            return ' ORDER BY p.post_modified DESC ';
217
+        }
218
+    }
219
+
220
+    /**
221
+     * Map the provided ids into entities (i.e. return the id if it's an entity
222
+     * or get the entities if it's a post).
223
+     *
224
+     * @param int|array $object_id An array of posts/entities' ids.
225
+     *
226
+     * @return array An array of entities' ids.
227
+     * @since 3.15.0
228
+     *
229
+     */
230
+    private function article_id_to_entity_id( $object_id ) {
231
+
232
+        $entity_service = Wordlift_Entity_Service::get_instance();
233
+
234
+        $relation_service = $this;
235
+
236
+        return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) {
237
+            if ( $entity_service->is_entity( $item ) ) {
238
+                return array_merge( $carry, (array) $item );
239
+            }
240
+
241
+            return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) );
242
+        }, array() );
243
+
244
+    }
245
+
246
+    /**
247
+     * Add the WHERE clause.
248
+     *
249
+     * @param int|array $object_id An array of {@link WP_Post}s' ids.
250
+     *
251
+     * @return string The WHERE clause.
252
+     * @since 3.15.0
253
+     *
254
+     */
255
+    private static function where_object_id( $object_id ) {
256
+
257
+        if ( empty( $object_id ) ) {
258
+            // self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) );
259
+
260
+            return ' WHERE 1 = 1';
261
+        }
262
+
263
+        return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )';
264
+    }
265
+
266
+    /**
267
+     * Add the exclude clause.
268
+     *
269
+     * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude.
270
+     *
271
+     * @return string The exclude clause.
272
+     * @since 3.15.0
273
+     *
274
+     */
275
+    private static function and_article_not_in( $exclude ) {
276
+
277
+        return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )';
278
+    }
279
+
280
+    /**
281
+     * Add the include clause.
282
+     *
283
+     * @param null|int|array $include An array of {@link WP_Post}s' ids.
284
+     *
285
+     * @return string An empty string if $include is null otherwise the include
286
+     *                clause.
287
+     * @since 3.15.0
288
+     *
289
+     */
290
+    private static function and_article_in( $include = null ) {
291
+
292
+        if ( null === $include ) {
293
+            return '';
294
+        }
295
+
296
+        return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )';
297
+    }
298
+
299
+    /**
300
+     * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}.
301
+     *
302
+     * @param int $object_id The object {@link WP_Post}'s id.
303
+     * @param string $fields The fields to return, 'ids' to only return ids or
304
+     *                               '*' to return all fields, by default '*'.
305
+     * @param null|string $status The status, by default all.
306
+     *
307
+     * @return array|object|null Database query results
308
+     * @since 3.15.0
309
+     *
310
+     */
311
+    public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) {
312
+        global $wpdb;
313
+
314
+        // The output fields.
315
+        $actual_fields = self::fields( $fields );
316
+
317
+        $sql = $wpdb->prepare(
318
+            "
319 319
 			SELECT p.$actual_fields
320 320
 			FROM {$this->relation_table} r
321 321
 			INNER JOIN $wpdb->posts p
322 322
 				ON p.id = r.subject_id
323 323
 			"
324
-			// Add the status clause.
325
-			. self::and_status( $status )
326
-			. self::inner_join_is_not_article()
327
-			. " WHERE r.object_id = %d "
328
-			. self::and_post_type_in()
329
-			,
330
-			$object_id
331
-		);
332
-
333
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
334
-	}
335
-
336
-	/**
337
-	 * Get the entities referenced by the specified {@link WP_Post}.
338
-	 *
339
-	 * @param int $subject_id The {@link WP_Post}'s id.
340
-	 * @param string $fields The fields to return, 'ids' to only return ids or
341
-	 *                                '*' to return all fields, by default '*'.
342
-	 * @param null|string $predicate The predicate (who|what|...), by default all.
343
-	 * @param null|string $status The status, by default all.
344
-	 *
345
-	 * @return array|object|null Database query results
346
-	 * @since 3.15.0
347
-	 *
348
-	 */
349
-	public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) {
350
-		global $wpdb;
351
-
352
-		// The output fields.
353
-		$actual_fields = self::fields( $fields );
354
-
355
-		$sql = $wpdb->prepare(
356
-			"
324
+            // Add the status clause.
325
+            . self::and_status( $status )
326
+            . self::inner_join_is_not_article()
327
+            . " WHERE r.object_id = %d "
328
+            . self::and_post_type_in()
329
+            ,
330
+            $object_id
331
+        );
332
+
333
+        return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
334
+    }
335
+
336
+    /**
337
+     * Get the entities referenced by the specified {@link WP_Post}.
338
+     *
339
+     * @param int $subject_id The {@link WP_Post}'s id.
340
+     * @param string $fields The fields to return, 'ids' to only return ids or
341
+     *                                '*' to return all fields, by default '*'.
342
+     * @param null|string $predicate The predicate (who|what|...), by default all.
343
+     * @param null|string $status The status, by default all.
344
+     *
345
+     * @return array|object|null Database query results
346
+     * @since 3.15.0
347
+     *
348
+     */
349
+    public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) {
350
+        global $wpdb;
351
+
352
+        // The output fields.
353
+        $actual_fields = self::fields( $fields );
354
+
355
+        $sql = $wpdb->prepare(
356
+            "
357 357
 			SELECT p.$actual_fields
358 358
 			FROM {$this->relation_table} r
359 359
 			INNER JOIN $wpdb->posts p
360 360
 				ON p.id = r.object_id
361 361
 			"
362
-			// Add the status clause.
363
-			. self::and_status( $status )
364
-			. self::inner_join_is_not_article()
365
-			. " WHERE r.subject_id = %d "
366
-			. self::and_post_type_in()
367
-			. self::and_predicate( $predicate )
368
-			,
369
-			$subject_id
370
-		);
371
-
372
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
373
-	}
374
-
375
-	/**
376
-	 * Add the `post_status` clause.
377
-	 *
378
-	 * @param null|string|array $status The status values.
379
-	 *
380
-	 * @return string An empty string if $status is null, otherwise the status clause.
381
-	 * @since 3.15.0
382
-	 *
383
-	 */
384
-	private static function and_status( $status = null ) {
385
-
386
-		if ( null === $status ) {
387
-			return '';
388
-		}
389
-
390
-		return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')";
391
-	}
392
-
393
-	/**
394
-	 * Add the `predicate` clause.
395
-	 *
396
-	 * @param null|string|array $predicate An array of predicates.
397
-	 *
398
-	 * @return string An empty string if $predicate is null otherwise the predicate
399
-	 *                clause.
400
-	 * @since 3.15.0
401
-	 *
402
-	 */
403
-	private static function and_predicate( $predicate = null ) {
404
-
405
-		if ( null === $predicate ) {
406
-			return '';
407
-		}
408
-
409
-		return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')";
410
-	}
411
-
412
-	/**
413
-	 * The select fields.
414
-	 *
415
-	 * @param string $fields Either 'ids' or '*', by default '*'.
416
-	 *
417
-	 * @return string The `id` field if `ids` otherwise `*`.
418
-	 * @since 3.15.0
419
-	 *
420
-	 */
421
-	private static function fields( $fields = '*' ) {
422
-
423
-		// The output fields.
424
-		return 'ids' === $fields ? 'id' : '*';
425
-	}
426
-
427
-	/**
428
-	 * The inner join clause for articles.
429
-	 *
430
-	 * @return string The articles inner join clause.
431
-	 * @since 3.15.0
432
-	 *
433
-	 */
434
-	private static function inner_join_is_article() {
435
-		global $wpdb;
436
-
437
-		return $wpdb->prepare(
438
-			"
362
+            // Add the status clause.
363
+            . self::and_status( $status )
364
+            . self::inner_join_is_not_article()
365
+            . " WHERE r.subject_id = %d "
366
+            . self::and_post_type_in()
367
+            . self::and_predicate( $predicate )
368
+            ,
369
+            $subject_id
370
+        );
371
+
372
+        return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
373
+    }
374
+
375
+    /**
376
+     * Add the `post_status` clause.
377
+     *
378
+     * @param null|string|array $status The status values.
379
+     *
380
+     * @return string An empty string if $status is null, otherwise the status clause.
381
+     * @since 3.15.0
382
+     *
383
+     */
384
+    private static function and_status( $status = null ) {
385
+
386
+        if ( null === $status ) {
387
+            return '';
388
+        }
389
+
390
+        return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')";
391
+    }
392
+
393
+    /**
394
+     * Add the `predicate` clause.
395
+     *
396
+     * @param null|string|array $predicate An array of predicates.
397
+     *
398
+     * @return string An empty string if $predicate is null otherwise the predicate
399
+     *                clause.
400
+     * @since 3.15.0
401
+     *
402
+     */
403
+    private static function and_predicate( $predicate = null ) {
404
+
405
+        if ( null === $predicate ) {
406
+            return '';
407
+        }
408
+
409
+        return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')";
410
+    }
411
+
412
+    /**
413
+     * The select fields.
414
+     *
415
+     * @param string $fields Either 'ids' or '*', by default '*'.
416
+     *
417
+     * @return string The `id` field if `ids` otherwise `*`.
418
+     * @since 3.15.0
419
+     *
420
+     */
421
+    private static function fields( $fields = '*' ) {
422
+
423
+        // The output fields.
424
+        return 'ids' === $fields ? 'id' : '*';
425
+    }
426
+
427
+    /**
428
+     * The inner join clause for articles.
429
+     *
430
+     * @return string The articles inner join clause.
431
+     * @since 3.15.0
432
+     *
433
+     */
434
+    private static function inner_join_is_article() {
435
+        global $wpdb;
436
+
437
+        return $wpdb->prepare(
438
+            "
439 439
 			INNER JOIN $wpdb->term_relationships tr
440 440
 			 ON p.id = tr.object_id
441 441
 			INNER JOIN $wpdb->term_taxonomy tt
@@ -445,23 +445,23 @@  discard block
 block discarded – undo
445 445
 			 ON t.term_id = tt.term_id
446 446
 			  AND t.slug = %s
447 447
 			",
448
-			'wl_entity_type',
449
-			'article'
450
-		);
451
-	}
452
-
453
-	/**
454
-	 * The inner join clause for non-articles.
455
-	 *
456
-	 * @return string The non-articles inner join clause.
457
-	 * @since 3.15.0
458
-	 *
459
-	 */
460
-	private static function inner_join_is_not_article() {
461
-		global $wpdb;
462
-
463
-		return $wpdb->prepare(
464
-			"
448
+            'wl_entity_type',
449
+            'article'
450
+        );
451
+    }
452
+
453
+    /**
454
+     * The inner join clause for non-articles.
455
+     *
456
+     * @return string The non-articles inner join clause.
457
+     * @since 3.15.0
458
+     *
459
+     */
460
+    private static function inner_join_is_not_article() {
461
+        global $wpdb;
462
+
463
+        return $wpdb->prepare(
464
+            "
465 465
 			INNER JOIN $wpdb->term_relationships tr
466 466
 			 ON p.id = tr.object_id
467 467
 			INNER JOIN $wpdb->term_taxonomy tt
@@ -471,29 +471,29 @@  discard block
 block discarded – undo
471 471
 			 ON t.term_id = tt.term_id
472 472
 			  AND NOT t.slug = %s
473 473
 			",
474
-			'wl_entity_type',
475
-			'article'
476
-		);
477
-	}
478
-
479
-	/**
480
-	 * Find all the subject IDs and their referenced/related object IDs. The
481
-	 * object IDs are returned as comma separated IDs in the `object_ids` key.
482
-	 *
483
-	 * @return mixed Database query results
484
-	 * @since 3.18.0
485
-	 */
486
-	public function find_all_grouped_by_subject_id() {
487
-		global $wpdb;
488
-
489
-		return $wpdb->get_results(
490
-			"
474
+            'wl_entity_type',
475
+            'article'
476
+        );
477
+    }
478
+
479
+    /**
480
+     * Find all the subject IDs and their referenced/related object IDs. The
481
+     * object IDs are returned as comma separated IDs in the `object_ids` key.
482
+     *
483
+     * @return mixed Database query results
484
+     * @since 3.18.0
485
+     */
486
+    public function find_all_grouped_by_subject_id() {
487
+        global $wpdb;
488
+
489
+        return $wpdb->get_results(
490
+            "
491 491
 			SELECT subject_id, GROUP_CONCAT( DISTINCT object_id ORDER BY object_id SEPARATOR ',' ) AS object_ids
492 492
 			FROM $this->relation_table
493 493
 			GROUP BY subject_id
494 494
 			"
495
-		);
495
+        );
496 496
 
497
-	}
497
+    }
498 498
 
499 499
 }
Please login to merge, or discard this patch.
Spacing   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
 	protected function __construct() {
45 45
 		global $wpdb;
46 46
 
47
-		self::$log = Wordlift_Log_Service::get_logger( get_class() );
47
+		self::$log = Wordlift_Log_Service::get_logger(get_class());
48 48
 
49 49
 		// The relations table.
50 50
 		$this->relation_table = "{$wpdb->prefix}wl_relation_instances";
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 	 */
70 70
 	public static function get_instance() {
71 71
 
72
-		if ( ! isset( self::$instance ) ) {
72
+		if ( ! isset(self::$instance)) {
73 73
 			self::$instance = new self();
74 74
 		}
75 75
 
@@ -96,24 +96,24 @@  discard block
 block discarded – undo
96 96
 	 * @return array|object|null Database query results
97 97
 	 * @since 3.15.0
98 98
 	 */
99
-	public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array(), $offset = null ) {
99
+	public function get_article_subjects($object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array(), $offset = null) {
100 100
 		global $wpdb;
101 101
 
102 102
 		// The output fields.
103
-		$actual_fields = self::fields( $fields );
103
+		$actual_fields = self::fields($fields);
104 104
 
105
-		self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' );
105
+		self::$log->trace('Getting article subjects for object '.implode(', ', (array) $object_id).'...');
106 106
 
107
-		$objects = $this->article_id_to_entity_id( $object_id );
107
+		$objects = $this->article_id_to_entity_id($object_id);
108 108
 
109 109
 		// If there are no related objects, return an empty array.
110
-		if ( empty( $objects ) ) {
111
-			self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' );
110
+		if (empty($objects)) {
111
+			self::$log->debug('No entities found for object '.implode(', ', (array) $object_id).'.');
112 112
 
113 113
 			return array();
114 114
 		}
115 115
 
116
-		self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' );
116
+		self::$log->debug(count($objects).' entity id(s) found for object '.implode(', ', (array) $object_id).'.');
117 117
 
118 118
 		$sql =
119 119
 			"
@@ -123,20 +123,20 @@  discard block
 block discarded – undo
123 123
 				ON p.id = r.subject_id
124 124
 			"
125 125
 			// Add the status clause.
126
-			. self::and_status( $status )
126
+			. self::and_status($status)
127 127
 			. self::inner_join_is_article()
128
-			. self::where_object_id( $objects )
128
+			. self::where_object_id($objects)
129 129
 			// Since `object_id` can be an article ID we need to exclude it from
130 130
 			// the results.
131
-			. self::and_article_not_in( array_merge( $excludes, (array) $object_id ) )
132
-			. self::and_article_in( $include )
133
-			. self::and_post_type_in( $post_types )
134
-			. self::and_predicate( $predicate )
135
-			. self::order_by( $order_by )
136
-			. self::limit( $limit )
137
-			. self::offset( $offset );
138
-
139
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
131
+			. self::and_article_not_in(array_merge($excludes, (array) $object_id))
132
+			. self::and_article_in($include)
133
+			. self::and_post_type_in($post_types)
134
+			. self::and_predicate($predicate)
135
+			. self::order_by($order_by)
136
+			. self::limit($limit)
137
+			. self::offset($offset);
138
+
139
+		return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql);
140 140
 	}
141 141
 
142 142
 	/**
@@ -148,16 +148,16 @@  discard block
 block discarded – undo
148 148
 	 * @return string The `post_type IN` clause.
149 149
 	 * @since 3.15.3
150 150
 	 */
151
-	private static function and_post_type_in( $post_types = array() ) {
151
+	private static function and_post_type_in($post_types = array()) {
152 152
 
153
-		if ( $post_types === array() ) {
153
+		if ($post_types === array()) {
154 154
 			$post_types = Wordlift_Entity_Service::valid_entity_post_types();
155 155
 		}
156 156
 
157 157
 		return " AND p.post_type IN ( '"
158 158
 		       . implode(
159 159
 			       "','",
160
-			       array_map( 'esc_sql', $post_types )
160
+			       array_map('esc_sql', $post_types)
161 161
 		       )
162 162
 		       . "' )";
163 163
 	}
@@ -171,9 +171,9 @@  discard block
 block discarded – undo
171 171
 	 * @since 3.15.0
172 172
 	 *
173 173
 	 */
174
-	private static function limit( $limit = null ) {
174
+	private static function limit($limit = null) {
175 175
 
176
-		if ( null === $limit ) {
176
+		if (null === $limit) {
177 177
 			return '';
178 178
 		}
179 179
 
@@ -189,9 +189,9 @@  discard block
 block discarded – undo
189 189
 	 * @since 3.35.11
190 190
 	 *
191 191
 	 */
192
-	private static function offset( $offset = null ) {
192
+	private static function offset($offset = null) {
193 193
 
194
-		if ( null === $offset ) {
194
+		if (null === $offset) {
195 195
 			return '';
196 196
 		}
197 197
 
@@ -203,14 +203,14 @@  discard block
 block discarded – undo
203 203
 	 *
204 204
 	 * @return string
205 205
 	 */
206
-	private static function order_by( $order_by ) {
207
-		if ( ! $order_by ) {
206
+	private static function order_by($order_by) {
207
+		if ( ! $order_by) {
208 208
 			return '';
209 209
 		}
210 210
 		$order_by         = (string) $order_by;
211
-		$order_by_clauses = array( 'DESC', 'ASC' );
211
+		$order_by_clauses = array('DESC', 'ASC');
212 212
 
213
-		if ( in_array( $order_by, $order_by_clauses, true ) ) {
213
+		if (in_array($order_by, $order_by_clauses, true)) {
214 214
 			return " ORDER BY p.post_modified ${order_by} ";
215 215
 		} else {
216 216
 			return ' ORDER BY p.post_modified DESC ';
@@ -227,19 +227,19 @@  discard block
 block discarded – undo
227 227
 	 * @since 3.15.0
228 228
 	 *
229 229
 	 */
230
-	private function article_id_to_entity_id( $object_id ) {
230
+	private function article_id_to_entity_id($object_id) {
231 231
 
232 232
 		$entity_service = Wordlift_Entity_Service::get_instance();
233 233
 
234 234
 		$relation_service = $this;
235 235
 
236
-		return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) {
237
-			if ( $entity_service->is_entity( $item ) ) {
238
-				return array_merge( $carry, (array) $item );
236
+		return array_reduce((array) $object_id, function($carry, $item) use ($entity_service, $relation_service) {
237
+			if ($entity_service->is_entity($item)) {
238
+				return array_merge($carry, (array) $item);
239 239
 			}
240 240
 
241
-			return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) );
242
-		}, array() );
241
+			return array_merge($carry, $relation_service->get_objects($item, 'ids'));
242
+		}, array());
243 243
 
244 244
 	}
245 245
 
@@ -252,15 +252,15 @@  discard block
 block discarded – undo
252 252
 	 * @since 3.15.0
253 253
 	 *
254 254
 	 */
255
-	private static function where_object_id( $object_id ) {
255
+	private static function where_object_id($object_id) {
256 256
 
257
-		if ( empty( $object_id ) ) {
257
+		if (empty($object_id)) {
258 258
 			// self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) );
259 259
 
260 260
 			return ' WHERE 1 = 1';
261 261
 		}
262 262
 
263
-		return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )';
263
+		return ' WHERE r.object_id IN ( '.implode(',', wp_parse_id_list((array) $object_id)).' )';
264 264
 	}
265 265
 
266 266
 	/**
@@ -272,9 +272,9 @@  discard block
 block discarded – undo
272 272
 	 * @since 3.15.0
273 273
 	 *
274 274
 	 */
275
-	private static function and_article_not_in( $exclude ) {
275
+	private static function and_article_not_in($exclude) {
276 276
 
277
-		return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )';
277
+		return ' AND NOT p.ID IN ( '.implode(',', wp_parse_id_list((array) $exclude)).' )';
278 278
 	}
279 279
 
280 280
 	/**
@@ -287,13 +287,13 @@  discard block
 block discarded – undo
287 287
 	 * @since 3.15.0
288 288
 	 *
289 289
 	 */
290
-	private static function and_article_in( $include = null ) {
290
+	private static function and_article_in($include = null) {
291 291
 
292
-		if ( null === $include ) {
292
+		if (null === $include) {
293 293
 			return '';
294 294
 		}
295 295
 
296
-		return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )';
296
+		return ' AND p.ID IN ( '.implode(',', wp_parse_id_list((array) $include)).' )';
297 297
 	}
298 298
 
299 299
 	/**
@@ -308,11 +308,11 @@  discard block
 block discarded – undo
308 308
 	 * @since 3.15.0
309 309
 	 *
310 310
 	 */
311
-	public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) {
311
+	public function get_non_article_subjects($object_id, $fields = '*', $status = null) {
312 312
 		global $wpdb;
313 313
 
314 314
 		// The output fields.
315
-		$actual_fields = self::fields( $fields );
315
+		$actual_fields = self::fields($fields);
316 316
 
317 317
 		$sql = $wpdb->prepare(
318 318
 			"
@@ -322,7 +322,7 @@  discard block
 block discarded – undo
322 322
 				ON p.id = r.subject_id
323 323
 			"
324 324
 			// Add the status clause.
325
-			. self::and_status( $status )
325
+			. self::and_status($status)
326 326
 			. self::inner_join_is_not_article()
327 327
 			. " WHERE r.object_id = %d "
328 328
 			. self::and_post_type_in()
@@ -330,7 +330,7 @@  discard block
 block discarded – undo
330 330
 			$object_id
331 331
 		);
332 332
 
333
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
333
+		return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql);
334 334
 	}
335 335
 
336 336
 	/**
@@ -346,11 +346,11 @@  discard block
 block discarded – undo
346 346
 	 * @since 3.15.0
347 347
 	 *
348 348
 	 */
349
-	public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) {
349
+	public function get_objects($subject_id, $fields = '*', $predicate = null, $status = null) {
350 350
 		global $wpdb;
351 351
 
352 352
 		// The output fields.
353
-		$actual_fields = self::fields( $fields );
353
+		$actual_fields = self::fields($fields);
354 354
 
355 355
 		$sql = $wpdb->prepare(
356 356
 			"
@@ -360,16 +360,16 @@  discard block
 block discarded – undo
360 360
 				ON p.id = r.object_id
361 361
 			"
362 362
 			// Add the status clause.
363
-			. self::and_status( $status )
363
+			. self::and_status($status)
364 364
 			. self::inner_join_is_not_article()
365 365
 			. " WHERE r.subject_id = %d "
366 366
 			. self::and_post_type_in()
367
-			. self::and_predicate( $predicate )
367
+			. self::and_predicate($predicate)
368 368
 			,
369 369
 			$subject_id
370 370
 		);
371 371
 
372
-		return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql );
372
+		return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql);
373 373
 	}
374 374
 
375 375
 	/**
@@ -381,13 +381,13 @@  discard block
 block discarded – undo
381 381
 	 * @since 3.15.0
382 382
 	 *
383 383
 	 */
384
-	private static function and_status( $status = null ) {
384
+	private static function and_status($status = null) {
385 385
 
386
-		if ( null === $status ) {
386
+		if (null === $status) {
387 387
 			return '';
388 388
 		}
389 389
 
390
-		return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')";
390
+		return " AND p.post_status IN ('".implode("', '", array_map('esc_sql', (array) $status))."')";
391 391
 	}
392 392
 
393 393
 	/**
@@ -400,13 +400,13 @@  discard block
 block discarded – undo
400 400
 	 * @since 3.15.0
401 401
 	 *
402 402
 	 */
403
-	private static function and_predicate( $predicate = null ) {
403
+	private static function and_predicate($predicate = null) {
404 404
 
405
-		if ( null === $predicate ) {
405
+		if (null === $predicate) {
406 406
 			return '';
407 407
 		}
408 408
 
409
-		return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')";
409
+		return " AND r.predicate IN ('".implode("', '", array_map('esc_sql', (array) $predicate))."')";
410 410
 	}
411 411
 
412 412
 	/**
@@ -418,7 +418,7 @@  discard block
 block discarded – undo
418 418
 	 * @since 3.15.0
419 419
 	 *
420 420
 	 */
421
-	private static function fields( $fields = '*' ) {
421
+	private static function fields($fields = '*') {
422 422
 
423 423
 		// The output fields.
424 424
 		return 'ids' === $fields ? 'id' : '*';
Please login to merge, or discard this patch.