Created
February 6, 2020 19:57
-
-
Save mahfelwp/6feabd70b57107e99e454161def2edd0 to your computer and use it in GitHub Desktop.
add member types to youzer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Register member types. | |
* | |
* From the wp-admin/extended profile | |
* it will be possible to set the user's member type | |
*/ | |
function using_mt_register_member_types() { | |
bp_register_member_type( 'company', array( | |
'labels' => array( | |
'name' => __( 'شرکت ها' ), | |
'singular_name' => __( 'شرکت' ) | |
), | |
'has_directory' => 'company',//can be given a slug that will be appended to member directory to list all users of this type | |
) ); | |
bp_register_member_type( 'organization', array( | |
'labels' => array( | |
'name' => __( 'سازمان ها', 'using-mt' ), | |
'singular_name' => __( 'سازمان', 'using-mt' ), | |
), | |
'has_directory' => 'organization', | |
) ); | |
} | |
add_action( 'bp_init', 'using_mt_register_member_types' ); | |
/** | |
* Get the singular name out of of member type's name or array of names | |
*/ | |
function using_mt_get_member_type_singular_name( $name = '' ) { | |
if ( empty( $name ) ) { | |
return false; | |
} | |
$output = ''; | |
if ( is_array( $name ) ) { | |
$member_types_objects = bp_get_member_types( array(), 'objects' ); | |
$output_array = array(); | |
foreach ( $name as $name_type ) { | |
if ( empty( $member_types_objects[ $name_type ]->labels['singular_name'] ) ) { | |
continue; | |
} | |
$output_array[] = $member_types_objects[ $name_type ]->labels['singular_name']; | |
} | |
$output = join( ', ', $output_array ); | |
} else { | |
$member_type_object = bp_get_member_type_object( $name ); | |
if ( ! empty( $member_type_object->labels['singular_name'] ) ) { | |
$output = $member_type_object->labels['singular_name']; | |
} | |
} | |
return $output; | |
} | |
/** | |
* Display in loop | |
* | |
* If a member has more than one member types, they will be comma separated | |
*/ | |
function using_mt_in_members_loop() { | |
$member_type = bp_get_member_type( bp_get_member_user_id(), false ); | |
if($member_type) | |
echo '<div class="yz-member-type" data-yztooltip="نوع کاربر"> | |
<div class="status-label">'.esc_html( using_mt_get_member_type_singular_name( $member_type ) ).'</div></div>'; | |
} | |
add_action( 'yz_user_tools', 'using_mt_in_members_loop' ); | |
/** | |
* Get the member type count | |
* | |
* @todo use a WP Taxonomy function ? | |
*/ | |
function using_mt_count_member_types( $member_type = '', $taxonomy = 'bp_member_type' ) { | |
global $wpdb; | |
$member_types = bp_get_member_types(); | |
if ( empty( $member_type ) || empty( $member_types[ $member_type ] ) ) { | |
return false; | |
} | |
$count_types = wp_cache_get( 'using_mt_count_member_types', 'using_mt_bp_member_type' ); | |
if ( ! $count_types ) { | |
if ( ! bp_is_root_blog() ) { | |
switch_to_blog( bp_get_root_blog_id() ); | |
} | |
$sql = array( | |
'select' => "SELECT t.slug, tt.count FROM {$wpdb->term_taxonomy} tt LEFT JOIN {$wpdb->terms} t", | |
'on' => 'ON tt.term_id = t.term_id', | |
'where' => $wpdb->prepare( 'WHERE tt.taxonomy = %s', $taxonomy ), | |
); | |
$count_types = $wpdb->get_results( join( ' ', $sql ) ); | |
wp_cache_set( 'using_mt_count_member_types', $count_types, 'using_mt_bp_member_type' ); | |
restore_current_blog(); | |
} | |
$type_count = wp_filter_object_list( $count_types, array( 'slug' => $member_type ), 'and', 'count' ); | |
$type_count = array_values( $type_count ); | |
if ( empty( $type_count ) ) { | |
return 0; | |
} | |
return (int) $type_count[0]; | |
} | |
/** | |
* Display tabs used to filter the members directory by types. | |
*/ | |
function using_mt_display_directory_tabs() { | |
$member_types = bp_get_member_types( array(), 'objects' ); | |
// Loop in member types to build the tabs | |
foreach ( $member_types as $member_type ) : ?> | |
<li id="members-<?php echo esc_attr( $member_type->name ) ;?>"> | |
<a href="<?php bp_members_directory_permalink(); ?>"><i class="fas fa-fire"></i><?php printf( '%s <span>%d</span>', $member_type->labels['name'], using_mt_count_member_types( $member_type->name ) ); ?></a> | |
</li> | |
<?php endforeach; | |
} | |
add_action( 'bp_members_directory_member_types', 'using_mt_display_directory_tabs' ); | |
/** | |
* Alter the members query arguments to filter the members query by selected type. | |
*/ | |
function using_mt_set_has_members_type_arg( $args = array() ) { | |
// Get member types to check scope | |
$member_types = bp_get_member_types(); | |
// Set the member type arg if scope match one of the registered member type | |
if ( ! empty( $args['scope'] ) && ! empty( $member_types[ $args['scope'] ] ) ) { | |
$args['member_type'] = $args['scope']; | |
} | |
return $args; | |
} | |
add_filter( 'bp_before_has_members_parse_args', 'using_mt_set_has_members_type_arg', 10, 1 ); | |
/** | |
* Clean count cache when one of the member type count has been updated | |
*/ | |
function using_mt_clean_count_cache( $term = 0, $taxonomy = null ) { | |
if ( empty( $term ) || empty( $taxonomy->name ) || 'bp_member_type' != $taxonomy->name ) { | |
return; | |
} | |
wp_cache_delete( 'using_mt_count_member_types', 'using_mt_bp_member_type' ); | |
} | |
add_action( 'edited_term_taxonomy', 'using_mt_clean_count_cache', 10, 2 ); | |
/** | |
* Display the member type of the displayed user | |
*/ | |
function using_mt_member_header_display() { | |
$member_type = bp_get_member_type( bp_displayed_user_id() ); | |
if ( empty( $member_type ) ) { | |
return; | |
} | |
$member_type_object = bp_get_member_type_object( $member_type ); | |
?> | |
<div class="yz-member-type" data-yztooltip="نوع کاربر"> | |
<div class="status-label"><?php echo esc_html( $member_type_object->labels['singular_name'] ); ?></div></div> | |
<?php | |
} | |
add_action( 'yz_user_tools', 'using_mt_member_header_display' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment