Making WordPress.org

Changeset 11781


Ignore:
Timestamp:
04/22/2022 07:09:18 PM (3 years ago)
Author:
iandunn
Message:

Profiles: Add activity handler for Slack props.

See https://github.com/WordPress/five-for-the-future/issues/169

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/profiles.wordpress.org/public_html/wp-content/plugins/wporg-profiles-activity-handler/wporg-profiles-activity-handler.php

    r11780 r11781  
    155155         */
    156156        public function handle_activity() {
     157            /*
     158             * This is useful for testing on your sandbox.
     159             *
     160             * e.g., Edit `$_POST['user_id']` so that activity goes to a test account rather than a real one.
     161             */
     162            do_action( 'wporg_profiles_before_handle_activity' );
     163
    157164            // Return error if not a valid activity request.
    158165            if ( true !== apply_filters( 'wporg_is_valid_activity_request', false ) ) {
     
    165172            }
    166173
    167             if ( empty( $_POST['user'] ) ) {
     174            $source = sanitize_text_field( $_POST['source'] );
     175
     176            // The `slack` source requires multiples users, so the parameters are named differently.
     177            if ( empty( $_POST['user'] ) && 'slack' !== $source ) {
    168178                die( '-1 No user specified.' );
    169179            }
    170 
    171             $source = $_POST['source'];
    172180
    173181            // Disable default BP moderation
     
    199207                case 'wordpress':
    200208                    $activity_id = $this->handle_wordpress_activity();
     209                    break;
     210                case 'slack':
     211                    $activity_id = $this->handle_slack_activity();
    201212                    break;
    202213                default:
     
    715726        }
    716727
     728        /**
     729         * Process activity stream requests from wordpress.slack.com (via api.wordpress.org).
     730         *
     731         * The giver/recipient IDs have already been validated by `api.w.org/.../props`, so we can assume they're
     732         * valid.
     733         *
     734         * @return bool|string `true` if all activities are added, string error message if any of them fail.
     735         */
     736        protected function handle_slack_activity() {
     737            $activity_type  = sanitize_text_field( $_POST['activity'] );
     738            $user_case_args = array();
     739            $errors         = '';
     740
     741            $default_args = array(
     742                'component'  => 'slack',
     743                'type'       => "slack_$activity_type",
     744                'error_type' => 'wp_error',
     745            );
     746
     747            switch ( $activity_type ) {
     748                case 'props_given':
     749                    $user_case_args = $this->handle_props_given( $_POST );
     750                    break;
     751
     752                default:
     753                    $errors .= "-1 Unrecognized Slack activity. ";
     754            }
     755
     756            if ( ! $errors ) {
     757                foreach ( $user_case_args as $case_args ) {
     758                    $new_activity_args = array_merge( $default_args, $case_args );
     759                    $activity_id       = bp_activity_add( $new_activity_args );
     760
     761                    if ( ! is_int( $activity_id ) ) {
     762                        $errors .= sprintf(
     763                            '-1 Unable to save activity for %d: %s. ',
     764                            $case_args['user_id'],
     765                            $activity_id->get_error_message()
     766                        );
     767                    }
     768                }
     769            }
     770
     771            return $errors ?: true;
     772        }
     773
     774        protected function handle_props_given( $post_unsafe ) {
     775            $giver_id       = (int) $post_unsafe['giver_user']['id'];
     776            $giver_username = sanitize_text_field( $post_unsafe['giver_user']['user_login'] );
     777            $recipient_ids  = array_map( 'intval', $post_unsafe['recipient_ids'] );
     778            $url            = wp_http_validate_url( $post_unsafe['url'] );
     779            $message        = sanitize_text_field( $post_unsafe['message'] );
     780            $message_id     = sanitize_text_field( $post_unsafe['message_id'] ); // {channel}-{timestamp}
     781
     782            $action_given = sprintf(
     783                '<a href="%s">Gave props</a> in <a href="https://make.wordpress.org/chat/">Slack</a>',
     784                esc_url_raw( $url ),
     785            );
     786
     787            $action_received = sprintf(
     788                '<a href="%1$s">Received props</a> from <a href="https://profiles.wordpress.org/%2$s/">@%2$s</a> in <a href="https://make.wordpress.org/chat/">Slack</a>',
     789                esc_url_raw( $url ),
     790                $giver_username,
     791            );
     792
     793            $user_case_args[] = array(
     794                'user_id'      => $giver_id,
     795                'item_id'      => $message_id,
     796                'primary_link' => $url,
     797                'action'       => $action_given,
     798                'content'      => wp_kses_data( $message ),
     799            );
     800
     801            foreach ( $recipient_ids as $recipient_id ) {
     802                $user_case_args[] = array(
     803                    'user_id'      => $recipient_id,
     804                    'item_id'      => $message_id,
     805                    'primary_link' => $url,
     806                    'action'       => $action_received,
     807                    'content'      => wp_kses_data( $message ),
     808                );
     809            }
     810
     811            return $user_case_args;
     812        }
     813
    717814    } /* /class WPOrg_Profiles_Activity_Handler */
    718815} /* if class_exists */
Note: See TracChangeset for help on using the changeset viewer.