Skip to:
Content

BuddyPress.org

Ticket #5121: 5121.patch

File 5121.patch, 13.3 KB (added by imath, 6 years ago)
  • src/bp-activity/bp-activity-filters.php

    diff --git src/bp-activity/bp-activity-filters.php src/bp-activity/bp-activity-filters.php
    index a85342e..de36837 100644
    function bp_activity_heartbeat_strings( $strings = array() ) { 
    571571        return $strings;
    572572}
    573573add_filter( 'bp_core_get_js_strings', 'bp_activity_heartbeat_strings', 10, 1 );
     574
     575/**
     576 * Map user caps for Activity component
     577 *
     578 * @since BuddyPress (?)
     579 *
     580 * @param array $caps The caps that WP associates with the given role.
     581 * @param array $caps The caps being tested for in WP_User::has_cap().
     582 * @param array $args Miscellaneous arguments passed to the user_has_cap filter.
     583 * @return array $caps The user's cap list.
     584 */
     585function bp_activity_map_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
     586
     587        switch( $cap ) {
     588                case 'delete_activity' :
     589                        if( empty( $args[0] )  ) {
     590                                $caps = array( 'bp_moderate' );
     591                        } else if ( isset( $args[0]['component'], $args[0]['item_id'] ) && 'activity' == $args[0]['component'] && $user_id != $args[0]['item_id'] && empty( $args[0]['moderate'] ) ) {
     592                                $caps = array( 'bp_moderate' );
     593                        }
     594                break;
     595        }
     596
     597        return apply_filters( 'bp_activity_map_meta_caps', $caps, $cap, $user_id, $args );
     598}
     599add_filter( 'bp_map_meta_caps', 'bp_activity_map_meta_caps', 10, 4 );
  • src/bp-activity/bp-activity-loader.php

    diff --git src/bp-activity/bp-activity-loader.php src/bp-activity/bp-activity-loader.php
    index 0f68f91..862f075 100644
    class BP_Activity_Component extends BP_Component { 
    359359
    360360                parent::setup_actions();
    361361        }
     362
     363        /**
     364         * Set up activity component user caps.
     365         *
     366         * @since BuddyPress (?)
     367         */
     368        public function setup_caps( $caps = array() ) {
     369                $caps = array(
     370                        'view_activity'   => true,
     371                );
     372
     373                if ( is_user_logged_in() ) {
     374                        $caps = array_merge( $caps, array(
     375                                'publish_activity'  => true,
     376                                'delete_activity'   => true,
     377                                'favorite_activity' => true,
     378                                'comment_activity'  => true,
     379                        ) );
     380                }
     381
     382                parent::setup_caps( $caps );
     383        }
    362384}
    363385
    364386/**
  • src/bp-activity/bp-activity-template.php

    diff --git src/bp-activity/bp-activity-template.php src/bp-activity/bp-activity-template.php
    index cd0c279..7e21cda 100644
    function bp_activity_user_can_delete( $activity = false ) { 
    16431643
    16441644        $can_delete = false;
    16451645
    1646         if ( bp_current_user_can( 'bp_moderate' ) )
    1647                 $can_delete = true;
    1648 
    1649         if ( is_user_logged_in() && $activity->user_id == bp_loggedin_user_id() )
    1650                 $can_delete = true;
     1646        $args = array(
     1647                'item_id'   => $activity->user_id,
     1648                'component' => 'activity',
     1649                'moderate'  => bp_is_item_admin() && bp_is_single_item()
     1650        );
    16511651
    1652         if ( bp_is_item_admin() && bp_is_single_item() )
    1653                 $can_delete = true;
     1652        $can_delete = bp_current_user_can( 'delete_activity', bp_get_root_blog_id(), $args );
    16541653
    16551654        return apply_filters( 'bp_activity_user_can_delete', $can_delete, $activity );
    16561655}
  • src/bp-core/bp-core-actions.php

    diff --git src/bp-core/bp-core-actions.php src/bp-core/bp-core-actions.php
    index df61f5a..65da36f 100644
    add_action( 'bp_loaded', 'bp_register_theme_directory', 14 ); 
    6666 */
    6767add_action( 'bp_init', 'bp_core_set_uri_globals',    2  );
    6868add_action( 'bp_init', 'bp_setup_globals',           4  );
     69add_action( 'bp_init', 'bp_setup_caps',              5  );
    6970add_action( 'bp_init', 'bp_setup_nav',               6  );
    7071add_action( 'bp_init', 'bp_setup_title',             8  );
    7172add_action( 'bp_init', 'bp_core_load_admin_bar_css', 12 );
  • src/bp-core/bp-core-caps.php

    diff --git src/bp-core/bp-core-caps.php src/bp-core/bp-core-caps.php
    index 2ed693d..bf6357f 100644
    function bp_current_user_can( $capability, $blog_id = 0 ) { 
    191191        if ( empty( $blog_id ) )
    192192                $blog_id = bp_get_root_blog_id();
    193193
    194         $retval = current_user_can_for_blog( $blog_id, $capability );
     194        $args = array_slice( func_get_args(), 2 );
     195        $args = array_merge( array( $blog_id, $capability ), $args );
     196
     197        $retval = call_user_func_array( 'current_user_can_for_blog', $args );
    195198
    196199        return (bool) apply_filters( 'bp_current_user_can', $retval, $capability, $blog_id );
    197200}
  • src/bp-core/bp-core-component.php

    diff --git src/bp-core/bp-core-component.php src/bp-core/bp-core-component.php
    index c16c17c..7c4b1fc 100644
    class BP_Component { 
    375375                // Generate rewrite rules
    376376                add_action( 'bp_generate_rewrite_rules', array( $this, 'generate_rewrite_rules' ), 10 );
    377377
     378                // Setup user caps for components
     379                add_action( 'bp_setup_caps',             array( $this, 'setup_caps'             ), 10 );
     380
    378381                // Additional actions can be attached here
    379382                do_action( 'bp_' . $this->id . '_setup_actions' );
    380383        }
    class BP_Component { 
    602605        public function generate_rewrite_rules() {
    603606                do_action( 'bp_' . $this->id . '_generate_rewrite_rules' );
    604607        }
     608
     609        /**
     610         * Set up the component user caps.
     611         *
     612         * @since BuddyPress (?)
     613         *
     614         * @uses bp_core_set_user_caps().
     615         */
     616        public function setup_caps( $caps = array() ) {
     617
     618                if ( ! empty( $caps ) ) {
     619                        bp_core_set_user_caps( $caps );
     620                }
     621
     622                do_action( 'bp_' . $this->id . '_setup_caps' );
     623        }
    605624}
    606625endif; // BP_Component
  • src/bp-core/bp-core-dependency.php

    diff --git src/bp-core/bp-core-dependency.php src/bp-core/bp-core-dependency.php
    index eaf839c..6634e48 100644
    function bp_setup_current_user() { 
    8585}
    8686
    8787/**
     88 * Fire the 'bp_setup_caps' action, where components should register their specific caps.
     89 */
     90function bp_setup_caps() {
     91        do_action( 'bp_setup_caps' );
     92}
     93
     94
     95/**
    8896 * Fire the 'bp_init' action, BuddyPress's main initialization hook.
    8997 */
    9098function bp_init() {
  • src/bp-groups/bp-groups-loader.php

    diff --git src/bp-groups/bp-groups-loader.php src/bp-groups/bp-groups-loader.php
    index 08edbff..41dbe4e 100644
    class BP_Groups_Component extends BP_Component { 
    622622
    623623                parent::setup_title();
    624624        }
     625
     626        /**
     627         * Set up groups component user caps.
     628         *
     629         * @since BuddyPress (?)
     630         */
     631        public function setup_caps( $caps = array() ) {
     632                $caps = array(
     633                        'view_groups' => true,
     634                        'view_group'  => true,
     635                );
     636
     637                $restricted = (int) bp_get_option( 'bp_restrict_group_creation', 0 );
     638
     639                if ( ( is_user_logged_in() && empty( $restricted ) ) || is_super_admin() ) {
     640                        $caps = array_merge( $caps, array(
     641                                'create_group' => true,
     642                                'delete_group' => true,
     643                                'edit_group'   => true,
     644                        ) );
     645                }
     646
     647                parent::setup_caps( $caps );
     648        }
    625649}
    626650
    627651
  • src/bp-groups/bp-groups-template.php

    diff --git src/bp-groups/bp-groups-template.php src/bp-groups/bp-groups-template.php
    index 7701aab..6e771a9 100644
    function bp_groups_members_filter() { 
    25352535 * @return bool True if user can create groups. False otherwise.
    25362536 */
    25372537function bp_user_can_create_groups() {
    2538         // Super admin can always create groups
    2539         if ( bp_current_user_can( 'bp_moderate' ) )
    2540                 return true;
     2538        $can_create = bp_current_user_can( 'create_group' );
    25412539
    2542         // Get group creation option, default to 0 (allowed)
    25432540        $restricted = (int) bp_get_option( 'bp_restrict_group_creation', 0 );
    25442541
    2545         // Allow by default
    2546         $can_create = true;
    2547 
    2548         // Are regular users restricted?
    2549         if ( $restricted )
    2550                 $can_create = false;
    2551 
    25522542        return apply_filters( 'bp_user_can_create_groups', $can_create, $restricted );
    25532543}
    25542544
  • src/bp-members/bp-members-filters.php

    diff --git src/bp-members/bp-members-filters.php src/bp-members/bp-members-filters.php
    index fed76b6..f32154f 100644
    function bp_members_edit_profile_url( $url, $user_id, $scheme = 'admin' ) { 
    7777        return apply_filters( 'bp_members_edit_profile_url', $profile_link, $url, $user_id, $scheme );
    7878}
    7979add_filter( 'edit_profile_url', 'bp_members_edit_profile_url', 10, 3 );
     80
     81/**
     82 * Map user caps for Member component
     83 *
     84 * @since BuddyPress (?)
     85 *
     86 * @param array $caps The caps that WP associates with the given role.
     87 * @param array $caps The caps being tested for in WP_User::has_cap().
     88 * @param array $args Miscellaneous arguments passed to the user_has_cap filter.
     89 * @return array $caps The user's cap list.
     90 */
     91function bp_members_map_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
     92
     93        switch( $cap ) {
     94                case 'edit_member' :
     95                        if( ! bp_is_my_profile() ) {
     96                                $caps = array( 'bp_moderate' );
     97                        }
     98                case 'delete_member' :
     99                        if( ! bp_is_my_profile() ) {
     100                                $caps = array( 'delete_users' );
     101                        }
     102                break;
     103        }
     104
     105        return apply_filters( 'bp_members_map_meta_caps', $caps, $cap, $user_id, $args );
     106}
     107add_filter( 'bp_map_meta_caps', 'bp_members_map_meta_caps', 10, 4 );
  • src/bp-members/bp-members-functions.php

    diff --git src/bp-members/bp-members-functions.php src/bp-members/bp-members-functions.php
    index 6e6a529..d81eb3e 100644
    function bp_core_get_core_userdata( $user_id ) { 
    181181}
    182182
    183183/**
     184 * Attach BP Components specific caps to current user allcaps
     185 *
     186 * @uses  wp_get_current_user()
     187 *
     188 * @param array $caps The caps to add.
     189 */
     190function bp_core_set_user_caps( $caps = array() ) {
     191        $user = wp_get_current_user();
     192
     193        if ( empty( $caps ) ) {
     194                return $user->allcaps;
     195        }
     196
     197        $bp_caps = apply_filters( 'bp_core_set_user_caps', $caps, $user );
     198
     199        $user->allcaps = array_merge( $user->allcaps, $bp_caps );
     200}
     201
     202/**
    184203 * Return the ID of a user, based on user_login.
    185204 *
    186205 * No longer used.
  • src/bp-members/bp-members-loader.php

    diff --git src/bp-members/bp-members-loader.php src/bp-members/bp-members-loader.php
    index 2685000..41583b4 100644
    class BP_Members_Component extends BP_Component { 
    254254
    255255                parent::setup_title();
    256256        }
     257
     258        /**
     259         * Set up members component user caps.
     260         *
     261         * @since BuddyPress (?)
     262         */
     263        public function setup_caps( $caps = array() ) {
     264                $caps = array(
     265                        'view_member'    => true,
     266                        'view_members'   => true,
     267                );
     268
     269                if ( is_user_logged_in() ) {
     270
     271                        if ( ! bp_disable_account_deletion() || is_super_admin() ) {
     272                                $caps = array_merge( $caps, array(
     273                                        'edit_member'   => true,
     274                                        'delete_member' => true
     275                                ) );
     276                        } else {
     277                                $caps['edit_member'] = true;
     278                        }
     279                       
     280                }
     281
     282                if ( is_super_admin() ) {
     283                        $caps['create_member'] = true;
     284                }
     285
     286                parent::setup_caps( $caps );
     287        }
    257288}
    258289
    259290/**
  • src/bp-settings/bp-settings-loader.php

    diff --git src/bp-settings/bp-settings-loader.php src/bp-settings/bp-settings-loader.php
    index 8d97f5a..6a2e5a9 100644
    class BP_Settings_Component extends BP_Component { 
    126126                }
    127127
    128128                // Add Delete Account nav item
    129                 if ( ( ! bp_disable_account_deletion() && bp_is_my_profile() ) || bp_current_user_can( 'delete_users' ) ) {
     129                if ( bp_current_user_can( 'delete_member' ) ) {
    130130                        $sub_nav[] = array(
    131131                                'name'            => __( 'Delete Account', 'buddypress' ),
    132132                                'slug'            => 'delete-account',
  • src/bp-templates/bp-legacy/buddypress/activity/index.php

    diff --git src/bp-templates/bp-legacy/buddypress/activity/index.php src/bp-templates/bp-legacy/buddypress/activity/index.php
    index 40d7725..03097e2 100644
     
    44
    55        <?php do_action( 'bp_before_directory_activity_content' ); ?>
    66
    7         <?php if ( is_user_logged_in() ) : ?>
     7        <?php if ( bp_current_user_can( 'publish_activity' ) ) : ?>
    88
    99                <?php bp_get_template_part( 'activity/post-form' ); ?>
    1010
  • src/bp-templates/bp-legacy/buddypress/activity/post-form.php

    diff --git src/bp-templates/bp-legacy/buddypress/activity/post-form.php src/bp-templates/bp-legacy/buddypress/activity/post-form.php
    index acf7368..aaf5238 100644
     
    66 * @package BuddyPress
    77 * @subpackage bp-legacy
    88 */
    9 
    109?>
    1110
    1211<form action="<?php bp_activity_post_form_action(); ?>" method="post" id="whats-new-form" name="whats-new-form" role="complementary">
  • src/bp-templates/bp-legacy/buddypress/groups/single/activity.php

    diff --git src/bp-templates/bp-legacy/buddypress/groups/single/activity.php src/bp-templates/bp-legacy/buddypress/groups/single/activity.php
    index e810cef..019d17f 100644
     
    1919
    2020<?php do_action( 'bp_before_group_activity_post_form' ); ?>
    2121
    22 <?php if ( is_user_logged_in() && bp_group_is_member() ) : ?>
     22<?php if ( bp_current_user_can( 'publish_activity' ) && bp_group_is_member() ) : ?>
    2323
    2424        <?php bp_get_template_part( 'activity/post-form' ); ?>
    2525
  • src/bp-templates/bp-legacy/buddypress/members/single/activity.php

    diff --git src/bp-templates/bp-legacy/buddypress/members/single/activity.php src/bp-templates/bp-legacy/buddypress/members/single/activity.php
    index af4ea66..2e36335 100644
     
    3131<?php do_action( 'bp_before_member_activity_post_form' ); ?>
    3232
    3333<?php
    34 if ( is_user_logged_in() && bp_is_my_profile() && ( !bp_current_action() || bp_is_current_action( 'just-me' ) ) )
     34if ( bp_current_user_can( 'publish_activity' ) && bp_is_my_profile() && ( !bp_current_action() || bp_is_current_action( 'just-me' ) ) )
    3535        bp_get_template_part( 'activity/post-form' );
    3636
    3737do_action( 'bp_after_member_activity_post_form' );