Making WordPress.org

Changeset 11272


Ignore:
Timestamp:
10/11/2021 03:45:17 PM (2 years ago)
Author:
iandunn
Message:

Profiles: Accept "course completed" activity from learn.w.org

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

    r9508 r11272  
    185185                case 'forum':
    186186                    $activity_id = $this->handle_forum_activity();
     187                    break;
     188                case 'learn':
     189                    $activity_id = $this->handle_learn_activity();
    187190                    break;
    188191                case 'plugin':
     
    332335                return true;
    333336            }
     337        }
     338
     339        /**
     340         * Process activity stream requests from learn.wordpress.org.
     341         *
     342         * @return int|string The activity ID on success; an error message on failure.
     343         */
     344        protected function handle_learn_activity() {
     345            $user = $this->get_user( $_POST['user'] );
     346
     347            if ( ! $user ) {
     348                return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $_POST['user'] );
     349            }
     350
     351            $activity_type = sanitize_text_field( $_POST['activity'] );
     352
     353            $default_args = array(
     354                'user_id'    => $user->ID,
     355                'component'  => 'learn',
     356                'type'       => "learn_$activity_type",
     357                'error_type' => 'wp_error',
     358            );
     359
     360            switch ( $activity_type ) {
     361                case 'course_complete':
     362                    $case_args = array(
     363                        'item_id'      => absint( $_POST['course_id'] ),
     364                        'primary_link' => esc_url_raw( $_POST['url'] ),
     365
     366                        'action' => sprintf(
     367                            'Completed the course <em><a href="%s">%s</a></em> on learn.wordpress.org',
     368                            esc_url( $_POST['url'] ),
     369                            esc_html( $_POST['course_title'] )
     370                        ),
     371                    );
     372                    break;
     373
     374                default:
     375                    $error = "-1 Unrecognized Learn activity.";
     376            }
     377
     378            if ( isset( $error ) ) {
     379                $result = $error;
     380            } else {
     381                $new_activity_args = array_merge( $default_args, $case_args );
     382                $activity_id       = bp_activity_add( $new_activity_args );
     383
     384                if ( is_int( $activity_id ) ) {
     385                    $result = $activity_id;
     386                } else {
     387                    $result = sprintf(
     388                        '-1 Unable to save activity: %s. Request was: %s',
     389                        $activity_id->get_error_message(),
     390                        wp_json_encode( $new_activity_args )
     391                    );
     392                }
     393            }
     394
     395            return $result;
    334396        }
    335397
Note: See TracChangeset for help on using the changeset viewer.