'', 'reason' => '', ); /** * @param string $status HIT|MISS|BYPASS|HIT-EARLY|OFF. * @param string $reason Reason code. */ public static function set( $status, $reason = '' ) { self::$last = array( 'status' => strtoupper( (string) $status ), 'reason' => preg_replace( '/[^a-z0-9_\-]/i', '', (string) $reason ), ); } /** * @return array{status:string,reason:string} */ public static function get() { return self::$last; } /** * Human label for admin bar. * * @param string $reason Code. * @return string */ public static function reason_label( $reason ) { $map = array( 'ok' => 'eligible', 'hit' => 'served from disk', 'method' => 'non-GET', 'admin' => 'wp-admin', 'feed' => 'feed', 'preview' => 'preview', 'donotcachepage' => 'DONOTCACHEPAGE', 'ajax' => 'AJAX', 'rest' => 'REST', 'password' => 'password post', 'signup' => 'signup', 'uri' => 'excluded URI', 'cart_cookie' => 'cart/session cookie', 'logged_in' => 'logged-in', 'cookie' => 'sensitive cookie', 'comment_cookie' => 'comment cookie', 'query' => 'rejected query arg', 'ua' => 'rejected user-agent', 'disabled' => 'page cache off', 'private' => 'private response', 'miss' => 'not in cache', 'no_file' => 'no cache file', ); $reason = (string) $reason; return isset( $map[ $reason ] ) ? $map[ $reason ] : $reason; } /** * Whether the main query has run (conditional tags like is_singular() are valid). * Before the `wp` action they always return false and WordPress logs a notice. * * @return bool */ public static function query_ready() { return function_exists( 'did_action' ) && did_action( 'wp' ) > 0; } /** * Build args for SafetyPolicy from current request (best-effort). * * @param array $settings Settings. * @return array */ public static function current_request_args( array $settings ) { $raw_uri = isset( $_SERVER['REQUEST_URI'] ) && is_string( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/'; $uri = sanitize_text_field( $raw_uri ); if ( '/' !== substr( $uri, 0, 1 ) || strlen( $uri ) > 8192 ) { $uri = '/'; } return array( 'method' => isset( $_SERVER['REQUEST_METHOD'] ) && is_string( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET', 'uri' => $uri, 'cookies' => isset( $_COOKIE ) && is_array( $_COOKIE ) ? $_COOKIE : array(), 'is_admin' => is_admin(), 'is_logged_in' => is_user_logged_in(), 'is_preview' => \QWSpeedCache\Cache\Status::query_ready() && function_exists( 'is_preview' ) && is_preview(), 'is_feed' => \QWSpeedCache\Cache\Status::query_ready() && function_exists( 'is_feed' ) && is_feed(), 'doing_ajax' => function_exists( 'wp_doing_ajax' ) && wp_doing_ajax(), 'rest_request' => defined( 'REST_REQUEST' ) && REST_REQUEST, 'donotcachepage' => defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE, 'has_post_password' => \QWSpeedCache\Cache\Status::query_ready() && function_exists( 'is_singular' ) && is_singular() && post_password_required(), 'is_multisite_signup'=> false, ); } }