'async', 'in_footer' => true, ) ); } /** * `wp_script_attributes` filter: the embed attributes for our loader tag only. * * @param array $attributes Attributes WordPress is about to print. * @return array */ public static function filter_attributes( $attributes ): array { $attributes = is_array( $attributes ) ? $attributes : array(); if ( ( $attributes['id'] ?? '' ) !== self::HANDLE . '-js' ) { return $attributes; } $site_key = AgentMySite_Options::get_site_key(); if ( ! AgentMySite_Key::is_valid( $site_key ) ) { return $attributes; } // The baked-in loader, never a filtered src; WordPress escapes every value. $attributes['src'] = AgentMySite_Config::loader_url(); $attributes['async'] = true; $attributes['data-site-key'] = $site_key; $attributes['data-api-base'] = AgentMySite_Config::api_origin(); $attributes['data-iframe-origin'] = AgentMySite_Config::widget_origin(); return $attributes; } /** * Whether this request should get the widget. */ private static function should_inject(): bool { if ( is_admin() || is_feed() || is_customize_preview() || self::is_login() || self::is_rest() ) { return false; } if ( ! AgentMySite_Key::is_valid( AgentMySite_Options::get_site_key() ) || ! AgentMySite_Options::is_widget_enabled() ) { return false; } return ! AgentMySite_Exclude_Matcher::matches( AgentMySite_Options::get_exclude_patterns(), self::request_uri() ); } /** * Whether this is the login screen. `is_login()` exists from WordPress 6.1. */ private static function is_login(): bool { if ( function_exists( 'is_login' ) ) { return (bool) is_login(); } return isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow']; } /** * Whether this is a REST API request. */ private static function is_rest(): bool { return defined( 'REST_REQUEST' ) && REST_REQUEST; } /** * Current request URI, sanitised as a URL. Only its path is compared * against the hide-on patterns; it is never output. */ private static function request_uri(): string { if ( ! isset( $_SERVER['REQUEST_URI'] ) || ! is_string( $_SERVER['REQUEST_URI'] ) ) { return '/'; } $uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ); return '' === $uri ? '/' : $uri; } }