Changeset 11817 for sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/profiles.php
- Timestamp:
- 05/04/2022 11:06:00 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/profiles.php
r11273 r11817 5 5 6 6 namespace WPOrg_Learn\Profiles; 7 use W P_Error;7 use WordPressdotorg\Profiles as Profiles_API; 8 8 9 9 defined( 'WPINC' ) || die(); … … 18 18 } 19 19 20 const PROFILES_HANDLER_URL = 'https://profiles.wordpress.org/wp-admin/admin-ajax.php';21 22 20 add_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 );24 21 25 22 /** 26 23 * Add an activity to a user's profile when they complete a course. 27 *28 * @param string $status29 * @param int $user_id30 * @param int $course_id31 24 */ 32 function add_course_completed_activity( $status, $user_id, $course_id ){25 function add_course_completed_activity( string $status, int $user_id, int $course_id ) : void { 33 26 if ( 'complete' !== $status ) { 34 27 return; … … 45 38 ); 46 39 47 request_profile_update( PROFILES_HANDLER_URL,$request_body );40 Profiles_API\api( $request_body ); 48 41 } 49 50 /**51 * Send Profiles request and handle errors.52 *53 * @param string $request_url54 * @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, and83 * so that changes to the Profiles code can be tested.84 *85 * @param false|array|WP_Error $preempt86 * @param array $request_args87 * @param string $request_url88 *89 * @return array|mixed|WP_Error90 */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 connecting105 * 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.