PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
← All changes | libraries/ct/includes/taxonomy.php +460 -0 5.8.3 → 6.0.3 View file →
@@ -1,0 +1,460 @@
1 +<?php
2 +/**
3 + * CT Taxonomy Functions
4 + *
5 + * @since 1.0.0
6 + */
7 +// Exit if accessed directly
8 +defined( 'ABSPATH' ) || exit;
9 +
10 +/**
11 + * Register a taxonomy custom table and its relationship table
12 + *
13 + * @since 1.0.0
14 + *
15 + * @param string $name
16 + * @param string|array $tables
17 + * @param array $args
18 + */
19 +function ct_register_taxonomy_table( $name, $tables, $args ) {
20 +
21 + global $ct_registered_tables, $ct_registered_taxonomies;
22 +
23 + // Init registered taxonomies
24 + if ( ! is_array( $ct_registered_taxonomies ) ) {
25 + $ct_registered_taxonomies = array();
26 + }
27 +
28 + $name = sanitize_key( $name );
29 +
30 + if( ! isset( $ct_registered_tables[ $name ] ) ) {
31 + $ct_table = ct_register_table( $name, $args );
32 +
33 + $ct_registered_taxonomies[$name] = $ct_table;
34 + }
35 +
36 + // Bail here if no tables provided
37 + if( empty( $tables ) ) {
38 + return;
39 + }
40 +
41 + if( ! is_array( $tables ) ) {
42 + $tables = array( $tables );
43 + }
44 +
45 + foreach( $tables as $table ) {
46 +
47 + // TODO: notice user here
48 + if( ! isset( $ct_registered_tables[$table] ) ) {
49 + continue;
50 + }
51 +
52 + ct_register_relationship_table( $name, $table, $args );
53 +
54 + }
55 +
56 +}
57 +
58 +/**
59 + * Register a relationship custom table
60 + *
61 + * @since 1.0.0
62 + *
63 + * @param string $table_a
64 + * @param string $table_b
65 + * @param array $args
66 + */
67 +function ct_register_relationship_table( $table_a, $table_b, $args ) {
68 +
69 + global $ct_registered_tables, $ct_relationships;
70 +
71 + // Init relationships global
72 + if ( ! is_array( $ct_relationships ) ) {
73 + $ct_relationships = array();
74 + }
75 +
76 + // Relationship table: ct_category . _relationships
77 + $table_name = $table_a . '_relationships';
78 +
79 + $args['show_ui'] = false;
80 + $args['supports'] = array();
81 +
82 + $term_id = $table_a . '_' . $ct_registered_tables[$table_a]->db->primary_key;
83 + $object_id = $table_b . '_' . $ct_registered_tables[$table_b]->db->primary_key;
84 +
85 + if( isset( $args['relationship'] ) ) {
86 +
87 + if( isset( $args['relationship']['term_id'] ) ) {
88 + $term_id = $args['relationship']['term_id'];
89 + }
90 +
91 + if( isset( $args['relationship']['object_id'] ) ) {
92 + $object_id = $args['relationship']['object_id'];
93 + }
94 +
95 + } else {
96 + $args['relationship'] = array(
97 + 'type' => 'single',
98 + 'term_id' => $term_id,
99 + 'object_id' => $object_id,
100 + );
101 + }
102 +
103 + // Override schema
104 + $args['schema'] = array(
105 + 'id' => array(
106 + 'type' => 'bigint',
107 + 'length' => '20',
108 + 'auto_increment' => true,
109 + 'primary_key' => true,
110 + ),
111 + $term_id => array(
112 + 'type' => 'bigint',
113 + 'length' => '20',
114 + 'key' => true,
115 + ),
116 + $object_id => array(
117 + 'type' => 'bigint',
118 + 'length' => '20',
119 + 'key' => true,
120 + ),
121 + );
122 +
123 + $ct_table = ct_register_table( $table_name, $args );
124 +
125 + $ct_relationships[$table_name] = (object) $args['relationship'];
126 +
127 + return $ct_relationships[$table_name];
128 +
129 +}
130 +
131 +/**
132 + * Find object relationship
133 + *
134 + * @since 1.0.0
135 + *
136 + * @param integer $object_id
137 + * @param integer $term_id
138 + *
139 + * @return bool
140 + */
141 +function ct_get_relationship_id( $object_id, $term_id ) {
142 +
143 + global $ct_table, $ct_relationships, $wpdb;
144 +
145 + if( ! isset( $ct_relationships[$ct_table->name] ) ) {
146 + return false;
147 + }
148 +
149 + $r = $ct_relationships[$ct_table->name];
150 +
151 + $object_id_field = $r->object_id;
152 + $term_id_field = $r->term_id;
153 +
154 + $found = absint( $wpdb->get_var( $wpdb->prepare(
155 + "SELECT {$ct_table->db->primary_key}
156 + FROM {$ct_table->db->table_name}
157 + WHERE 1=1
158 + AND {$object_id_field} = %d
159 + AND {$term_id_field} = %d",
160 + absint( $object_id ),
161 + absint( $term_id ),
162 + ) ) );
163 +
164 + return $found;
165 +}
166 +
167 +/**
168 + * Add object relationships (without remove old ones)
169 + *
170 + * @since 1.0.0
171 + *
172 + * @param integer $object_id
173 + * @param integer|array $term_id
174 + *
175 + * @return bool
176 + */
177 +function ct_add_object_relationship( $object_id, $term_id ) {
178 +
179 + return ct_update_object_relationships( $object_id, $term_id, false );
180 +}
181 +
182 +/**
183 + * Update object relationships
184 + *
185 + * @since 1.0.0
186 + *
187 + * @param integer $object_id
188 + * @param integer|array $term_id
189 + * @param bool $remove_old
190 + *
191 + * @return bool
192 + */
193 +function ct_update_object_relationships( $object_id, $term_id, $remove_old = true ) {
194 +
195 + global $ct_table, $ct_relationships, $wpdb;
196 +
197 + if( ! isset( $ct_relationships[$ct_table->name] ) ) {
198 + return false;
199 + }
200 +
201 + $r = $ct_relationships[$ct_table->name];
202 +
203 + // Remove all old relationships
204 + if( $remove_old ) {
205 + ct_delete_object_relationships( $object_id );
206 + }
207 +
208 + if( ! is_array( $term_id ) ) {
209 + $term_id = array( $term_id );
210 + }
211 +
212 + $object_id_field = $r->object_id;
213 + $term_id_field = $r->term_id;
214 +
215 + if( $r->type === 'single' ) {
216 + // Adds the relationship
217 + ct_insert_object( array(
218 + $object_id_field => $object_id,
219 + $term_id_field => $term_id[0],
220 + ) );
221 +
222 + } else if( $r->type === 'multiple' ) {
223 +
224 + foreach( $term_id as $id ) {
225 +
226 + // Adds the relationship
227 + ct_insert_object( array(
228 + $object_id_field => $object_id,
229 + $term_id_field => $id,
230 + ) );
231 +
232 + }
233 +
234 + }
235 +
236 + return true;
237 +}
238 +
239 +/**
240 + * Delete object relationships
241 + *
242 + * @since 1.0.0
243 + *
244 + * @param integer $object_id
245 + *
246 + * @return bool
247 + */
248 +function ct_delete_object_relationships( $object_id ) {
249 +
250 + global $ct_table, $ct_relationships, $wpdb;
251 +
252 + // Checks if relationship table exists
253 + if( ! isset( $ct_relationships[$ct_table->name] ) ) {
254 + return false;
255 + }
256 +
257 + $r = $ct_relationships[$ct_table->name];
258 +
259 + // Delete all object relationships
260 + $wpdb->query( $wpdb->prepare(
261 + "DELETE FROM {$ct_table->db->table_name} WHERE {$r->object_id} = %d",
262 + $object_id
263 + ) );
264 +
265 + return true;
266 +
267 +}
268 +
269 +/**
270 + * Get object relationships
271 + *
272 + * @since 1.0.0
273 + *
274 + * @param int|stdClass|null $object_id Object primary key.
275 + * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
276 + * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
277 + *
278 + * @return stdClass|array|null
279 + */
280 +function ct_get_object_relationships( $object_id, $output = OBJECT ) {
281 + global $ct_table, $ct_relationships, $wpdb;
282 +
283 + if( ! isset( $ct_relationships[$ct_table->name] ) ) {
284 + return null;
285 + }
286 +
287 + $r = $ct_relationships[$ct_table->name];
288 +
289 + // Get all relationships of the given object
290 + $relationships = $wpdb->get_results( $wpdb->prepare(
291 + "SELECT * FROM {$ct_table->db->table_name} WHERE {$r->object_id} = %d",
292 + $object_id
293 + ), $output );
294 +
295 + return $relationships;
296 +}
297 +
298 +/**
299 + * Get object relationships
300 + *
301 + * @since 1.0.0
302 + *
303 + * @param int|stdClass|null $object_id Object primary key.
304 + *
305 + * @return stdClass|array|null
306 + */
307 +function ct_get_object_relationships_count( $object_id ) {
308 + global $ct_table, $ct_relationships, $wpdb;
309 +
310 + if( ! isset( $ct_relationships[$ct_table->name] ) ) {
311 + return 0;
312 + }
313 +
314 + $r = $ct_relationships[$ct_table->name];
315 +
316 + // Get all relationships of the given object
317 + $count = absint( $wpdb->get_var( $wpdb->prepare(
318 + "SELECT COUNT({$ct_table->db->primary_key}) FROM {$ct_table->db->table_name} WHERE {$r->object_id} = %d",
319 + $object_id
320 + ) ) );
321 +
322 + return $count;
323 +}
324 +
325 +/**
326 + * Get term relationships
327 + *
328 + * @since 1.0.0
329 + *
330 + * @param int|stdClass|null $object_id Object primary key.
331 + * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
332 + * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
333 + *
334 + * @return stdClass|array|null
335 + */
336 +function ct_get_term_relationships( $term_id, $output = OBJECT ) {
337 + global $ct_table, $ct_relationships, $wpdb;
338 +
339 + if( ! isset( $ct_relationships[$ct_table->name] ) ) {
340 + return null;
341 + }
342 +
343 + $r = $ct_relationships[$ct_table->name];
344 +
345 + // Get all relationships of the given object
346 + $relationships = $wpdb->get_results( $wpdb->prepare(
347 + "SELECT * FROM {$ct_table->db->table_name} WHERE {$r->term_id} = %d",
348 + $term_id
349 + ), $output );
350 +
351 + return $relationships;
352 +}
353 +
354 +/**
355 + * Get term relationships
356 + *
357 + * @since 1.0.0
358 + *
359 + * @param int|stdClass|null $object_id Object primary key.
360 + *
361 + * @return stdClass|array|null
362 + */
363 +function ct_get_term_relationships_count( $term_id ) {
364 + global $ct_table, $ct_relationships, $wpdb;
365 +
366 + if( ! isset( $ct_relationships[$ct_table->name] ) ) {
367 + return 0;
368 + }
369 +
370 + $r = $ct_relationships[$ct_table->name];
371 +
372 + // Get all relationships of the given object
373 + $count = absint( $wpdb->get_var( $wpdb->prepare(
374 + "SELECT COUNT({$ct_table->db->primary_key}) FROM {$ct_table->db->table_name} WHERE {$r->term_id} = %d",
375 + $term_id
376 + ) ) );
377 +
378 + return $count;
379 +}
380 +
381 +/**
382 + * Get object relationships
383 + *
384 + * @since 1.0.0
385 + *
386 + * @param int|stdClass|null $object_id Object primary key.
387 + * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
388 + * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
389 + *
390 + * @return stdClass|array|null
391 + */
392 +function ct_get_object_terms_ids( $object_id, $output = OBJECT ) {
393 + global $ct_table, $ct_relationships, $wpdb;
394 +
395 + if( ! isset( $ct_relationships[$ct_table->name] ) ) {
396 + return null;
397 + }
398 +
399 + $r = $ct_relationships[$ct_table->name];
400 +
401 + $limit = '';
402 +
403 + if( $r->type === 'single' ) {
404 + $limit = 'LIMIT 1';
405 + }
406 +
407 + // Get all terms ids from the relationship table
408 + $terms_ids = $wpdb->get_col( $wpdb->prepare(
409 + "SELECT {$r->term_id} FROM {$ct_table->db->table_name} WHERE {$r->object_id} = %d {$limit}",
410 + $object_id
411 + ) );
412 +
413 + return $terms_ids;
414 +}
415 +
416 +/**
417 + * Get object terms
418 + *
419 + * @since 1.0.0
420 + *
421 + * @param int|stdClass|null $object_id Object primary key.
422 + * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
423 + * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
424 + *
425 + * @return stdClass|array|null
426 + */
427 +function ct_get_object_terms( $object_id, $output = OBJECT ) {
428 + global $ct_table, $ct_registered_tables, $ct_relationships, $wpdb;
429 +
430 + if( ! isset( $ct_relationships[$ct_table->name] ) ) {
431 + return null;
432 + }
433 +
434 + $r = $ct_relationships[$ct_table->name];
435 +
436 + $terms_ids = ct_get_object_terms_ids( $object_id, ARRAY_N );
437 +
438 + if( ! count( $terms_ids ) ) {
439 + return null;
440 + }
441 +
442 + $table_name = str_replace( '_relationships', '', $ct_table->name );
443 +
444 + $term_table = $ct_registered_tables[$table_name];
445 + $in_pattern = implode(', ', array_fill( 0, count($terms_ids), '%d' ) );
446 +
447 + $limit = '';
448 +
449 + if( $r->type === 'single' ) {
450 + $limit = 'LIMIT 1';
451 + }
452 +
453 + // Get all relationships of the given object
454 + $terms = $wpdb->get_results( $wpdb->prepare(
455 + "SELECT * FROM {$term_table->db->table_name} WHERE {$term_table->db->primary_key} IN ({$in_pattern}) {$limit}",
456 + $terms_ids
457 + ), $output );
458 +
459 + return $terms;
460 +}