Skip to:
Content

BuddyPress.org

Ticket #8444: 8444.2.diff

File 8444.2.diff, 13.3 KB (added by dcavins, 4 years ago)

Add invitation date_modified changes and tests. Improve inline documentation.

  • src/bp-core/classes/class-bp-invitation-manager.php

    diff --git src/bp-core/classes/class-bp-invitation-manager.php src/bp-core/classes/class-bp-invitation-manager.php
    index a8bb047ce..0b7d2f009 100644
    abstract class BP_Invitation_Manager { 
    139139
    140140                // "Send" the invite if necessary.
    141141                if ( $invite_id && $r['send_invite'] ) {
    142                         $sent = $this->send_invitation_by_id( $invite_id );
     142                        $sent = $this->send_invitation_by_id( $invite_id, $r );
    143143                        if ( ! $sent ) {
    144144                                return false;
    145145                        }
    abstract class BP_Invitation_Manager { 
    154154         * @since 5.0.0
    155155         * @access public
    156156         *
    157          * @param int $invitation_id ID of invitation to send.
     157         * @param int   $invitation_id ID of invitation to send.
     158         * @param array $args          See BP_Invitation::mark_sent().
    158159         *
    159160         * @return int|bool The number of rows updated, or false on error.
    160161         */
    161         public function send_invitation_by_id( $invitation_id = 0 ) {
     162        public function send_invitation_by_id( $invitation_id = 0, $args = array() ) {
    162163                $updated = false;
    163164
    164165                $invitation = new BP_Invitation( $invitation_id );
    abstract class BP_Invitation_Manager { 
    196197                // Perform the send action.
    197198                $this->run_send_action( $invitation );
    198199
    199                 $updated = BP_Invitation::mark_sent( $invitation->id );
     200                $updated = BP_Invitation::mark_sent( $invitation->id, $args );
    200201
    201202                return $updated;
    202203        }
    abstract class BP_Invitation_Manager { 
    305306         * @since 5.0.0
    306307         * @access public
    307308         *
    308          * @param int $request_id ID of request to send.
     309         * @param int   $request_id ID of request to send.
     310         * @param array $args       See BP_Invitation::mark_sent().
    309311         *
    310312         * @return int|bool The number of rows updated, or false on error.
    311313         */
    312         public function send_request_notification_by_id( $request_id = 0 ) {
     314        public function send_request_notification_by_id( $request_id = 0, $args = array() ) {
    313315                $updated = false;
    314316
    315317                $request = new BP_Invitation( $request_id );
    abstract class BP_Invitation_Manager { 
    325327                 * Before sending notifications, check for outstanding invitations to the same item.
    326328                 * A sent invitation + a request = acceptance.
    327329                 */
    328                 $args = array(
     330                $invite_args = array(
    329331                        'user_id'           => $request->user_id,
    330332                        'invitee_email'     => $request->invitee_email,
    331333                        'item_id'           => $request->item_id,
    332334                        'secondary_item_id' => $request->secondary_item_id,
    333335                        'invite_sent'       => 'sent'
    334336                );
    335                 $invites = $this->invitation_exists( $args );
     337                $invites = $this->invitation_exists( $invite_args );
    336338
    337339                if ( ! empty( $invites ) ) {
    338340                        // Accept the request.
    339                         return $this->accept_invitation( $args );
     341                        return $this->accept_invitation( $invite_args );
    340342                }
    341343
    342344                // Perform the send action.
    343345                $this->run_send_action( $request );
    344346
    345                 $updated = BP_Invitation::mark_sent( $request->id );
     347                $updated = BP_Invitation::mark_sent( $request->id, $args );
    346348
    347349                return $updated;
    348350        }
    abstract class BP_Invitation_Manager { 
    452454         * @see BP_Invitation::get() for a description of
    453455         *      accepted update/where arguments.
    454456         *
    455          * @param array $update_args Associative array of fields to update,
    456          *              and the values to update them to. Of the format
    457          *              array( 'user_id' => 4 )
     457         * @param array $args {
     458         *     Invitation characteristics. Some basic info is required to accept an invitation,
     459         *     because we'll need to accept all similar invitations and requests.
     460         *
     461         *     @type int    $user_id           User ID of the invitee.
     462         *                                     Either 'user_id' or 'invitee_email' is required.
     463         *     @type string $invitee_email     Email address of the invitee.
     464         *                                     Either 'user_id' or 'invitee_email' is required.
     465         *     @type int    $item_id           Item ID of the invitation to accept.
     466         *     @type int    $secondary_item_id Optional. Secondary item ID if needed.
     467         *     @type string $invite_sent       Optional. Defaults to only allowing the
     468         *                                     acceptance of sent invitations.
     469         *     @type string $date_modified     Modified time in 'Y-m-d h:i:s' format, GMT.
     470         *                                     Defaults to current time if not specified.
     471         * }
    458472         *
    459473         * @return int|bool Number of rows updated on success, false on failure.
    460474         */
    461475         public function accept_invitation( $args = array() ) {
    462476
    463                 /*
    464                  * Some basic info is required to accept an invitation,
    465                  * because we'll need to mark all similar invitations and requests.
    466                  * The following, except the optional 'secondary_item_id', are required.
    467                  */
    468477                $r = bp_parse_args( $args, array(
    469478                        'user_id'           => 0,
    470479                        'invitee_email'     => '',
    471480                        'item_id'           => null,
    472481                        'secondary_item_id' => null,
    473482                        'invite_sent'       => 'sent',
     483                        'date_modified'     => bp_core_current_time(),
    474484                ), 'accept_invitation' );
    475485                $r['class'] = $this->class_name;
    476486
    abstract class BP_Invitation_Manager { 
    501511         * @see BP_Invitation::get() for a description of
    502512         *      accepted update/where arguments.
    503513         *
    504          * @param array $update_args Associative array of fields to update,
    505          *              and the values to update them to. Of the format
    506          *              array( 'user_id' => 4 )
     514         * @param array $args {
     515         *     Invitation characteristics. Some basic info is required to accept an invitation,
     516         *     because we'll need to accept all similar invitations and requests.
     517         *
     518         *     @type int    $user_id           User ID of the invitee.
     519         *     @type int    $item_id           Item ID of the invitation to accept.
     520         *     @type int    $secondary_item_id Optional. Secondary item ID if needed.
     521         *     @type string $date_modified     Modified time in 'Y-m-d h:i:s' format, GMT.
     522         *                                     Defaults to current time if not specified.
     523         * }
    507524         *
    508525         * @return bool Number of rows updated on success, false on failure.
    509526         */
    510527         public function accept_request( $args = array() ) {
    511                 /*
    512                  * Some basic info is required to accept an invitation,
    513                  * because we'll need to accept all similar invitations and requests.
    514                  * The following, except the optional 'secondary_item_id', are required.
    515                  */
     528
    516529                $r = bp_parse_args( $args, array(
    517530                        'user_id'           => 0,
    518531                        'item_id'           => null,
    519532                        'secondary_item_id' => null,
     533                        'date_modified'     => bp_core_current_time(),
    520534                ), 'accept_request' );
    521535                $r['class'] = $this->class_name;
    522536
    abstract class BP_Invitation_Manager { 
    618632         *
    619633         * @since 5.0.0
    620634         *
    621          * @param int $id The ID of the invitation to mark as sent.
     635         * @see BP_Invitation::mark_accepted()
     636         *      for a description of arguments.
    622637         * @return bool True on success, false on failure.
    623638         */
    624         public function mark_accepted_by_id( $id ) {
    625                 return BP_Invitation::mark_accepted( $id );
     639        public function mark_accepted_by_id( $id, $args ) {
     640                return BP_Invitation::mark_accepted( $id, $args );
    626641        }
    627642
    628643        /**
  • src/bp-core/classes/class-bp-invitation.php

    diff --git src/bp-core/classes/class-bp-invitation.php src/bp-core/classes/class-bp-invitation.php
    index b7ca0d7eb..e15aa5fa5 100644
    class BP_Invitation { 
    640640                        }
    641641                }
    642642
     643                // date_modified
     644                if ( ! empty( $args['date_modified'] ) ) {
     645                        $where_clauses['data']['date_modified'] = $args['date_modified'];
     646                        $where_clauses['format'][] = '%s';
     647                }
     648
    643649                return $where_clauses;
    644650        }
    645651
    class BP_Invitation { 
    987993         *
    988994         * @since 5.0.0
    989995         *
    990          * @param int $id The ID of the invitation to mark as sent.
     996         * @param int   $id   The ID of the invitation to mark as sent.
     997         * @param array $args {
     998         *     Optional. Invitation characteristics used
     999         *     to override certain sending behaviors.
     1000         *
     1001         *     @type string $date_modified Modified time in 'Y-m-d h:i:s' format, GMT.
     1002         *                                 Defaults to current time if not specified.
     1003         * }
     1004         * @return int|bool The number of rows updated, or false on error.
    9911005         */
    992         public static function mark_sent( $id = 0 ) {
     1006        public static function mark_sent( $id = 0, $args = array() ) {
    9931007
    9941008                if ( ! $id ) {
    9951009                        return false;
    class BP_Invitation { 
    9971011
    9981012                // Values to be updated.
    9991013                $update_args = array(
    1000                         'invite_sent' => 'sent',
     1014                        'invite_sent'   => 'sent',
     1015                        'date_modified' => bp_core_current_time(),
    10011016                );
     1017                // Respect a specified `date-modified`.
     1018                if ( ! empty( $args['date_modified'] ) ) {
     1019                        $update_args['date_modified'] = $args['date_modified'];
     1020                }
    10021021
    10031022                // WHERE clauses.
    10041023                $where_args = array(
    class BP_Invitation { 
    10141033         *
    10151034         * @since 5.0.0
    10161035         *
    1017          * @param array $args See BP_Invitation::update().
     1036         * @param array $args See BP_Invitation::update().
     1037         * @return int|bool The number of rows updated, or false on error.
    10181038         */
    10191039        public static function mark_sent_by_data( $args ) {
    10201040
    10211041                // Values to be updated.
    10221042                $update_args = array(
    1023                         'invite_sent' => 'sent',
     1043                        'invite_sent'   => 'sent',
     1044                        'date_modified' => bp_core_current_time(),
    10241045                );
     1046                // Respect a specified `date-modified`.
     1047                if ( ! empty( $args['date_modified'] ) ) {
     1048                        $update_args['date_modified'] = $args['date_modified'];
     1049                }
    10251050
    10261051                return self::update( $update_args, $args );
    10271052        }
    class BP_Invitation { 
    10331058         *
    10341059         * @since 5.0.0
    10351060         *
    1036          * @param int $id The ID of the invitation to mark as sent.
     1061         * @param int   $id   The ID of the invitation to mark as sent.
     1062         * @param array $args {
     1063         *     Optional. Invitation characteristics used
     1064         *     to override certain sending behaviors.
     1065         *
     1066         *     @type string $date_modified Modified time in 'Y-m-d h:i:s' format, GMT.
     1067         *                                 Defaults to current time if not specified.
     1068         * }
     1069         * @return int|bool The number of rows updated, or false on error.
    10371070         */
    1038         public static function mark_accepted( $id = 0 ) {
     1071        public static function mark_accepted( $id = 0, $args = array() ) {
    10391072
    10401073                if ( ! $id ) {
    10411074                        return false;
    class BP_Invitation { 
    10431076
    10441077                // Values to be updated.
    10451078                $update_args = array(
    1046                         'accepted' => 'accepted',
     1079                        'accepted'      => 'accepted',
     1080                        'date_modified' => bp_core_current_time(),
    10471081                );
     1082                // Respect a specified `date-modified`.
     1083                if ( ! empty( $args['date_modified'] ) ) {
     1084                        $update_args['date_modified'] = $args['date_modified'];
     1085                }
    10481086
    10491087                // WHERE clauses.
    10501088                $where_args = array(
    class BP_Invitation { 
    10601098         *
    10611099         * @since 5.0.0
    10621100         *
    1063          * @param array $args See BP_Invitation::update().
     1101         * @param array $args See BP_Invitation::update().
     1102         * @return int|bool The number of rows updated, or false on error.
    10641103         */
    10651104        public static function mark_accepted_by_data( $args ) {
    10661105
    10671106                // Values to be updated.
    10681107                $update_args = array(
    1069                         'accepted' => 'accepted',
     1108                        'accepted'      => 'accepted',
     1109                        'date_modified' => bp_core_current_time(),
    10701110                );
     1111                // Respect a specified `date-modified`.
     1112                if ( ! empty( $args['date_modified'] ) ) {
     1113                        $update_args['date_modified'] = $args['date_modified'];
     1114                }
    10711115
    10721116                return self::update( $update_args, $args );
    10731117        }
  • src/bp-groups/bp-groups-functions.php

    diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
    index d1af9ad1f..e8c5639b0 100644
    function groups_send_membership_request( ...$args ) { 
    20442044
    20452045        // If a new request was created, send the emails.
    20462046        if ( $request_id && is_int( $request_id ) ) {
    2047                 $invites_class->send_request_notification_by_id( $request_id );
     2047                $invites_class->send_request_notification_by_id( $request_id, $r );
    20482048                $admins = groups_get_group_admins( $r['group_id'] );
    20492049
    20502050                /**
  • tests/phpunit/testcases/core/invitations.php

    diff --git tests/phpunit/testcases/core/invitations.php tests/phpunit/testcases/core/invitations.php
    index 0646557a4..d8f1f4efb 100644
    include_once BP_TESTS_DIR . 'assets/invitations-extensions.php'; 
    332332                $this->set_current_user( $old_current_user );
    333333        }
    334334
     335        public function test_bp_invitations_add_request_with_date_modified() {
     336                $old_current_user = get_current_user_id();
     337
     338                $u1 = $this->factory->user->create();
     339                $this->set_current_user( $u1 );
     340
     341                $invites_class = new BPTest_Invitation_Manager_Extension();
     342
     343                $time = gmdate( 'Y-m-d H:i:s', time() - 100 );
     344                $args = array(
     345                        'user_id'           => $u1,
     346                        'item_id'           => 7,
     347                        'date_modified'     => $time,
     348                );
     349                $r1 = $invites_class->add_request( $args );
     350
     351                $req = new BP_Invitation( $r1 );
     352                $this->assertEquals( $time, $req->date_modified );
     353
     354                $this->set_current_user( $old_current_user );
     355        }
     356
     357        public function test_bp_invitations_add_invite_with_date_modified() {
     358                $old_current_user = get_current_user_id();
     359
     360                $u1 = $this->factory->user->create();
     361                $u2 = $this->factory->user->create();
     362                $this->set_current_user( $u1 );
     363
     364                $invites_class = new BPTest_Invitation_Manager_Extension();
     365                $time = gmdate( 'Y-m-d H:i:s', time() - 100 );
     366
     367                // Create an invitation.
     368                $invite_args = array(
     369                        'user_id'           => $u2,
     370                        'inviter_id'            => $u1,
     371                        'item_id'           => 1,
     372                        'send_invite'       => 1,
     373                        'date_modified'     => $time,
     374                );
     375                $i1 = $invites_class->add_invitation( $invite_args );
     376
     377                $inv = new BP_Invitation( $i1 );
     378                $this->assertEquals( $time, $inv->date_modified );
     379
     380                $this->set_current_user( $old_current_user );
     381        }
     382
    335383}