bp-core-template.php 102 KB
Newer Older
root's avatar
root committed
1
<?php
lechuck's avatar
lechuck committed
2
/**
lechuck's avatar
lechuck committed
3
 * Core component template tag functions.
lechuck's avatar
lechuck committed
4
5
6
 *
 * @package BuddyPress
 * @subpackage TemplateFunctions
lechuck's avatar
lechuck committed
7
 * @since 1.5.0
lechuck's avatar
lechuck committed
8
 */
lechuck's avatar
lechuck committed
9

lechuck's avatar
lechuck committed
10
// Exit if accessed directly.
ale's avatar
ale committed
11
defined( 'ABSPATH' ) || exit;
root's avatar
root committed
12
13

/**
lechuck's avatar
lechuck committed
14
 * Output the "options nav", the secondary-level single item navigation menu.
root's avatar
root committed
15
 *
lucha's avatar
lucha committed
16
 * Uses the component's nav global to render out the sub navigation for the
lechuck's avatar
lechuck committed
17
18
19
20
21
 * current component. Each component adds to its sub navigation array within
 * its own setup_nav() function.
 *
 * This sub navigation array is the secondary level navigation, so for profile
 * it contains:
root's avatar
root committed
22
23
 *      [Public, Edit Profile, Change Avatar]
 *
lechuck's avatar
lechuck committed
24
25
 * The function will also analyze the current action for the current component
 * to determine whether or not to highlight a particular sub nav item.
root's avatar
root committed
26
 *
lechuck's avatar
lechuck committed
27
28
 * @since 1.0.0
 *
lechuck's avatar
lechuck committed
29
 *       viewed user.
lechuck's avatar
lechuck committed
30
31
32
 *
 * @param string $parent_slug Options nav slug.
 * @return string
root's avatar
root committed
33
 */
ale's avatar
ale committed
34
function bp_get_options_nav( $parent_slug = '' ) {
lucha's avatar
lucha committed
35
	$bp = buddypress();
root's avatar
root committed
36

lucha's avatar
lucha committed
37
	// If we are looking at a member profile, then the we can use the current
lechuck's avatar
lechuck committed
38
	// component as an index. Otherwise we need to use the component's root_slug.
lechuck's avatar
lechuck committed
39
	$component_index = !empty( $bp->displayed_user ) ? bp_current_component() : bp_get_root_slug( bp_current_component() );
ale's avatar
ale committed
40
	$selected_item   = bp_current_action();
root's avatar
root committed
41

lucha's avatar
lucha committed
42
	// Default to the Members nav.
lucha's avatar
lucha committed
43
	if ( ! bp_is_single_item() ) {
lucha's avatar
lucha committed
44
45
46
47
48
49
50
51
		// Set the parent slug, if not provided.
		if ( empty( $parent_slug ) ) {
			$parent_slug = $component_index;
		}

		$secondary_nav_items = $bp->members->nav->get_secondary( array( 'parent_slug' => $parent_slug ) );

		if ( ! $secondary_nav_items ) {
root's avatar
root committed
52
53
			return false;
		}
lucha's avatar
lucha committed
54
55

	// For a single item, try to use the component's nav.
root's avatar
root committed
56
	} else {
ale's avatar
ale committed
57
		$current_item = bp_current_item();
lucha's avatar
lucha committed
58
		$single_item_component = bp_current_component();
ale's avatar
ale committed
59

lucha's avatar
lucha committed
60
		// Adjust the selected nav item for the current single item if needed.
ale's avatar
ale committed
61
62
63
64
65
		if ( ! empty( $parent_slug ) ) {
			$current_item  = $parent_slug;
			$selected_item = bp_action_variable( 0 );
		}

lucha's avatar
lucha committed
66
67
68
		// If the nav is not defined by the parent component, look in the Members nav.
		if ( ! isset( $bp->{$single_item_component}->nav ) ) {
			$secondary_nav_items = $bp->members->nav->get_secondary( array( 'parent_slug' => $current_item ) );
root's avatar
root committed
69
		} else {
lucha's avatar
lucha committed
70
71
72
73
74
			$secondary_nav_items = $bp->{$single_item_component}->nav->get_secondary( array( 'parent_slug' => $current_item ) );
		}

		if ( ! $secondary_nav_items ) {
			return false;
root's avatar
root committed
75
76
77
		}
	}

lechuck's avatar
lechuck committed
78
	// Loop through each navigation item.
lucha's avatar
lucha committed
79
80
	foreach ( $secondary_nav_items as $subnav_item ) {
		if ( empty( $subnav_item->user_has_access ) ) {
root's avatar
root committed
81
			continue;
lucha's avatar
lucha committed
82
		}
root's avatar
root committed
83
84

		// If the current action or an action variable matches the nav item id, then add a highlight CSS class.
lucha's avatar
lucha committed
85
		if ( $subnav_item->slug === $selected_item ) {
root's avatar
root committed
86
87
88
89
90
			$selected = ' class="current selected"';
		} else {
			$selected = '';
		}

lechuck's avatar
lechuck committed
91
		// List type depends on our current component.
root's avatar
root committed
92
93
		$list_type = bp_is_group() ? 'groups' : 'personal';

ale's avatar
ale committed
94
95
96
97
98
		/**
		 * Filters the "options nav", the secondary-level single item navigation menu.
		 *
		 * This is a dynamic filter that is dependent on the provided css_id value.
		 *
lechuck's avatar
lechuck committed
99
		 * @since 1.1.0
ale's avatar
ale committed
100
101
102
103
104
		 *
		 * @param string $value         HTML list item for the submenu item.
		 * @param array  $subnav_item   Submenu array item being displayed.
		 * @param string $selected_item Current action.
		 */
lucha's avatar
lucha committed
105
		echo apply_filters( 'bp_get_options_nav_' . $subnav_item->css_id, '<li id="' . esc_attr( $subnav_item->css_id . '-' . $list_type . '-li' ) . '" ' . $selected . '><a id="' . esc_attr( $subnav_item->css_id ) . '" href="' . esc_url( $subnav_item->link ) . '">' . $subnav_item->name . '</a></li>', $subnav_item, $selected_item );
root's avatar
root committed
106
107
108
	}
}

lechuck's avatar
lechuck committed
109
110
111
112
/**
 * Get the 'bp_options_title' property from the BP global.
 *
 * Not currently used in BuddyPress.
lechuck's avatar
lechuck committed
113
 *
lechuck's avatar
lechuck committed
114
115
 * @todo Deprecate.
 */
root's avatar
root committed
116
function bp_get_options_title() {
lucha's avatar
lucha committed
117
	$bp = buddypress();
root's avatar
root committed
118

lucha's avatar
lucha committed
119
	if ( empty( $bp->bp_options_title ) ) {
root's avatar
root committed
120
		$bp->bp_options_title = __( 'Options', 'buddypress' );
lucha's avatar
lucha committed
121
	}
root's avatar
root committed
122
123
124
125

	echo apply_filters( 'bp_get_options_title', esc_attr( $bp->bp_options_title ) );
}

lechuck's avatar
lechuck committed
126
127
128
129
130
131
/**
 * Get the directory title for a component.
 *
 * Used for the <title> element and the page header on the component directory
 * page.
 *
lechuck's avatar
lechuck committed
132
 * @since 2.0.0
lechuck's avatar
lechuck committed
133
 *
lechuck's avatar
lechuck committed
134
 * @param string $component Component to get directory title for.
lechuck's avatar
lechuck committed
135
136
137
138
139
 * @return string
 */
function bp_get_directory_title( $component = '' ) {
	$title = '';

lechuck's avatar
lechuck committed
140
	// Use the string provided by the component.
lechuck's avatar
lechuck committed
141
142
143
	if ( ! empty( buddypress()->{$component}->directory_title ) ) {
		$title = buddypress()->{$component}->directory_title;

lechuck's avatar
lechuck committed
144
	// If none is found, concatenate.
lucha's avatar
lucha committed
145
	} elseif ( isset( buddypress()->{$component}->name ) ) {
lechuck's avatar
lechuck committed
146
147
148
		$title = sprintf( __( '%s Directory', 'buddypress' ), buddypress()->{$component}->name );
	}

ale's avatar
ale committed
149
150
151
	/**
	 * Filters the directory title for a component.
	 *
lechuck's avatar
lechuck committed
152
	 * @since 2.0.0
ale's avatar
ale committed
153
154
155
156
	 *
	 * @param string $title     Text to be used in <title> tag.
	 * @param string $component Current componet being displayed.
	 */
lechuck's avatar
lechuck committed
157
158
159
	return apply_filters( 'bp_get_directory_title', $title, $component );
}

root's avatar
root committed
160
161
162
/** Avatars *******************************************************************/

/**
lechuck's avatar
lechuck committed
163
164
165
166
 * Check to see if there is an options avatar.
 *
 * An options avatar is an avatar for something like a group, or a friend.
 * Basically an avatar that appears in the sub nav options bar.
root's avatar
root committed
167
 *
lechuck's avatar
lechuck committed
168
169
 * Not currently used in BuddyPress.
 *
lechuck's avatar
lechuck committed
170
 * @return bool $value Returns true if an options avatar has been set, otherwise false.
root's avatar
root committed
171
172
 */
function bp_has_options_avatar() {
lucha's avatar
lucha committed
173
	return (bool) buddypress()->bp_options_avatar;
root's avatar
root committed
174
175
}

lechuck's avatar
lechuck committed
176
177
178
179
180
181
182
/**
 * Output the options avatar.
 *
 * Not currently used in BuddyPress.
 *
 * @todo Deprecate.
 */
root's avatar
root committed
183
function bp_get_options_avatar() {
lucha's avatar
lucha committed
184
	echo apply_filters( 'bp_get_options_avatar', buddypress()->bp_options_avatar );
root's avatar
root committed
185
186
}

lechuck's avatar
lechuck committed
187
188
189
190
191
/**
 * Output a comment author's avatar.
 *
 * Not currently used in BuddyPress.
 */
root's avatar
root committed
192
193
194
function bp_comment_author_avatar() {
	global $comment;

lucha's avatar
lucha committed
195
196
197
198
199
200
201
	if ( function_exists( 'bp_core_fetch_avatar' ) ) {
		echo apply_filters( 'bp_comment_author_avatar', bp_core_fetch_avatar( array(
			'item_id' => $comment->user_id,
			'type'    => 'thumb',
			'alt'     => sprintf( __( 'Profile photo of %s', 'buddypress' ), bp_core_get_user_displayname( $comment->user_id ) )
		) ) );
	} elseif ( function_exists( 'get_avatar' ) ) {
root's avatar
root committed
202
		get_avatar();
lucha's avatar
lucha committed
203
	}
root's avatar
root committed
204
205
}

lechuck's avatar
lechuck committed
206
207
208
209
210
/**
 * Output a post author's avatar.
 *
 * Not currently used in BuddyPress.
 */
root's avatar
root committed
211
212
213
function bp_post_author_avatar() {
	global $post;

lucha's avatar
lucha committed
214
215
216
217
218
219
220
	if ( function_exists( 'bp_core_fetch_avatar' ) ) {
		echo apply_filters( 'bp_post_author_avatar', bp_core_fetch_avatar( array(
			'item_id' => $post->post_author,
			'type'    => 'thumb',
			'alt'     => sprintf( __( 'Profile photo of %s', 'buddypress' ), bp_core_get_user_displayname( $post->post_author ) )
		) ) );
	} elseif ( function_exists( 'get_avatar' ) ) {
root's avatar
root committed
221
		get_avatar();
lucha's avatar
lucha committed
222
	}
root's avatar
root committed
223
224
}

lechuck's avatar
lechuck committed
225
226
/**
 * Output the current avatar upload step.
lechuck's avatar
lechuck committed
227
228
 *
 * @since 1.1.0
lechuck's avatar
lechuck committed
229
 */
root's avatar
root committed
230
231
232
function bp_avatar_admin_step() {
	echo bp_get_avatar_admin_step();
}
lechuck's avatar
lechuck committed
233
234
235
	/**
	 * Return the current avatar upload step.
	 *
lechuck's avatar
lechuck committed
236
237
	 * @since 1.1.0
	 *
lechuck's avatar
lechuck committed
238
239
240
	 * @return string The current avatar upload step. Returns 'upload-image'
	 *         if none is found.
	 */
root's avatar
root committed
241
	function bp_get_avatar_admin_step() {
lucha's avatar
lucha committed
242
243
244
245
		$bp   = buddypress();
		$step = isset( $bp->avatar_admin->step )
			? $step = $bp->avatar_admin->step
			: 'upload-image';
root's avatar
root committed
246

ale's avatar
ale committed
247
248
249
		/**
		 * Filters the current avatar upload step.
		 *
lechuck's avatar
lechuck committed
250
		 * @since 1.1.0
ale's avatar
ale committed
251
252
253
		 *
		 * @param string $step The current avatar upload step.
		 */
root's avatar
root committed
254
255
256
		return apply_filters( 'bp_get_avatar_admin_step', $step );
	}

lechuck's avatar
lechuck committed
257
258
/**
 * Output the URL of the avatar to crop.
lechuck's avatar
lechuck committed
259
260
 *
 * @since 1.1.0
lechuck's avatar
lechuck committed
261
 */
root's avatar
root committed
262
263
264
function bp_avatar_to_crop() {
	echo bp_get_avatar_to_crop();
}
lechuck's avatar
lechuck committed
265
266
267
	/**
	 * Return the URL of the avatar to crop.
	 *
lechuck's avatar
lechuck committed
268
269
	 * @since 1.1.0
	 *
lechuck's avatar
lechuck committed
270
271
	 * @return string URL of the avatar awaiting cropping.
	 */
root's avatar
root committed
272
	function bp_get_avatar_to_crop() {
lucha's avatar
lucha committed
273
274
275
276
		$bp  = buddypress();
		$url = isset( $bp->avatar_admin->image->url )
			? $bp->avatar_admin->image->url
			: '';
root's avatar
root committed
277

ale's avatar
ale committed
278
279
280
		/**
		 * Filters the URL of the avatar to crop.
		 *
lechuck's avatar
lechuck committed
281
		 * @since 1.1.0
ale's avatar
ale committed
282
283
284
		 *
		 * @param string $url URL for the avatar.
		 */
root's avatar
root committed
285
286
287
		return apply_filters( 'bp_get_avatar_to_crop', $url );
	}

lechuck's avatar
lechuck committed
288
289
/**
 * Output the relative file path to the avatar to crop.
lechuck's avatar
lechuck committed
290
291
 *
 * @since 1.1.0
lechuck's avatar
lechuck committed
292
 */
root's avatar
root committed
293
294
295
function bp_avatar_to_crop_src() {
	echo bp_get_avatar_to_crop_src();
}
lechuck's avatar
lechuck committed
296
297
298
	/**
	 * Return the relative file path to the avatar to crop.
	 *
lechuck's avatar
lechuck committed
299
300
	 * @since 1.1.0
	 *
lechuck's avatar
lechuck committed
301
302
	 * @return string Relative file path to the avatar.
	 */
root's avatar
root committed
303
	function bp_get_avatar_to_crop_src() {
lucha's avatar
lucha committed
304
305
306
307
		$bp  = buddypress();
		$src = isset( $bp->avatar_admin->image->dir )
			? str_replace( WP_CONTENT_DIR, '', $bp->avatar_admin->image->dir )
			: '';
root's avatar
root committed
308

ale's avatar
ale committed
309
310
311
		/**
		 * Filters the relative file path to the avatar to crop.
		 *
lechuck's avatar
lechuck committed
312
		 * @since 1.1.0
ale's avatar
ale committed
313
314
315
		 *
		 * @param string $src Relative file path for the avatar.
		 */
lucha's avatar
lucha committed
316
		return apply_filters( 'bp_get_avatar_to_crop_src', $src );
root's avatar
root committed
317
318
	}

lechuck's avatar
lechuck committed
319
320
321
322
323
324
325
/**
 * Output the avatar cropper <img> markup.
 *
 * No longer used in BuddyPress.
 *
 * @todo Deprecate.
 */
root's avatar
root committed
326
function bp_avatar_cropper() {
lucha's avatar
lucha committed
327
328
329
?>
	<img id="avatar-to-crop" class="avatar" src="<?php echo esc_url( buddypress()->avatar_admin->image ); ?>" />
<?php
root's avatar
root committed
330
331
}

lechuck's avatar
lechuck committed
332
/**
lechuck's avatar
lechuck committed
333
 * Output the name of the BP site. Used in RSS headers.
lechuck's avatar
lechuck committed
334
335
 *
 * @since 1.0.0
lechuck's avatar
lechuck committed
336
 */
root's avatar
root committed
337
function bp_site_name() {
lechuck's avatar
lechuck committed
338
	echo bp_get_site_name();
root's avatar
root committed
339
}
lechuck's avatar
lechuck committed
340
	/**
lechuck's avatar
lechuck committed
341
	 * Returns the name of the BP site. Used in RSS headers.
lechuck's avatar
lechuck committed
342
	 *
lechuck's avatar
lechuck committed
343
344
345
	 * @since 1.6.0
	 *
	 * @return string
lechuck's avatar
lechuck committed
346
347
	 */
	function bp_get_site_name() {
ale's avatar
ale committed
348
349
350
351

		/**
		 * Filters the name of the BP site. Used in RSS headers.
		 *
lechuck's avatar
lechuck committed
352
		 * @since 1.0.0
ale's avatar
ale committed
353
354
355
		 *
		 * @param string $value Current BP site name.
		 */
lechuck's avatar
lechuck committed
356
357
		return apply_filters( 'bp_site_name', get_bloginfo( 'name', 'display' ) );
	}
root's avatar
root committed
358

lechuck's avatar
lechuck committed
359
/**
lechuck's avatar
lechuck committed
360
 * Format a date based on a UNIX timestamp.
lechuck's avatar
lechuck committed
361
 *
ale's avatar
ale committed
362
363
364
365
366
367
368
369
370
371
372
 * This function can be used to turn a UNIX timestamp into a properly formatted
 * (and possibly localized) string, userful for ouputting the date & time an
 * action took place.
 *
 * Not to be confused with `bp_core_time_since()`, this function is best used
 * for displaying a more exact date and time vs. a human-readable time.
 *
 * Note: This function may be improved or removed at a later date, as it is
 * hardly used and adds an additional layer of complexity to calculating dates
 * and times together with timezone offsets and i18n.
 *
lechuck's avatar
lechuck committed
373
 * @since 1.1.0
ale's avatar
ale committed
374
 *
lechuck's avatar
lechuck committed
375
376
377
378
379
380
381
382
383
 * @param int|string $time         The UNIX timestamp to be formatted.
 * @param bool       $exclude_time Optional. True to return only the month + day, false
 *                                 to return month, day, and time. Default: false.
 * @param bool       $gmt          Optional. True to display in local time, false to
 *                                  leave in GMT. Default: true.
 * @return mixed A string representation of $time, in the format
 *               "March 18, 2014 at 2:00 pm" (or whatever your
 *               'date_format' and 'time_format' settings are
 *               on your root blog). False on failure.
lechuck's avatar
lechuck committed
384
 */
ale's avatar
ale committed
385
function bp_format_time( $time = '', $exclude_time = false, $gmt = true ) {
lucha's avatar
lucha committed
386

ale's avatar
ale committed
387
	// Bail if time is empty or not numeric
lechuck's avatar
lechuck committed
388
	// @todo We should output something smarter here.
ale's avatar
ale committed
389
	if ( empty( $time ) || ! is_numeric( $time ) ) {
root's avatar
root committed
390
		return false;
lucha's avatar
lucha committed
391
	}
root's avatar
root committed
392

lechuck's avatar
lechuck committed
393
	// Get GMT offset from root blog.
ale's avatar
ale committed
394
395
	if ( true === $gmt ) {

lechuck's avatar
lechuck committed
396
		// Use Timezone string if set.
ale's avatar
ale committed
397
398
399
400
401
		$timezone_string = bp_get_option( 'timezone_string' );
		if ( ! empty( $timezone_string ) ) {
			$timezone_object = timezone_open( $timezone_string );
			$datetime_object = date_create( "@{$time}" );
			$timezone_offset = timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS;
root's avatar
root committed
402

lechuck's avatar
lechuck committed
403
		// Fall back on less reliable gmt_offset.
ale's avatar
ale committed
404
405
406
407
		} else {
			$timezone_offset = bp_get_option( 'gmt_offset' );
		}

lechuck's avatar
lechuck committed
408
		// Calculate time based on the offset.
ale's avatar
ale committed
409
410
		$calculated_time = $time + ( $timezone_offset * HOUR_IN_SECONDS );

lechuck's avatar
lechuck committed
411
	// No localizing, so just use the time that was submitted.
ale's avatar
ale committed
412
413
414
	} else {
		$calculated_time = $time;
	}
root's avatar
root committed
415

lechuck's avatar
lechuck committed
416
	// Formatted date: "March 18, 2014".
ale's avatar
ale committed
417
	$formatted_date = date_i18n( bp_get_option( 'date_format' ), $calculated_time, $gmt );
root's avatar
root committed
418
419

	// Should we show the time also?
ale's avatar
ale committed
420
421
	if ( true !== $exclude_time ) {

lechuck's avatar
lechuck committed
422
		// Formatted time: "2:00 pm".
ale's avatar
ale committed
423
		$formatted_time = date_i18n( bp_get_option( 'time_format' ), $calculated_time, $gmt );
root's avatar
root committed
424

lechuck's avatar
lechuck committed
425
		// Return string formatted with date and time.
ale's avatar
ale committed
426
		$formatted_date = sprintf( esc_html__( '%1$s at %2$s', 'buddypress' ), $formatted_date, $formatted_time );
root's avatar
root committed
427
428
	}

ale's avatar
ale committed
429
430
431
	/**
	 * Filters the date based on a UNIX timestamp.
	 *
lechuck's avatar
lechuck committed
432
	 * @since 1.0.0
ale's avatar
ale committed
433
434
435
436
	 *
	 * @param string $formatted_date Formatted date from the timestamp.
	 */
	return apply_filters( 'bp_format_time', $formatted_date );
root's avatar
root committed
437
438
}

lechuck's avatar
lechuck committed
439
440
441
442
443
444
445
446
447
/**
 * Select between two dynamic strings, according to context.
 *
 * This function can be used in cases where a phrase used in a template will
 * differ for a user looking at his own profile and a user looking at another
 * user's profile (eg, "My Friends" and "Joe's Friends"). Pass both versions
 * of the phrase, and bp_word_or_name() will detect which is appropriate, and
 * do the necessary argument swapping for dynamic phrases.
 *
lechuck's avatar
lechuck committed
448
449
450
451
452
453
454
455
456
457
 * @since 1.0.0
 *
 * @param string $youtext    The "you" version of the phrase (eg "Your Friends").
 * @param string $nametext   The other-user version of the phrase. Should be in
 *                           a format appropriate for sprintf() - use %s in place of the displayed
 *                           user's name (eg "%'s Friends").
 * @param bool   $capitalize Optional. Force into title case. Default: true.
 * @param bool   $echo       Optional. True to echo the results, false to return them.
 *                           Default: true.
 * @return string|null $nametext If ! $echo, returns the appropriate string.
lechuck's avatar
lechuck committed
458
 */
root's avatar
root committed
459
460
function bp_word_or_name( $youtext, $nametext, $capitalize = true, $echo = true ) {

lucha's avatar
lucha committed
461
	if ( ! empty( $capitalize ) ) {
lechuck's avatar
lechuck committed
462
		$youtext = bp_core_ucfirst( $youtext );
lucha's avatar
lucha committed
463
	}
root's avatar
root committed
464

lechuck's avatar
lechuck committed
465
466
	if ( bp_displayed_user_id() == bp_loggedin_user_id() ) {
		if ( true == $echo ) {
ale's avatar
ale committed
467
468
469
470

			/**
			 * Filters the text used based on context of own profile or someone else's profile.
			 *
lechuck's avatar
lechuck committed
471
			 * @since 1.0.0
ale's avatar
ale committed
472
473
474
			 *
			 * @param string $youtext Context-determined string to display.
			 */
root's avatar
root committed
475
			echo apply_filters( 'bp_word_or_name', $youtext );
lechuck's avatar
lechuck committed
476
		} else {
ale's avatar
ale committed
477
478

			/** This filter is documented in bp-core/bp-core-template.php */
root's avatar
root committed
479
			return apply_filters( 'bp_word_or_name', $youtext );
lechuck's avatar
lechuck committed
480
		}
root's avatar
root committed
481
	} else {
lechuck's avatar
lechuck committed
482
483
		$fullname = bp_get_displayed_user_fullname();
		$fullname = (array) explode( ' ', $fullname );
root's avatar
root committed
484
		$nametext = sprintf( $nametext, $fullname[0] );
lechuck's avatar
lechuck committed
485
		if ( true == $echo ) {
ale's avatar
ale committed
486
487

			/** This filter is documented in bp-core/bp-core-template.php */
root's avatar
root committed
488
			echo apply_filters( 'bp_word_or_name', $nametext );
lechuck's avatar
lechuck committed
489
		} else {
ale's avatar
ale committed
490
491

			/** This filter is documented in bp-core/bp-core-template.php */
root's avatar
root committed
492
			return apply_filters( 'bp_word_or_name', $nametext );
lechuck's avatar
lechuck committed
493
		}
root's avatar
root committed
494
495
496
	}
}

lechuck's avatar
lechuck committed
497
498
499
500
501
502
503
/**
 * Do the 'bp_styles' action, and call wp_print_styles().
 *
 * No longer used in BuddyPress.
 *
 * @todo Deprecate.
 */
root's avatar
root committed
504
505
506
507
508
509
510
function bp_styles() {
	do_action( 'bp_styles' );
	wp_print_styles();
}

/** Search Form ***************************************************************/

lechuck's avatar
lechuck committed
511
512
513
/**
 * Return the "action" attribute for search forms.
 *
lechuck's avatar
lechuck committed
514
515
 * @since 1.0.0
 *
lechuck's avatar
lechuck committed
516
517
 * @return string URL action attribute for search forms, eg example.com/search/.
 */
root's avatar
root committed
518
function bp_search_form_action() {
ale's avatar
ale committed
519
520
521
522

	/**
	 * Filters the "action" attribute for search forms.
	 *
lechuck's avatar
lechuck committed
523
	 * @since 1.0.0
ale's avatar
ale committed
524
525
526
	 *
	 * @param string $value Search form action url.
	 */
lechuck's avatar
lechuck committed
527
	return apply_filters( 'bp_search_form_action', trailingslashit( bp_get_root_domain() . '/' . bp_get_search_slug() ) );
root's avatar
root committed
528
529
530
}

/**
lechuck's avatar
lechuck committed
531
532
 * Generate the basic search form as used in BP-Default's header.
 *
lechuck's avatar
lechuck committed
533
 * @since 1.0.0
root's avatar
root committed
534
 *
lechuck's avatar
lechuck committed
535
 * @return string HTML <select> element.
root's avatar
root committed
536
537
538
539
540
 */
function bp_search_form_type_select() {

	$options = array();

lucha's avatar
lucha committed
541
542
543
	if ( bp_is_active( 'xprofile' ) ) {
		$options['members'] = _x( 'Members', 'search form', 'buddypress' );
	}
root's avatar
root committed
544

lucha's avatar
lucha committed
545
546
547
	if ( bp_is_active( 'groups' ) ) {
		$options['groups']  = _x( 'Groups', 'search form', 'buddypress' );
	}
root's avatar
root committed
548

lucha's avatar
lucha committed
549
550
551
	if ( bp_is_active( 'blogs' ) && is_multisite() ) {
		$options['blogs']   = _x( 'Blogs', 'search form', 'buddypress' );
	}
root's avatar
root committed
552

lucha's avatar
lucha committed
553
	$options['posts'] = _x( 'Posts', 'search form', 'buddypress' );
root's avatar
root committed
554
555

	// Eventually this won't be needed and a page will be built to integrate all search results.
lucha's avatar
lucha committed
556
	$selection_box  = '<label for="search-which" class="accessibly-hidden">' . _x( 'Search these:', 'search form', 'buddypress' ) . '</label>';
root's avatar
root committed
557
558
	$selection_box .= '<select name="search-which" id="search-which" style="width: auto">';

ale's avatar
ale committed
559
560
561
	/**
	 * Filters all of the component options available for search scope.
	 *
lechuck's avatar
lechuck committed
562
	 * @since 1.5.0
ale's avatar
ale committed
563
564
565
	 *
	 * @param array $options Array of options to add to select field.
	 */
root's avatar
root committed
566
	$options = apply_filters( 'bp_search_form_type_select_options', $options );
lucha's avatar
lucha committed
567
	foreach( (array) $options as $option_value => $option_title ) {
root's avatar
root committed
568
		$selection_box .= sprintf( '<option value="%s">%s</option>', $option_value, $option_title );
lucha's avatar
lucha committed
569
	}
root's avatar
root committed
570
571
572

	$selection_box .= '</select>';

ale's avatar
ale committed
573
574
575
	/**
	 * Filters the complete <select> input used for search scope.
	 *
lechuck's avatar
lechuck committed
576
	 * @since 1.0.0
ale's avatar
ale committed
577
578
579
	 *
	 * @param string $selection_box <select> input for selecting search scope.
	 */
root's avatar
root committed
580
581
582
	return apply_filters( 'bp_search_form_type_select', $selection_box );
}

lucha's avatar
lucha committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/**
 * Output the 'name' attribute for search form input element.
 *
 * @since 2.7.0
 *
 * @param string $component See bp_get_search_input_name().
 */
function bp_search_input_name( $component = '' ) {
	echo esc_attr( bp_get_search_input_name( $component ) );
}

/**
 * Get the 'name' attribute for the search form input element.
 *
 * @since 2.7.0
 *
 * @param string $component Component name. Defaults to current component.
 * @return string Text for the 'name' attribute.
 */
function bp_get_search_input_name( $component = '' ) {
	if ( ! $component ) {
		$component = bp_current_component();
	}

	$bp = buddypress();

	$name = '';
	if ( isset( $bp->{$component}->id ) ) {
		$name = $bp->{$component}->id . '_search';
	}

	return $name;
}

/**
 * Output the placeholder text for the search box for a given component.
 *
 * @since 2.7.0
 *
 * @param string $component See bp_get_search_placeholder().
 */
function bp_search_placeholder( $component = '' ) {
	echo esc_attr( bp_get_search_placeholder( $component ) );
}

/**
 * Get the placeholder text for the search box for a given component.
 *
 * @since 2.7.0
 *
 * @param string $component Component name. Defaults to current component.
 * @return string Placeholder text for the search field.
 */
function bp_get_search_placeholder( $component = '' ) {
	$query_arg = bp_core_get_component_search_query_arg( $component );

	if ( $query_arg && ! empty( $_REQUEST[ $query_arg ] ) ) {
		$placeholder = wp_unslash( $_REQUEST[ $query_arg ] );
	} else {
		$placeholder = bp_get_search_default_text( $component );
	}

	return $placeholder;
}

root's avatar
root committed
648
/**
lechuck's avatar
lechuck committed
649
 * Output the default text for the search box for a given component.
root's avatar
root committed
650
 *
lechuck's avatar
lechuck committed
651
 * @since 1.5.0
lechuck's avatar
lechuck committed
652
653
654
655
 *
 * @see bp_get_search_default_text()
 *
 * @param string $component See {@link bp_get_search_default_text()}.
root's avatar
root committed
656
657
658
659
 */
function bp_search_default_text( $component = '' ) {
	echo bp_get_search_default_text( $component );
}
lechuck's avatar
lechuck committed
660
661
662
	/**
	 * Return the default text for the search box for a given component.
	 *
lechuck's avatar
lechuck committed
663
	 * @since 1.5.0
lechuck's avatar
lechuck committed
664
665
666
667
	 *
	 * @param string $component Component name. Default: current component.
	 * @return string Placeholder text for search field.
	 */
root's avatar
root committed
668
	function bp_get_search_default_text( $component = '' ) {
ale's avatar
ale committed
669
670

		$bp = buddypress();
root's avatar
root committed
671

lucha's avatar
lucha committed
672
		if ( empty( $component ) ) {
root's avatar
root committed
673
			$component = bp_current_component();
lucha's avatar
lucha committed
674
		}
root's avatar
root committed
675
676
677

		$default_text = __( 'Search anything...', 'buddypress' );

lechuck's avatar
lechuck committed
678
		// Most of the time, $component will be the actual component ID.
root's avatar
root committed
679
680
681
682
683
		if ( !empty( $component ) ) {
			if ( !empty( $bp->{$component}->search_string ) ) {
				$default_text = $bp->{$component}->search_string;
			} else {
				// When the request comes through AJAX, we need to get the component
lechuck's avatar
lechuck committed
684
				// name out of $bp->pages.
root's avatar
root committed
685
686
				if ( !empty( $bp->pages->{$component}->slug ) ) {
					$key = $bp->pages->{$component}->slug;
lucha's avatar
lucha committed
687
					if ( !empty( $bp->{$key}->search_string ) ) {
root's avatar
root committed
688
						$default_text = $bp->{$key}->search_string;
lucha's avatar
lucha committed
689
					}
root's avatar
root committed
690
691
692
693
				}
			}
		}

ale's avatar
ale committed
694
695
696
		/**
		 * Filters the default text for the search box for a given component.
		 *
lechuck's avatar
lechuck committed
697
		 * @since 1.5.0
ale's avatar
ale committed
698
699
700
701
		 *
		 * @param string $default_text Default text for search box.
		 * @param string $component    Current component displayed.
		 */
root's avatar
root committed
702
703
704
		return apply_filters( 'bp_get_search_default_text', $default_text, $component );
	}

lechuck's avatar
lechuck committed
705
706
707
708
709
710
711
/**
 * Fire the 'bp_custom_profile_boxes' action.
 *
 * No longer used in BuddyPress.
 *
 * @todo Deprecate.
 */
root's avatar
root committed
712
713
714
715
function bp_custom_profile_boxes() {
	do_action( 'bp_custom_profile_boxes' );
}

lechuck's avatar
lechuck committed
716
717
718
719
720
721
722
/**
 * Fire the 'bp_custom_profile_sidebar_boxes' action.
 *
 * No longer used in BuddyPress.
 *
 * @todo Deprecate.
 */
root's avatar
root committed
723
724
725
726
function bp_custom_profile_sidebar_boxes() {
	do_action( 'bp_custom_profile_sidebar_boxes' );
}

ale's avatar
ale committed
727
728
729
/**
 * Output the attributes for a form field.
 *
lechuck's avatar
lechuck committed
730
 * @since 2.2.0
ale's avatar
ale committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
 *
 * @param string $name       The field name to output attributes for.
 * @param array  $attributes Array of existing attributes to add.
 */
function bp_form_field_attributes( $name = '', $attributes = array() ) {
	echo bp_get_form_field_attributes( $name, $attributes );
}
	/**
	 * Get the attributes for a form field.
	 *
	 * Primarily to add better support for touchscreen devices, but plugin devs
	 * can use the 'bp_get_form_field_extra_attributes' filter for further
	 * manipulation.
	 *
lechuck's avatar
lechuck committed
745
	 * @since 2.2.0
ale's avatar
ale committed
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
	 *
	 * @param string $name       The field name to get attributes for.
	 * @param array  $attributes Array of existing attributes to add.
	 * @return string
	 */
	function bp_get_form_field_attributes( $name = '', $attributes = array() ) {
		$retval = '';

		if ( empty( $attributes ) ) {
			$attributes = array();
		}

		$name = strtolower( $name );

		switch ( $name ) {
			case 'username' :
			case 'blogname' :
				$attributes['autocomplete']   = 'off';
				$attributes['autocapitalize'] = 'none';
				break;

			case 'email' :
				if ( wp_is_mobile() ) {
					$attributes['autocapitalize'] = 'none';
				}
				break;

			case 'password' :
				$attributes['spellcheck']   = 'false';
				$attributes['autocomplete'] = 'off';

				if ( wp_is_mobile() ) {
					$attributes['autocorrect']    = 'false';
					$attributes['autocapitalize'] = 'none';
				}
				break;
		}

		/**
		 * Filter the attributes for a field before rendering output.
		 *
lechuck's avatar
lechuck committed
787
		 * @since 2.2.0
ale's avatar
ale committed
788
		 *
lechuck's avatar
lechuck committed
789
790
		 * @param array  $attributes The field attributes.
		 * @param string $name       The field name.
ale's avatar
ale committed
791
792
793
794
		 */
		$attributes = (array) apply_filters( 'bp_get_form_field_attributes', $attributes, $name );

		foreach( $attributes as $attr => $value ) {
lucha's avatar
lucha committed
795
796
797
798
799
800
801
802
			// Numeric keyed array.
			if (is_numeric( $attr ) ) {
				$retval .= sprintf( ' %s', esc_attr( $value ) );

			// Associative keyed array.
			} else {
				$retval .= sprintf( ' %s="%s"', sanitize_key( $attr ), esc_attr( $value ) );
			}
ale's avatar
ale committed
803
804
805
806
807
		}

		return $retval;
	}

root's avatar
root committed
808
/**
lechuck's avatar
lechuck committed
809
 * Create and output a button.
root's avatar
root committed
810
 *
lechuck's avatar
lechuck committed
811
812
 * @since 1.2.6
 *
root's avatar
root committed
813
 * @see bp_get_button()
lechuck's avatar
lechuck committed
814
 *
lechuck's avatar
lechuck committed
815
 * @param array|string $args See {@link BP_Button}.
root's avatar
root committed
816
817
818
819
820
 */
function bp_button( $args = '' ) {
	echo bp_get_button( $args );
}
	/**
lechuck's avatar
lechuck committed
821
	 * Create and return a button.
root's avatar
root committed
822
	 *
lechuck's avatar
lechuck committed
823
824
	 * @since 1.2.6
	 *
lechuck's avatar
lechuck committed
825
	 * @see BP_Button for a description of arguments and return value.
root's avatar
root committed
826
	 *
lechuck's avatar
lechuck committed
827
	 * @param array|string $args See {@link BP_Button}.
lechuck's avatar
lechuck committed
828
	 * @return string HTML markup for the button.
root's avatar
root committed
829
830
831
	 */
	function bp_get_button( $args = '' ) {
		$button = new BP_Button( $args );
ale's avatar
ale committed
832
833
834
835

		/**
		 * Filters the requested button output.
		 *
lechuck's avatar
lechuck committed
836
		 * @since 1.2.6
ale's avatar
ale committed
837
838
839
840
841
		 *
		 * @param string    $contents  Button context to be used.
		 * @param array     $args      Array of args for the button.
		 * @param BP_Button $button    BP_Button object.
		 */
root's avatar
root committed
842
843
844
845
		return apply_filters( 'bp_get_button', $button->contents, $args, $button );
	}

/**
lechuck's avatar
lechuck committed
846
 * Truncate text.
root's avatar
root committed
847
848
849
850
851
852
853
 *
 * Cuts a string to the length of $length and replaces the last characters
 * with the ending if the text is longer than length.
 *
 * This function is borrowed from CakePHP v2.0, under the MIT license. See
 * http://book.cakephp.org/view/1469/Text#truncate-1625
 *
lechuck's avatar
lechuck committed
854
 * @since 1.0.0
lucha's avatar
lucha committed
855
 * @since 2.6.0 Added 'strip_tags' and 'remove_links' as $options args.
lechuck's avatar
lechuck committed
856
857
858
859
860
 *
 * @param string $text   String to truncate.
 * @param int    $length Optional. Length of returned string, including ellipsis.
 *                       Default: 225.
 * @param array  $options {
lechuck's avatar
lechuck committed
861
 *     An array of HTML attributes and options. Each item is optional.
lechuck's avatar
lechuck committed
862
863
864
865
866
867
868
869
 *     @type string $ending            The string used after truncation.
 *                                     Default: ' [&hellip;]'.
 *     @type bool   $exact             If true, $text will be trimmed to exactly $length.
 *                                     If false, $text will not be cut mid-word. Default: false.
 *     @type bool   $html              If true, don't include HTML tags when calculating
 *                                     excerpt length. Default: true.
 *     @type bool   $filter_shortcodes If true, shortcodes will be stripped.
 *                                     Default: true.
lucha's avatar
lucha committed
870
871
872
873
 *     @type bool   $strip_tags        If true, HTML tags will be stripped. Default: false.
 *                                     Only applicable if $html is set to false.
 *     @type bool   $remove_links      If true, URLs will be stripped. Default: false.
 *                                     Only applicable if $html is set to false.
lechuck's avatar
lechuck committed
874
 * }
root's avatar
root committed
875
876
877
 * @return string Trimmed string.
 */
function bp_create_excerpt( $text, $length = 225, $options = array() ) {
lucha's avatar
lucha committed
878

lechuck's avatar
lechuck committed
879
	// Backward compatibility. The third argument used to be a boolean $filter_shortcodes.
root's avatar
root committed
880
881
	$filter_shortcodes_default = is_bool( $options ) ? $options : true;

lucha's avatar
lucha committed
882
	$r = bp_parse_args( $options, array(
root's avatar
root committed
883
884
885
		'ending'            => __( ' [&hellip;]', 'buddypress' ),
		'exact'             => false,
		'html'              => true,
lucha's avatar
lucha committed
886
887
888
		'filter_shortcodes' => $filter_shortcodes_default,
		'strip_tags'        => false,
		'remove_links'      => false,
lucha's avatar
lucha committed
889
	), 'create_excerpt' );
root's avatar
root committed
890

lechuck's avatar
lechuck committed
891
	// Save the original text, to be passed along to the filter.
root's avatar
root committed
892
893
	$original_text = $text;

ale's avatar
ale committed
894
895
896
	/**
	 * Filters the excerpt length to trim text to.
	 *
lechuck's avatar
lechuck committed
897
	 * @since 1.5.0
ale's avatar
ale committed
898
899
900
	 *
	 * @param int $length Length of returned string, including ellipsis.
	 */
lucha's avatar
lucha committed
901
	$length = apply_filters( 'bp_excerpt_length',      $length      );
ale's avatar
ale committed
902
903
904
905

	/**
	 * Filters the excerpt appended text value.
	 *
lechuck's avatar
lechuck committed
906
	 * @since 1.5.0
ale's avatar
ale committed
907
908
909
	 *
	 * @param string $value Text to append to the end of the excerpt.
	 */
lucha's avatar
lucha committed
910
	$ending = apply_filters( 'bp_excerpt_append_text', $r['ending'] );
root's avatar
root committed
911

lechuck's avatar
lechuck committed
912
	// Remove shortcodes if necessary.
lucha's avatar
lucha committed
913
	if ( ! empty( $r['filter_shortcodes'] ) ) {
root's avatar
root committed
914
		$text = strip_shortcodes( $text );
lucha's avatar
lucha committed
915
	}
root's avatar
root committed
916
917

	// When $html is true, the excerpt should be created without including HTML tags in the
lechuck's avatar
lechuck committed
918
	// excerpt length.
lucha's avatar
lucha committed
919
920
	if ( ! empty( $r['html'] ) ) {

lechuck's avatar
lechuck committed
921
		// The text is short enough. No need to truncate.
root's avatar
root committed
922
923
924
925
926
927
928
929
		if ( mb_strlen( preg_replace( '/<.*?>/', '', $text ) ) <= $length ) {
			return $text;
		}

		$totalLength = mb_strlen( strip_tags( $ending ) );
		$openTags    = array();
		$truncate    = '';

lechuck's avatar
lechuck committed
930
		// Find all the tags and HTML comments and put them in a stack for later use.
lechuck's avatar
lechuck committed
931
		preg_match_all( '/(<\/?([\w+!]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER );
lucha's avatar
lucha committed
932

ale's avatar
ale committed
933
		foreach ( $tags as $tag ) {
lechuck's avatar
lechuck committed
934
			// Process tags that need to be closed.
root's avatar
root committed
935
936
937
			if ( !preg_match( '/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s',  $tag[2] ) ) {
				if ( preg_match( '/<[\w]+[^>]*>/s', $tag[0] ) ) {
					array_unshift( $openTags, $tag[2] );
lucha's avatar
lucha committed
938
				} elseif ( preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag ) ) {
root's avatar
root committed
939
940
941
942
943
944
945
					$pos = array_search( $closeTag[1], $openTags );
					if ( $pos !== false ) {
						array_splice( $openTags, $pos, 1 );
					}
				}
			}

lucha's avatar
lucha committed
946
			$truncate     .= $tag[1];
root's avatar
root committed
947
			$contentLength = mb_strlen( preg_replace( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3] ) );
lucha's avatar
lucha committed
948

root's avatar
root committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
			if ( $contentLength + $totalLength > $length ) {
				$left = $length - $totalLength;
				$entitiesLength = 0;
				if ( preg_match_all( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE ) ) {
					foreach ( $entities[0] as $entity ) {
						if ( $entity[1] + 1 - $entitiesLength <= $left ) {
							$left--;
							$entitiesLength += mb_strlen( $entity[0] );
						} else {
							break;
						}
					}
				}

				$truncate .= mb_substr( $tag[3], 0 , $left + $entitiesLength );
				break;
			} else {
				$truncate .= $tag[3];
				$totalLength += $contentLength;
			}
			if ( $totalLength >= $length ) {
				break;
			}
		}
	} else {
lucha's avatar
lucha committed
974
975
976
977
978
979
980
981
982
983
		// Strip HTML tags if necessary.
		if ( ! empty( $r['strip_tags'] ) ) {
			$text = strip_tags( $text );
		}

		// Remove links if necessary.
		if ( ! empty( $r['remove_links'] ) ) {
			$text = preg_replace( '#^\s*(https?://[^\s"]+)\s*$#im', '', $text );
		}

root's avatar
root committed
984
		if ( mb_strlen( $text ) <= $length ) {
lucha's avatar
lucha committed
985
986
987
988
989
990
991
992
993
994
995
			/**
			 * Filters the final generated excerpt.
			 *
			 * @since 1.1.0
			 *
			 * @param string $truncate      Generated excerpt.
			 * @param string $original_text Original text provided.
			 * @param int    $length        Length of returned string, including ellipsis.
			 * @param array  $options       Array of HTML attributes and options.
			 */
			return apply_filters( 'bp_create_excerpt', $text, $original_text, $length, $options );
root's avatar
root committed
996
997
998
999
1000
		} else {
			$truncate = mb_substr( $text, 0, $length - mb_strlen( $ending ) );
		}
	}

lechuck's avatar
lechuck committed
1001
	// If $exact is false, we can't break on words.
lucha's avatar
lucha committed
1002
	if ( empty( $r['exact'] ) ) {
lechuck's avatar
lechuck committed
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
		// Find the position of the last space character not part of a tag.
		preg_match_all( '/<[a-z\!\/][^>]*>/', $truncate, $_truncate_tags, PREG_OFFSET_CAPTURE );

		// Rekey tags by the string index of their last character.
		$truncate_tags = array();
		if ( ! empty( $_truncate_tags[0] ) ) {
			foreach ( $_truncate_tags[0] as $_tt ) {
				$_tt['start'] = $_tt[1];
				$_tt['end']   = $_tt[1] + strlen( $_tt[0] );
				$truncate_tags[ $_tt['end'] ] = $_tt;
			}
		}

		$truncate_length = mb_strlen( $truncate );
		$spacepos = $truncate_length + 1;
		for ( $pos = $truncate_length - 1; $pos >= 0; $pos-- ) {
			// Word boundaries are spaces and the close of HTML tags, when the tag is preceded by a space.
			$is_word_boundary = ' ' === $truncate[ $pos ];
			if ( ! $is_word_boundary && isset( $truncate_tags[ $pos - 1 ] ) ) {
				$preceding_tag    = $truncate_tags[ $pos - 1 ];
				if ( ' ' === $truncate[ $preceding_tag['start'] - 1 ] ) {
					$is_word_boundary = true;
					break;
				}
			}

			if ( ! $is_word_boundary ) {
				continue;
			}

			// If there are no tags in the string, the first space found is the right one.
			if ( empty( $truncate_tags ) ) {
				$spacepos = $pos;
				break;
			}

			// Look at each tag to see if the space is inside of it.
			$intag = false;
			foreach ( $truncate_tags as $tt ) {
				if ( $pos > $tt['start'] && $pos < $tt['end'] ) {
					$intag = true;
					break;
				}
			}

			if ( ! $intag ) {
				$spacepos = $pos;
				break;
			}
		}

		if ( $r['html'] ) {
			$bits = mb_substr( $truncate, $spacepos );
			preg_match_all( '/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER );
			if ( !empty( $droppedTags ) ) {
				foreach ( $droppedTags as $closingTag ) {
					if ( !in_array( $closingTag[1], $openTags ) ) {
						array_unshift( $openTags, $closingTag[1] );
root's avatar
root committed
1061
1062
1063
1064
					}
				}
			}
		}
lechuck's avatar
lechuck committed
1065
1066

		$truncate = rtrim( mb_substr( $truncate, 0, $spacepos ) );
root's avatar
root committed
1067
1068
1069
	}
	$truncate .= $ending;

lucha's avatar
lucha committed
1070
	if ( !empty( $r['html'] ) ) {
root's avatar
root committed
1071
1072
1073
1074
1075
		foreach ( $openTags as $tag ) {
			$truncate .= '</' . $tag . '>';
		}
	}

lucha's avatar
lucha committed
1076
	/** This filter is documented in /bp-core/bp-core-template.php */
root's avatar
root committed
1077
1078
	return apply_filters( 'bp_create_excerpt', $truncate, $original_text, $length, $options );
}
lucha's avatar
lucha committed
1079
add_filter( 'bp_create_excerpt', 'stripslashes_deep'  );
root's avatar
root committed
1080
1081
add_filter( 'bp_create_excerpt', 'force_balance_tags' );

lechuck's avatar
lechuck committed
1082
/**
lechuck's avatar
lechuck committed
1083
 * Output the total member count for the site.
lechuck's avatar
lechuck committed
1084
1085
 *
 * @since 1.2.0
lechuck's avatar
lechuck committed
1086
 */
root's avatar
root committed
1087
1088
1089
function bp_total_member_count() {
	echo bp_get_total_member_count();
}
lechuck's avatar
lechuck committed
1090
	/**
lechuck's avatar
lechuck committed
1091
1092
1093
1094
1095
1096
	 * Return the total member count in your BP instance.
	 *
	 * Since BuddyPress 1.6, this function has used bp_core_get_active_member_count(),
	 * which counts non-spam, non-deleted users who have last_activity.
	 * This value will correctly match the total member count number used
	 * for pagination on member directories.
lechuck's avatar
lechuck committed
1097
	 *
lechuck's avatar
lechuck committed
1098
1099
1100
	 * Before BuddyPress 1.6, this function used bp_core_get_total_member_count(),
	 * which did not take into account last_activity, and thus often
	 * resulted in higher counts than shown by member directory pagination.
lechuck's avatar
lechuck committed
1101
	 *
lechuck's avatar
lechuck committed
1102
1103
	 * @since 1.2.0
	 *
lechuck's avatar
lechuck committed
1104
	 * @return int Member count.
lechuck's avatar
lechuck committed
1105
	 */
root's avatar
root committed
1106
	function bp_get_total_member_count() {
ale's avatar
ale committed
1107
1108
1109
1110

		/**
		 * Filters the total member count in your BP instance.
		 *
lechuck's avatar
lechuck committed
1111
		 * @since 1.2.0
ale's avatar
ale committed
1112
1113
1114
		 *
		 * @param int $value Member count.
		 */
lechuck's avatar
lechuck committed
1115
		return apply_filters( 'bp_get_total_member_count', bp_core_get_active_member_count() );
root's avatar
root committed
1116
1117
1118
	}
	add_filter( 'bp_get_total_member_count', 'bp_core_number_format' );

lechuck's avatar
lechuck committed
1119
1120
1121
1122
1123
/**
 * Output whether blog signup is allowed.
 *
 * @todo Deprecate. It doesn't make any sense to echo a boolean.
 */
root's avatar
root committed
1124
1125
1126
function bp_blog_signup_allowed() {
	echo bp_get_blog_signup_allowed();
}
lechuck's avatar
lechuck committed
1127
1128
1129
1130
1131
1132
	/**
	 * Is blog signup allowed?
	 *
	 * Returns true if is_multisite() and blog creation is enabled at
	 * Network Admin > Settings.
	 *
lechuck's avatar
lechuck committed
1133
1134
	 * @since 1.2.0
	 *
lechuck's avatar
lechuck committed
1135
1136
	 * @return bool True if blog signup is allowed, otherwise false.
	 */
root's avatar
root committed
1137
1138
	function bp_get_blog_signup_allowed() {

lucha's avatar
lucha committed
1139
		if ( ! is_multisite() ) {
root's avatar
root committed
1140
			return false;
lucha's avatar
lucha committed
1141
		}
root's avatar
root committed
1142

ale's avatar
ale committed
1143
		$status = bp_core_get_root_option( 'registration' );
lucha's avatar
lucha committed
1144
		if ( ( 'none' !== $status ) && ( 'user' !== $status ) ) {
root's avatar
root committed
1145
			return true;
lucha's avatar
lucha committed
1146
		}
root's avatar
root committed
1147
1148
1149
1150

		return false;
	}

lechuck's avatar
lechuck committed
1151
1152
1153
/**
 * Check whether an activation has just been completed.
 *
lechuck's avatar
lechuck committed
1154
1155
 * @since 1.1.0
 *
lechuck's avatar
lechuck committed
1156
 * @return bool True if the activation_complete global flag has been set,
lechuck's avatar
lechuck committed
1157
 *              otherwise false.
lechuck's avatar
lechuck committed
11