PluginProbe
bbPress / 2.6.6
bbPress v2.6.6
trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.2.2 All 71 releases
bbpress / includes / core / functions.php

functions.php in bbPress 2.6.6, at includes/core/functions.php

658 lines 14.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Core Functions
5 *
6 * @package bbPress
7 * @subpackage Functions
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 /** Versions ******************************************************************/
14
15 /**
16 * Output the bbPress version
17 *
18 * @since 2.0.0 bbPress (r3468)
19 */
20 function bbp_version() {
21 echo bbp_get_version();
22 }
23 /**
24 * Return the bbPress version
25 *
26 * @since 2.0.0 bbPress (r3468)
27 *
28 * @retrun string The bbPress version
29 */
30 function bbp_get_version() {
31 return bbpress()->version;
32 }
33
34 /**
35 * Output the bbPress database version
36 *
37 * @since 2.0.0 bbPress (r3468)
38 */
39 function bbp_db_version() {
40 echo bbp_get_db_version();
41 }
42 /**
43 * Return the bbPress database version
44 *
45 * @since 2.0.0 bbPress (r3468)
46 *
47 * @retrun string The bbPress version
48 */
49 function bbp_get_db_version() {
50 return bbpress()->db_version;
51 }
52
53 /**
54 * Output the bbPress database version directly from the database
55 *
56 * @since 2.0.0 bbPress (r3468)
57 */
58 function bbp_db_version_raw() {
59 echo bbp_get_db_version_raw();
60 }
61 /**
62 * Return the bbPress database version directly from the database
63 *
64 * @since 2.0.0 bbPress (r3468)
65 *
66 * @retrun string The current bbPress version
67 */
68 function bbp_get_db_version_raw() {
69 return get_option( '_bbp_db_version', '' );
70 }
71
72 /** Post Meta *****************************************************************/
73
74 /**
75 * Update the forum meta ID of a post
76 *
77 * @since 2.0.0 bbPress (r3181)
78 *
79 * @param int $post_id The post to update
80 * @param int $forum_id The forum
81 */
82 function bbp_update_forum_id( $post_id = 0, $forum_id = 0 ) {
83
84 // Allow the forum ID to be updated 'just in time' before save
85 $forum_id = (int) apply_filters( 'bbp_update_forum_id', $forum_id, $post_id );
86
87 // Update the post meta forum ID
88 update_post_meta( $post_id, '_bbp_forum_id', $forum_id );
89
90 return $forum_id;
91 }
92
93 /**
94 * Update the topic meta ID of a post
95 *
96 * @since 2.0.0 bbPress (r3181)
97 *
98 * @param int $post_id The post to update
99 * @param int $topic_id The topic
100 */
101 function bbp_update_topic_id( $post_id = 0, $topic_id = 0 ) {
102
103 // Allow the topic ID to be updated 'just in time' before save
104 $topic_id = (int) apply_filters( 'bbp_update_topic_id', $topic_id, $post_id );
105
106 // Update the post meta topic ID
107 update_post_meta( $post_id, '_bbp_topic_id', $topic_id );
108
109 return $topic_id;
110 }
111
112 /**
113 * Update the reply meta ID of a post
114 *
115 * @since 2.0.0 bbPress (r3181)
116 *
117 * @param int $post_id The post to update
118 * @param int $reply_id The reply
119 */
120 function bbp_update_reply_id( $post_id = 0, $reply_id = 0 ) {
121
122 // Allow the reply ID to be updated 'just in time' before save
123 $reply_id = (int) apply_filters( 'bbp_update_reply_id', $reply_id, $post_id );
124
125 // Update the post meta reply ID
126 update_post_meta( $post_id, '_bbp_reply_id', $reply_id );
127
128 return $reply_id;
129 }
130
131 /**
132 * Update the reply-to meta ID of a post
133 *
134 * @since 2.6.0 bbPress (r5735)
135 *
136 * @param int $post_id The post to update
137 * @param int $reply_id The reply ID
138 */
139 function bbp_update_reply_to_id( $post_id = 0, $reply_id = 0 ) {
140
141 // Allow the reply ID to be updated 'just in time' before save
142 $reply_id = (int) apply_filters( 'bbp_update_reply_to_id', $reply_id, $post_id );
143
144 // Update the post meta reply ID
145 update_post_meta( $post_id, '_bbp_reply_to', $reply_id );
146
147 return $reply_id;
148 }
149
150 /** Views *********************************************************************/
151
152 /**
153 * Get the registered views
154 *
155 * Does nothing much other than return the {@link $bbp->views} variable
156 *
157 * @since 2.0.0 bbPress (r2789)
158 *
159 * @return array Views
160 */
161 function bbp_get_views() {
162 return bbpress()->views;
163 }
164
165 /**
166 * Register a bbPress view
167 *
168 * @since 2.0.0 bbPress (r2789)
169 *
170 * @param string $view View name
171 * @param string $title View title
172 * @param mixed $query_args {@link bbp_has_topics()} arguments.
173 * @param bool $feed Have a feed for the view? Defaults to true.
174 * @param string $capability Capability that the current user must have
175 *
176 * @return array The just registered (but processed) view
177 */
178 function bbp_register_view( $view, $title, $query_args = '', $feed = true, $capability = '' ) {
179
180 // Bail if user does not have capability
181 if ( ! empty( $capability ) && ! current_user_can( $capability ) ) {
182 return false;
183 }
184
185 $bbp = bbpress();
186 $view = sanitize_title( $view );
187 $title = esc_html( $title );
188
189 if ( empty( $view ) || empty( $title ) ) {
190 return false;
191 }
192
193 $query_args = bbp_parse_args( $query_args, '', 'register_view' );
194
195 // Set show_stickies to false if it wasn't supplied
196 if ( ! isset( $query_args['show_stickies'] ) ) {
197 $query_args['show_stickies'] = false;
198 }
199
200 $bbp->views[ $view ] = array(
201 'title' => $title,
202 'query' => $query_args,
203 'feed' => $feed
204 );
205
206 return $bbp->views[ $view ];
207 }
208
209 /**
210 * Deregister a bbPress view
211 *
212 * @since 2.0.0 bbPress (r2789)
213 *
214 * @param string $view View name
215 * @return bool False if the view doesn't exist, true on success
216 */
217 function bbp_deregister_view( $view ) {
218 $bbp = bbpress();
219 $view = sanitize_title( $view );
220
221 if ( ! isset( $bbp->views[ $view ] ) ) {
222 return false;
223 }
224
225 unset( $bbp->views[ $view ] );
226
227 return true;
228 }
229
230 /**
231 * Run the query of a topic-view
232 *
233 * @since 2.0.0 bbPress (r2789)
234 *
235 * @param string $view Optional. View id
236 * @param mixed $new_args New arguments. See {@link bbp_has_topics()}
237 * @return bool False if the view doesn't exist, otherwise if topics are there
238 */
239 function bbp_view_query( $view = '', $new_args = '' ) {
240
241 // Get view, or bail
242 $view = bbp_get_view_id( $view );
243 if ( empty( $view ) ) {
244 return false;
245 }
246
247 $query_args = bbp_get_view_query_args( $view );
248
249 if ( ! empty( $new_args ) ) {
250 $new_args = bbp_parse_args( $new_args, '', 'view_query' );
251 $query_args = array_merge( $query_args, $new_args );
252 }
253
254 return bbp_has_topics( $query_args );
255 }
256
257 /**
258 * Return the query arguments of a topic-view
259 *
260 * @since 2.0.0 bbPress (r2789)
261 *
262 * @param string $view View name
263 * @return array Query arguments
264 */
265 function bbp_get_view_query_args( $view = '' ) {
266 $bbp = bbpress();
267 $view = bbp_get_view_id( $view );
268 $retval = ! empty( $view ) && ! empty( $bbp->views[ $view ] )
269 ? $bbp->views[ $view ]['query']
270 : array();
271
272 // Filter & return
273 return (array) apply_filters( 'bbp_get_view_query_args', $retval, $view );
274 }
275
276 /** Errors ********************************************************************/
277
278 /**
279 * Adds an error message to later be output in the theme
280 *
281 * @since 2.0.0 bbPress (r3381)
282 *
283 * @see WP_Error()
284 *
285 * @param string $code Unique code for the error message
286 * @param string $message Translated error message
287 * @param string $data Any additional data passed with the error message
288 */
289 function bbp_add_error( $code = '', $message = '', $data = '' ) {
290 bbpress()->errors->add( $code, $message, $data );
291 }
292
293 /**
294 * Check if error messages exist in queue
295 *
296 * @since 2.0.0 bbPress (r3381)
297 *
298 * @see WP_Error()
299 */
300 function bbp_has_errors() {
301 $has_errors = bbpress()->errors->get_error_codes()
302 ? true
303 : false;
304
305 return (bool) apply_filters( 'bbp_has_errors', $has_errors, bbpress()->errors );
306 }
307
308 /** Mentions ******************************************************************/
309
310 /**
311 * Set the pattern used for matching usernames for mentions.
312 *
313 * Moved into its own function to allow filtering of the regex pattern
314 * anywhere mentions might be used.
315 *
316 * @since 2.4.0 bbPress (r4997)
317 * @deprecated 2.6.0 bbp_make_clickable()
318 *
319 * @return string Pattern to match usernames with
320 */
321 function bbp_find_mentions_pattern() {
322
323 // Filter & return
324 return apply_filters( 'bbp_find_mentions_pattern', '/[@]+([A-Za-z0-9-_\.@]+)\b/' );
325 }
326
327 /**
328 * Searches through the content to locate usernames, designated by an @ sign.
329 *
330 * @since 2.2.0 bbPress (r4323)
331 * @deprecated 2.6.0 bbp_make_clickable()
332 *
333 * @param string $content The content
334 * @return bool|array $usernames Existing usernames. False if no matches.
335 */
336 function bbp_find_mentions( $content = '' ) {
337 $pattern = bbp_find_mentions_pattern();
338 preg_match_all( $pattern, $content, $usernames );
339 $usernames = array_unique( array_filter( $usernames[1] ) );
340
341 // Bail if no usernames
342 if ( empty( $usernames ) ) {
343 $usernames = false;
344 }
345
346 // Filter & return
347 return apply_filters( 'bbp_find_mentions', $usernames, $pattern, $content );
348 }
349
350 /**
351 * Finds and links @-mentioned users in the content
352 *
353 * @since 2.2.0 bbPress (r4323)
354 * @deprecated 2.6.0 bbp_make_clickable()
355 *
356 * @return string $content Content filtered for mentions
357 */
358 function bbp_mention_filter( $content = '' ) {
359
360 // Get Usernames and bail if none exist
361 $usernames = bbp_find_mentions( $content );
362 if ( empty( $usernames ) ) {
363 return $content;
364 }
365
366 // Loop through usernames and link to profiles
367 foreach ( (array) $usernames as $username ) {
368
369 // Skip if username does not exist or user is not active
370 $user = get_user_by( 'slug', $username );
371 if ( empty( $user->ID ) || bbp_is_user_inactive( $user->ID ) ) {
372 continue;
373 }
374
375 // Link
376 $profile_url = bbp_get_user_profile_url( $user->ID );
377 $profile_link = sprintf( '<a href="%1$s">@%2$s</a>', esc_url( $profile_url ), esc_html( $username ) );
378 $no_followed = bbp_rel_nofollow( $profile_link );
379 $pattern = "/(@{$username}\b)/";
380
381 // Replace name in content
382 $content = preg_replace( $pattern, $no_followed, $content );
383 }
384
385 // Return modified content
386 return $content;
387 }
388
389 /** Post Statuses *************************************************************/
390
391 /**
392 * Return the public post status ID
393 *
394 * @since 2.0.0 bbPress (r3504)
395 *
396 * @return string
397 */
398 function bbp_get_public_status_id() {
399 return bbpress()->public_status_id;
400 }
401
402 /**
403 * Return the pending post status ID
404 *
405 * @since 2.1.0 bbPress (r3581)
406 *
407 * @return string
408 */
409 function bbp_get_pending_status_id() {
410 return bbpress()->pending_status_id;
411 }
412
413 /**
414 * Return the private post status ID
415 *
416 * @since 2.0.0 bbPress (r3504)
417 *
418 * @return string
419 */
420 function bbp_get_private_status_id() {
421 return bbpress()->private_status_id;
422 }
423
424 /**
425 * Return the hidden post status ID
426 *
427 * @since 2.0.0 bbPress (r3504)
428 *
429 * @return string
430 */
431 function bbp_get_hidden_status_id() {
432 return bbpress()->hidden_status_id;
433 }
434
435 /**
436 * Return the closed post status ID
437 *
438 * @since 2.0.0 bbPress (r3504)
439 *
440 * @return string
441 */
442 function bbp_get_closed_status_id() {
443 return bbpress()->closed_status_id;
444 }
445
446 /**
447 * Return the spam post status ID
448 *
449 * @since 2.0.0 bbPress (r3504)
450 *
451 * @return string
452 */
453 function bbp_get_spam_status_id() {
454 return bbpress()->spam_status_id;
455 }
456
457 /**
458 * Return the trash post status ID
459 *
460 * @since 2.0.0 bbPress (r3504)
461 *
462 * @return string
463 */
464 function bbp_get_trash_status_id() {
465 return bbpress()->trash_status_id;
466 }
467
468 /**
469 * Return the orphan post status ID
470 *
471 * @since 2.0.0 bbPress (r3504)
472 *
473 * @return string
474 */
475 function bbp_get_orphan_status_id() {
476 return bbpress()->orphan_status_id;
477 }
478
479 /** Rewrite IDs ***************************************************************/
480
481 /**
482 * Return the unique ID for user profile rewrite rules
483 *
484 * @since 2.1.0 bbPress (r3762)
485 *
486 * @return string
487 */
488 function bbp_get_user_rewrite_id() {
489 return bbpress()->user_id;
490 }
491
492 /**
493 * Return the unique ID for all edit rewrite rules (forum|topic|reply|tag|user)
494 *
495 * @since 2.1.0 bbPress (r3762)
496 *
497 * @return string
498 */
499 function bbp_get_edit_rewrite_id() {
500 return bbpress()->edit_id;
501 }
502
503 /**
504 * Return the unique ID for all search rewrite rules
505 *
506 * @since 2.3.0 bbPress (r4579)
507 *
508 * @return string
509 */
510 function bbp_get_search_rewrite_id() {
511 return bbpress()->search_id;
512 }
513
514 /**
515 * Return the unique ID for user topics rewrite rules
516 *
517 * @since 2.2.0 bbPress (r4321)
518 *
519 * @return string
520 */
521 function bbp_get_user_topics_rewrite_id() {
522 return bbpress()->tops_id;
523 }
524
525 /**
526 * Return the unique ID for user replies rewrite rules
527 *
528 * @since 2.2.0 bbPress (r4321)
529 *
530 * @return string
531 */
532 function bbp_get_user_replies_rewrite_id() {
533 return bbpress()->reps_id;
534 }
535
536 /**
537 * Return the unique ID for user favorites rewrite rules
538 *
539 * @since 2.2.0 bbPress (r4181)
540 *
541 * @return string
542 */
543 function bbp_get_user_favorites_rewrite_id() {
544 return bbpress()->favs_id;
545 }
546
547 /**
548 * Return the unique ID for user subscriptions rewrite rules
549 *
550 * @since 2.2.0 bbPress (r4181)
551 *
552 * @return string
553 */
554 function bbp_get_user_subscriptions_rewrite_id() {
555 return bbpress()->subs_id;
556 }
557
558 /**
559 * Return the unique ID for user engagement rewrite rules
560 *
561 * @since 2.6.0 bbPress (r6320)
562 *
563 * @return string
564 */
565 function bbp_get_user_engagements_rewrite_id() {
566 return bbpress()->engagements_id;
567 }
568
569 /**
570 * Return the unique ID for topic view rewrite rules
571 *
572 * @since 2.1.0 bbPress (r3762)
573 *
574 * @return string
575 */
576 function bbp_get_view_rewrite_id() {
577 return bbpress()->view_id;
578 }
579
580 /** Rewrite Extras ************************************************************/
581
582 /**
583 * Get the id used for paginated requests
584 *
585 * @since 2.4.0 bbPress (r4926)
586 *
587 * @return string
588 */
589 function bbp_get_paged_rewrite_id() {
590 return bbpress()->paged_id;
591 }
592
593 /**
594 * Delete a blogs rewrite rules, so that they are automatically rebuilt on
595 * the subsequent page load.
596 *
597 * @since 2.2.0 bbPress (r4198)
598 */
599 function bbp_delete_rewrite_rules() {
600 delete_option( 'rewrite_rules' );
601 }
602
603 /** Requests ******************************************************************/
604
605 /**
606 * Return true|false if this is a POST request
607 *
608 * @since 2.3.0 bbPress (r4790)
609 *
610 * @return bool
611 */
612 function bbp_is_post_request() {
613 return (bool) ( 'POST' === strtoupper( $_SERVER['REQUEST_METHOD'] ) );
614 }
615
616 /**
617 * Return true|false if this is a GET request
618 *
619 * @since 2.3.0 bbPress (r4790)
620 *
621 * @return bool
622 */
623 function bbp_is_get_request() {
624 return (bool) ( 'GET' === strtoupper( $_SERVER['REQUEST_METHOD'] ) );
625 }
626
627 /** Redirection ***************************************************************/
628
629 /**
630 * Perform a safe, local redirect somewhere inside the current site
631 *
632 * On some setups, passing the value of wp_get_referer() may result in an empty
633 * value for $location, which results in an error on redirection. If $location
634 * is empty, we can safely redirect back to the forum root. This might change
635 * in a future version, possibly to the site root.
636 *
637 * @since 2.6.0 bbPress (r5658)
638 *
639 * @see bbp_redirect_to_field()
640 *
641 * @param string $location The URL to redirect the user to.
642 * @param int $status Optional. The numeric code to give in the redirect
643 * headers. Default: 302.
644 */
645 function bbp_redirect( $location = '', $status = 302 ) {
646
647 // Prevent errors from empty $location
648 if ( empty( $location ) ) {
649 $location = bbp_get_forums_url();
650 }
651
652 // Setup the safe redirect
653 wp_safe_redirect( $location, $status );
654
655 // Exit so the redirect takes place immediately
656 exit();
657 }
658