sanitize_text_field( (string) $decoded['name'] ), 'description' => isset( $decoded['description'] ) ? sanitize_text_field( (string) $decoded['description'] ) : '', 'profileUrl' => isset( $decoded['profileUrl'] ) ? esc_url_raw( (string) $decoded['profileUrl'], array( 'https', 'http' ) ) : '', 'jsonld' => $decoded['jsonld'], ); set_transient( $cache_key . '_good', $data, AIPROFILE_INSIDE_GOOD_TTL ); } } } if ( empty( $data ) ) { $good = get_transient( $cache_key . '_good' ); $data = is_array( $good ) ? $good : array(); set_transient( $cache_key, $data, AIPROFILE_INSIDE_RETRY_TTL ); return $data; } set_transient( $cache_key, $data, AIPROFILE_INSIDE_BUSINESS_TTL ); return $data; } /** * The SEO plugin that already describes the site as an organisation or * business, or ''. Then we print no LocalBusiness of our own. * * @return string Plugin name, or ''. */ function aiprofile_inside_schema_conflict() { $found = ''; if ( defined( 'WPSEO_LOCAL_VERSION' ) ) { $found = 'Yoast Local SEO'; } elseif ( defined( 'WPSEO_VERSION' ) ) { $titles = get_option( 'wpseo_titles' ); if ( ! is_array( $titles ) || ! isset( $titles['company_or_person'] ) || 'person' !== $titles['company_or_person'] ) { $found = 'Yoast SEO'; } } elseif ( defined( 'RANK_MATH_VERSION' ) ) { $titles = get_option( 'rank-math-options-titles' ); if ( ! is_array( $titles ) || ! isset( $titles['knowledgegraph_type'] ) || 'person' !== $titles['knowledgegraph_type'] ) { $found = 'Rank Math'; } } elseif ( defined( 'AIOSEO_VERSION' ) ) { $options = json_decode( (string) get_option( 'aioseo_options', '' ), true ); $type = isset( $options['searchAppearance']['global']['schema']['siteRepresents'] ) ? $options['searchAppearance']['global']['schema']['siteRepresents'] : ''; if ( 'person' !== $type ) { $found = 'All in One SEO'; } } /** * Filters which SEO plugin already prints a business or organisation * schema ('' for none), so a site can force our LocalBusiness on or off. * * @param string $found Plugin name or ''. */ return (string) apply_filters( 'aiprofile_inside_schema_conflict', $found ); } /** Print the LocalBusiness JSON-LD in the head, where the owner chose. */ function aiprofile_inside_print_business_schema() { $where = (string) aiprofile_inside_option( 'schema' ); if ( 'site' !== $where && 'faq' !== $where ) { return; } $settings = aiprofile_inside_settings(); if ( empty( $settings['key'] ) ) { return; } if ( 'faq' === $where && ( empty( $settings['page_id'] ) || ! is_page( (int) $settings['page_id'] ) ) ) { return; } if ( '' !== aiprofile_inside_schema_conflict() ) { return; } $business = aiprofile_inside_business(); if ( empty( $business['jsonld'] ) || ! is_array( $business['jsonld'] ) ) { return; } $node = $business['jsonld']; $node['@id'] = home_url( '/#aiprofile-business' ); if ( empty( $node['url'] ) ) { $node['url'] = home_url( '/' ); } $json = wp_json_encode( $node, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP ); if ( is_string( $json ) ) { wp_print_inline_script_tag( $json, array( 'type' => 'application/ld+json' ) ); } } add_action( 'wp_head', 'aiprofile_inside_print_business_schema' ); /** * Where the site's own files live (robots.txt, llms.txt). * * @return string Path with a trailing slash. */ function aiprofile_inside_home_path() { if ( ! function_exists( 'get_home_path' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } return trailingslashit( get_home_path() ); } /** Does the site have an llms.txt file of its own? */ function aiprofile_inside_has_llms_file() { return file_exists( aiprofile_inside_home_path() . 'llms.txt' ); } /** * The text of /llms.txt (llmstxt.org): the business, the answers page, the * profile on AiProfile. * * @return string Markdown, or '' when there is nothing to say. */ function aiprofile_inside_llms_text() { $settings = aiprofile_inside_settings(); if ( empty( $settings['key'] ) || empty( $settings['page_id'] ) || 'publish' !== get_post_status( (int) $settings['page_id'] ) ) { return ''; } $business = aiprofile_inside_business(); $name = ! empty( $business['name'] ) ? $business['name'] : ( ! empty( $settings['name'] ) ? $settings['name'] : get_bloginfo( 'name' ) ); $clean = function ( $s ) { return trim( (string) preg_replace( '/\s+/', ' ', wp_strip_all_tags( (string) $s ) ) ); }; $lines = array(); $lines[] = '# ' . $clean( $name ); $lines[] = ''; $summary = ! empty( $business['description'] ) ? $business['description'] : sprintf( 'Questions and answers about %s, kept up to date by the business.', $name ); $lines[] = '> ' . $clean( $summary ); $lines[] = ''; $lines[] = '## Answers'; $lines[] = ''; $lines[] = '- [' . $clean( get_the_title( (int) $settings['page_id'] ) ) . '](' . esc_url_raw( get_permalink( (int) $settings['page_id'] ) ) . '): ' . $clean( sprintf( 'Frequently asked questions about %s, with the answers of the business.', $name ) ); $lines[] = '- [' . $clean( get_bloginfo( 'name' ) ) . '](' . esc_url_raw( home_url( '/' ) ) . '): The website of ' . $clean( $name ) . '.'; if ( ! empty( $business['profileUrl'] ) ) { $lines[] = ''; $lines[] = '## Optional'; $lines[] = ''; $lines[] = '- [' . $clean( $name ) . ' on AiProfile](' . esc_url_raw( $business['profileUrl'] ) . '): The verified profile with the same answers and their sources.'; } return implode( "\n", $lines ) . "\n"; } /** * Serve /llms.txt when the owner switched it on and the site has no such * file. Runs before WordPress looks for a post, so the URL needs no rewrite * rule and works with any permalink setting. */ function aiprofile_inside_serve_llms() { if ( ! aiprofile_inside_option( 'llms' ) ) { return; } $uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; $path = (string) wp_parse_url( $uri, PHP_URL_PATH ); $home = (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH ); if ( trailingslashit( $home ) . 'llms.txt' !== $path ) { return; } $text = aiprofile_inside_llms_text(); if ( '' === $text ) { return; } status_header( 200 ); header( 'Content-Type: text/plain; charset=utf-8' ); header( 'X-Content-Type-Options: nosniff' ); // Plain text, never HTML: every value went through wp_strip_all_tags or // esc_url_raw above, and HTML escaping would garble names like "Aram's". echo $text; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped exit; } add_action( 'parse_request', 'aiprofile_inside_serve_llms', 0 );