PluginProbe
bbPress / 2.6.15
bbPress v2.6.15
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.15, at includes/core/functions.php

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