$print && ! is_user_logged_in() ). * * @param bool $wanted True when the script goes on the page. */ return (bool) apply_filters( 'nvivio_print_script', $wanted ); } /** * Registers the script in the head, with defer and without a version in its address. */ public static function enqueue(): void { if ( ! self::wanted() ) { return; } $settings = Nvivio_Settings::get(); wp_enqueue_script( self::HANDLE, Nvivio_Settings::script_url( $settings['site'] ), array(), null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- the script is versioned by Nvivio; a ?ver would only defeat its cache. array( 'strategy' => 'defer', 'in_footer' => false, ) ); if ( $settings['early_calls'] ) { wp_add_inline_script( self::HANDLE, self::QUEUE_JS, 'before' ); } } /** * The tag as the documentation gives it: defer and src, plus the attributes chosen in the * settings. WordPress's own id, strategy and type attributes are left off, and WordPress * escapes every attribute when it prints the tag (wp_sanitize_script_attributes). * * @param array $attributes The tag's attributes. * @return array */ public static function attributes( $attributes ): array { if ( ! is_array( $attributes ) || ( $attributes['id'] ?? '' ) !== self::HANDLE . '-js' ) { return is_array( $attributes ) ? $attributes : array(); } $settings = Nvivio_Settings::get(); $tag = array( 'defer' => true, 'src' => $attributes['src'], ); if ( '' !== $settings['endpoint'] ) { $tag['data-api'] = $settings['endpoint']; } if ( $settings['consent_none'] ) { $tag['data-consent'] = 'none'; } return $tag + Nvivio_Settings::own_attributes( $attributes ); } /** * The queue line without WordPress's id. * * @param array $attributes The inline tag's attributes. * @return array */ public static function inline_attributes( $attributes ): array { if ( ! is_array( $attributes ) ) { return array(); } if ( ( $attributes['id'] ?? '' ) === self::HANDLE . '-js-before' ) { unset( $attributes['id'] ); if ( 'text/javascript' === ( $attributes['type'] ?? '' ) ) { unset( $attributes['type'] ); } } return $attributes; } }