forked from imath/bp-groups-taxo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbp-groups-taxonomy.php
409 lines (353 loc) · 9.48 KB
/
bp-groups-taxonomy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'BP_Groups_Terms' ) ) :
class BP_Groups_Terms {
public static $wp_term_taxonomy = '';
public static $wp_term_relationships = '';
public static $wp_terms = '';
public static $bp_term_taxonomy = '';
public static $bp_term_relationships = '';
public static $bp_terms = '';
/**
* Start the class
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*/
public static function start() {
$bp = buddypress();
if ( empty( $bp->groups->terms ) ) {
$bp->groups->terms = new self;
}
return $bp->groups->terms;
}
/**
* Constructor
*
* @access public
* @since BP Groups Taxo (1.0.0)
*/
public function __construct() {
$this->setup_globals();
}
/**
* Set globals
*
* @access public
* @since BP Groups Taxo (1.0.0)
*/
public function setup_globals() {
global $wpdb;
self::$wp_term_taxonomy = $wpdb->term_taxonomy;
self::$wp_term_relationships = $wpdb->term_relationships;
self::$wp_terms = $wpdb->terms;
$bp_prefix = bp_core_get_table_prefix();
self::$bp_term_taxonomy = $bp_prefix . 'term_taxonomy';
self::$bp_term_relationships = $bp_prefix . 'term_relationships';
self::$bp_terms = $bp_prefix . 'terms';
}
/**
* Set needed $wpdb->tables to be the one of root blog id
*
* This is needed for Multisite configs in case a groups tag
* loop is build from a child blog.
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*/
public static function set_tables() {
global $wpdb;
$wpdb->term_taxonomy = self::$bp_term_taxonomy;
$wpdb->term_relationships = self::$bp_term_relationships;
$wpdb->terms = self::$bp_terms;
}
/**
* Reset $wpdb->tables to the one set by WordPress
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*/
public static function reset_tables() {
global $wpdb;
$wpdb->term_taxonomy = self::$wp_term_taxonomy;
$wpdb->term_relationships = self::$wp_term_relationships;
$wpdb->terms = self::$wp_terms;
}
/**
* Update term count
* Hidden groups mustn't be in the taxonomy count
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*/
public static function update_term_count( $terms, $taxonomy = 'bp_group_tags' ) {
global $wpdb;
$bp = buddypress();
self::set_tables();
if ( ! is_object( $taxonomy ) ) {
$taxonomy = get_taxonomy( $taxonomy );
}
if ( empty( $taxonomy ) ) {
return;
}
$object_types = (array) $taxonomy->object_type;
if ( false === array_search( 'bp_group', $object_types ) ) {
_update_generic_term_count( $terms, $taxonomy );
} else {
$other_type = array_diff( $object_types, array( 'bp_group' ) );
$sql_get = array(
'select' => "SELECT COUNT(*) FROM {$wpdb->term_relationships} tr, {$bp->groups->table_name} g",
'where' => array(
'join' => 'g.id = tr.object_id',
'status' => $wpdb->prepare( 'g.status != %s', 'hidden' ),
)
);
// Update term count for group tags
foreach ( (array) $terms as $term ) {
$count = 0;
$sql_get['where']['term_taxo_id'] = $wpdb->prepare( 'term_taxonomy_id = %d', $term );
$count += (int) $wpdb->get_var( $sql_get['select'] . ' WHERE ' . join( ' AND ', $sql_get['where'] ) );
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
}
// If somebody use this taxonomy, he'll have to handle the term count
if ( ! empty( $other_type ) ) {
do_action( 'bp_groups_taxo_terms_update_count', $terms, $taxonomy );
}
}
self::reset_tables();
}
/**
* Get group tags
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses wp_get_object_terms()
*/
public static function get_object_terms( $object_ids, $taxonomies = 'bp_group_tags', $args = array() ) {
global $wpdb;
self::set_tables();
$return = wp_get_object_terms( $object_ids, $taxonomies, $args );
self::reset_tables();
return $return;
}
/**
* Set group tags
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses wp_set_object_terms()
*/
public static function set_object_terms( $object_id, $terms, $taxonomy = 'bp_group_tags', $append = false ) {
global $wpdb;
self::set_tables();
$return = wp_set_object_terms( $object_id, $terms, $taxonomy, $append );
self::reset_tables();
return $return;
}
/**
* Insert a term based on arguments provided.
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses wp_insert_term()
*/
public static function insert_term( $term, $taxonomy, $args = array() ) {
self::set_tables();
$return = wp_insert_term( $term, $taxonomy, $args );
self::reset_tables();
return $return;
}
/**
* Update term based on arguments provided.
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses wp_update_term()
*/
public static function update_term( $term_id, $taxonomy, $args = array() ) {
self::set_tables();
$return = wp_update_term( $term_id, $taxonomy, $args );
self::reset_tables();
return $return;
}
/**
* Remove group tags
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses wp_remove_object_terms()
*/
public static function remove_object_terms( $object_id, $terms, $taxonomy = 'bp_group_tags' ) {
global $wpdb;
self::set_tables();
$return = wp_remove_object_terms( $object_id, $terms, $taxonomy );
self::reset_tables();
return $return;
}
/**
* Remove all group relationships
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses wp_delete_object_term_relationships()
*/
public static function delete_object_term_relationships( $object_id, $taxonomies = 'bp_group_tags' ) {
global $wpdb;
self::set_tables();
$return = wp_delete_object_term_relationships( $object_id, $taxonomies );
self::reset_tables();
return $return;
}
/**
* Delete term based on arguments provided.
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses wp_delete_term()
*/
public static function delete_term( $term_id, $taxonomy, $args = array() ) {
self::set_tables();
$return = wp_delete_term( $term_id, $taxonomy, $args );
self::reset_tables();
return $return;
}
/**
* Get group ids for a given tag
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses get_objects_in_term()
*/
public static function get_objects_in_term( $term_ids, $taxonomies = 'bp_group_tags', $args = array() ) {
global $wpdb;
self::set_tables();
$return = get_objects_in_term( $term_ids, $taxonomies, $args );
self::reset_tables();
return $return;
}
/**
* Get all Term data from database by Term ID
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses get_term()
*/
public static function get_term( $term, $taxonomy, $output = OBJECT, $filter = 'raw' ) {
self::set_tables();
$return = get_term( $term, $taxonomy, $output, $filter );
self::reset_tables();
return $return;
}
/**
* Get all terms for a given taxonomy
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses get_terms()
*/
public static function get_terms( $taxonomies = 'bp_group_tags', $args = '' ) {
global $wpdb;
self::set_tables();
$return = get_terms( $taxonomies, $args );
self::reset_tables();
return $return;
}
/**
* Get term thanks to a specific field
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses get_term_by()
*/
public static function get_term_by( $field, $value, $taxonomy = 'bp_group_tags', $output = OBJECT, $filter = 'raw' ) {
global $wpdb;
self::set_tables();
$return = get_term_by( $field, $value, $taxonomy, $output, $filter = 'raw' );
self::reset_tables();
return $return;
}
/**
* Get the term link
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses get_term_link()
*/
public static function get_term_link( $term, $taxonomy = 'bp_group_tags' ) {
global $wpdb;
self::set_tables();
$return = get_term_link( $term, $taxonomy );
self::reset_tables();
return $return;
}
/**
* Copy WordPress get_the_term_list without using get_the_terms()
* function as it checks for an existing post.
*
* @access public
* @since BP Groups Taxo (1.0.0)
* @static
*
* @uses self::get_object_terms()
* @uses self::get_term_link()
*/
public static function get_the_term_list( $group_id, $taxonomy = 'bp_group_tags', $before = '', $sep = '', $after = '', $description = 0, $admin_link = '' ) {
$terms = self::get_object_terms( $group_id, $taxonomy );
if ( is_wp_error( $terms ) ) {
return $terms;
}
if ( empty( $terms ) ) {
return false;
}
foreach ( $terms as $term ) {
// make sure to reset at each pass
$term_link = array();
if ( empty( $admin_link ) ) {
$link = self::get_term_link( $term, $taxonomy );
} else {
$link = add_query_arg( 'tag', $term->slug, $admin_link );
}
if ( is_wp_error( $link ) ) {
return $link;
}
$term_link[] = 'href = "' . esc_url( $link ) . '"';
if ( ! empty( $description ) ) {
$term_link[] = 'title="' . esc_attr( wp_trim_words( $term->description, (int) $description ) ) . '"';
}
$term_links[] = '<a ' . join( ' ', $term_link ) . ' rel="tag">' . esc_html( $term->name ) . '</a>';
}
$term_links = apply_filters( "term_links-$taxonomy", $term_links );
return $before . join( $sep, $term_links ) . $after;
}
}
endif;
add_action( 'bp_init', array( 'BP_Groups_Terms', 'start' ), 11 );