[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BP Friends Blocks Functions. 4 * 5 * @package BuddyPress 6 * @subpackage FriendsBlocks 7 * @since 9.0.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * Adds specific script data for the BP Friends blocks. 15 * 16 * Only used for the BP Friends block. 17 * 18 * @since 9.0.0 19 */ 20 function bp_friends_blocks_add_script_data() { 21 $friends_blocks = array_filter( buddypress()->friends->block_globals['bp/friends']->items ); 22 23 if ( ! $friends_blocks ) { 24 return; 25 } 26 27 $path = sprintf( 28 '/%1$s/%2$s/%3$s', 29 bp_rest_namespace(), 30 bp_rest_version(), 31 buddypress()->members->id 32 ); 33 34 wp_localize_script( 35 'bp-friends-script', 36 'bpFriendsSettings', 37 array( 38 'path' => ltrim( $path, '/' ), 39 'root' => esc_url_raw( get_rest_url() ), 40 'nonce' => wp_create_nonce( 'wp_rest' ), 41 ) 42 ); 43 44 // Include the common JS template. 45 echo bp_get_dynamic_template_part( 'assets/widgets/friends.php' ); 46 47 // List the block specific props. 48 wp_add_inline_script( 49 'bp-friends-script', 50 sprintf( 'var bpFriendsBlocks = %s;', wp_json_encode( array_values( $friends_blocks ) ) ), 51 'before' 52 ); 53 } 54 55 /** 56 * Callback function to render the BP Friends Block. 57 * 58 * @since 9.0.0 59 * 60 * @param array $attributes The block attributes. 61 * @return string HTML output. 62 */ 63 function bp_friends_render_friends_block( $attributes = array() ) { 64 $block_args = bp_parse_args( 65 $attributes, 66 array( 67 'maxFriends' => 5, 68 'friendDefault' => 'active', 69 'linkTitle' => false, 70 'postId' => 0, // If the postId attribute is defined, post author friends are needed. 71 ) 72 ); 73 74 $user_id = 0; 75 if ( $block_args['postId'] ) { 76 $user_id = (int) get_post_field( 'post_author', $block_args['postId'] ); 77 } else { 78 $user_id = bp_displayed_user_id(); 79 80 if ( ! $user_id && isset( $_SERVER['REQUEST_URI'] ) ) { 81 $request_uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ); 82 $request_path = wp_parse_url( $request_uri, PHP_URL_PATH ); 83 $regex = addcslashes( sprintf( '%s/.*bp/friends', rest_get_url_prefix() ), '/' ); 84 85 if ( preg_match( "/{$regex}/", $request_path ) ) { 86 $user_id = bp_loggedin_user_id(); 87 } 88 } 89 } 90 91 if ( ! $user_id ) { 92 return ''; 93 } 94 95 $classnames = 'widget_bp_core_friends_widget buddypress widget'; 96 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); 97 98 $max_friends = (int) $block_args['maxFriends']; 99 $no_friends = __( 'Sorry, no members were found.', 'buddypress' ); 100 101 /** 102 * Filters the separator of the friends block nav links. 103 * 104 * @since 9.0.0 105 * 106 * @param string $separator Separator string. Default '|'. 107 */ 108 $separator = apply_filters( 'bp_friends_block_nav_links_separator', '|' ); 109 110 // Make sure the widget ID is unique. 111 $widget_id = uniqid( 'friends-list-' ); 112 113 $link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_friends_slug() ); 114 115 /* translators: %s: member name */ 116 $title = sprintf( __( '%s\'s Friends', 'buddypress' ), bp_core_get_user_displayname( $user_id ) ); 117 118 // Set the Block's title. 119 if ( true === $block_args['linkTitle'] ) { 120 $widget_content = sprintf( 121 '<h2 class="widget-title"><a href="%1$s">%2$s</a></h2>', 122 esc_url( $link ), 123 esc_html( $title ) 124 ); 125 } else { 126 $widget_content = sprintf( '<h2 class="widget-title">%s</h2>', esc_html( $title ) ); 127 } 128 129 $item_options = array( 130 'newest' => array( 131 'class' => '', 132 'label' => __( 'Newest', 'buddypress' ), 133 ), 134 'active' => array( 135 'class' => '', 136 'label' => __( 'Active', 'buddypress' ), 137 ), 138 'popular' => array( 139 'class' => '', 140 'label' => __( 'Popular', 'buddypress' ), 141 ), 142 ); 143 144 $item_options_output = array(); 145 $separator_output = sprintf( ' <span class="bp-separator" role="separator">%s</span> ', esc_html( $separator ) ); 146 147 foreach ( $item_options as $item_type => $item_attr ) { 148 if ( $block_args['friendDefault'] === $item_type ) { 149 $item_attr['class'] = ' class="selected"'; 150 } 151 152 $item_options_output[] = sprintf( 153 '<a href="%1$s" data-bp-sort="%2$s"%3$s>%4$s</a>', 154 esc_url( $link ), 155 esc_attr( $item_type ), 156 $item_attr['class'], 157 esc_html( $item_attr['label'] ) 158 ); 159 } 160 161 $preview = ''; 162 $default_args = array( 163 'user_id' => $user_id, 164 'type' => $block_args['friendDefault'], 165 'per_page' => $max_friends, 166 'populate_extras' => true, 167 ); 168 169 // Previewing the Block inside the editor. 170 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { 171 $bp_query = bp_core_get_users( $default_args ); 172 $preview = sprintf( '<div class="widget-error">%s</div>', $no_friends ); 173 174 if ( is_array( $bp_query['users'] ) && 0 < count( $bp_query['users'] ) ) { 175 $preview = ''; 176 foreach ( $bp_query['users'] as $user ) { 177 if ( 'newest' === $block_args['friendDefault'] ) { 178 /* translators: %s is time elapsed since the registration date happened */ 179 $extra = sprintf( _x( 'Registered %s', 'The timestamp when the user registered', 'buddypress' ), bp_core_time_since( $user->user_registered ) ); 180 } elseif ( 'popular' === $block_args['friendDefault'] && isset( $item_options['popular'] ) && isset( $user->total_friend_count ) ) { 181 /* translators: %s: total friend count */ 182 $extra = sprintf( _n( '%s friend', '%s friends', $user->total_friend_count, 'buddypress' ), number_format_i18n( $user->total_friend_count ) ); 183 } else { 184 /* translators: %s: last activity timestamp (e.g. "Active 1 hour ago") */ 185 $extra = sprintf( __( 'Active %s', 'buddypress' ), bp_core_time_since( $user->last_activity ) ); 186 } 187 188 $preview .= bp_get_dynamic_template_part( 189 'assets/widgets/friends.php', 190 'php', 191 array( 192 'data.link' => bp_core_get_user_domain( $user->ID, $user->user_nicename, $user->user_login ), 193 'data.name' => $user->display_name, 194 'data.avatar_urls.thumb' => bp_core_fetch_avatar( 195 array( 196 'item_id' => $user->ID, 197 'html' => false, 198 ) 199 ), 200 'data.avatar_alt' => esc_attr( 201 sprintf( 202 /* translators: %s: member name */ 203 __( 'Profile picture of %s', 'buddypress' ), 204 $user->display_name 205 ) 206 ), 207 'data.id' => $user->ID, 208 'data.extra' => $extra, 209 ) 210 ); 211 } 212 } 213 } elseif ( defined( 'WP_USE_THEMES' ) ) { 214 // Get corresponding friends. 215 $path = sprintf( 216 '/%1$s/%2$s/%3$s', 217 bp_rest_namespace(), 218 bp_rest_version(), 219 buddypress()->members->id 220 ); 221 222 $default_path = add_query_arg( 223 $default_args, 224 $path 225 ); 226 227 $preloaded_friends = rest_preload_api_request( '', $default_path ); 228 229 buddypress()->friends->block_globals['bp/friends']->items[ $widget_id ] = (object) array( 230 'selector' => $widget_id, 231 'query_args' => $default_args, 232 'preloaded' => reset( $preloaded_friends ), 233 ); 234 235 // Only enqueue common/specific scripts and data once per page load. 236 if ( ! has_action( 'wp_footer', 'bp_friends_blocks_add_script_data', 1 ) ) { 237 wp_set_script_translations( 'bp-friends-script', 'buddypress' ); 238 wp_enqueue_script( 'bp-friends-script' ); 239 240 add_action( 'wp_footer', 'bp_friends_blocks_add_script_data', 1 ); 241 } 242 } 243 244 $widget_content .= sprintf( 245 '<div class="item-options"> 246 %1$s 247 </div> 248 <ul id="%2$s" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true"> 249 %3$s 250 </ul>', 251 implode( $separator_output, $item_options_output ), 252 esc_attr( $widget_id ), 253 $preview 254 ); 255 256 // Adds a container to make sure the block is styled even when used into the Columns parent block. 257 $widget_content = sprintf( '<div class="bp-dynamic-block-container">%s</div>', "\n" . $widget_content . "\n" ); 258 259 // Only add a block wrapper if not loaded into a Widgets sidebar. 260 if ( ! did_action( 'dynamic_sidebar_before' ) ) { 261 return sprintf( 262 '<div %1$s>%2$s</div>', 263 $wrapper_attributes, 264 $widget_content 265 ); 266 } 267 268 return $widget_content; 269 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:57 2024 | Cross-referenced by PHPXref 0.7.1 |