Skip to:
Content

BuddyPress.org

Changeset 10858


Ignore:
Timestamp:
06/01/2016 09:00:21 PM (10 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.

Location:
trunk/src
Files:
5 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
  • trunk/src/bp-activity/classes/class-bp-activity-activity.php

    r10853 r10858  
    135135
    136136        /**
     137         * Error holder.
     138         *
     139         * @since 2.6.0
     140         *
     141         * @var WP_Error
     142         */
     143        public $errors;
     144
     145        /**
     146         * Error type to return. Either 'bool' or 'wp_error'.
     147         *
     148         * @since 2.6.0
     149         *
     150         * @var string
     151         */
     152        public $error_type = 'bool';
     153
     154        /**
    137155         * Constructor method.
    138156         *
     
    142160         */
    143161        public function __construct( $id = false ) {
     162                // Instantiate errors object.
     163                $this->errors = new WP_Error;
     164
    144165                if ( !empty( $id ) ) {
    145166                        $this->id = (int) $id;
     
    236257                do_action_ref_array( 'bp_activity_before_save', array( &$this ) );
    237258
     259                if ( 'wp_error' === $this->error_type && $this->errors->get_error_code() ) {
     260                        return $this->errors;
     261                }
     262
    238263                if ( empty( $this->component ) || empty( $this->type ) ) {
    239                         return false;
     264                        if ( 'bool' === $this->error_type ) {
     265                                return false;
     266                        } else {
     267                                if ( empty( $this->component ) ) {
     268                                        $this->errors->add( 'bp_activity_missing_component' );
     269                                } else {
     270                                        $this->errors->add( 'bp_activity_missing_type' );
     271                                }
     272
     273                                return $this->errors;
     274                        }
    240275                }
    241276
  • trunk/src/bp-groups/bp-groups-activity.php

    r10454 r10858  
    380380                'secondary_item_id' => false,
    381381                'recorded_time'     => bp_core_current_time(),
    382                 'hide_sitewide'     => $hide_sitewide
     382                'hide_sitewide'     => $hide_sitewide,
     383                'error_type'        => 'bool'
    383384        ) );
    384385
  • trunk/src/bp-groups/bp-groups-functions.php

    r10819 r10858  
    11501150 *
    11511151 * @since 1.2.0
     1152 * @since 2.6.0 Added 'error_type' parameter to $args.
    11521153 *
    11531154 * @param array|string $args {
     
    11691170
    11701171        $defaults = array(
    1171                 'content'  => false,
    1172                 'user_id'  => bp_loggedin_user_id(),
    1173                 'group_id' => 0
     1172                'content'    => false,
     1173                'user_id'    => bp_loggedin_user_id(),
     1174                'group_id'   => 0,
     1175                'error_type' => 'bool'
    11741176        );
    11751177
     
    12121214
    12131215        $activity_id = groups_record_activity( array(
    1214                 'user_id' => $user_id,
    1215                 'action'  => $action,
    1216                 'content' => $content_filtered,
    1217                 'type'    => 'activity_update',
    1218                 'item_id' => $group_id
     1216                'user_id'    => $user_id,
     1217                'action'     => $action,
     1218                'content'    => $content_filtered,
     1219                'type'       => 'activity_update',
     1220                'item_id'    => $group_id,
     1221                'error_type' => $error_type
    12191222        ) );
    12201223
  • trunk/src/bp-templates/bp-legacy/buddypress-functions.php

    r10825 r10858  
    920920
    921921        if ( ! $object && bp_is_active( 'activity' ) ) {
    922                 $activity_id = bp_activity_post_update( array( 'content' => $_POST['content'] ) );
     922                $activity_id = bp_activity_post_update( array( 'content' => $_POST['content'], 'error_type' => 'wp_error' ) );
    923923
    924924        } elseif ( 'groups' === $object ) {
    925925                if ( $item_id && bp_is_active( 'groups' ) )
    926                         $activity_id = groups_post_update( array( 'content' => $_POST['content'], 'group_id' => $item_id ) );
     926                        $activity_id = groups_post_update( array( 'content' => $_POST['content'], 'group_id' => $item_id, 'error_type' => 'wp_error' ) );
    927927
    928928        } else {
     
    932932        }
    933933
    934         if ( empty( $activity_id ) )
     934        if ( false === $activity_id ) {
    935935                exit( '-1<div id="message" class="error bp-ajax-message"><p>' . __( 'There was a problem posting your update. Please try again.', 'buddypress' ) . '</p></div>' );
     936        } elseif ( is_wp_error( $activity_id ) && $activity_id->get_error_code() ) {
     937                exit( '-1<div id="message" class="error bp-ajax-message"><p>' . $activity_id->get_error_message() . '</p></div>' );
     938        }
    936939
    937940        $last_recorded = ! empty( $_POST['since'] ) ? date( 'Y-m-d H:i:s', intval( $_POST['since'] ) ) : 0;
     
    9981001                'content'     => $_POST['content'],
    9991002                'parent_id'   => $_POST['comment_id'],
     1003                'error_type'  => 'wp_error'
    10001004        ) );
    10011005
    1002         if ( ! $comment_id ) {
    1003                 if ( ! empty( $bp->activity->errors['new_comment'] ) && is_wp_error( $bp->activity->errors['new_comment'] ) ) {
    1004                         $feedback = $bp->activity->errors['new_comment']->get_error_message();
    1005                         unset( $bp->activity->errors['new_comment'] );
    1006                 }
    1007 
    1008                 exit( '-1<div id="message" class="error bp-ajax-message"><p>' . esc_html( $feedback ) . '</p></div>' );
     1006        if ( is_wp_error( $comment_id ) ) {
     1007                exit( '-1<div id="message" class="error bp-ajax-message"><p>' . esc_html( $comment_id->get_error_message() ) . '</p></div>' );
    10091008        }
    10101009
Note: See TracChangeset for help on using the changeset viewer.