ACF
Updater::request
Makes a request to the ACF connect server.
Method of the class: Updater{}
No Hooks.
Returns
(Array|String|WP_Error).
Usage
$Updater = new Updater(); $Updater->request( $endpoint, $body );
- $endpoint(string)
- The API endpoint.
Default:'' - $body(array)
- The body to post.
Default:null
Changelog
| Since 5.5.10 | Introduced. |
Updater::request() Updater::request code ACF 6.8.8
public function request( $endpoint = '', $body = null ) {
$site_url = acf_get_home_url();
if ( empty( $site_url ) || ! is_string( $site_url ) ) {
$site_url = '';
}
$headers = array(
'X-ACF-Version' => ACF_VERSION,
'X-ACF-URL' => $site_url,
);
// Add update channel header if defined.
if ( defined( 'ACF_UPDATE_CHANNEL' ) && ACF_UPDATE_CHANNEL ) {
$headers['X-ACF-Update-Channel'] = ACF_UPDATE_CHANNEL;
}
// Add plugin override header if defined.
if ( defined( 'ACF_RELEASE_ACCESS_KEY' ) && ACF_RELEASE_ACCESS_KEY ) {
$headers['X-ACF-Release-Access-Key'] = ACF_RELEASE_ACCESS_KEY;
}
$url = "https://connect.advancedcustomfields.com/$endpoint";
// Staging environment.
if ( defined( 'ACF_DEV_API' ) && ACF_DEV_API ) {
$url = trailingslashit( ACF_DEV_API ) . $endpoint;
acf_log( $url, $body );
}
// If we're posting an ACF license key, set it as the header.
if ( is_array( $body ) && isset( $body['acf_license'] ) ) {
$headers['X-ACF-License'] = $body['acf_license'];
}
// Determine URL.
if ( acf_is_pro() ) {
if ( empty( $headers['X-ACF-License'] ) ) {
$license_key = acf_pro_get_license_key();
if ( empty( $license_key ) || ! is_string( $license_key ) ) {
$license_key = '';
}
$headers['X-ACF-License'] = $license_key;
}
$headers['X-ACF-Plugin'] = 'pro';
} else {
$headers['X-ACF-Plugin'] = 'acf';
}
// Make request.
$raw_response = wp_remote_post(
$url,
array(
'timeout' => 20,
'body' => $body,
'headers' => $headers,
)
);
// Handle response error.
if ( is_wp_error( $raw_response ) ) {
return $raw_response;
// Handle http error.
} elseif ( wp_remote_retrieve_response_code( $raw_response ) !== 200 ) {
return new WP_Error( 'server_error', wp_remote_retrieve_response_message( $raw_response ) );
}
// Decode JSON response.
$json = json_decode( wp_remote_retrieve_body( $raw_response ), true );
// Allow non json value.
if ( $json === null ) {
return wp_remote_retrieve_body( $raw_response );
}
return $json;
}