-
-
Save imath/716164a35a5126144fb0 to your computer and use it in GitHub Desktop.
Contributors directory
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 | |
/** | |
* BuddyPress Custom | |
* | |
* Directory of contributors. | |
*/ | |
// Exit if accessed directly | |
defined( 'ABSPATH' ) || exit; | |
define( 'BP_DEFAULT_COMPONENT', 'profile' ); | |
// Define the user's profile nav as first one | |
function imath_wp_profile_nav_position() { | |
if ( defined( 'BP_DEFAULT_COMPONENT' ) && 'profile' == BP_DEFAULT_COMPONENT ) { | |
buddypress()->bp_nav['profile']['position'] = 1; | |
} | |
} | |
add_action( 'bp_activity_setup_nav', 'imath_wp_profile_nav_position' ); | |
// Remove the admin nav if user is not a contributor | |
function imath_maybe_remove_activity_admin_nav( $wp_admin_nav = array() ) { | |
if ( ! bp_current_user_can( 'edit_posts' ) ) { | |
$wp_admin_nav = array(); | |
} | |
return $wp_admin_nav; | |
} | |
add_action( 'bp_activity_admin_nav', 'imath_maybe_remove_activity_admin_nav', 10, 1 ); | |
add_action( 'bp_xprofile_admin_nav', 'imath_maybe_remove_activity_admin_nav', 10, 1 ); | |
// Only activate members, activity and blogs component at install | |
function imath_restrict_default_component( $default = array() ) { | |
return array( | |
'members' => 1, | |
'activity' => 1, | |
'blogs' => 1, | |
); | |
} | |
add_filter( 'bp_new_install_default_components', 'imath_restrict_default_component', 10, 1 ); | |
// Exclude some directory pages from WordPress pages listing | |
function imath_exclude_directory_pages( $pages = array() ) { | |
$exclude = array(); | |
if ( bp_is_active( 'activity' ) ) { | |
$exclude[] = buddypress()->pages->activity->id; | |
} | |
$pages = array_merge( $pages, $exclude ); | |
return $pages; | |
} | |
add_filter( 'bp_core_exclude_pages', 'imath_exclude_directory_pages', 10, 1 ); | |
// Get contributors & count their posts | |
function imath_get_contributors() { | |
$bp = buddypress(); | |
if ( empty( $bp->members->site_contributors ) ) { | |
$contributors = wp_list_pluck( get_users( array( 'fields' => array( 'ID' ), 'who' => 'authors' ) ), 'ID' ); | |
$bp->members->site_contributors = count_many_users_posts( $contributors, 'post', true ); | |
} | |
return $bp->members->site_contributors; | |
} | |
// Add a new option to filter members on the directory page | |
function imath_order_by_post_num_option() { | |
?> | |
<option value="post_num"><?php _e( 'Amount of posts', 'text-domain' ); ?></option> | |
<?php | |
} | |
add_action( 'bp_members_directory_order_options', 'imath_order_by_post_num_option' ); | |
// Display the post count for a member's entry in the members directory | |
function imath_display_post_count() { | |
$contributors = imath_get_contributors(); | |
if ( isset( $contributors[ bp_get_member_user_id() ] ) ) { | |
$count = $contributors[ bp_get_member_user_id() ]; | |
printf( _n( | |
'<span class="imath-post-count">%s post</span>', | |
'<span class="imath-post-count">%s posts</span>', | |
$count, | |
'text-domain' | |
), number_format_i18n( $count ) ); | |
} | |
} | |
add_action( 'bp_directory_members_actions', 'imath_display_post_count' ); | |
// Sort contributors by number of posts | |
function imath_filter_by_post_num( $query = null ) { | |
if ( 'post_num' !== $query->query_vars['type'] ) { | |
return; | |
} | |
$contributors = imath_get_contributors(); | |
arsort( $contributors ); | |
$contributors_ids = array_merge( array( 'u.id' ), wp_parse_id_list( array_keys( $contributors ) ) ); | |
$contributors_ids_sql = implode( ',', $contributors_ids ); | |
$query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $contributors_ids_sql . ")"; | |
} | |
add_action( 'bp_pre_user_query', 'imath_filter_by_post_num', 10, 1 ); | |
// Replace the members count by contributors count | |
function imath_count_contributors( $count = 0 ) { | |
$contributors = imath_get_contributors(); | |
if ( is_array( $contributors ) ) { | |
$count = count( $contributors ); | |
} | |
return $count; | |
} | |
add_filter( 'bp_core_get_active_member_count', 'imath_count_contributors', 10, 1 ); | |
// Make sure to only fetch contributors in members loops | |
function imath_list_contributors( $args = array() ) { | |
$args['include'] = join( ',', array_keys( (array) imath_get_contributors() ) ); | |
return $args; | |
} | |
add_filter( 'bp_before_has_members_parse_args', 'imath_list_contributors', 10, 1 ); | |
// Disable BuddyPress directories from displaying except the members directory | |
function imath_disable_directories( $bool, $component = '' ) { | |
$allowed_components = array( | |
'register' => true, | |
'activate' => true, | |
'members' => true, | |
); | |
if ( ! isset( $allowed_components[ $component ] ) && ! bp_is_user() ) { | |
return false; | |
} | |
if ( bp_displayed_user_id() && ! user_can( bp_displayed_user_id(), 'edit_posts' ) ) { | |
return false; | |
} | |
return $bool; | |
} | |
add_filter( 'bp_is_current_component', 'imath_disable_directories', 10, 2 ); | |
// Only allow contributors profile and members directory to be viewed | |
function imath_disable_buddypress_front_end_parts() { | |
if ( is_buddypress() && ! bp_is_register_page() && ! bp_is_activation_page() && ! bp_is_members_directory() && ! bp_is_user() ) { | |
bp_do_404(); | |
return; | |
} | |
if ( bp_displayed_user_id() && ! user_can( bp_displayed_user_id(), 'edit_posts' ) ) { | |
bp_do_404(); | |
return; | |
} | |
} | |
add_action( 'bp_actions', 'imath_disable_buddypress_front_end_parts', 1 ); | |
/** | |
* In wp-admin/extended profile remove not needed metaboxes for regular users | |
*/ | |
function imath_disable_members_admin_major_metabox() { | |
global $wp_meta_boxes; | |
if ( bp_is_active( 'xprofile' ) && bp_current_user_can( 'edit_posts' ) ) { | |
return; | |
} | |
// Only keep stat metabox for regular users | |
if ( ! empty( $wp_meta_boxes['profile_page_bp-profile-edit'] ) ) { | |
foreach ( $wp_meta_boxes['profile_page_bp-profile-edit'] as $meta_box_context => $meta_box_priorities ) { | |
foreach ( $meta_box_priorities as $priority => $meta_box ) { | |
foreach( $meta_box as $meta_box_id => $meta_box_content ) { | |
if ( 'bp_members_admin_user_stats' !== $meta_box_id ) { | |
remove_meta_box( $meta_box_id, get_current_screen(), $meta_box_context ); | |
} | |
} | |
} | |
} | |
} | |
} | |
add_action( 'bp_members_admin_user_metaboxes', 'imath_disable_members_admin_major_metabox' ); | |
// Disable Activity form | |
function imath_disable_activity_form( $templates, $slug, $name ) { | |
if ( bp_is_user() && 'activity/post-form' === $slug ) { | |
$templates = false; | |
} | |
return $templates; | |
} | |
add_filter( 'bp_get_template_part', 'imath_disable_activity_form', 10, 3 ); | |
// Disable mentions | |
add_filter( 'bp_activity_do_mentions', '__return_false' ); | |
// Disable favorites | |
add_filter( 'bp_activity_can_favorite', '__return_false' ); | |
// Remove Activity update actions | |
function imath_restrict_activity_actions( $action_params = array(), $component_id = '' ) { | |
if ( buddypress()->activity->id === $component_id ) { | |
$action_params['context'] = array_diff( $action_params['context'], array( 'member' ) ); | |
} | |
return $action_params; | |
} | |
add_filter( 'bp_activity_set_action', 'imath_restrict_activity_actions', 10, 2 ); | |
// Edit the author link | |
function imath_filter_author_link( $link = '', $author_id = 0, $author_nicename = '' ) { | |
if( ! empty( $author_id ) ) { | |
$link = bp_core_get_userlink( $author_id, false, true ); | |
} | |
return $link; | |
} | |
add_filter( 'author_link', 'imath_filter_author_link', 10, 3 ); | |
// Remove member's link if not a contributor | |
function imath_get_user_domain( $domain = '', $user_id = 0 ) { | |
if ( ! user_can( $user_id, 'edit_posts' ) ) { | |
return false; | |
} | |
return $domain; | |
} | |
add_filter( 'bp_core_get_user_domain', 'imath_get_user_domain', 10, 2 ); | |
// Keep WordPress admin profile if not a contributor | |
function imath_edit_profile_url( $profile_link = '', $url = '', $user_id = 0 ) { | |
if ( ! user_can( $user_id, 'edit_posts' ) ) { | |
$profile_link = $url; | |
} | |
return $profile_link; | |
} | |
add_filter( 'bp_members_edit_profile_url', 'imath_edit_profile_url', 10, 3 ); | |
function imath_add_author_descripton() { | |
if ( ! bp_is_active( 'xprofile' ) ) { | |
return; | |
} | |
// Display the regular WordPress description if xProfile is active | |
the_author_meta( 'description', bp_displayed_user_id() ); | |
// Remove the link to member's directory search for xProfile field data | |
remove_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9, 2 ); | |
} | |
add_action( 'bp_profile_header_meta', 'imath_add_author_descripton' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment