web/wp-content/plugins/bbpress/includes/core/functions.php
changeset 196 5e8dcbe22c24
equal deleted inserted replaced
195:c7c0fbc09788 196:5e8dcbe22c24
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * bbPress Core Functions
       
     5  *
       
     6  * @package bbPress
       
     7  * @subpackage Functions
       
     8  */
       
     9 
       
    10 // Exit if accessed directly
       
    11 if ( !defined( 'ABSPATH' ) ) exit;
       
    12 
       
    13 /** Versions ******************************************************************/
       
    14 
       
    15 /**
       
    16  * Output the bbPress version
       
    17  *
       
    18  * @since bbPress (r3468)
       
    19  * @uses bbp_get_version() To get the bbPress version
       
    20  */
       
    21 function bbp_version() {
       
    22 	echo bbp_get_version();
       
    23 }
       
    24 	/**
       
    25 	 * Return the bbPress version
       
    26 	 *
       
    27 	 * @since bbPress (r3468)
       
    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 bbPress (r3468)
       
    38  * @uses bbp_get_version() To get the bbPress version
       
    39  */
       
    40 function bbp_db_version() {
       
    41 	echo bbp_get_db_version();
       
    42 }
       
    43 	/**
       
    44 	 * Return the bbPress database version
       
    45 	 *
       
    46 	 * @since bbPress (r3468)
       
    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 bbPress (r3468)
       
    57  * @uses bbp_get_version() To get the current bbPress version
       
    58  */
       
    59 function bbp_db_version_raw() {
       
    60 	echo bbp_get_db_version_raw();
       
    61 }
       
    62 	/**
       
    63 	 * Return the bbPress database version directly from the database
       
    64 	 *
       
    65 	 * @since bbPress (r3468)
       
    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 a posts forum meta ID
       
    76  *
       
    77  * @since 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, $forum_id ) {
       
    83 
       
    84 	// Allow the forum ID to be updated 'just in time' before save
       
    85 	$forum_id = 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', (int) $forum_id );
       
    89 }
       
    90 
       
    91 /**
       
    92  * Update a posts topic meta ID
       
    93  *
       
    94  * @since bbPress (r3181)
       
    95  *
       
    96  * @param int $post_id The post to update
       
    97  * @param int $forum_id The forum
       
    98  */
       
    99 function bbp_update_topic_id( $post_id, $topic_id ) {
       
   100 
       
   101 	// Allow the topic ID to be updated 'just in time' before save
       
   102 	$topic_id = apply_filters( 'bbp_update_topic_id', $topic_id, $post_id );
       
   103 
       
   104 	// Update the post meta topic ID
       
   105 	update_post_meta( $post_id, '_bbp_topic_id', (int) $topic_id );
       
   106 }
       
   107 
       
   108 /**
       
   109  * Update a posts reply meta ID
       
   110  *
       
   111  * @since bbPress (r3181)
       
   112  *
       
   113  * @param int $post_id The post to update
       
   114  * @param int $forum_id The forum
       
   115  */
       
   116 function bbp_update_reply_id( $post_id, $reply_id ) {
       
   117 
       
   118 	// Allow the reply ID to be updated 'just in time' before save
       
   119 	$reply_id = apply_filters( 'bbp_update_reply_id', $reply_id, $post_id );
       
   120 
       
   121 	// Update the post meta reply ID
       
   122 	update_post_meta( $post_id, '_bbp_reply_id',(int) $reply_id );
       
   123 }
       
   124 
       
   125 /** Views *********************************************************************/
       
   126 
       
   127 /**
       
   128  * Get the registered views
       
   129  *
       
   130  * Does nothing much other than return the {@link $bbp->views} variable
       
   131  *
       
   132  * @since bbPress (r2789)
       
   133  *
       
   134  * @return array Views
       
   135  */
       
   136 function bbp_get_views() {
       
   137 	return bbpress()->views;
       
   138 }
       
   139 
       
   140 /**
       
   141  * Register a bbPress view
       
   142  *
       
   143  * @todo Implement feeds - See {@link http://trac.bbpress.org/ticket/1422}
       
   144  *
       
   145  * @since bbPress (r2789)
       
   146  *
       
   147  * @param string $view View name
       
   148  * @param string $title View title
       
   149  * @param mixed $query_args {@link bbp_has_topics()} arguments.
       
   150  * @param bool $feed Have a feed for the view? Defaults to true. NOT IMPLEMENTED
       
   151  * @param string $capability Capability that the current user must have
       
   152  * @uses sanitize_title() To sanitize the view name
       
   153  * @uses esc_html() To sanitize the view title
       
   154  * @return array The just registered (but processed) view
       
   155  */
       
   156 function bbp_register_view( $view, $title, $query_args = '', $feed = true, $capability = '' ) {
       
   157 
       
   158 	// Bail if user does not have capability
       
   159 	if ( ! empty( $capability ) && ! current_user_can( $capability ) )
       
   160 		return false;
       
   161 
       
   162 	$bbp   = bbpress();
       
   163 	$view  = sanitize_title( $view );
       
   164 	$title = esc_html( $title );
       
   165 
       
   166 	if ( empty( $view ) || empty( $title ) )
       
   167 		return false;
       
   168 
       
   169 	$query_args = bbp_parse_args( $query_args, '', 'register_view' );
       
   170 
       
   171 	// Set show_stickies to false if it wasn't supplied
       
   172 	if ( !isset( $query_args['show_stickies'] ) )
       
   173 		$query_args['show_stickies'] = false;
       
   174 
       
   175 	$bbp->views[$view] = array(
       
   176 		'title'  => $title,
       
   177 		'query'  => $query_args,
       
   178 		'feed'   => $feed
       
   179 	);
       
   180 
       
   181 	return $bbp->views[$view];
       
   182 }
       
   183 
       
   184 /**
       
   185  * Deregister a bbPress view
       
   186  *
       
   187  * @since bbPress (r2789)
       
   188  *
       
   189  * @param string $view View name
       
   190  * @uses sanitize_title() To sanitize the view name
       
   191  * @return bool False if the view doesn't exist, true on success
       
   192  */
       
   193 function bbp_deregister_view( $view ) {
       
   194 	$bbp  = bbpress();
       
   195 	$view = sanitize_title( $view );
       
   196 
       
   197 	if ( !isset( $bbp->views[$view] ) )
       
   198 		return false;
       
   199 
       
   200 	unset( $bbp->views[$view] );
       
   201 
       
   202 	return true;
       
   203 }
       
   204 
       
   205 /**
       
   206  * Run the view's query
       
   207  *
       
   208  * @since bbPress (r2789)
       
   209  *
       
   210  * @param string $view Optional. View id
       
   211  * @param mixed $new_args New arguments. See {@link bbp_has_topics()}
       
   212  * @uses bbp_get_view_id() To get the view id
       
   213  * @uses bbp_get_view_query_args() To get the view query args
       
   214  * @uses sanitize_title() To sanitize the view name
       
   215  * @uses bbp_has_topics() To make the topics query
       
   216  * @return bool False if the view doesn't exist, otherwise if topics are there
       
   217  */
       
   218 function bbp_view_query( $view = '', $new_args = '' ) {
       
   219 
       
   220 	$view = bbp_get_view_id( $view );
       
   221 	if ( empty( $view ) )
       
   222 		return false;
       
   223 
       
   224 	$query_args = bbp_get_view_query_args( $view );
       
   225 
       
   226 	if ( !empty( $new_args ) ) {
       
   227 		$new_args   = bbp_parse_args( $new_args, '', 'view_query' );
       
   228 		$query_args = array_merge( $query_args, $new_args );
       
   229 	}
       
   230 
       
   231 	return bbp_has_topics( $query_args );
       
   232 }
       
   233 
       
   234 /**
       
   235  * Return the view's query arguments
       
   236  *
       
   237  * @since bbPress (r2789)
       
   238  *
       
   239  * @param string $view View name
       
   240  * @uses bbp_get_view_id() To get the view id
       
   241  * @return array Query arguments
       
   242  */
       
   243 function bbp_get_view_query_args( $view ) {
       
   244 	$view   = bbp_get_view_id( $view );
       
   245 	$retval = !empty( $view ) ? bbpress()->views[$view]['query'] : false;
       
   246 
       
   247 	return apply_filters( 'bbp_get_view_query_args', $retval, $view );
       
   248 }
       
   249 
       
   250 /** Errors ********************************************************************/
       
   251 
       
   252 /**
       
   253  * Adds an error message to later be output in the theme
       
   254  *
       
   255  * @since bbPress (r3381)
       
   256  *
       
   257  * @see WP_Error()
       
   258  * @uses WP_Error::add();
       
   259  *
       
   260  * @param string $code Unique code for the error message
       
   261  * @param string $message Translated error message
       
   262  * @param string $data Any additional data passed with the error message
       
   263  */
       
   264 function bbp_add_error( $code = '', $message = '', $data = '' ) {
       
   265 	bbpress()->errors->add( $code, $message, $data );
       
   266 }
       
   267 
       
   268 /**
       
   269  * Check if error messages exist in queue
       
   270  *
       
   271  * @since bbPress (r3381)
       
   272  *
       
   273  * @see WP_Error()
       
   274  *
       
   275  * @uses is_wp_error()
       
   276  * @usese WP_Error::get_error_codes()
       
   277  */
       
   278 function bbp_has_errors() {
       
   279 	$has_errors = bbpress()->errors->get_error_codes() ? true : false;
       
   280 
       
   281 	return apply_filters( 'bbp_has_errors', $has_errors, bbpress()->errors );
       
   282 }
       
   283 
       
   284 /**
       
   285  * Searches through the content to locate usernames, designated by an @ sign.
       
   286  *
       
   287  * @since bbPress (r4323)
       
   288  *
       
   289  * @param string $content The content
       
   290  * @return bool|array $usernames Existing usernames. False if no matches.
       
   291  */
       
   292 function bbp_find_mentions( $content = '' ) {
       
   293 	$pattern   = '/[@]+([A-Za-z0-9-_\.@]+)\b/';
       
   294 	preg_match_all( $pattern, $content, $usernames );
       
   295 	$usernames = array_unique( array_filter( $usernames[1] ) );
       
   296 
       
   297 	// Bail if no usernames
       
   298 	if ( empty( $usernames ) )
       
   299 		return false;
       
   300 
       
   301 	return $usernames;
       
   302 }
       
   303 
       
   304 /**
       
   305  * Finds and links @-mentioned users in the content
       
   306  *
       
   307  * @since bbPress (r4323)
       
   308  *
       
   309  * @uses bbp_find_mentions() To get usernames in content areas
       
   310  * @return string $content Content filtered for mentions
       
   311  */
       
   312 function bbp_mention_filter( $content = '' ) {
       
   313 
       
   314 	// Get Usernames and bail if none exist
       
   315 	$usernames = bbp_find_mentions( $content );
       
   316 	if ( empty( $usernames ) )
       
   317 		return $content;
       
   318 
       
   319 	// Loop through usernames and link to profiles
       
   320 	foreach( (array) $usernames as $username ) {
       
   321 
       
   322 		// Skip if username does not exist or user is not active
       
   323 		$user_id = username_exists( $username );
       
   324 		if ( empty( $user_id ) || bbp_is_user_inactive( $user_id ) )
       
   325 			continue;
       
   326 
       
   327 		// Replace name in content
       
   328 		$content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bbp_get_user_profile_url( $user_id ) . "' rel='nofollow' class='bbp-mention-link $username'>@$username</a>", $content );
       
   329 	}
       
   330 
       
   331 	// Return modified content
       
   332 	return $content;
       
   333 }
       
   334 
       
   335 /** Post Statuses *************************************************************/
       
   336 
       
   337 /**
       
   338  * Return the public post status ID
       
   339  *
       
   340  * @since bbPress (r3504)
       
   341  *
       
   342  * @return string
       
   343  */
       
   344 function bbp_get_public_status_id() {
       
   345 	return bbpress()->public_status_id;
       
   346 }
       
   347 
       
   348 /**
       
   349  * Return the pending post status ID
       
   350  *
       
   351  * @since bbPress (r3581)
       
   352  *
       
   353  * @return string
       
   354  */
       
   355 function bbp_get_pending_status_id() {
       
   356 	return bbpress()->pending_status_id;
       
   357 }
       
   358 
       
   359 /**
       
   360  * Return the private post status ID
       
   361  *
       
   362  * @since bbPress (r3504)
       
   363  *
       
   364  * @return string
       
   365  */
       
   366 function bbp_get_private_status_id() {
       
   367 	return bbpress()->private_status_id;
       
   368 }
       
   369 
       
   370 /**
       
   371  * Return the hidden post status ID
       
   372  *
       
   373  * @since bbPress (r3504)
       
   374  *
       
   375  * @return string
       
   376  */
       
   377 function bbp_get_hidden_status_id() {
       
   378 	return bbpress()->hidden_status_id;
       
   379 }
       
   380 
       
   381 /**
       
   382  * Return the closed post status ID
       
   383  *
       
   384  * @since bbPress (r3504)
       
   385  *
       
   386  * @return string
       
   387  */
       
   388 function bbp_get_closed_status_id() {
       
   389 	return bbpress()->closed_status_id;
       
   390 }
       
   391 
       
   392 /**
       
   393  * Return the spam post status ID
       
   394  *
       
   395  * @since bbPress (r3504)
       
   396  *
       
   397  * @return string
       
   398  */
       
   399 function bbp_get_spam_status_id() {
       
   400 	return bbpress()->spam_status_id;
       
   401 }
       
   402 
       
   403 /**
       
   404  * Return the trash post status ID
       
   405  *
       
   406  * @since bbPress (r3504)
       
   407  *
       
   408  * @return string
       
   409  */
       
   410 function bbp_get_trash_status_id() {
       
   411 	return bbpress()->trash_status_id;
       
   412 }
       
   413 
       
   414 /**
       
   415  * Return the orphan post status ID
       
   416  *
       
   417  * @since bbPress (r3504)
       
   418  *
       
   419  * @return string
       
   420  */
       
   421 function bbp_get_orphan_status_id() {
       
   422 	return bbpress()->orphan_status_id;
       
   423 }
       
   424 
       
   425 /** Rewrite IDs ***************************************************************/
       
   426 
       
   427 /**
       
   428  * Return the unique ID for user profile rewrite rules
       
   429  *
       
   430  * @since bbPress (r3762)
       
   431  * @return string
       
   432  */
       
   433 function bbp_get_user_rewrite_id() {
       
   434 	return bbpress()->user_id;
       
   435 }
       
   436 
       
   437 /**
       
   438  * Return the enique ID for all edit rewrite rules (forum|topic|reply|tag|user)
       
   439  *
       
   440  * @since bbPress (r3762)
       
   441  * @return string
       
   442  */
       
   443 function bbp_get_edit_rewrite_id() {
       
   444 	return bbpress()->edit_id;
       
   445 }
       
   446 
       
   447 /**
       
   448  * Return the unique ID for user topics rewrite rules
       
   449  *
       
   450  * @since bbPress (r4321)
       
   451  * @return string
       
   452  */
       
   453 function bbp_get_user_topics_rewrite_id() {
       
   454 	return bbpress()->tops_id;
       
   455 }
       
   456 
       
   457 /**
       
   458  * Return the unique ID for user replies rewrite rules
       
   459  *
       
   460  * @since bbPress (r4321)
       
   461  * @return string
       
   462  */
       
   463 function bbp_get_user_replies_rewrite_id() {
       
   464 	return bbpress()->reps_id;
       
   465 }
       
   466 
       
   467 /**
       
   468  * Return the unique ID for user caps rewrite rules
       
   469  *
       
   470  * @since bbPress (r4181)
       
   471  * @return string
       
   472  */
       
   473 function bbp_get_user_favorites_rewrite_id() {
       
   474 	return bbpress()->favs_id;
       
   475 }
       
   476 
       
   477 /**
       
   478  * Return the unique ID for user caps rewrite rules
       
   479  *
       
   480  * @since bbPress (r4181)
       
   481  * @return string
       
   482  */
       
   483 function bbp_get_user_subscriptions_rewrite_id() {
       
   484 	return bbpress()->subs_id;
       
   485 }
       
   486 
       
   487 /**
       
   488  * Return the unique ID for topic view rewrite rules
       
   489  *
       
   490  * @since bbPress (r3762)
       
   491  * @return string
       
   492  */
       
   493 function bbp_get_view_rewrite_id() {
       
   494 	return bbpress()->view_id;
       
   495 }
       
   496 
       
   497 /**
       
   498  * Delete a blogs rewrite rules, so that they are automatically rebuilt on
       
   499  * the subsequent page load.
       
   500  *
       
   501  * @since bbPress (r4198)
       
   502  */
       
   503 function bbp_delete_rewrite_rules() {
       
   504 	delete_option( 'rewrite_rules' );
       
   505 }