Skip to:
Content

BuddyPress.org

Changeset 13367


Ignore:
Timestamp:
11/13/2022 12:50:54 PM (2 years ago)
Author:
imath
Message:

Improve Activity & Group Activity post_update functions

  1. Improve the bp_activity_post_update() function's extensibility including a filter name ('activity_post_update') into the bp_parse_args() function used to merge given arguments with default ones. This allows Plugins or BuddyPress add-ons such as BP Attachments to create a content from a different input than the Activity Post Form's textarea using one of these two filters:
    • 'bp_before_activity_post_update_parse_args' or
    • 'bp_after_activity_post_update_parse_args'
  1. groups_post_update() now completely respects the $error_type argument making sure to return a WP_Error object when it is defined to wp_error. If not, it returns false.
  1. To avoid code/i18n string duplication adapt bp_nouveau_ajax_post_update() so that it enjoys the 2 above improvements.

Closes https://github.com/buddypress/buddypress/pull/37
Fixes #8764

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-functions.php

    r13337 r13367  
    21142114            'user_id'    => bp_loggedin_user_id(),
    21152115            'error_type' => 'bool',
    2116         )
     2116        ),
     2117        'activity_post_update'
    21172118    );
    21182119
  • trunk/src/bp-groups/bp-groups-activity.php

    r13312 r13367  
    579579    }
    580580
    581     $content = $r['content'];
    582     $user_id = (int) $r['user_id'];
    583     if ( ! $content || ! strlen( trim( $content ) ) || ! $user_id || ! $group_id ) {
    584         return false;
    585     }
    586 
    587     $bp->groups->current_group = groups_get_group( $group_id );
    588 
    589     // Be sure the user is a member of the group before posting.
    590     if ( ! bp_current_user_can( 'bp_moderate' ) && ! groups_is_user_member( $user_id, $group_id ) ) {
     581    $content          = $r['content'];
     582    $user_id          = (int) $r['user_id'];
     583    $is_user_active   = bp_is_user_active( $user_id );
     584    $is_group_allowed = $group_id && ( bp_current_user_can( 'bp_moderate' ) || groups_is_user_member( $user_id, $group_id ) );
     585
     586    if ( ! $content || ! strlen( trim( $content ) ) || ! $is_user_active || ! $is_group_allowed ) {
     587        if ( 'wp_error' === $r['error_type'] ) {
     588            $error_code         = 'bp_activity_missing_content';
     589            $error_code_message = __( 'Please enter some content to post.', 'buddypress' );
     590
     591            if ( ! $is_user_active ) {
     592                $error_code         = 'bp_activity_inactive_user';
     593                $error_code_message = __( 'User account has not yet been activated.', 'buddypress' );
     594            } elseif ( ! $is_group_allowed ) {
     595                $error_code         = 'bp_activity_unallowed_group';
     596                $error_code_message = __( 'You need to be a member of this group to share updates with their members.', 'buddypress' );
     597            }
     598
     599            return new WP_Error( $error_code, $error_code_message );
     600        }
     601
    591602        return false;
    592603    }
  • trunk/src/bp-templates/bp-nouveau/includes/activity/ajax.php

    r13222 r13367  
    507507    }
    508508
    509     if ( empty( $_POST['content'] ) ) {
    510         wp_send_json_error(
    511             array(
    512                 'message' => __( 'Please enter some content to post.', 'buddypress' ),
    513             )
    514         );
    515     }
    516 
    517509    $activity_id = 0;
    518510    $item_id     = 0;
     
    537529
    538530    if ( 'user' === $object && bp_is_active( 'activity' ) ) {
    539         $activity_id = bp_activity_post_update( array( 'content' => $_POST['content'] ) );
     531        $activity_id = bp_activity_post_update(
     532            array(
     533                'content'    => $_POST['content'],
     534                'error_type' => 'wp_error',
     535            )
     536        );
    540537
    541538    } elseif ( 'group' === $object ) {
     
    544541            $activity_id = groups_post_update(
    545542                array(
    546                     'content'  => $_POST['content'],
    547                     'group_id' => $item_id,
     543                    'content'    => $_POST['content'],
     544                    'group_id'   => $item_id,
     545                    'error_type' => 'wp_error',
    548546                )
    549547            );
     
    566564    }
    567565
    568     if ( empty( $activity_id ) ) {
     566    if ( is_wp_error( $activity_id ) ) {
     567        wp_send_json_error(
     568            array(
     569                'message' => $activity_id->get_error_message(),
     570            )
     571        );
     572    } elseif ( empty( $activity_id ) ) {
    569573        wp_send_json_error(
    570574            array(
Note: See TracChangeset for help on using the changeset viewer.