Making WordPress.org


Ignore:
Timestamp:
04/22/2026 04:17:07 AM (6 months ago)
Author:
dd32
Message:

Profiles: Activity handlers: Rejig them to not be tied so tightly to being ajax endpoints.

This is part of an optimization for bulk updates to allow profile to track higher-throughput items.

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

    r14549 r14821  
    3333
    3434                /**
     35                 * @var WPOrg_Profiles_Activity_Handler|null
     36                 */
     37                protected static $instance = null;
     38
     39                /**
     40                 * Returns the shared instance, constructing it on first call.
     41                 *
     42                 * Used by both the bootstrap at the bottom of this file and by the
     43                 * CLI queue processor to avoid duplicate hook registration.
     44                 */
     45                public static function get_instance() {
     46                        return self::$instance ??= new self();
     47                }
     48
     49                /**
    3550                 * Constructor.
    3651                 */
    … …  
    8297                 */
    8398                public function plugins_loaded() {
    84                         add_action( 'wp_ajax_nopriv_wporg_handle_activity', array( $this, 'handle_activity' ) );
     99                        add_action( 'wp_ajax_nopriv_wporg_handle_activity', array( $this, 'ajax_handle_activity' ) );
    85100                }
    86101
    … …  
    156171
    157172                /**
    158                  * Checks that required POST arguments are defined and have a value.
     173                 * Checks that required arguments are defined and have a value.
    159174                 *
    160175                 * Ends request and reports on any missing arguments, if necessary.
    161176                 *
    162                  * @param array $args The array of required POST arguments.
    163                  */
    164                 protected function require_args( $args ) {
     177                 * @param array $args The array of required argument names.
     178                 * @param array $data The request data to check against.
     179                 */
     180                protected function require_args( $args, array $data ) {
    165181                        $missing = array();
    166182
    167183                        foreach ( $args as $arg ) {
    168                                 if ( empty( $_POST[ $arg ] ) ) {
     184                                if ( empty( $data[ $arg ] ) ) {
    169185                                        $missing[] = $arg;
    170186                                }
    … …  
    177193
    178194                /**
    179                  * Primary AJAX handler.
     195                 * AJAX wrapper for handle_activity().
     196                 *
     197                 * Validates the request, delegates to handle_activity(), and dies with the result.
     198                 */
     199                public function ajax_handle_activity() {
     200                        try {
     201                                do_action( 'wporg_profiles_before_handle_activity' );
     202
     203                                if ( true !== apply_filters( 'wporg_is_valid_activity_request', false ) ) {
     204                                        status_header( 400 );
     205                                        die( '-1 Not a valid activity request' );
     206                                }
     207
     208                                $result = $this->handle_activity( wp_unslash( $_POST ) );
     209
     210                                if ( is_wp_error( $result ) ) {
     211                                        $status = $result->get_error_data()['status'] ?? 500;
     212                                        status_header( $status );
     213                                        trigger_error( $result->get_error_message(), E_USER_WARNING );
     214                                        die( '-1 ' . $result->get_error_message() );
     215                                }
     216
     217                                die( '1' );
     218                        } catch ( Exception $exception ) {
     219                                status_header( 500 );
     220                                trigger_error( $exception->getMessage(), E_USER_WARNING );
     221                                die( '-1 ' . $exception->getMessage() );
     222                        }
     223                }
     224
     225                /**
     226                 * Primary handler for activity requests.
    180227                 *
    181228                 * Funnels incoming requests to appropriate sub-handler based on
    182                  * $_POST['source'] value.
    183                  *
    184                  * By default (and for security), this does nothing. The filter
    185                  * 'wporg_is_valid_activity_request' must be hooked in order to provide
    186                  * the appropriate validity checks on the request to permit the incoming
    187                  * activity notification to be handled.
    188                  *
    189                  * TODO: Make this a generic handler and require sub-handlers to
    190                  * register themselves.
    191                  */
    192                 public function handle_activity() {
    193                         try {
    194                                 /*
    195                                  * This is useful for testing on your sandbox.
    196                                  *
    197                                  * e.g., Edit `$_POST['user_id']` so that activity goes to a test account rather than a real one.
    198                                  */
    199                                 do_action( 'wporg_profiles_before_handle_activity' );
    200 
    201                                 // Return error if not a valid activity request.
    202                                 if ( true !== apply_filters( 'wporg_is_valid_activity_request', false ) ) {
    203                                         throw new Exception( '-1 Not a valid activity request' );
    204                                 }
    205 
    206                                 // Return error if activities are not enabled.
    207                                 if ( ! bp_is_active( 'activity' ) ) {
    208                                         throw new Exception( '-1 Activity component not activated' );
    209                                 }
    210 
    211                                 $source = sanitize_text_field( $_POST['source'] ?? $_POST['component'] );
    212 
    213                                 // The original `action` was the `admin-ajax.php` action, which is no longer needed.
    214                                 // Renaming this allows simple handlers to pass the sanitized $_POST directly to `bp_activity_add()`.
    215                                 $_POST['action'] = $_POST['message'] ?? '';
    216 
    217                                 // Slack and GlotPress sometimes include user IDs in a different location, and they always use
    218                                 // `sanitize_activity()`, which checks for a valid user ID. Checking here too adds complexity and
    219                                 // is unnecessary.
    220                                 if ( ! in_array( $source, array( 'slack', 'glotpress' ), true ) ) {
    221                                         if ( empty( $_POST['user'] ) && empty( $_POST['user_id'] ) ) {
    222                                                 throw new Exception( '-1 No user specified.' );
    223                                         }
    224                                 }
    225 
    226                                 // Disable default BP moderation
    227                                 remove_action( 'bp_activity_before_save', 'bp_activity_check_moderation_keys', 2 );
    228                                 remove_action( 'bp_activity_before_save', 'bp_activity_check_blacklist_keys',  2 );
    229 
    230                                 // Disable requirement that user have a display_name set
    231                                 remove_filter( 'bp_activity_before_save', 'bporg_activity_requires_display_name' );
    232 
    233                                 // If an activity doesn't require special logic, then `add_activity()` can be called directly. Compare
    234                                 // Learn and Slack to see the difference.
    235                                 switch ( $source ) {
    236                                         case 'forum':
    237                                                 $activity_id = $this->handle_forum_activity();
    238                                                 break;
    239                                         case 'learn':
    240                                                 $activity_id = bp_activity_add( $this->sanitize_activity( $_POST ) );
    241                                                 break;
    242                                         case 'glotpress':
    243                                                 $activity_id = $this->handle_glotpress_activity( $_POST );
    244                                                 break;
    245                                         case 'plugin':
    246                                                 $activity_id = $this->handle_plugin_activity();
    247                                                 break;
    248                                         case 'theme':
    249                                                 $activity_id = $this->handle_theme_activity();
    250                                                 break;
    251                                         case 'trac':
    252                                                 $activity_id = $this->handle_trac_activity();
    253                                                 break;
    254                                         case 'wordcamp':
    255                                                 $activity_id = $this->handle_wordcamp_activity();
    256                                                 break;
    257                                         case 'wordpress':
    258                                                 $activity_id = $this->handle_wordpress_activity();
    259                                                 break;
    260                                         case 'slack':
    261                                                 $activity_id = $this->handle_slack_activity();
    262                                                 break;
    263                                         default:
    264                                                 throw new Exception( '-1 Unrecognized activity source' );
    265                                                 break;
    266                                 }
    267 
    268                                 if ( is_wp_error( $activity_id ) ) {
    269                                         throw new Exception( '-1 Unable to save activity: ' . $activity_id->get_error_message() );
    270                                 } elseif ( str_starts_with( $activity_id, '-1' ) ) {
    271                                         throw new Exception( $activity_id );
    272                                 } elseif ( false === $activity_id || intval( $activity_id ) <= 0 ) {
    273                                         throw new Exception( '-1 Unable to save activity' );
    274                                 } else {
    275                                         $this->maybe_update_last_activity();
    276                                 }
    277 
    278                                 $response = '1';
    279 
    280                         } catch ( Exception $exception ) {
    281                                 trigger_error( $exception->getMessage(), E_USER_WARNING );
    282 
    283                                 $response = $exception->getMessage();
    284                         }
    285 
    286                         die( $response );
     229                 * $data['source'] value.
     230                 *
     231                 * @param array $data The request data.
     232                 * @return true|WP_Error
     233                 */
     234                public function handle_activity( array $data ) {
     235                        if ( ! bp_is_active( 'activity' ) ) {
     236                                return new WP_Error( 'activity_inactive', 'Activity component not activated', [ 'status' => 500 ] );
     237                        }
     238
     239                        $source = sanitize_text_field( $data['source'] ?? $data['component'] );
     240
     241                        // The original `action` was the `admin-ajax.php` action, which is no longer needed.
     242                        // Renaming this allows simple handlers to pass the sanitized data directly to `bp_activity_add()`.
     243                        $data['action'] = $data['message'] ?? '';
     244
     245                        // Slack and GlotPress sometimes include user IDs in a different location, and they always use
     246                        // `sanitize_activity()`, which checks for a valid user ID. Checking here too adds complexity and
     247                        // is unnecessary.
     248                        if ( ! in_array( $source, array( 'slack', 'glotpress' ), true ) ) {
     249                                if ( empty( $data['user'] ) && empty( $data['user_id'] ) ) {
     250                                        return new WP_Error( 'no_user', 'No user specified.', [ 'status' => 400 ] );
     251                                }
     252                        }
     253
     254                        // Disable default BP moderation
     255                        remove_action( 'bp_activity_before_save', 'bp_activity_check_moderation_keys', 2 );
     256                        remove_action( 'bp_activity_before_save', 'bp_activity_check_blacklist_keys',  2 );
     257
     258                        // Disable requirement that user have a display_name set
     259                        remove_filter( 'bp_activity_before_save', 'bporg_activity_requires_display_name' );
     260
     261                        // If an activity doesn't require special logic, then `add_activity()` can be called directly. Compare
     262                        // Learn and Slack to see the difference.
     263                        switch ( $source ) {
     264                                case 'forum':
     265                                        $activity_id = $this->handle_forum_activity( $data );
     266                                        break;
     267                                case 'generic':
     268                                case 'learn':
     269                                        $activity_id = bp_activity_add( $this->sanitize_activity( $data ) );
     270                                        break;
     271                                case 'glotpress':
     272                                        $activity_id = $this->handle_glotpress_activity( $data );
     273                                        break;
     274                                case 'plugin':
     275                                        $activity_id = $this->handle_plugin_activity( $data );
     276                                        break;
     277                                case 'theme':
     278                                        $activity_id = $this->handle_theme_activity( $data );
     279                                        break;
     280                                case 'trac':
     281                                        $activity_id = $this->handle_trac_activity( $data );
     282                                        break;
     283                                case 'wordcamp':
     284                                        $activity_id = $this->handle_wordcamp_activity( $data );
     285                                        break;
     286                                case 'wordpress':
     287                                        $activity_id = $this->handle_wordpress_activity( $data );
     288                                        break;
     289                                case 'slack':
     290                                        $activity_id = $this->handle_slack_activity( $data );
     291                                        break;
     292                                default:
     293                                        return new WP_Error( 'unknown_source', 'Unrecognized activity source', [ 'status' => 400 ] );
     294                        }
     295
     296                        if ( is_wp_error( $activity_id ) ) {
     297                                return new WP_Error( 'activity_failed', 'Unable to save activity: ' . $activity_id->get_error_message(), [ 'status' => 500 ] );
     298                        } elseif ( is_string( $activity_id ) && str_starts_with( $activity_id, '-1' ) ) {
     299                                return new WP_Error( 'activity_failed', $activity_id, [ 'status' => 500 ] );
     300                        } elseif ( false === $activity_id || intval( $activity_id ) <= 0 ) {
     301                                return new WP_Error( 'activity_failed', 'Unable to save activity', [ 'status' => 500 ] );
     302                        }
     303
     304                        $this->maybe_update_last_activity( $data );
     305
     306                        return true;
    287307                }
    288308
    … …  
    292312                 *
    293313                 */
    294                 public static function maybe_update_last_activity() {
    295 
    296                         $user = self::get_user( $_POST['user'] ?? ( $_POST['user_id'] ?? 0 ) );
     314                public static function maybe_update_last_activity( array $data = array() ) {
     315
     316                        $user = self::get_user( $data['user'] ?? ( $data['user_id'] ?? 0 ) );
    297317                        if ( ! $user ) {
    298318                                return;
    … …  
    317337
    318338                /**
    319                  * Sanitize `$_POST` args intended for `bp_activity_add()`.
     339                 * Sanitize request args intended for `bp_activity_add()`.
    320340                 *
    321341                 * @throws Exception
    … …  
    374394                 *  - Removing a topic reply
    375395                 */
    376                 private function handle_forum_activity() {
    377                         $user = self::get_user( $_POST['user'] );
     396                private function handle_forum_activity( array $data ) {
     397                        $user = self::get_user( $data['user'] );
    378398                        $type = '';
    379399
    380400                        // Check for valid user.
    381401                        if ( ! $user ) {
    382                                 return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $_POST['user'] );
     402                                return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $data['user'] );
    383403                        }
    384404
    … …  
    390410                                'remove-reply' => 'forum_reply_remove',
    391411                        );
    392                         if ( ! empty( $_POST['activity'] ) && ! empty( $activities[ $_POST['activity'] ] ) ) {
    393                                 $type = $activities[ $_POST['activity'] ];
     412                        if ( ! empty( $data['activity'] ) && ! empty( $activities[ $data['activity'] ] ) ) {
     413                                $type = $activities[ $data['activity'] ];
    394414                        } else {
    395415                                return '-1 Unrecognized forum activity.';
    … …  
    408428                                $required_args[] = 'post_id';
    409429                        }
    410                         $this->require_args( $required_args );
     430                        $this->require_args( $required_args, $data );
    411431
    412432                        // Determine 'item_id' value based on context.
    413433                        $item_id = in_array( $type, array( 'forum_topic_create', 'forum_topic_remove' ) ) ?
    414                                 intval( $_POST['topic_id'] ) :
    415                                 intval( $_POST['post_id'] );
     434                                intval( $data['topic_id'] ) :
     435                                intval( $data['post_id'] );
    416436
    417437                        // Find an existing activity uniquely identified by the reported criteria.
    … …  
    425445                                'type'              => str_replace( 'remove', 'create', $type ),
    426446                                'item_id'           => $item_id,
    427                                 'secondary_item_id' => intval( $_POST['forum_id'] ),
     447                                'secondary_item_id' => intval( $data['forum_id'] ),
    428448                        );
    429449                        $activity_id  = bp_activity_get_activity_id( $args );
    … …  
    443463                                        $action = sprintf(
    444464                                                'Created a topic, <i><a href="%s">%s</a></i>, on the site %s',
    445                                                 esc_url( $_POST['url'] ),
    446                                                 esc_html( $_POST['title'] ),
    447                                                 esc_html( $_POST['site'] )
     465                                                esc_url( $data['url'] ),
     466                                                esc_html( $data['title'] ),
     467                                                esc_html( $data['site'] )
    448468                                        );
    449469
    … …  
    452472                                        $action = sprintf(
    453473                                                'Posted a <a href="%s">reply</a> to <i>%s</i>, on the site %s',
    454                                                 esc_url( $_POST['url'] ),
    455                                                 esc_html( $_POST['title'] ),
    456                                                 esc_html( $_POST['site'] )
     474                                                esc_url( $data['url'] ),
     475                                                esc_html( $data['title'] ),
     476                                                esc_html( $data['site'] )
    457477                                        );
    458478                                }
    … …  
    461481                                        'user_id'           => $user->ID,
    462482                                        'action'            => $action,
    463                                         'content'           => esc_html( $_POST['message'] ),
    464                                         'primary_link'      => esc_url( $_POST['url'] ),
     483                                        'content'           => esc_html( $data['message'] ),
     484                                        'primary_link'      => esc_url( $data['url'] ),
    465485                                        'component'         => 'forums',
    466486                                        'type'              => $type,
    467487                                        'item_id'           => $item_id,
    468                                         'secondary_item_id' => intval( $_POST['forum_id'] ),
     488                                        'secondary_item_id' => intval( $data['forum_id'] ),
    469489                                        'hide_sitewide'     => false,
    470490                                );
    … …  
    497517                 *  - Creating new plugin
    498518                 */
    499                 private function handle_plugin_activity() {
    500                         $user = self::get_user( $_POST['user'] );
     519                private function handle_plugin_activity( array $data ) {
     520                        $user = self::get_user( $data['user'] );
    501521
    502522                        if ( ! $user ) {
    503                                 return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $_POST['user'] );
     523                                return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $data['user'] );
    504524                        }
    505525
    … …  
    508528                                'action'            => sprintf(
    509529                                        'Released a new plugin, <a href="%s">%s</a>',
    510                                         esc_url( $_POST['url'] ),
    511                                         $_POST['title']
     530                                        esc_url( $data['url'] ),
     531                                        $data['title']
    512532                                ),
    513533                                'content'           => '',
    514                                 'primary_link'      => $_POST['url'],
     534                                'primary_link'      => $data['url'],
    515535                                'component'         => 'plugins',
    516536                                'type'              => 'plugin_create',
    517                                 'item_id'           => intval( $_POST['plugin_id'] ),
     537                                'item_id'           => intval( $data['plugin_id'] ),
    518538                                'secondary_item_id' => false,
    519539                                'hide_sitewide'     => false,
    … …  
    529549                 *  - Creating new theme
    530550                 */
    531                 private function handle_theme_activity() {
    532                         $user = self::get_user( $_POST['user'] );
     551                private function handle_theme_activity( array $data ) {
     552                        $user = self::get_user( $data['user'] );
    533553
    534554                        if ( ! $user ) {
    535                                 return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $_POST['user'] );
     555                                return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $data['user'] );
    536556                        }
    537557
    … …  
    540560                                'action'            => sprintf(
    541561                                        'Released a new theme, <a href="%s">%s</a>',
    542                                         esc_url( $_POST['url'] ),
    543                                         $_POST['title']
     562                                        esc_url( $data['url'] ),
     563                                        $data['title']
    544564                                ),
    545565                                'content'           => '',
    546                                 'primary_link'      => $_POST['url'],
     566                                'primary_link'      => $data['url'],
    547567                                'component'         => 'themes',
    548568                                'type'              => 'theme_create',
    549                                 'item_id'           => intval( $_POST['theme_id'] ),
     569                                'item_id'           => intval( $data['theme_id'] ),
    550570                                'secondary_item_id' => false,
    551571                                'hide_sitewide'     => false,
    … …  
    564584                 *  - Receiving props on a commit
    565585                 */
    566                 private function handle_trac_activity() {
    567                         $user = self::get_user( $_POST['user'] );
     586                private function handle_trac_activity( array $data ) {
     587                        $user = self::get_user( $data['user'] );
    568588
    569589                        if ( ! $user ) {
    570                                 return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $_POST['user'] );
    571                         }
    572 
    573                         if ( ! empty( $_POST['description'] ) ) {
     590                                return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $data['user'] );
     591                        }
     592
     593                        if ( ! empty( $data['description'] ) ) {
    574594
    575595                                $args = array(
    576596                                        'user_id'           => $user->ID,
    577                                         'action'            => sprintf( 'Created a new ticket in %s Trac', $_POST['trac'] ),
    578                                         'content'           => $_POST['title'],
     597                                        'action'            => sprintf( 'Created a new ticket in %s Trac', $data['trac'] ),
     598                                        'content'           => $data['title'],
    579599                                        'component'         => 'tracs',
    580600                                        'type'              => 'trac_ticket_create',
    581                                         'item_id'           => intval( $_POST['id'] ),
     601                                        'item_id'           => intval( $data['id'] ),
    582602                                        'secondary_item_id' => false,
    583603                                        'hide_sitewide'     => false,
    … …  
    586606                                return bp_activity_add( $args );
    587607
    588                         } elseif ( ! empty( $_POST['comment'] ) ) {
     608                        } elseif ( ! empty( $data['comment'] ) ) {
    589609
    590610                                $args = array(
    591611                                        'user_id'           => $user->ID,
    592                                         'action'            => sprintf( 'Posted a reply to <i>%s</i> in %s Trac', $_POST['title'], $_POST['trac'] ),
    593                                         'content'           => $_POST['comment'],
     612                                        'action'            => sprintf( 'Posted a reply to <i>%s</i> in %s Trac', $data['title'], $data['trac'] ),
     613                                        'content'           => $data['comment'],
    594614                                        'component'         => 'tracs',
    595615                                        'type'              => 'trac_comment_create',
    596                                         'item_id'           => intval( $_POST['id'] ),
     616                                        'item_id'           => intval( $data['id'] ),
    597617                                        'secondary_item_id' => false,
    598618                                        'hide_sitewide'     => false,
    … …  
    606626                                $args = array(
    607627                                        'user_id'           => $user->ID,
    608                                         'action'            => sprintf( 'Committed [%s] to %s Trac', $_POST['changeset'], $_POST['trac'] ),
    609                                         'content'           => $_POST['message'],
     628                                        'action'            => sprintf( 'Committed [%s] to %s Trac', $data['changeset'], $data['trac'] ),
     629                                        'content'           => $data['message'],
    610630                                        'component'         => 'tracs',
    611631                                        'type'              => 'trac_commit_create',
    612                                         'item_id'           => intval( $_POST['changeset'] ),
     632                                        'item_id'           => intval( $data['changeset'] ),
    613633                                        'secondary_item_id' => false,
    614634                                        'hide_sitewide'     => false,
    … …  
    619639                                // Record props for each listed user
    620640                                $regex = '/props\s+((?:(?:\w+\b(?<!\bfixes))(?:[,][ ]*)?)+)/i';
    621                                 preg_match_all( $regex, $_POST['message'], $matches );
     641                                preg_match_all( $regex, $data['message'], $matches );
    622642                                $usernames = explode( ',', $matches[1][0] );
    623643                                $usernames = array_map( 'trim', $usernames );
    … …  
    630650                                        $args = array(
    631651                                                'user_id'           => $user->ID,
    632                                                 'action'            => sprintf( 'Received props in %s', $_POST['trac'] ),
    633                                                 'content'           => $_POST['message'],
     652                                                'action'            => sprintf( 'Received props in %s', $data['trac'] ),
     653                                                'content'           => $data['message'],
    634654                                                'component'         => 'tracs',
    635655                                                'type'              => 'trac_props_mention',
    636                                                 'item_id'           => intval( $_POST['changeset'] ),
     656                                                'item_id'           => intval( $data['changeset'] ),
    637657                                                'secondary_item_id' => false,
    638658                                                'hide_sitewide'     => false,
    … …  
    648668                 * Handles incoming activities for WordCamp.
    649669                 */
    650                 private function handle_wordcamp_activity() {
    651                         $user = self::get_user( $_POST['user'] );
     670                private function handle_wordcamp_activity( array $data ) {
     671                        $user = self::get_user( $data['user'] );
    652672                        $type = '';
    653673
    654674                        if ( ! $user ) {
    655                                 return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $_POST['user'] );
    656                         }
    657 
    658                         if ( isset( $_POST['speaker_id'] ) && ! empty( $_POST['speaker_id'] ) ) {
     675                                return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $data['user'] );
     676                        }
     677
     678                        if ( isset( $data['speaker_id'] ) && ! empty( $data['speaker_id'] ) ) {
    659679                                $type    = 'wordcamp_speaker_add';
    660                                 $item_id = $_POST['speaker_id'];
     680                                $item_id = $data['speaker_id'];
    661681
    662682                                $action = sprintf(
    663683                                        'Confirmed as a speaker for <a href="%s">%s</a>',
    664                                         esc_url( $_POST['url'] ),
    665                                         $_POST['wordcamp_name']
     684                                        esc_url( $data['url'] ),
     685                                        $data['wordcamp_name']
    666686                                );
    667687
    668                         } elseif ( isset( $_POST['organizer_id'] ) && ! empty( $_POST['organizer_id'] ) ) {
     688                        } elseif ( isset( $data['organizer_id'] ) && ! empty( $data['organizer_id'] ) ) {
    669689                                $type    = 'wordcamp_organizer_add';
    670                                 $item_id = $_POST['organizer_id'];
     690                                $item_id = $data['organizer_id'];
    671691
    672692                                $action = sprintf(
    673693                                        'Joined the organizing team for <a href="%s">%s</a>',
    674                                         esc_url( $_POST['url'] ),
    675                                         $_POST['wordcamp_name']
     694                                        esc_url( $data['url'] ),
     695                                        $data['wordcamp_name']
    676696                                );
    677697
    678                         } elseif ( isset( $_POST['type'] ) && 'mentor_assign' === $_POST['type'] ) {
     698                        } elseif ( isset( $data['type'] ) && 'mentor_assign' === $data['type'] ) {
    679699                                $type          = 'wordcamp_mentor_assign';
    680                                 $item_id       = absint( $_POST['wordcamp_id'] );
    681                                 $wordcamp_name = sanitize_text_field( $_POST['wordcamp_name'] );
    682 
    683                                 if ( empty( $_POST['url'] ) ) {
     700                                $item_id       = absint( $data['wordcamp_id'] );
     701                                $wordcamp_name = sanitize_text_field( $data['wordcamp_name'] );
     702
     703                                if ( empty( $data['url'] ) ) {
    684704                                        $action = 'Started mentoring ' . $wordcamp_name;
    685705                                } else {
    686706                                        $action = sprintf(
    687707                                                'Started mentoring <a href="%s">%s</a>',
    688                                                 sanitize_url( $_POST['url'] ),
     708                                                sanitize_url( $data['url'] ),
    689709                                                $wordcamp_name
    690710                                        );
    691711                                }
    692712
    693                         } elseif ( isset( $_POST['attendee_id'] ) && ! empty( $_POST['attendee_id'] ) ) {
    694                                 $item_id = $_POST['attendee_id'];
    695 
    696                                 if ( 'attendee_registered' == $_POST['activity_type'] ) {
     713                        } elseif ( isset( $data['attendee_id'] ) && ! empty( $data['attendee_id'] ) ) {
     714                                $item_id = $data['attendee_id'];
     715
     716                                if ( 'attendee_registered' == $data['activity_type'] ) {
    697717                                        $type = 'wordcamp_attendee_add';
    698718
    699719                                        $action = sprintf(
    700720                                                'Registered to attend <a href="%s">%s</a>',
    701                                                 esc_url( $_POST['url'] ),
    702                                                 $_POST['wordcamp_name']
     721                                                esc_url( $data['url'] ),
     722                                                $data['wordcamp_name']
    703723                                        );
    704724
    705                                 } elseif ( 'attendee_checked_in' == $_POST['activity_type'] ) {
     725                                } elseif ( 'attendee_checked_in' == $data['activity_type'] ) {
    706726                                        $type  = 'wordcamp_attendee_checked_in';
    707                                         $order = absint( $_POST['checked_in_count'] );
     727                                        $order = absint( $data['checked_in_count'] );
    708728
    709729                                        $action = sprintf(
    710730                                                'Is the %s person to arrive at <a href="%s">%s</a>',
    711731                                                $this->append_ordinal_suffix( $order ),
    712                                                 esc_url( $_POST['url'] ),
    713                                                 $_POST['wordcamp_name']
     732                                                esc_url( $data['url'] ),
     733                                                $data['wordcamp_name']
    714734                                        );
    715735                                }
    … …  
    724744                                'action'            => $action,
    725745                                'content'           => '',
    726                                 'primary_link'      => $_POST['url'] ?? '',
     746                                'primary_link'      => $data['url'] ?? '',
    727747                                'component'         => 'wordcamp',
    728748                                'type'              => $type,
    729749                                'item_id'           => intval( $item_id ),
    730                                 'secondary_item_id' => intval( $_POST['wordcamp_id'] ),
     750                                'secondary_item_id' => intval( $data['wordcamp_id'] ),
    731751                                'hide_sitewide'     => false,
    732752                        );
    … …  
    767787                 * Handles incoming activities for a WordPress install.
    768788                 */
    769                 private function handle_wordpress_activity() {
    770                         $user = self::get_user( $_POST['user'] );
    771                         $content      = $_POST['content'];
    772                         $primary_link = sanitize_url( $_POST['url'] );
     789                private function handle_wordpress_activity( array $data ) {
     790                        $user = self::get_user( $data['user'] );
     791                        $content      = $data['content'];
     792                        $primary_link = sanitize_url( $data['url'] );
    773793
    774794                        if ( ! $user ) {
    775                                 return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $_POST['user'] );
    776                         }
    777 
    778                         if ( isset( $_POST['comment_id'] ) && ! empty( $_POST['comment_id'] ) ) {
     795                                return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $data['user'] );
     796                        }
     797
     798                        if ( isset( $data['comment_id'] ) && ! empty( $data['comment_id'] ) ) {
    779799                                $type    = 'blog_comment_create';
    780                                 $item_id = $_POST['comment_id'];
     800                                $item_id = $data['comment_id'];
    781801                                $action  = sprintf(
    782802                                        'Wrote a <a href="%s">comment</a> on the post <i>%s</i>, on the site %s',
    783                                         esc_url( $_POST['url'] ),
    784                                         $_POST['title'],
    785                                         $_POST['blog']
     803                                        esc_url( $data['url'] ),
     804                                        $data['title'],
     805                                        $data['blog']
    786806                                );
    787                         } elseif ( isset( $_POST['type'] ) && 'new' === $_POST['type'] ) {
     807                        } elseif ( isset( $data['type'] ) && 'new' === $data['type'] ) {
    788808                                $type    = 'blog_post_create';
    789                                 $item_id = $_POST['post_id'];
    790 
    791                                 switch ( $_POST['post_type'] ) {
     809                                $item_id = $data['post_id'];
     810
     811                                switch ( $data['post_type'] ) {
    792812                                        case 'wporg_workshop':
    793813                                                $post_type = 'workshop';
    … …  
    803823
    804824                                        case 'handbook':
    805                                         case str_contains( $_POST['post_type'], '-handbook' ):
     825                                        case str_contains( $data['post_type'], '-handbook' ):
    806826                                                $post_type = 'handbook page';
    807827                                                break;
    … …  
    814834                                        'Wrote a new %s, <i><a href="%s">%s</a></i>, on the site %s',
    815835                                        $post_type,
    816                                         esc_url( $_POST['url'] ),
    817                                         $_POST['title'],
    818                                         $_POST['blog']
     836                                        esc_url( $data['url'] ),
     837                                        $data['title'],
     838                                        $data['blog']
    819839                                );
    820                         } elseif ( isset( $_POST['type'] ) && 'update' === $_POST['type'] ) {
     840                        } elseif ( isset( $data['type'] ) && 'update' === $data['type'] ) {
    821841                                // Handbooks are currently the only post type that send notifications of updates.
    822842                                $type    = 'blog_handbook_update';
    823                                 $item_id = $_POST['post_id'];
     843                                $item_id = $data['post_id'];
    824844                                $action  = ''; // Will be set by `digest_bump()`
    825845                                $content = false;
    826                                 $primary_link = sanitize_url( $_POST['blog_url'] ); // To group digest entries by site.
     846                                $primary_link = sanitize_url( $data['blog_url'] ); // To group digest entries by site.
    827847
    828848                                $singular = sprintf(
    829849                                        'Updated a handbook page on <a href="%s">%s</a>.',
    830                                         sanitize_url( $_POST['blog_url'] ),
    831                                         sanitize_text_field( $_POST['blog'] )
     850                                        sanitize_url( $data['blog_url'] ),
     851                                        sanitize_text_field( $data['blog'] )
    832852                                );
    833853
    … …  
    835855                                        'Made %s updates to handbook pages on <a href="%s">%s</a>.',
    836856                                        '%d',
    837                                         sanitize_url( $_POST['blog_url'] ),
    838                                         sanitize_text_field( $_POST['blog'] )
     857                                        sanitize_url( $data['blog_url'] ),
     858                                        sanitize_text_field( $data['blog'] )
    839859                                );
    840860                        }
    … …  
    869889                 * @return bool|string `true` if all activities are added, string error message if any of them fail.
    870890                 */
    871                 protected function handle_slack_activity() {
    872                         $activity_type  = sanitize_text_field( $_POST['activity'] );
     891                protected function handle_slack_activity( array $data ) {
     892                        $activity_type  = sanitize_text_field( $data['activity'] );
    873893                        $user_case_args = array();
    874894                        $errors         = '';
    … …  
    882902                        switch ( $activity_type ) {
    883903                                case 'props_given':
    884                                         $user_case_args = $this->handle_props_given( $_POST );
     904                                        $user_case_args = $this->handle_props_given( $data );
    885905                                        break;
    886906
    … …  
    10911111
    10921112if ( class_exists( 'WPOrg_Profiles_Activity_Handler' ) ) {
    1093         new WPOrg_Profiles_Activity_Handler();
     1113        WPOrg_Profiles_Activity_Handler::get_instance();
    10941114}
    10951115
Note: See TracChangeset for help on using the changeset viewer.