PluginProbe
bbPress / trunk
bbPress vtrunk
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 trunk, at includes/core/functions.php

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