Making WordPress.org


Ignore:
Timestamp:
05/04/2022 11:06:00 PM (3 years ago)
Author:
iandunn
Message:

Learn: Sync with git WordPress/learn@288a94e455e02062ee4d0e37fff0f2a8ccbfb079

File:
1 edited

Legend:

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

    r11273 r11817  
    55
    66namespace WPOrg_Learn\Profiles;
    7 use WP_Error;
     7use WordPressdotorg\Profiles as Profiles_API;
    88
    99defined( 'WPINC' ) || die();
     
    1818}
    1919
    20 const PROFILES_HANDLER_URL = 'https://profiles.wordpress.org/wp-admin/admin-ajax.php';
    21 
    2220add_action( 'sensei_course_status_updated', __NAMESPACE__ . '\add_course_completed_activity', 9, 3 ); // Before `redirect_to_course_completed_page()`.
    23 add_filter( 'pre_http_request', __NAMESPACE__ . '\redirect_sandbox_requests', 10, 3 );
    2421
    2522/**
    2623 * Add an activity to a user's profile when they complete a course.
    27  *
    28  * @param string $status
    29  * @param int    $user_id
    30  * @param int    $course_id
    3124 */
    32 function add_course_completed_activity( $status, $user_id, $course_id ) {
     25function add_course_completed_activity( string $status, int $user_id, int $course_id ) : void {
    3326    if ( 'complete' !== $status ) {
    3427        return;
     
    4538    );
    4639
    47     request_profile_update( PROFILES_HANDLER_URL, $request_body );
     40    Profiles_API\api( $request_body );
    4841}
    49 
    50 /**
    51  * Send Profiles request and handle errors.
    52  *
    53  * @param string $request_url
    54  * @param array  $body The value intended to be passed to `wp_remote_post()` as `$args['body']`.
    55  *
    56  * @return array|WP_Error The response from `wp_remote_post()`.
    57  */
    58 function request_profile_update( $request_url, $body ) {
    59     $response = wp_remote_post( $request_url, array( 'body' => $body ) );
    60 
    61     if ( is_wp_error( $response ) ) {
    62         $error = $response->get_error_message();
    63 
    64     } elseif ( 200 != wp_remote_retrieve_response_code( $response ) || 1 != (int) wp_remote_retrieve_body( $response ) ) {
    65         $error = sprintf(
    66             'Error %s %s',
    67             $response['response']['code'],
    68             $response['body']
    69         );
    70     }
    71 
    72     if ( isset( $error ) ) {
    73         trigger_error( wp_kses_post( $error ), E_USER_WARNING );
    74     }
    75 
    76     return $response;
    77 }
    78 
    79 /**
    80  * Send test requests to the current sandbox, instead of production.
    81  *
    82  * When making changes to code in this file, the request should go to the dev's sandbox so it can be debugged, and
    83  * so that changes to the Profiles code can be tested.
    84  *
    85  * @param false|array|WP_Error $preempt
    86  * @param array                $request_args
    87  * @param string               $request_url
    88  *
    89  * @return array|mixed|WP_Error
    90  */
    91 function redirect_sandbox_requests( $preempt, $request_args, $request_url ) {
    92     if ( ! defined( 'WPORG_SANDBOXED' ) || ! WPORG_SANDBOXED ) {
    93         return $preempt;
    94     }
    95 
    96     if ( PROFILES_HANDLER_URL !== $request_url ) {
    97         return $preempt;
    98     }
    99 
    100     $path                            = wp_parse_url( $request_url, PHP_URL_PATH );
    101     $request_args['headers']['Host'] = 'profiles.wordpress.org';
    102 
    103     /*
    104      * It's expected that the sandbox certificate won't be valid. This is safe because we're only connecting
    105      * to `127.0.0.1`.
    106      */
    107     $request_args['sslverify'] = false;
    108 
    109     return wp_remote_post( "https://127.0.0.1$path", $request_args );
    110 }
Note: See TracChangeset for help on using the changeset viewer.