Skip to content

Function

Make an API call the store of an extension.

tml_extension_api_call( string $url, array $args = array() )

Parameters

$url string
The store URL.
$args array Default array()
Optional. An array of arguments for making an API call.
$edd_action string
The API action.
$license string
The extension license key.
$item_id int
The extension item ID.
$slug string
The extension slug.
$url string
The client WP URL.
$beta bool
Whether to include beta versions or not.

Return

object|false The response object or false on failure.

Source

function tml_extension_api_call( $url, $args = array() ) {
    $args = wp_parse_args( $args, tml_get_extension_api_call_arg_defaults() );

    // The passive version check is safe to cache across requests since it drives
    // background/cron update checks; license activation/deactivation/status
    // calls are one-off, user-initiated actions that must always reach the
    // store live, so they're only deduped for the current request.
    $cacheable = ( 'get_version' === $args['edd_action'] );

    $cache_key = tml_get_extension_api_call_cache_key( $args );

    $response = wp_cache_get( $cache_key, 'tml_api_calls' );
    if ( ! $response && $cacheable ) {
        $response = get_site_transient( 'tml_api_call-' . $cache_key );
    }

    if ( ! $response ) {
        $response = wp_remote_post(
            $url,
            array(
                'timeout'   => 30,
                'sslverify' => true,
                'body'      => $args,
            )
        );

        if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
            return false;
        }

        $response = json_decode( wp_remote_retrieve_body( $response ), true );
        if ( is_array( $response ) ) {
            $response = (object) $response;
        }

        if ( is_object( $response ) ) {
            if ( isset( $response->sections ) ) {
                $response->sections = tml_maybe_unserialize_no_objects( $response->sections );
            }
            if ( isset( $response->banners ) ) {
                $response->banners = tml_maybe_unserialize_no_objects( $response->banners );
            }
            if ( isset( $response->icons ) ) {
                $response->icons = tml_maybe_unserialize_no_objects( $response->icons );
            }
        } else {
            $response = false;
        }

        wp_cache_set( $cache_key, $response, 'tml_api_calls' );
        if ( $cacheable ) {
            set_site_transient( 'tml_api_call-' . $cache_key, $response, DAY_IN_SECONDS / 2 );
        }
    }

    return $response;
}

Uses

Used by

Changelog

VersionDescription
7.0 Introduced.