Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/01/2016 09:00:21 PM (8 years ago)
Author:
r-a-y
Message:

Activity: Introduce error handling to the BP_Activity_Activity class.

To support frontend user messages for activity failures, we need the
ability to add errors.

This commit:

  • Introduces the $errors object and $error_type property to the BP_Activity_Activity class.
  • Adds an 'error_type' parameter to all applicable, activity functions.
  • Modifies bp-legacy to output an error message for activity failures.

See #6719.

File:
1 edited

Legend:

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

    r10853 r10858  
    17831783 *
    17841784 * @since 1.1.0
     1785 * @since 2.6.0 Added 'error_type' parameter to $args.
    17851786 *
    17861787 * @param array|string $args {
     
    18141815 *                                       Default: false.
    18151816 *     @type bool     $is_spam           Should the item be marked as spam? Default: false.
     1817 *     @type string   $error_type        Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.
    18161818 * }
    18171819 * @return int|bool The ID of the activity on success. False on error.
     
    18321834        'hide_sitewide'     => false,                  // Should this be hidden on the sitewide activity stream?
    18331835        'is_spam'           => false,                  // Is this activity item to be marked as spam?
     1836        'error_type'        => 'bool'
    18341837    ), 'activity_add' );
    18351838
     
    18551858    $activity->hide_sitewide     = $r['hide_sitewide'];
    18561859    $activity->is_spam           = $r['is_spam'];
     1860    $activity->error_type        = $r['error_type'];
    18571861    $activity->action            = ! empty( $r['action'] )
    1858                                         ? $r['action']
    1859                                         : bp_activity_generate_action_string( $activity );
    1860 
    1861     if ( ! $activity->save() ) {
     1862                        ? $r['action']
     1863                        : bp_activity_generate_action_string( $activity );
     1864
     1865    $save = $activity->save();
     1866
     1867    if ( 'wp_error' === $r['error_type'] && is_wp_error( $save ) ) {
     1868        return $save;
     1869    } elseif ('bool' === $r['error_type'] && false === $save ) {
    18621870        return false;
    18631871    }
     
    18991907
    19001908    $r = wp_parse_args( $args, array(
    1901         'content' => false,
    1902         'user_id' => bp_loggedin_user_id()
     1909        'content'    => false,
     1910        'user_id'    => bp_loggedin_user_id(),
     1911        'error_type' => 'bool',
    19031912    ) );
    19041913
     
    19401949        'component'    => buddypress()->activity->id,
    19411950        'type'         => 'activity_update',
     1951        'error_type'   => $r['error_type']
    19421952    ) );
     1953
     1954    if ( is_wp_error( $activity_id ) ) {
     1955        return $activity_id;
     1956    }
    19431957
    19441958    /**
     
    24922506 * @since 2.5.0 Add a new possible parameter $skip_notification for the array of arguments.
    24932507 *              Add the $primary_link parameter for the array of arguments.
     2508 * @since 2.6.0 Added 'error_type' parameter to $args.
    24942509 *
    24952510 * @param array|string $args {
     
    25082523 *     @type bool   $skip_notification Optional. false to send a comment notification, false otherwise.
    25092524 *                                     Defaults to false.
     2525 *     @type string $error_type        Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.
    25102526 * }
    25112527 * @return int|bool The ID of the comment on success, otherwise false.
    25122528 */
    25132529function bp_activity_new_comment( $args = '' ) {
    2514     $bp       = buddypress();
    2515     $errors   = new WP_Error();
    2516     $feedback = __( 'There was an error posting your reply. Please try again.', 'buddypress' );
    2517 
    2518     if ( empty( $bp->activity->errors ) ) {
    2519         $bp->activity->errors = array();
    2520     }
     2530    $bp = buddypress();
    25212531
    25222532    $r = wp_parse_args( $args, array(
     
    25282538        'primary_link'      => '',
    25292539        'skip_notification' => false,
     2540        'error_type'        => 'bool'
    25302541    ) );
     2542
     2543    // Error type is boolean; need to initialize some variables for backpat.
     2544    if ( 'bool' === $r['error_type'] ) {
     2545        if ( empty( $bp->activity->errors ) ) {
     2546            $bp->activity->errors = array();
     2547        }
     2548    }
     2549
     2550    // Default error message.
     2551    $feedback = __( 'There was an error posting your reply. Please try again.', 'buddypress' );
    25312552
    25322553    // Bail if missing necessary data.
    25332554    if ( empty( $r['content'] ) || empty( $r['user_id'] ) || empty( $r['activity_id'] ) ) {
    2534         $errors->add( 'missing_data', $feedback );
    2535         $bp->activity->errors['new_comment'] = $errors;
    2536 
    2537         return false;
     2555        $error = new WP_Error( 'missing_data', $feedback );
     2556
     2557        if ( 'wp_error' === $r['error_type'] ) {
     2558            return $error;
     2559
     2560        // Backpat.
     2561        } else {
     2562            $bp->activity->errors['new_comment'] = $error;
     2563            return false;
     2564        }
    25382565    }
    25392566
     
    25502577    // Bail if the parent activity does not exist.
    25512578    if ( empty( $activity->date_recorded ) ) {
    2552         $errors->add( 'missing_activity', __( 'Sorry, the item you are replying to no longer exists.', 'buddypress' ) );
    2553         $bp->activity->errors['new_comment'] = $errors;
    2554 
    2555         return false;
     2579        $error = new WP_Error( 'missing_activity', __( 'The item you were replying to no longer exists.', 'buddypress' ) );
     2580
     2581        if ( 'wp_error' === $r['error_type'] ) {
     2582            return $error;
     2583
     2584        // Backpat.
     2585        } else {
     2586            $bp->activity->errors['new_comment'] = $error;
     2587            return false;
     2588        }
     2589
    25562590    }
    25572591
     
    25782612        'item_id'           => $activity_id,
    25792613        'secondary_item_id' => $r['parent_id'],
    2580         'hide_sitewide'     => $is_hidden
     2614        'hide_sitewide'     => $is_hidden,
     2615        'error_type'        => $r['error_type']
    25812616    ) );
     2617
     2618    // Return WP Error.
     2619    if ( is_wp_error( $comment_id ) && 'wp_error' === $r['error_type'] ) {
     2620        return $comment_id;
     2621    }
    25822622
    25832623    // Comment caches are stored only with the top-level item.
     
    26202660
    26212661    if ( empty( $comment_id ) ) {
    2622         $errors->add( 'comment_failed', $feedback );
    2623         $bp->activity->errors['new_comment'] = $errors;
     2662        $error = new WP_Error( 'comment_failed', $feedback );
     2663
     2664        if ( 'wp_error' === $r['error_type'] ) {
     2665            return $error;
     2666
     2667        // Backpat.
     2668        } else {
     2669            $bp->activity->errors['new_comment'] = $error;
     2670        }
    26242671    }
    26252672
Note: See TracChangeset for help on using the changeset viewer.