false, // Register the site for pings (sends the site address and a key). 'questions' => false, // The question form under the answers. 'schema' => 'faq', // LocalBusiness JSON-LD: off, faq (the answers page) or site. 'llms' => false, // Serve /llms.txt. 'credit' => false, // The "Powered by AiProfile" link (wordpress.org guideline 10: opt-in). 'robots_fix' => false, // Let AI crawlers in through the virtual robots.txt. ); $s = aiprofile_inside_settings(); return array_key_exists( $name, $s ) ? $s[ $name ] : ( isset( $defaults[ $name ] ) ? $defaults[ $name ] : null ); } /** * The profile key from a link or the shortcode. Accepts: * - the short link: aiprofile.com/, with or without https:// or www.; * - a profile URL or path: /nl/netherlands/amsterdam/cafe-de-jaren; * - the business id or the handle on its own. * * @param string $raw What the owner pasted or the shortcode said. * @return string Key with only safe characters, or ''. */ function aiprofile_inside_normalize_key( $raw ) { $raw = trim( (string) $raw ); $raw = (string) preg_replace( '#^[a-z]+://#i', '', $raw ); // A host in front (aiprofile.com/..., www.aiprofile.com/...): keep the path. if ( preg_match( '#^[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)+(:\d+)?(/.*)?$#', $raw, $m ) ) { $raw = isset( $m[3] ) ? $m[3] : ''; } $raw = (string) preg_replace( '#[?\#].*$#', '', $raw ); $parts = array_values( array_filter( explode( '/', $raw ), 'strlen' ) ); if ( 4 === count( $parts ) ) { $key = '/' . implode( '/', $parts ); } elseif ( 1 === count( $parts ) ) { $key = $parts[0]; } else { return ''; } return substr( (string) preg_replace( '#[^A-Za-z0-9_/-]#', '', $key ), 0, 200 ); } /** * The business name from the fragment's own heading, in any of its languages. * * @param string $html Fragment HTML (with its title). * @return string */ function aiprofile_inside_name_from_html( $html ) { if ( ! preg_match( '#

(.*?)

#s', (string) $html, $m ) ) { return ''; } $title = html_entity_decode( wp_strip_all_tags( $m[1] ), ENT_QUOTES, 'UTF-8' ); $patterns = array( '/^Veelgestelde vragen over (.+)$/u', '/^Frequently asked questions about (.+)$/u', '/^Häufige Fragen zu (.+)$/u', '/^Questions fréquentes sur (.+)$/u', '/^Preguntas frecuentes sobre (.+)$/u', '/^Domande frequenti su (.+)$/u', '/^Συχνές ερωτήσεις για (.+)$/u', '/^(.+) hakkında sık sorulan sorular$/u', '/^关于(.+)的常见问题$/u', ); foreach ( $patterns as $pattern ) { if ( preg_match( $pattern, $title, $n ) ) { return trim( $n[1] ); } } return trim( $title ); } /** * The language to ask for: the shortcode's `taal` (or `lang`), else the * site's own language. When the profile does not have that language, * AiProfile serves the profile's home language (the business's country * language, then English). A profile URL's own /nl/ does not count: it only * says which profile. * * @param string $attr The shortcode's taal or lang attribute. * @param string $key The normalized profile key. * @return string Two or three lowercase letters. */ function aiprofile_inside_lang( $attr, $key ) { unset( $key ); $lang = strtolower( (string) $attr ); if ( '' === $lang ) { $lang = (string) get_locale(); } $lang = strtolower( substr( (string) preg_replace( '/[^A-Za-z].*$/', '', $lang ), 0, 3 ) ); return '' !== $lang ? $lang : 'en'; } /** * The tags and attributes the fragment uses, and nothing else. * * @return array wp_kses allow-list. */ function aiprofile_inside_allowed_html() { return array( 'section' => array( 'class' => true, 'lang' => true, 'data-aiprofile-version' => true, ), 'h2' => array( 'class' => true ), 'h3' => array( 'class' => true ), 'details' => array( 'class' => true ), 'summary' => array( 'class' => true ), 'p' => array( 'class' => true ), 'small' => array(), 'a' => array( 'href' => true, 'rel' => true, 'target' => true, 'class' => true, ), ); } /** * Sanitize the fragment's HTML: scripts and styles go entirely (content * included), then wp_kses keeps only the allow-list with http(s) links. * * @param string $html Fragment HTML. * @return string Safe HTML. */ function aiprofile_inside_kses( $html ) { $html = (string) preg_replace( '#<(script|style)\b[^>]*>.*?#is', '', (string) $html ); return wp_kses( $html, aiprofile_inside_allowed_html(), array( 'http', 'https' ) ); } /** * Fit the fragment's headings into the host page. By default the page's own * title (its H1) is the heading, so our "Frequently asked questions about ..." * title goes and the theme groups move up from h3 to h2: H1 page title, H2 * theme, then the questions. With $with_title the title stays as an h2 and the * groups stay h3, for a block in the middle of other content. * * @param string $html Sanitized fragment. * @param bool $with_title Keep our own heading. * @return string */ function aiprofile_inside_headings( $html, $with_title ) { if ( $with_title ) { return $html; } $html = (string) preg_replace( '#

.*?

\s*#s', '', $html ); return (string) preg_replace( '#.*?)#s', '', $html ); } /** * The "Powered by AiProfile" link in the fragment's stamp. Off by * default (wordpress.org guideline 10: a credit link is opt-in). When the * owner switches it on it is a normal, followed link: the point is that his * site and his verified profile are seen to belong together. * * @param string $html Sanitized fragment. * @param bool $show Whether the owner switched the link on. * @return string */ function aiprofile_inside_credit( $html, $show ) { if ( ! $show ) { return (string) preg_replace( '#

.*?

\s*#s', '', $html ); } return (string) preg_replace_callback( '#

.*?

#s', function ( $m ) { return str_replace( 'rel="nofollow noopener"', 'rel="noopener"', $m[0] ); }, $html ); } /** * The stamp at the top of the block (since 1.3.0): the check mark of * "✓ Verified by ..." gets a span of its own, so the stylesheet can colour * it green. Our own markup, added after sanitising. * * @param string $html Sanitized fragment. * @return string */ function aiprofile_inside_stamp( $html ) { return (string) preg_replace( '#(

)\x{2713}\s*#u', '$1 ', $html ); } /** The block's small stylesheet: the stamp, sentence case and spacing (assets/inside.css). */ function aiprofile_inside_enqueue_style() { wp_enqueue_style( 'aiprofile-inside', plugins_url( 'assets/inside.css', AIPROFILE_INSIDE_FILE ), array(), AIPROFILE_INSIDE_VERSION ); } /** * Load it in the head already on a page whose content holds the shortcode, * so a classic theme does not print it late in the footer. Nowhere else. */ function aiprofile_inside_maybe_enqueue_style() { if ( ! is_singular() ) { return; } $post = get_post(); if ( $post && has_shortcode( (string) $post->post_content, 'aiprofile' ) ) { aiprofile_inside_enqueue_style(); } } add_action( 'wp_enqueue_scripts', 'aiprofile_inside_maybe_enqueue_style' ); /** * The profile URL on AiProfile, from the fragment's credit link. * * @param string $html Fragment. * @return string */ function aiprofile_inside_profile_url_from_html( $html ) { if ( preg_match( '#

.*?href="([^"]+)"#s', (string) $html, $m ) ) { return esc_url_raw( html_entity_decode( $m[1], ENT_QUOTES, 'UTF-8' ), array( 'https', 'http' ) ); } return ''; } /** * Is a shortcode attribute switched on? Accepts ja, yes, 1, true, on. * * @param string $value Attribute value. * @return bool */ function aiprofile_inside_truthy( $value ) { return in_array( strtolower( trim( (string) $value ) ), array( '1', 'ja', 'yes', 'true', 'on', 'y', 'j' ), true ); } /** * The arguments for every request to AiProfile: short timeout, our * User-Agent, and the site's address as Referer (as WordPress itself sends in * its own User-Agent), so AiProfile can count which sites show which profile. * Nothing about visitors. * * @param array $extra Arguments to add or override. * @return array */ function aiprofile_inside_http_args( $extra = array() ) { return array_merge( array( 'timeout' => 5, 'redirection' => 2, 'limit_response_size' => 1024 * 1024, 'user-agent' => 'AiProfileInside/' . AIPROFILE_INSIDE_VERSION . ' (WordPress/' . get_bloginfo( 'version' ) . '; ' . home_url( '/' ) . ')', 'headers' => array( 'Accept' => 'text/html', 'Referer' => home_url( '/' ), ), ), $extra ); } /** * Fetch the fragment from AiProfile. * * @param string $key Profile key. * @param string $lang Language. * @return array { * @type string $status 'ok', 'not_found' or 'error'. * @type string $html Sanitized fragment (status ok). * @type array $jsonld Decoded FAQPage JSON-LD, or null. * @type string $detail HTTP code or error text, for the status line. * } */ function aiprofile_inside_fetch( $key, $lang ) { $origin = aiprofile_inside_origin(); if ( '' === $origin ) { return array( 'status' => 'error', 'detail' => 'origin is not an https URL', ); } $url = $origin . '/api/v1/embed?id=' . rawurlencode( $key ) . '&lang=' . rawurlencode( $lang ); // After a ping: a new address, so no cache on the way serves the old copy. $bust = aiprofile_inside_bust(); if ( $bust ) { $url .= '&v=' . $bust; } $response = wp_safe_remote_get( $url, aiprofile_inside_http_args() ); if ( is_wp_error( $response ) ) { return array( 'status' => 'error', 'detail' => $response->get_error_message(), ); } $code = (int) wp_remote_retrieve_response_code( $response ); if ( 404 === $code || 400 === $code ) { // Authoritative: the profile does not exist, is archived, or has no answers. return array( 'status' => 'not_found', 'detail' => (string) $code, ); } $body = (string) wp_remote_retrieve_body( $response ); if ( 200 !== $code || false === strpos( $body, '

'error', 'detail' => 'HTTP ' . $code, ); } $jsonld = null; if ( preg_match( '##s', $body, $m ) ) { $decoded = json_decode( $m[1], true ); if ( is_array( $decoded ) ) { $jsonld = $decoded; } } return array( 'status' => 'ok', 'html' => aiprofile_inside_kses( $body ), 'jsonld' => $jsonld, 'detail' => (string) $code, ); } /** * Remember a cache key so uninstall and a ping can remove it even with an * object cache. * * @param string $cache_key Transient name. */ function aiprofile_inside_remember_key( $cache_key ) { $keys = get_option( AIPROFILE_INSIDE_KEYS_OPTION, array() ); if ( ! is_array( $keys ) ) { $keys = array(); } if ( in_array( $cache_key, $keys, true ) ) { return; } $keys[] = $cache_key; update_option( AIPROFILE_INSIDE_KEYS_OPTION, array_slice( $keys, -500 ), false ); } /** Drop the fresh cached copy of every profile; the last good copies stay. */ function aiprofile_inside_clear_fresh_cache() { $keys = get_option( AIPROFILE_INSIDE_KEYS_OPTION, array() ); foreach ( is_array( $keys ) ? $keys : array() as $cache_key ) { delete_transient( (string) $cache_key ); } } /** * The transient name for a key and language. Not keyed by origin: a wrong * AIPROFILE_INSIDE_ORIGIN keeps the last good copy. * * @param string $key Profile key. * @param string $lang Language. * @return string */ function aiprofile_inside_cache_key( $key, $lang ) { return 'aiprofile_inside_' . md5( $key . '|' . $lang ); } /** * The copy we keep of a successful fetch. Since 1.3.2 it says which profile * and language it is, so the twice-daily check (includes/page-cache.php) * knows what to fetch again. * * @param string $key Profile key. * @param string $lang Language. * @param array $result A fetch result with status ok. * @return array */ function aiprofile_inside_payload( $key, $lang, $result ) { return array( 'html' => $result['html'], 'jsonld' => $result['jsonld'], 'fetched_at' => time(), 'stale' => false, 'key' => $key, 'lang' => $lang, ); } /** * The fragment for a key and language, from cache or from AiProfile. * * @param string $key Profile key. * @param string $lang Language. * @return array { html, jsonld, fetched_at, stale, key, lang } or an empty array. */ function aiprofile_inside_get( $key, $lang ) { $cache_key = aiprofile_inside_cache_key( $key, $lang ); $good_key = $cache_key . '_good'; $cached = get_transient( $cache_key ); if ( is_array( $cached ) ) { return $cached; } aiprofile_inside_remember_key( $cache_key ); $result = aiprofile_inside_fetch( $key, $lang ); $now = time(); if ( 'ok' === $result['status'] ) { $payload = aiprofile_inside_payload( $key, $lang, $result ); set_transient( $cache_key, $payload, AIPROFILE_INSIDE_FRESH_TTL ); set_transient( $good_key, $payload, AIPROFILE_INSIDE_GOOD_TTL ); $served = 'fresh'; } elseif ( 'not_found' === $result['status'] ) { $payload = array(); set_transient( $cache_key, $payload, AIPROFILE_INSIDE_FRESH_TTL ); delete_transient( $good_key ); $served = 'nothing (not found)'; } else { $good = get_transient( $good_key ); if ( is_array( $good ) && ! empty( $good['html'] ) ) { $payload = $good; $payload['stale'] = true; $served = 'last good copy'; } else { $payload = array(); $served = 'nothing (no copy yet)'; } set_transient( $cache_key, $payload, AIPROFILE_INSIDE_RETRY_TTL ); } update_option( AIPROFILE_INSIDE_STATUS_OPTION, array( 'time' => $now, 'key' => $key, 'lang' => $lang, 'status' => $result['status'], 'detail' => isset( $result['detail'] ) ? substr( (string) $result['detail'], 0, 200 ) : '', 'served' => $served, ), false ); return $payload; } /** * [aiprofile] or [aiprofile profiel="cmot2owhi1peqp2cpww02jr8v" taal="nl"] * * The page the wizard makes holds the bare [aiprofile]: the profile connected * in Settings, AiProfile. The attributes are the advanced route: * * profiel (or profile, or id): which AiProfile to show. The business id, the * owner's short handle (aiprofile.com/), or the profile's URL * or path such as /nl/netherlands/amsterdam/cafe-de-jaren. * taal (or lang): optional; defaults to the site's language when the profile * has it, then the profile's home language. * titel (or title): "ja" adds our own heading ("Veelgestelde vragen over * ...") as an h2. Off by default: the page's title is the heading. * jsonld: "0" leaves out the FAQPage structured data (for example when an SEO * plugin already prints it). Also the `aiprofile_inside_print_jsonld` * filter. * * @param array|string $atts Shortcode attributes. * @return string HTML. */ function aiprofile_inside_shortcode( $atts ) { $atts = shortcode_atts( array( 'profiel' => '', 'profile' => '', 'id' => '', 'taal' => '', 'lang' => '', 'titel' => '', 'title' => '', 'jsonld' => '1', ), $atts, 'aiprofile' ); $raw = '' !== $atts['profiel'] ? $atts['profiel'] : ( '' !== $atts['profile'] ? $atts['profile'] : $atts['id'] ); $settings = aiprofile_inside_settings(); $connected = '' === trim( (string) $raw ); if ( $connected ) { // [aiprofile] on its own: the profile connected in Settings, AiProfile. $raw = isset( $settings['key'] ) ? $settings['key'] : ''; } $key = aiprofile_inside_normalize_key( $raw ); if ( '' === $key ) { return ''; } $lang = aiprofile_inside_lang( '' !== $atts['taal'] ? $atts['taal'] : $atts['lang'], $key ); $data = aiprofile_inside_get( $key, $lang ); if ( empty( $data['html'] ) ) { return ''; } $with_title = aiprofile_inside_truthy( '' !== $atts['titel'] ? $atts['titel'] : $atts['title'] ); $html = aiprofile_inside_headings( aiprofile_inside_kses( $data['html'] ), $with_title ); $html = aiprofile_inside_credit( $html, (bool) aiprofile_inside_option( 'credit' ) ); $html = aiprofile_inside_stamp( $html ); $out = '
' . $html; aiprofile_inside_enqueue_style(); $print_jsonld = apply_filters( 'aiprofile_inside_print_jsonld', '0' !== (string) $atts['jsonld'], $key, $lang ); if ( $print_jsonld && ! empty( $data['jsonld'] ) && is_array( $data['jsonld'] ) ) { // Re-encoded from the decoded data, never the raw text: JSON_HEX_TAG // writes < and > as unicode escapes, so nothing can close the tag. $json = wp_json_encode( $data['jsonld'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP ); if ( is_string( $json ) ) { $out .= ''; } } $out .= '
'; // The question form only under the connected profile: the site is // registered for that one. if ( $connected && function_exists( 'aiprofile_inside_question_form' ) ) { $out .= aiprofile_inside_question_form( isset( $settings['name'] ) ? (string) $settings['name'] : '' ); } if ( ! empty( $data['stale'] ) && ! empty( $data['fetched_at'] ) ) { $out .= ''; } return $out; } add_shortcode( 'aiprofile', 'aiprofile_inside_shortcode' ); /** After activation, open the "Connect your AiProfile" screen once. */ function aiprofile_inside_activate() { set_transient( 'aiprofile_inside_activation_redirect', get_current_user_id(), 5 * MINUTE_IN_SECONDS ); } register_activation_hook( __FILE__, 'aiprofile_inside_activate' ); require_once __DIR__ . '/includes/site-link.php'; require_once __DIR__ . '/includes/schema.php'; require_once __DIR__ . '/includes/robots.php'; require_once __DIR__ . '/includes/form.php'; require_once __DIR__ . '/includes/footer.php'; require_once __DIR__ . '/includes/page-cache.php'; // Only in the ZIP for direct installs, not in the WordPress.org build. if ( is_readable( __DIR__ . '/includes/languages.php' ) ) { require_once __DIR__ . '/includes/languages.php'; } if ( is_admin() ) { require_once __DIR__ . '/includes/admin.php'; }