Making WordPress.org

Changeset 11815


Ignore:
Timestamp:
05/04/2022 11:01:43 PM (2 years ago)
Author:
iandunn
Message:

Profiles: Integrate WPOrg_Learn\Profiles\ helpers.

This allows request_profile_update() and redirect_sandbox_requests() to be replaced with a call to api().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/mu-plugins/pub/profile-helpers.php

    r11791 r11815  
    11<?php
     2
    23namespace WordPressdotorg\Profiles;
     4use WP_Error;
    35
    46/**
     
    5759 *
    5860 * `$request_args` should match what the handler expects for a particular request.
    59  * See `wporg-profiles-activity-handler.php` and `wporg-profiles-association-handler.php`.
     61 * See `wporg-profiles-activity-handler.php` and `wporg-profiles-association-handler.php`
     62 *
     63 * @return array|WP_Error
    6064 */
    6165function api( array $args ) {
     
    6367    $headers   = [];
    6468    $sslverify = true;
     69    $error     = '';
    6570
    6671    // Requests to w.org sandbox should also use profiles.w.org sandbox, for testing end to end.
    6772    if ( 'staging' === wp_get_environment_type() ) {
    68         $url       = str_replace( 'profiles.wordpress.org', '127.0.0.1', $url );
    69         $sslverify = false; // wp_remote_get() cannot verify SSL if the hostname is 127.0.0.1.
     73        $url             = str_replace( 'profiles.wordpress.org', '127.0.0.1', $url );
     74        $sslverify       = false; // wp_remote_get() cannot verify SSL if the hostname is 127.0.0.1.
     75        $headers['host'] = 'profiles.wordpress.org';
    7076    }
    7177
    72     // Note: Authentication is handled elsewhere transparently.
    73     return wp_remote_post(
     78    // Note: Authentication is handled transparently by `enable_wporg_profiles_ajax_handler()` on Profiles.
     79    $response = wp_remote_post(
    7480        $url,
    7581        [
    7682            'body'      => $args,
    77             'headers'   => [
    78                 'host' => 'profiles.wordpress.org',
    79             ],
     83            'timeout'   => 10,
     84            'headers'   => $headers,
    8085            'sslverify' => $sslverify,
    8186        ]
    8287    );
     88
     89    if ( is_wp_error( $response ) ) {
     90        $error = $response->get_error_message();
     91
     92    } elseif ( 200 != wp_remote_retrieve_response_code( $response ) || 1 !== (int) wp_remote_retrieve_body( $response ) ) {
     93        $error = sprintf(
     94            'Error %s %s',
     95            $response['response']['code'],
     96            $response['body']
     97        );
     98    }
     99
     100    if ( $error ) {
     101        trigger_error( wp_kses_post( $error ), E_USER_WARNING );
     102    }
     103
     104    return $response;
    83105}
Note: See TracChangeset for help on using the changeset viewer.