" 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. Since 1.3.7 * "none" also offers "Make my AiProfile" (a link to the build on AiProfile) * and "I made it, look again". * * 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; the one exception is * instant updates, whose box on the Connect form starts ticked (since 1.4.2). * * Since 1.4.2 the screen also asks the owner to turn on WordPress's own * automatic updates for this plugin. Only the owner's click does that. * * @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( '/' ), ), ) ) ); $failed = true; 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; $failed = false; 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; } /** * "I made it, look again" (since 1.3.7): after "Make my AiProfile" the owner * comes back, and the screen asks AiProfile again instead of showing the * cached "none" for up to ten minutes. */ function aiprofile_inside_handle_look_again() { aiprofile_inside_require_admin(); check_admin_referer( 'aiprofile_inside_look_again' ); $failed = false; $matches = aiprofile_inside_detect( true, $failed ); $args = array(); if ( $failed ) { $args['msg'] = 'unreachable'; } elseif ( empty( $matches ) ) { $args['msg'] = 'stillnone'; } wp_safe_redirect( aiprofile_inside_admin_url( $args ) ); exit; } add_action( 'admin_post_aiprofile_inside_look_again', 'aiprofile_inside_handle_look_again' ); /** "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 choices on the found screen: instant updates (ticked by default // since 1.4.2), (since 1.4.0) the visit reports and (since 1.4.1) the link // to the AiProfile, both unticked by default. Connect with the link box on // or off is the owner's answer, so the separate question after Connect is // not shown (credit_asked). $settings['instant'] = ! empty( $_POST['aiprofile_instant'] ); $settings['visits'] = ! empty( $_POST['aiprofile_visits'] ); $settings['credit'] = ! empty( $_POST['aiprofile_credit'] ); $settings['credit_asked'] = true; // Stored before the first content request below, so that request already // reports the owner's choice to AiProfile (X-AiProfile-Credit, 1.3.5). update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $settings, false ); // The language the answers come in (since 1.4.0), for the page title and // the footer link's text; kept up to date by aiprofile_inside_get(). // No X-AiProfile-Page while connecting: the page is not settled yet. And // no rename from that content request: Connect renames after its final // save below, so nothing writes the settings behind its back. aiprofile_inside_skip_page_header( true ); aiprofile_inside_connecting( true ); $settings['answers_lang'] = aiprofile_inside_answers_lang( $settings['key'], true ); // The site's own language (since 1.4.1): the answers' language may follow // a fetch in this language later, never one in another. $site_locale = (string) get_option( 'WPLANG' ); $settings['site_lang'] = aiprofile_inside_lang( '' !== $site_locale ? $site_locale : 'en_US', '' ); // The page title and footer alt text AiProfile sent with the answers // (since 1.4.1, AIP-703), from the copy the fetch above made or found. $texts = aiprofile_inside_kept_texts( $settings['key'] ); if ( ! empty( $texts ) ) { $settings['texts'] = $texts; } else { unset( $settings['texts'] ); } $page = aiprofile_inside_ensure_page( $settings ); aiprofile_inside_skip_page_header( false ); $settings['page_id'] = $page['id']; $settings['adopted'] = $page['adopted']; if ( ! empty( $page['created'] ) ) { // The title the plugin gave the new page, so a later title from // AiProfile may follow it (aiprofile_inside_retitle_page()). $settings['title_set'] = get_post_field( 'post_title', $page['id'], 'raw' ); } 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() ); aiprofile_inside_connecting( false ); // A page made before (connecting again, maybe with another profile) gets // AiProfile's title too, while its title is still one the plugin gave it. // After the final save, so the title it records (title_set) stays. aiprofile_inside_retitle_page( aiprofile_inside_settings() ); if ( $page['id'] ) { // A new page, or one that was not public before: into the sitemap search engines get. aiprofile_inside_refresh_sitemaps(); } // 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, the question form or the visit reports are on, forget it * when all three 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'] ); $settings['visits'] = ! empty( $_POST['aiprofile_visits'] ); $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; // The page layout (since 1.3.6): only a change of the box counts, so a // save leaves the plugin's own choice (auto) alone. if ( isset( $_POST['aiprofile_layout_shown'] ) ) { $shown = ! empty( $_POST['aiprofile_layout_shown'] ); $wanted = ! empty( $_POST['aiprofile_layout'] ); if ( $wanted !== $shown ) { $settings['layout'] = $wanted ? 'plugin' : 'theme'; } } update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $settings, false ); if ( ! $settings['visits'] ) { // Switched off: what still waits for WP-Cron is not sent. aiprofile_inside_clear_visit_queue(); } 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 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, created: bool (a new page) } */ 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, ); } // Since 1.4.0 "AiProfile of ", in the language of the answers // (includes/answers-page.php). The slug comes from WordPress, once. // Since 1.4.1 the title AiProfile sends ("AiProfile van , in // "), else the plugin's own. $title = ! empty( $settings['texts']['pageTitle'] ) ? (string) $settings['texts']['pageTitle'] : aiprofile_inside_answers_page_title( ! empty( $settings['answers_lang'] ) ? (string) $settings['answers_lang'] : aiprofile_inside_answers_lang( isset( $settings['key'] ) ? $settings['key'] : '', true ), isset( $settings['name'] ) ? (string) $settings['name'] : '' ); $id = wp_insert_post( wp_slash( array( 'post_type' => 'page', 'post_status' => 'publish', 'post_title' => $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, 'created' => ! is_wp_error( $id ) && $id, ); } /** * 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' ); /** * The plugin's entry in WordPress's list of plugins that update by * themselves (since 1.4.2): aiprofile-inside/aiprofile-inside.php. * * @return string */ function aiprofile_inside_plugin_file() { return plugin_basename( AIPROFILE_INSIDE_FILE ); } /** * Whether WordPress's own automatic updates are on for this plugin. * * @return bool */ function aiprofile_inside_auto_update_on() { return in_array( aiprofile_inside_plugin_file(), (array) get_site_option( 'auto_update_plugins', array() ), true ); } /** * Whether a filter forces automatic updates on or off for this plugin, the * way the Plugins screen asks it: null when nothing forces them. * * @return bool|null */ function aiprofile_inside_auto_update_forced() { $file = aiprofile_inside_plugin_file(); $item = (object) array( 'id' => $file, 'slug' => 'aiprofile-inside', 'plugin' => $file, 'new_version' => '', 'url' => '', 'package' => '', ); if ( function_exists( 'wp_is_auto_update_forced_for_item' ) ) { return wp_is_auto_update_forced_for_item( 'plugin', null, $item ); } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress core's own filter, read as core reads it. return apply_filters( 'auto_update_plugin', null, $item ); } /** * Whether to ask the owner to turn on automatic updates (since 1.4.2): only * on a single site (on multisite the setting is network-wide and belongs in * network admin, never on a subsite's page), when WordPress allows them for * plugins, they are not on yet, nothing forces them on or off, the owner did * not say no, and this user may change them. * * @return bool */ function aiprofile_inside_can_ask_auto_update() { if ( is_multisite() ) { return false; } $settings = aiprofile_inside_settings(); if ( ! empty( $settings['auto_update_dismissed'] ) ) { return false; } if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED ) { return false; } if ( ! function_exists( 'wp_is_auto_update_enabled_for_type' ) || ! wp_is_auto_update_enabled_for_type( 'plugin' ) ) { return false; } if ( ! current_user_can( 'update_plugins' ) ) { return false; } if ( aiprofile_inside_auto_update_on() ) { return false; } return null === aiprofile_inside_auto_update_forced(); } /** * The automatic updates card (since 1.4.2): "Turn on automatic updates" adds * this plugin to the auto_update_plugins site option, with the same option * and value format as core's toggle on the Plugins screen (plugin basenames, * deleted plugins dropped); "No thanks" hides the card for good. Never on * multisite, where that option is network-wide. */ function aiprofile_inside_handle_auto_update() { aiprofile_inside_require_admin(); check_admin_referer( 'aiprofile_inside_auto_update' ); $op = isset( $_POST['aiprofile_op'] ) ? sanitize_key( wp_unslash( $_POST['aiprofile_op'] ) ) : ''; $args = array(); if ( 'dismiss' === $op ) { $settings = aiprofile_inside_settings(); $settings['auto_update_dismissed'] = true; update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $settings, false ); } elseif ( 'on' === $op ) { if ( is_multisite() || ! current_user_can( 'update_plugins' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'aiprofile-inside' ), 403 ); } if ( aiprofile_inside_can_ask_auto_update() ) { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $list = (array) get_site_option( 'auto_update_plugins', array() ); $list[] = aiprofile_inside_plugin_file(); // As core does: drop entries of plugins that are no longer installed. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress core's own filter, applied as core's toggle applies it. $installed = array_keys( apply_filters( 'all_plugins', get_plugins() ) ); update_site_option( 'auto_update_plugins', array_values( array_intersect( array_unique( $list ), $installed ) ) ); $args['done'] = 'auto_update_on'; } } wp_safe_redirect( aiprofile_inside_admin_url( $args ) ); exit; } add_action( 'admin_post_aiprofile_inside_auto_update', 'aiprofile_inside_handle_auto_update' ); require_once __DIR__ . '/admin-screen.php';