" with Connect straight away, more matches a list to pick * from, none the paste field as before. Viewing the screen only caches that * answer; nothing is pending until the owner clicks Connect. * * One field: the owner's AiProfile link. "Check" asks AiProfile whether the * profile exists and shows its name; "Connect" then makes the page with the * answers (or reuses the one it made before, or a page that already holds the * shortcode) and puts a link "AiProfile" to it in the site's footer * (includes/footer.php; up to 1.2.x it went into the main menu). Nothing is * created twice. * * Once connected, the same screen holds the settings (since 1.2.0): instant * updates, the question form, the AiProfile link, LocalBusiness structured * data, /llms.txt, and the check whether AI crawlers may read the site. Every * switch that sends more to AiProfile than the content request says so next * to it, and is off until the owner switches it on. * * @package aiprofile-inside */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** The screen's slug. */ define( 'AIPROFILE_INSIDE_PAGE_SLUG', 'aiprofile-inside' ); /** * URL of the screen, with optional query args. * * @param array $args Query args. * @return string */ function aiprofile_inside_admin_url( $args = array() ) { return add_query_arg( array_merge( array( 'page' => AIPROFILE_INSIDE_PAGE_SLUG ), $args ), admin_url( 'options-general.php' ) ); } /** The screen under Settings. */ function aiprofile_inside_admin_menu() { add_options_page( __( 'Connect your AiProfile', 'aiprofile-inside' ), 'AiProfile', 'manage_options', AIPROFILE_INSIDE_PAGE_SLUG, 'aiprofile_inside_render_screen' ); } add_action( 'admin_menu', 'aiprofile_inside_admin_menu' ); /** * "Connect your AiProfile" next to Deactivate on the Plugins screen. * * @param array $links Action links. * @return array */ function aiprofile_inside_action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Connect your AiProfile', 'aiprofile-inside' ) . '' ); return $links; } add_filter( 'plugin_action_links_' . plugin_basename( AIPROFILE_INSIDE_FILE ), 'aiprofile_inside_action_links' ); /** Right after activation, open the screen once (not after a bulk activation). */ function aiprofile_inside_activation_redirect() { $user_id = get_transient( 'aiprofile_inside_activation_redirect' ); if ( false === $user_id ) { return; } if ( wp_doing_ajax() || is_network_admin() || (int) $user_id !== get_current_user_id() ) { return; } delete_transient( 'aiprofile_inside_activation_redirect' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only reads which screen this is. if ( isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_options' ) ) { return; } $settings = aiprofile_inside_settings(); if ( ! empty( $settings['key'] ) ) { return; } wp_safe_redirect( aiprofile_inside_admin_url() ); exit; } add_action( 'admin_init', 'aiprofile_inside_activation_redirect' ); /** * The site's language as AiProfile knows it (nl, en, ...). * * @return string */ function aiprofile_inside_site_lang() { return aiprofile_inside_lang( '', '' ); } /** Transient name for the profile a user checked but has not connected yet. */ function aiprofile_inside_pending_key() { return 'aiprofile_inside_pending_' . get_current_user_id(); } /** * Stop unless the user is an admin. Each handler checks its own nonce right * after this, with check_admin_referer(). */ function aiprofile_inside_require_admin() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'aiprofile-inside' ), 403 ); } } /** Step 1: check the pasted link against AiProfile. */ function aiprofile_inside_handle_check() { aiprofile_inside_require_admin(); check_admin_referer( 'aiprofile_inside_check' ); $link = isset( $_POST['aiprofile_link'] ) ? sanitize_text_field( wp_unslash( $_POST['aiprofile_link'] ) ) : ''; $key = aiprofile_inside_normalize_key( $link ); if ( '' === $key ) { wp_safe_redirect( aiprofile_inside_admin_url( array( 'msg' => 'empty', 'change' => 1 ) ) ); exit; } $lang = aiprofile_inside_site_lang(); $result = aiprofile_inside_fetch( $key, $lang ); if ( 'ok' !== $result['status'] ) { $msg = 'not_found' === $result['status'] ? 'notfound' : 'unreachable'; wp_safe_redirect( aiprofile_inside_admin_url( array( 'msg' => $msg, 'change' => 1 ) ) ); exit; } set_transient( aiprofile_inside_pending_key(), array( 'key' => $key, 'name' => aiprofile_inside_name_from_html( $result['html'] ), 'count' => substr_count( $result['html'], ' array( 'Accept' => 'application/json', 'Referer' => home_url( '/' ), ), ) ) ); if ( ! is_wp_error( $response ) && 200 === (int) wp_remote_retrieve_response_code( $response ) ) { $body = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( is_array( $body ) && isset( $body['matches'] ) && is_array( $body['matches'] ) ) { $ttl = 10 * MINUTE_IN_SECONDS; foreach ( array_slice( $body['matches'], 0, 5 ) as $match ) { if ( ! is_array( $match ) ) { continue; } $key = aiprofile_inside_normalize_key( isset( $match['key'] ) ? (string) $match['key'] : '' ); $count = isset( $match['answers'] ) ? absint( $match['answers'] ) : 0; if ( '' === $key || $count < 1 ) { continue; } $matches[] = array( 'key' => $key, 'name' => isset( $match['name'] ) ? sanitize_text_field( (string) $match['name'] ) : '', 'count' => $count, 'url' => isset( $match['profileUrl'] ) ? esc_url_raw( (string) $match['profileUrl'], array( 'https' ) ) : '', ); } } } set_transient( $cache_key, $matches, $ttl ); return $matches; } /** * One of the profiles found for this site's address, by its key. * * @param string $key Profile key. * @return array|null { key, name, count, url }, or null when it is not one of them. */ function aiprofile_inside_detected_match( $key ) { foreach ( aiprofile_inside_detect() as $match ) { if ( $match['key'] === $key ) { return $match; } } return null; } /** "Use another link": forget the checked profile. */ function aiprofile_inside_handle_cancel() { aiprofile_inside_require_admin(); check_admin_referer( 'aiprofile_inside_cancel' ); delete_transient( aiprofile_inside_pending_key() ); wp_safe_redirect( aiprofile_inside_admin_url( array( 'change' => 1 ) ) ); exit; } add_action( 'admin_post_aiprofile_inside_cancel', 'aiprofile_inside_handle_cancel' ); /** Step 2: connect. Save the profile, make or reuse the page, link to it from the footer. */ function aiprofile_inside_handle_connect() { aiprofile_inside_require_admin(); check_admin_referer( 'aiprofile_inside_connect' ); // Since 1.3.3 the screen can offer a profile it found by the site's own // address: then the form names it, and it counts only when it is one of // the profiles AiProfile has for this address. Otherwise: the profile the // owner checked with a pasted link. $picked = isset( $_POST['aiprofile_key'] ) ? aiprofile_inside_normalize_key( sanitize_text_field( wp_unslash( $_POST['aiprofile_key'] ) ) ) : ''; if ( '' !== $picked ) { $pending = aiprofile_inside_detected_match( $picked ); if ( null === $pending ) { wp_safe_redirect( aiprofile_inside_admin_url( array( 'msg' => 'notfound', 'change' => 1 ) ) ); exit; } } else { $pending = get_transient( aiprofile_inside_pending_key() ); } if ( ! is_array( $pending ) || empty( $pending['key'] ) ) { wp_safe_redirect( aiprofile_inside_admin_url( array( 'change' => 1 ) ) ); exit; } $settings = aiprofile_inside_settings(); $settings['key'] = $pending['key']; $settings['name'] = $pending['name']; // The one choice on the found screen: instant updates, unticked by default. $settings['instant'] = ! empty( $_POST['aiprofile_instant'] ); $page = aiprofile_inside_ensure_page( $settings ); $settings['page_id'] = $page['id']; $settings['adopted'] = $page['adopted']; if ( $page['id'] ) { // A page made by hand may name a profile in its shortcode; that would // win over the connected one, so the page follows the connection. aiprofile_inside_unpin_page( $page['id'] ); } // Since 1.3.0 a link "AiProfile" in the footer, not the main menu. A site // whose page an older version put in the main menu keeps it there until // the owner moves it; a link the owner took out of the footer stays out. $footer = isset( $settings['footer']['status'] ) ? $settings['footer']['status'] : ''; if ( $page['id'] && ! aiprofile_inside_in_header_menu( $settings ) && 'removed' !== $footer ) { $settings['footer'] = aiprofile_inside_ensure_footer_link( $page['id'] ); } update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $settings, false ); delete_transient( aiprofile_inside_pending_key() ); // Show the new profile at once: drop the fresh copies of every profile // (old and new, all languages; the last good copies stay) and the page cache. aiprofile_inside_clear_fresh_cache(); aiprofile_inside_purge_block_pages(); // Register (again: the profile may be another one) only when the owner // asked for instant updates or the question form. aiprofile_inside_sync_link( true ); wp_safe_redirect( aiprofile_inside_admin_url( array( 'connected' => 1 ) ) ); exit; } add_action( 'admin_post_aiprofile_inside_connect', 'aiprofile_inside_handle_connect' ); /** * Bring the link with AiProfile in line with the switches: register when * instant updates or the question form is on, forget it when both are off. * * @param bool $again Register even when already registered (another profile). */ function aiprofile_inside_sync_link( $again = false ) { $site = aiprofile_inside_site(); if ( aiprofile_inside_wants_link() ) { $settings = aiprofile_inside_settings(); $current = ! empty( $site['secret'] ) && isset( $site['key'], $settings['key'] ) && $site['key'] === $settings['key']; if ( $again || ! $current || empty( $site['verified_at'] ) ) { aiprofile_inside_register_site(); } } elseif ( ! empty( $site['secret'] ) ) { aiprofile_inside_unregister_site(); } } /** The settings form: every switch at once. */ function aiprofile_inside_handle_settings() { aiprofile_inside_require_admin(); check_admin_referer( 'aiprofile_inside_settings' ); $settings = aiprofile_inside_settings(); $settings['instant'] = ! empty( $_POST['aiprofile_instant'] ); $settings['questions'] = ! empty( $_POST['aiprofile_questions'] ); $settings['credit'] = ! empty( $_POST['aiprofile_credit'] ); $settings['llms'] = ! empty( $_POST['aiprofile_llms'] ); $settings['robots_fix'] = ! empty( $_POST['aiprofile_robots_fix'] ); $schema = isset( $_POST['aiprofile_schema'] ) ? sanitize_key( wp_unslash( $_POST['aiprofile_schema'] ) ) : 'faq'; $settings['schema'] = in_array( $schema, array( 'off', 'faq', 'site' ), true ) ? $schema : 'faq'; $settings['credit_asked'] = true; update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $settings, false ); aiprofile_inside_sync_link(); // The page shows the link, the form or neither from now on. aiprofile_inside_clear_fresh_cache(); aiprofile_inside_purge_block_pages(); wp_safe_redirect( aiprofile_inside_admin_url( array( 'saved' => 1 ) ) ); exit; } add_action( 'admin_post_aiprofile_inside_settings', 'aiprofile_inside_handle_settings' ); /** The one question after connecting: link to your AiProfile? Asked once. */ function aiprofile_inside_handle_credit() { aiprofile_inside_require_admin(); check_admin_referer( 'aiprofile_inside_credit' ); $settings = aiprofile_inside_settings(); $settings['credit'] = ! empty( $_POST['aiprofile_credit'] ); $settings['credit_asked'] = true; update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $settings, false ); aiprofile_inside_clear_fresh_cache(); aiprofile_inside_purge_block_pages(); wp_safe_redirect( aiprofile_inside_admin_url( array( 'saved' => 1 ) ) ); exit; } add_action( 'admin_post_aiprofile_inside_credit', 'aiprofile_inside_handle_credit' ); /** * Rewrite every [aiprofile ...] on the page that names a profile (profiel, * profile or id) to one without, so it shows the connected profile. Other * attributes (taal, titel, jsonld) stay. Nothing else on the page changes. * * @param int $page_id The page. * @return bool Whether the page was changed. */ function aiprofile_inside_unpin_page( $page_id ) { $post = get_post( $page_id ); if ( ! $post || '' === $post->post_content ) { return false; } $content = preg_replace_callback( '/' . get_shortcode_regex( array( 'aiprofile' ) ) . '/', 'aiprofile_inside_unpin_shortcode', $post->post_content ); if ( ! is_string( $content ) || $content === $post->post_content ) { return false; } // The content was already stored by the owner; write it back as it is, // with only the shortcode changed, so kses cannot strip anything of his. $had_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ); kses_remove_filters(); $updated = wp_update_post( wp_slash( array( 'ID' => $page_id, 'post_content' => $content, ) ), true ); if ( $had_kses ) { kses_init_filters(); } return ! is_wp_error( $updated ) && $updated; } /** * One shortcode match: drop profiel/profile/id, keep the rest. * * @param array $m Match from get_shortcode_regex(). * @return string */ function aiprofile_inside_unpin_shortcode( $m ) { if ( '[' === $m[1] && ']' === $m[6] ) { return $m[0]; // [[aiprofile]] is an escaped, literal shortcode. } $atts = shortcode_parse_atts( $m[3] ); if ( ! is_array( $atts ) ) { return $m[0]; } $kept = ''; $changed = false; foreach ( $atts as $name => $value ) { if ( in_array( $name, array( 'profiel', 'profile', 'id' ), true ) ) { $changed = true; continue; } $value = str_replace( array( '"', '[', ']' ), '', (string) $value ); $kept .= is_int( $name ) ? ' ' . $value : ' ' . $name . '="' . $value . '"'; } if ( ! $changed ) { return $m[0]; } return $m[1] . '[aiprofile' . $kept . ']' . ( '' !== $m[5] ? $m[5] . '[/aiprofile]' : '' ) . $m[6]; } /** * The page title in the site's language (not the admin's own language). * * @return string */ function aiprofile_inside_page_title() { $switched = switch_to_locale( get_locale() ); $title = __( 'Frequently asked questions', 'aiprofile-inside' ); if ( $switched ) { restore_previous_locale(); } return $title; } /** * The page that shows the answers: the one we made before, else a page that * already holds the shortcode (made by hand), else a new published page. * * @param array $settings Current settings. * @return array { id: int, adopted: bool } */ function aiprofile_inside_ensure_page( $settings ) { if ( ! empty( $settings['page_id'] ) ) { $status = get_post_status( (int) $settings['page_id'] ); if ( $status && 'trash' !== $status && 'page' === get_post_type( (int) $settings['page_id'] ) ) { return array( 'id' => (int) $settings['page_id'], 'adopted' => ! empty( $settings['adopted'] ), ); } } global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- one lookup at connect time. $existing = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'page' AND post_status IN ('publish','private','draft','future','pending') AND ( post_content LIKE %s OR post_content LIKE %s ) ORDER BY ( post_status = 'publish' ) DESC, ID ASC LIMIT 1", '%' . $wpdb->esc_like( '[aiprofile]' ) . '%', '%' . $wpdb->esc_like( '[aiprofile ' ) . '%' ) ); if ( $existing ) { return array( 'id' => $existing, 'adopted' => true, ); } $id = wp_insert_post( wp_slash( array( 'post_type' => 'page', 'post_status' => 'publish', 'post_title' => aiprofile_inside_page_title(), 'post_content' => "\n[aiprofile]\n", 'comment_status' => 'closed', 'ping_status' => 'closed', ) ), true ); return array( 'id' => is_wp_error( $id ) ? 0 : (int) $id, 'adopted' => false, ); } /** * Did the page go into the main menu by an older version (1.2.x)? Those * sites keep it there until the owner moves it. * * @param array $settings Current settings. * @return bool */ function aiprofile_inside_in_header_menu( $settings ) { return isset( $settings['menu']['status'] ) && in_array( $settings['menu']['status'], array( 'added', 'exists', 'page-list' ), true ); } /** * Classic theme: the main menu as 1.2.x picked it, the menu in the primary * location (or the first location with a menu). * * @return WP_Term|null */ function aiprofile_inside_header_classic_menu() { $locations = get_nav_menu_locations(); $order = array( 'primary', 'main', 'main-menu', 'primary-menu', 'header', 'header-menu', 'menu-1', 'top' ); $keys = array_unique( array_merge( array_intersect( $order, array_keys( $locations ) ), array_keys( $locations ) ) ); foreach ( $keys as $location ) { if ( ! empty( $locations[ $location ] ) ) { $menu = wp_get_nav_menu_object( $locations[ $location ] ); if ( $menu ) { return $menu; } } } return null; } /** * Take the answers page out of the main menu: only the one item a 1.2.x * version added there itself, nothing the owner put in his menu. * * @param int $page_id The answers page. * @param array $menu The stored menu state of 1.2.x. * @return bool Whether the item was found and removed. */ function aiprofile_inside_remove_header_item( $page_id, $menu ) { if ( ! isset( $menu['status'] ) || 'added' !== $menu['status'] ) { return false; } if ( isset( $menu['theme'] ) && 'block' === $menu['theme'] ) { // 1.2.x used WordPress's own fallback menu when the header had no ref. $nav_id = aiprofile_inside_part_nav_id( 'header', true ); return $nav_id && aiprofile_inside_nav_remove( $nav_id, function ( $block ) use ( $page_id ) { return isset( $block['blockName'] ) && 'core/navigation-link' === $block['blockName'] && isset( $block['attrs']['id'], $block['attrs']['kind'] ) && (int) $block['attrs']['id'] === (int) $page_id && 'post-type' === $block['attrs']['kind'] && ! aiprofile_inside_is_footer_block( $block ); }, true ); } $header = aiprofile_inside_header_classic_menu(); if ( ! $header ) { return false; } $found = 0; foreach ( (array) wp_get_nav_menu_items( $header->term_id, array( 'post_status' => 'any' ) ) as $item ) { // The last one: 1.2.x added its item at the end. if ( 'page' === $item->object && (int) $item->object_id === (int) $page_id && ! aiprofile_inside_is_footer_item( $item ) ) { $found = (int) $item->ID; } } return $found && wp_delete_post( $found, true ); } /** * The footer link buttons: put it in, take it out, or move the page from the * main menu (1.2.x) to the footer. */ function aiprofile_inside_handle_footer() { aiprofile_inside_require_admin(); check_admin_referer( 'aiprofile_inside_footer' ); $settings = aiprofile_inside_settings(); $page_id = isset( $settings['page_id'] ) ? (int) $settings['page_id'] : 0; $op = isset( $_POST['aiprofile_op'] ) ? sanitize_key( wp_unslash( $_POST['aiprofile_op'] ) ) : ''; $done = ''; if ( 'remove' === $op ) { $settings['footer'] = aiprofile_inside_remove_footer_link( aiprofile_inside_footer_state() ); $done = 'footer_removed'; } elseif ( $page_id && in_array( $op, array( 'add', 'move' ), true ) ) { if ( 'move' === $op && aiprofile_inside_in_header_menu( $settings ) ) { aiprofile_inside_remove_header_item( $page_id, $settings['menu'] ); $settings['menu'] = array( 'status' => 'moved' ); } $settings['footer'] = aiprofile_inside_ensure_footer_link( $page_id ); $done = 'move' === $op ? 'footer_moved' : 'footer_added'; } update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $settings, false ); wp_safe_redirect( aiprofile_inside_admin_url( array( 'done' => $done ) ) . '#aiprofile-footer' ); exit; } add_action( 'admin_post_aiprofile_inside_footer', 'aiprofile_inside_handle_footer' ); /** The robots.txt fix: the owner's click turns it on, or off again. */ function aiprofile_inside_handle_robots() { aiprofile_inside_require_admin(); check_admin_referer( 'aiprofile_inside_robots' ); $settings = aiprofile_inside_settings(); $settings['robots_fix'] = empty( $_POST['aiprofile_undo'] ); update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $settings, false ); wp_safe_redirect( aiprofile_inside_admin_url( array( 'saved' => 1 ) ) . '#aiprofile-ai-check' ); exit; } add_action( 'admin_post_aiprofile_inside_robots', 'aiprofile_inside_handle_robots' ); require_once __DIR__ . '/admin-screen.php';