Making WordPress.org

Changeset 13114


Ignore:
Timestamp:
01/16/2024 05:05:42 AM (3 years ago)
Author:
dd32
Message:

Plugin Directory: HelpScout Client: Fix the client to work with POST requests, which require specific content-type handling.

See #7384.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/clients/class-helpscout.php

    r11728 r13114  
    1515        const TIMEOUT = 15;
    1616
    17         public static function api( $url, $args = null, $method = 'GET' ) {
     17        public static function api( $url, $args = null, $method = 'GET', & $response_code = null ) {
    1818                // Verify the configuration variables are available.
    1919                if ( ! defined( 'HELPSCOUT_APP_ID' ) || ! defined( 'HELPSCOUT_APP_SECRET' ) ) {
     
    3030                }
    3131
     32                $body    = null;
     33                $headers = [
     34                        'Accept'        => 'application/json',
     35                        'Authorization' => self::get_auth_string(),
     36                ];
     37                // All editable requests must have a json content-type.
     38                // See https://developer.helpscout.com/mailbox-api/overview/content_type/
     39                if ( in_array( $method, [ 'POST', 'PUT', 'PATCH' ], true ) ) {
     40                        $headers['Content-Type'] = 'application/json';
     41                        $body                    = json_encode( $args );
     42                }
     43
    3244                $request = wp_remote_request(
    3345                        $url,
    3446                        array(
    3547                                'method'  => $method,
    36                                 'headers' => [
    37                                         'Accept'        => 'application/json',
    38                                         'Authorization' => self::get_auth_string(),
    39                                 ],
     48                                'headers' => $headers,
    4049                                'timeout' => self::TIMEOUT,
    41                                 'body'    => ( 'POST' === $method && $args ) ? $args : null,
     50                                'body'    => $body,
    4251                        )
    4352                );
     53
     54                $response_code = wp_remote_retrieve_response_code( $request );
    4455
    4556                return json_decode( wp_remote_retrieve_body( $request ) );
Note: See TracChangeset for help on using the changeset viewer.