Making WordPress.org

Changeset 11816


Ignore:
Timestamp:
05/04/2022 11:02:22 PM (4 years ago)
Author:
iandunn
Message:

Profiles: Modularize Learn handler to simplify and declutter.

The other handlers would also benefit from this.

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

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

    r11781 r11816  
    222222                        $success = intval( $activity_id ) > 0 ? '1' : $activity_id;
    223223                        die( $success );
     224                }
     225
     226                /**
     227                 * Add an activity payload to a profile
     228                 *
     229                 * @param string $component
     230                 * @param string $type
     231                 * @param mixed  $user
     232                 * @param array  $activity_args The arguments that should be passed to `bp_activity_add()`, minus the other params passed in to this function
     233                 *
     234                 * @return int|string
     235                 */
     236                protected function add_activity( string $component, string $type, $user, array $activity_args ) {
     237                        $user = $this->get_user( $user );
     238
     239                        if ( ! $user ) {
     240                                return '-1 Activity reported for unrecognized user : ' . $user;
     241                        }
     242
     243                        $required_args = array(
     244                                'user_id'    => $user->ID,
     245                                'component'  => $component,
     246                                'type'       => "{$component}_$type",
     247                                'error_type' => 'wp_error',
     248                        );
     249
     250                        $new_activity_args = array_merge( $activity_args, $required_args );
     251                        $activity_id       = bp_activity_add( $new_activity_args );
     252
     253                        if ( is_int( $activity_id ) ) {
     254                                $result = $activity_id;
     255
     256                        } else {
     257                                $result = sprintf(
     258                                        '-1 Unable to save activity: %s. Request was: %s',
     259                                        $activity_id->get_error_message(),
     260                                        wp_json_encode( $new_activity_args )
     261                                );
     262                        }
     263
     264                        return $result;
    224265                }
    225266
     
    350391                 */
    351392                protected function handle_learn_activity() {
    352                         $user = $this->get_user( $_POST['user'] );
    353 
    354                         if ( ! $user ) {
    355                                 return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $_POST['user'] );
    356                         }
    357 
     393                        $error         = '';
     394                        $activity_args = array();
    358395                        $activity_type = sanitize_text_field( $_POST['activity'] );
    359 
    360                         $default_args = array(
    361                                 'user_id'    => $user->ID,
    362                                 'component'  => 'learn',
    363                                 'type'       => "learn_$activity_type",
    364                                 'error_type' => 'wp_error',
    365                         );
     396                        $user          = sanitize_text_field( $_POST['user'] );
    366397
    367398                        switch ( $activity_type ) {
    368399                                case 'course_complete':
    369                                         $case_args = array(
     400                                        $activity_args = array(
    370401                                                'item_id'      => absint( $_POST['course_id'] ),
    371402                                                'primary_link' => esc_url_raw( $_POST['url'] ),
     
    380411
    381412                                default:
    382                                         $error = "-1 Unrecognized Learn activity.";
    383                         }
    384 
    385                         if ( isset( $error ) ) {
    386                                 $result = $error;
    387                         } else {
    388                                 $new_activity_args = array_merge( $default_args, $case_args );
    389                                 $activity_id       = bp_activity_add( $new_activity_args );
    390 
    391                                 if ( is_int( $activity_id ) ) {
    392                                         $result = $activity_id;
    393                                 } else {
    394                                         $result = sprintf(
    395                                                 '-1 Unable to save activity: %s. Request was: %s',
    396                                                 $activity_id->get_error_message(),
    397                                                 wp_json_encode( $new_activity_args )
    398                                         );
    399                                 }
    400                         }
     413                                        $error = '-1 Unrecognized Learn activity.';
     414                        }
     415
     416                        $result = $error || $this->add_activity( 'learn', $activity_type, $user, $activity_args );
    401417
    402418                        return $result;
Note: See TracChangeset for help on using the changeset viewer.