* * @phpstan-type TcfFramework array{key: string, label: string} */ final class TcfFrameworks { /** * Resource path, relative to the WordPress API base. * * @since 4.4.0 * @var string */ const RESOURCE = 'cmp/tcf_frameworks'; /** * Transient holding the last list fetched. * * @since 4.4.0 * @var string */ const TRANSIENT = 'actirise_tcf_frameworks'; /** * Lifetime of a fetched list, in seconds: the hourly cron refreshes it first. * * @since 4.4.0 * @var int */ const TTL = 3600; /** * Lifetime of a failed or empty fetch, in seconds. * * @since 4.4.0 * @var int */ const FAILURE_TTL = 300; /** * Request timeout, in seconds. * * A cold cache is filled while the admin page renders, so an unreachable API * holds the page this long at most, once per FAILURE_TTL. The list is a * handful of entries: a healthy gateway answers well within it. * * @since 4.4.0 * @var int */ const TIMEOUT = 5; /** * The frameworks, from the cache when it holds them, from the API otherwise. * * @since 4.4.0 * @return array Empty when the list could not be fetched. */ public static function get() { $cached = get_transient( self::TRANSIENT ); if ( is_array( $cached ) ) { return self::normalize( $cached ); } return self::refresh(); } /** * Fetch the frameworks from the API and cache them. * * Issues nothing for a site with no Actirise account: the call needs the * publisher's token, and the cron running this hourly also runs on every * free install. * * A `401` yields no list but keeps the publisher logged in: this read is not * about the session, and a gateway or ACL refusing it (SPA-26047 not * deployed yet, for instance) must not log the publisher out of the * plugin. An expired token is still caught by the plugin's other calls. * * @since 4.4.0 * @return array Empty when the list could not be fetched. */ public static function refresh() { if ( ! Helpers::is_user_authenticated() ) { return array(); } /** @var string $token */ $token = Options::get( 'settings-analytics-token', '' ); $api = new Api(); $response = $api->get( 'api', self::RESOURCE, array(), $token, false, array( 'forget_user_on_401' => false, 'timeout' => self::TIMEOUT, ) ); if ( is_wp_error( $response ) ) { Logger::add_log( 'TCF frameworks fetch error ' . $response->get_error_code(), ConfigSync::LOG_CONTEXT, 'error' ); $frameworks = array(); } else { $frameworks = self::normalize( $response ); if ( empty( $frameworks ) ) { Logger::add_log( 'TCF frameworks fetch returned no framework', ConfigSync::LOG_CONTEXT, 'error' ); } } set_transient( self::TRANSIENT, $frameworks, empty( $frameworks ) ? self::FAILURE_TTL : self::TTL ); return $frameworks; } /** * Keep the well-formed entries of an answer, as `key` and `label` only. * * An entry without a non-empty string key and label is dropped, and so is a * key already listed: the key is what the select stores. * * @since 4.4.0 * @param mixed $response The decoded answer. * @return array */ private static function normalize( $response ) { if ( ! is_array( $response ) ) { return array(); } $frameworks = array(); foreach ( $response as $item ) { if ( ! is_array( $item ) || ! isset( $item['key'], $item['label'] ) ) { continue; } $key = $item['key']; $label = $item['label']; if ( ! is_string( $key ) || ! is_string( $label ) || $key === '' || $label === '' || isset( $frameworks[ $key ] ) ) { continue; } $frameworks[ $key ] = array( 'key' => $key, 'label' => $label, ); } return array_values( $frameworks ); } }