is_admin(), 'is_customize_preview' => function_exists( 'is_customize_preview' ) && is_customize_preview(), 'is_feed' => is_feed(), 'is_robots' => is_robots(), 'is_rest' => ( function_exists( 'wp_is_serving_rest_request' ) && wp_is_serving_rest_request() ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ), 'user_can_edit' => is_user_logged_in() && current_user_can( 'edit_posts' ), ); } /** wp_head, priority 20: the verification meta tag whenever a token is set. */ function promvia_print_head() { $meta = promvia_meta_tag( promvia_settings()['token'] ); if ( '' !== $meta ) { echo $meta . "\n"; } // phpcs:ignore WordPress.Security.EscapeOutput -- built from a pattern-validated token with esc_attr } add_action( 'wp_head', 'promvia_print_head', 20 ); /** wp_enqueue_scripts: the pixel when the gate allows, deferred in ; when consent-gated, inert plus the inline loader. */ function promvia_enqueue_pixel() { $settings = promvia_settings(); if ( ! promvia_should_print_pixel( $settings, promvia_front_end_context() ) ) { return; } $pixel = promvia_pixel_script( $settings, PROMVIA_ENDPOINT, promvia_cookie_domain( home_url() ), function_exists( 'wp_has_consent' ) ); if ( null === $pixel ) { return; } add_filter( 'script_loader_tag', function ( $tag, $handle ) use ( $pixel ) { return 'promvia-pixel' === $handle ? promvia_pixel_loader_tag( $tag, $pixel ) : $tag; }, 10, 2 ); wp_enqueue_script( 'promvia-pixel', $pixel['src'], array(), PROMVIA_VERSION, $pixel['gated'] ? array( 'in_footer' => false ) : array( 'strategy' => 'defer', 'in_footer' => false ) ); if ( $pixel['gated'] ) { wp_add_inline_script( 'promvia-pixel', promvia_consent_loader_js() ); } } add_action( 'wp_enqueue_scripts', 'promvia_enqueue_pixel' ); /** * The approved fixes (1.1.0), refreshed at most every ten minutes and only once the owner pasted a * feed address. A failed refresh keeps the last good copy (promvia_next_feed); a 404 clears it. * `$fetch` is injectable for tests; it returns array( status, body ). */ function promvia_fix_feed( $fetch = null ) { $url = promvia_settings()['feed_url']; if ( '' === $url ) { return null; } $cached = get_transient( 'promvia_fix_feed' ); if ( is_array( $cached ) && ( $cached['url'] ?? '' ) === $url ) { return $cached['feed']; } if ( null === $fetch ) { $fetch = function ( $u ) { $r = wp_remote_get( $u, array( 'timeout' => 5, 'redirection' => 0 ) ); return is_wp_error( $r ) ? array( 0, '' ) : array( (int) wp_remote_retrieve_response_code( $r ), (string) wp_remote_retrieve_body( $r ) ); }; } list( $status, $body ) = $fetch( $url ); $last = get_option( 'promvia_fix_feed_last', null ); $last = is_array( $last ) && ( $last['url'] ?? '' ) === $url ? $last['feed'] : null; $feed = promvia_next_feed( $status, $body, $last ); if ( null === $feed ) { delete_option( 'promvia_fix_feed_last' ); } else { update_option( 'promvia_fix_feed_last', array( 'url' => $url, 'feed' => $feed ), false ); } set_transient( 'promvia_fix_feed', array( 'url' => $url, 'feed' => $feed ), PROMVIA_FEED_TTL ); return $feed; } /** wp_head, priority 1: the approved JSON-LD, on every public page, for every visitor alike. */ function promvia_print_fixes_head() { $ctx = promvia_front_end_context(); if ( ! empty( $ctx['is_admin'] ) || ! empty( $ctx['is_feed'] ) || ! empty( $ctx['is_robots'] ) ) { return; } $feed = promvia_fix_feed(); foreach ( $feed ? $feed['jsonld'] : array() as $json ) { wp_print_inline_script_tag( $json, array( 'type' => 'application/ld+json' ) ); } } add_action( 'wp_head', 'promvia_print_fixes_head', 1 ); /** WordPress's generated robots.txt, with the approved crawlers let in. A robots.txt file on disk is served by the web server and never reaches this filter. */ function promvia_filter_robots_txt( $output, $is_public ) { $feed = promvia_fix_feed(); if ( ! $feed || empty( $feed['robots'] ) ) { return $output; } return promvia_merge_robots_allow( $output, $feed['robots'] ); } add_filter( 'robots_txt', 'promvia_filter_robots_txt', 20, 2 ); /** /llms.txt when the owner approved one. A real llms.txt file on disk is served by the web server first. */ function promvia_maybe_serve_llms() { $uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; if ( ! promvia_is_llms_request( $uri, home_url() ) ) { return; } $feed = promvia_fix_feed(); if ( ! $feed || null === $feed['llms'] ) { return; } header( 'Content-Type: text/plain; charset=utf-8' ); header( 'Cache-Control: public, max-age=300' ); echo $feed['llms']; // phpcs:ignore WordPress.Security.EscapeOutput -- served as text/plain, not HTML exit; } add_action( 'parse_request', 'promvia_maybe_serve_llms', 0 ); /** A new feed address takes effect at once, not after the cached copy expires. */ function promvia_forget_feed() { delete_transient( 'promvia_fix_feed' ); } add_action( 'update_option_' . PROMVIA_OPTION, 'promvia_forget_feed' );