rmdir( $dir ); } delete_option( 'velischat_context_file' ); delete_option( 'velischat_context_dir_moved' ); // The size shown in "Instructions and content" was the PDF's $generated = get_option( 'velischat_context_generated', false ); if( is_array( $generated ) ){ $generated[ 'size' ] = strlen( (string) get_option( 'velischat_context_text', '' ) ); update_option( 'velischat_context_generated', $generated, false ); } } add_action( 'admin_init', 'velischat_remove_context_pdf' ); function velischat_get_custom_post_types(){ // Public post types registered by the theme or by plugins, keyed by name $types = get_post_types( array( 'public' => true, 'show_ui' => true, '_builtin' => false ), 'objects' ); // Page builder templates are public too, but they aren't content for the visitors $excluded = array( 'elementor_library', 'e-floating-buttons', 'et_pb_layout', 'fl-builder-template', 'brizy_template', 'ct_template', 'oxy_user_library', 'bricks_template', 'kadence_element', 'wpb_templates', 'wpb_gutenberg_param', 'salient_g_sections', 'nectar_slider', 'home_slider' ); $types = array_diff_key( $types, array_flip( $excluded ) ); // Add-ons that give the assistant some types in another way (e.g. a product catalog) take them out of the list return (array) apply_filters( 'velischat_context_custom_post_types', $types ); } function velischat_get_context_post_types(){ // Post types that can be used as chatbot content: posts, pages and the public types of the theme and plugins $types = array_merge( array( 'post', 'page' ), array_keys( velischat_get_custom_post_types() ) ); $types = (array) apply_filters( 'velischat_context_post_types', $types ); return array_values( array_unique( $types ) ); } function velischat_get_context_type_labels(){ // Singular name of each content type, used in the contents list and in the contents $labels = array(); foreach( velischat_get_context_post_types() as $name ){ $type = get_post_type_object( $name ); $labels[ $name ] = 'page' === $name ? __( 'Page', 'velischat' ) : ( $type ? $type->labels->singular_name : $name ); } return (array) apply_filters( 'velischat_context_type_labels', $labels ); } function velischat_is_shop_product_type( $type ){ // WooCommerce products: their prices, stock and variations aren't in their text, and change without the product // being edited, so the contents can't hold them return 'product' === $type && class_exists( 'WooCommerce' ); } function velischat_is_calendar_event_type( $type ){ // Events of The Events Calendar: their dates, times, venue, organizer and cost are kept apart from their text, so // the contents don't hold them return 'tribe_events' === $type && class_exists( 'Tribe__Events__Main' ); } function velischat_context_has_post_type( $type ){ // Whether content of this type is among the contents the assistant reads if( ! in_array( $type, velischat_get_context_post_types(), true ) ){ return false; } foreach( velischat_get_context_post_ids() as $id ){ if( get_post_type( $id ) === $type ){ return true; } } return false; } function velischat_get_context_type_instructions(){ // Instructions for each content type: posts and pages have their own option, the other types share an array $custom = get_option( 'velischat_content_cpt_instructions', array() ); $custom = is_array( $custom ) ? $custom : array(); $rules = array(); foreach( velischat_get_context_post_types() as $type ){ if( 'page' === $type ){ $rules[ $type ] = get_option( 'velischat_content_pages_instructions', '' ); } elseif( 'post' === $type ){ $rules[ $type ] = get_option( 'velischat_content_posts_instructions', '' ); } else { $rules[ $type ] = isset( $custom[ $type ] ) ? $custom[ $type ] : ''; } } $rules = (array) apply_filters( 'velischat_context_type_instructions', $rules ); return array_map( 'trim', array_map( 'strval', $rules ) ); } function velischat_get_context_type_markers(){ // Label written before each content title in the contents, so the AI can tell the content types apart $markers = array(); foreach( velischat_get_context_type_labels() as $name => $label ){ $markers[ $name ] = '[' . $label . ']'; } // Add-ons that put other kinds of content in the document (e.g. other files) add their label, keyed like the // sections they add and the rules of velischat_context_type_instructions return (array) apply_filters( 'velischat_context_type_markers', $markers ); } function velischat_get_context_written_markers(){ // The labels written in the current contents, saved with them: the labels are translated, and the instructions // are built in the visitor's language while the contents were built in the admin's one $markers = get_option( 'velischat_context_markers', false ); return is_array( $markers ) ? $markers + velischat_get_context_type_markers() : velischat_get_context_type_markers(); } function velischat_get_saved_context_post_ids(){ // The IDs of the posts selected as chatbot content, as saved $ids = get_option( 'velischat_content_post_ids', array() ); return is_array( $ids ) ? array_map( 'absint', $ids ) : array(); } function velischat_get_context_post_ids(){ // Return the IDs of the posts selected as chatbot content. On multilingual sites one version of each content, in // the main language when it was selected: the same text in more languages would only cost more return array_values( velischat_one_version_per_post( velischat_get_saved_context_post_ids() ) ); } function velischat_sanitize_post_ids( $ids ){ // Keep only valid, unique post IDs if( ! is_array( $ids ) ){ return array(); } return array_values( array_unique( array_filter( array_map( 'absint', $ids ) ) ) ); } function velischat_html_to_text( $html ){ // Turn rendered HTML into readable plain text, keeping paragraphs and list items $html = preg_replace( '#<(script|style)[^>]*>.*?#is', '', $html ); // Drop shortcode tags that weren't rendered, keeping the text they wrap $html = preg_replace( '#\[/?[a-z][a-z0-9_-]*(\s[^\]]*)?/?\]#i', '', $html ); $html = preg_replace( '#]*>#i', "\n- ", $html ); $html = preg_replace( '##i', "\n", $html ); $html = preg_replace( '##i', "\n\n", $html ); $html = preg_replace( '##i', " ", $html ); $text = wp_strip_all_tags( $html ); $text = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); $text = str_replace( "\xC2\xA0", ' ', $text ); $text = preg_replace( "/[ \t]+/", ' ', $text ); $text = preg_replace( "/ *\n */", "\n", $text ); $text = preg_replace( "/\n{3,}/", "\n\n", $text ); return trim( $text ); } function velischat_is_builder_text_key( $key ){ // Guess from a builder setting name whether it holds visible text (title, editor, tab_content...) or styling $key = strtolower( (string) $key ); if( $key === '' || $key[ 0 ] === '_' ){ return false; } $allowed = array( 'title', 'text', 'editor', 'content', 'description', 'desc', 'heading', 'subheading', 'subtitle', 'caption', 'name', 'job', 'label', 'question', 'answer', 'quote', 'author', 'price', 'period', 'feature', 'features', 'message', 'html', 'testimonial' ); $denied = array( 'color', 'size', 'tag', 'align', 'alignment', 'typography', 'font', 'family', 'weight', 'transform', 'decoration', 'class', 'css', 'id', 'url', 'link', 'icon', 'image', 'img', 'animation', 'position', 'width', 'height', 'spacing', 'margin', 'padding', 'border', 'radius', 'shadow', 'background', 'bg', 'view', 'layout', 'style', 'skin', 'type', 'hover', 'gap', 'display', 'show', 'hide', 'effect', 'duration', 'speed', 'direction', 'line', 'letter', 'unit', 'offset', 'mobile', 'tablet', 'laptop', 'widescreen', 'source', 'el', 'selected', 'library', 'opacity', 'filter', 'overlay', 'blend', 'z', 'index', 'responsive', 'target', 'rel', 'nofollow', 'external', 'attributes', 'custom', 'gradient', 'stroke', 'flex', 'justify', 'desktop', 'phone' ); $tokens = preg_split( '/[_\-]+/', $key ); return (bool) array_intersect( $tokens, $allowed ) && ! array_intersect( $tokens, $denied ); } function velischat_builder_value_to_text( $value ){ // Clean a single builder text value, discarding values that are clearly not prose $text = velischat_html_to_text( (string) $value ); // Switches and CSS keywords (alignment, positions) are settings, not text $settings = array( 'yes', 'no', 'true', 'false', 'default', 'on', 'off', 'center', 'left', 'right', 'top', 'bottom', 'middle', 'none', 'auto', 'inherit', 'initial', 'flex-start', 'flex-end', 'space-between', 'space-around', 'stretch', 'justify' ); if( $text === '' || in_array( strtolower( $text ), $settings, true ) ){ return ''; } if( preg_match( '#^((https?:)?//|\#[0-9a-f]{3,8}$|[\d.]+(px|em|rem|%|vh|vw)?$)#i', $text ) && ! is_numeric( $text ) ){ return ''; } return $text; } function velischat_collect_builder_texts( $settings, &$texts ){ // Walk builder settings (repeaters included) and collect the values of the text fields in order foreach( (array) $settings as $key => $value ){ if( is_array( $value ) ){ if( ! is_string( $key ) || $key === '' || $key[ 0 ] !== '_' ){ velischat_collect_builder_texts( $value, $texts ); } continue; } if( ! is_string( $value ) || ! is_string( $key ) || ! velischat_is_builder_text_key( $key ) ){ continue; } $text = velischat_builder_value_to_text( $value ); // Skip repeated values (e.g. the same title saved for desktop and mobile) if( $text !== '' && end( $texts ) !== $text ){ $texts[] = $text; } } } function velischat_get_elementor_data( $post_id ){ // Return the Elementor element tree of a post, or an empty array if it isn't built with Elementor if( get_post_meta( $post_id, '_elementor_edit_mode', true ) !== 'builder' ){ return array(); } $data = get_post_meta( $post_id, '_elementor_data', true ); if( is_string( $data ) ){ $data = json_decode( $data, true ); } return is_array( $data ) ? $data : array(); } function velischat_elementor_is_hidden( $settings ){ // An element hidden on every device (Advanced → Responsive): Elementor has no other way to switch one off. The extra // breakpoints count only when the element has their setting, i.e. when the site uses them foreach( array( 'desktop', 'tablet', 'mobile' ) as $device ){ if( empty( $settings[ 'hide_' . $device ] ) ){ return false; } } foreach( array( 'widescreen', 'laptop', 'tablet_extra', 'mobile_extra' ) as $device ){ if( isset( $settings[ 'hide_' . $device ] ) && empty( $settings[ 'hide_' . $device ] ) ){ return false; } } return true; } function velischat_elementor_elements_to_texts( $elements, &$texts, &$visited ){ // Collect the texts of an Elementor element tree, following global widgets and embedded templates foreach( (array) $elements as $element ){ if( ! is_array( $element ) ){ continue; } $settings = isset( $element[ 'settings' ] ) && is_array( $element[ 'settings' ] ) ? $element[ 'settings' ] : array(); // What the visitors never see, children included, isn't told to them either if( velischat_elementor_is_hidden( $settings ) ){ continue; } $template = 0; if( isset( $element[ 'widgetType' ] ) && $element[ 'widgetType' ] === 'global' && ! empty( $element[ 'templateID' ] ) ){ $template = (int) $element[ 'templateID' ]; } elseif( isset( $element[ 'widgetType' ] ) && $element[ 'widgetType' ] === 'template' && ! empty( $settings[ 'template_id' ] ) ){ $template = (int) $settings[ 'template_id' ]; } if( $template ){ if( ! isset( $visited[ $template ] ) ){ $visited[ $template ] = true; velischat_elementor_elements_to_texts( velischat_get_elementor_data( $template ), $texts, $visited ); } } elseif( isset( $element[ 'elType' ] ) && $element[ 'elType' ] === 'widget' ){ velischat_collect_builder_texts( $settings, $texts ); } if( ! empty( $element[ 'elements' ] ) ){ velischat_elementor_elements_to_texts( $element[ 'elements' ], $texts, $visited ); } } } function velischat_elementor_to_text( $post_id ){ // Plain text of a page built with Elementor, read from its saved data so it works without rendering $texts = array(); $visited = array( (int) $post_id => true ); velischat_elementor_elements_to_texts( velischat_get_elementor_data( $post_id ), $texts, $visited ); return trim( implode( "\n\n", $texts ) ); } function velischat_wpbakery_is_hidden( $atts ){ // An element the visitors never see: switched off ("Disable element", rows and sections) or hidden on every device // (columns, "Responsive options") if( isset( $atts[ 'disable_element' ] ) && $atts[ 'disable_element' ] === 'yes' ){ return true; } if( empty( $atts[ 'offset' ] ) || ! is_string( $atts[ 'offset' ] ) ){ return false; } $classes = preg_split( '/\s+/', $atts[ 'offset' ] ); return ! array_diff( array( 'vc_hidden-lg', 'vc_hidden-md', 'vc_hidden-sm', 'vc_hidden-xs' ), $classes ); } function velischat_wpbakery_remove_hidden( $content ){ // Drop the WPBakery elements the visitors never see, with everything inside them: rendering the page drops them // too, reading the saved shortcodes doesn't. Like WordPress, an element ends at the first closing tag with its // name (an element without one is just its tag). Content without hidden elements comes back unchanged $content = (string) $content; if( strpos( $content, 'disable_element' ) === false && strpos( $content, 'vc_hidden-' ) === false ){ return $content; } $output = ''; $position = 0; while( preg_match( '#\[([a-z][\w-]*)((?:[^\]"\']|"[^"]*"|\'[^\']*\')*)\]#i', $content, $match, PREG_OFFSET_CAPTURE, $position ) ){ $start = $match[ 0 ][ 1 ]; $end = $start + strlen( $match[ 0 ][ 0 ] ); $atts = trim( $match[ 2 ][ 0 ] ) !== '' ? shortcode_parse_atts( rtrim( trim( $match[ 2 ][ 0 ] ), '/' ) ) : array(); if( ! is_array( $atts ) || ! velischat_wpbakery_is_hidden( $atts ) ){ $output .= substr( $content, $position, $end - $position ); $position = $end; continue; } $output .= substr( $content, $position, $start - $position ); $closing = '[/' . $match[ 1 ][ 0 ] . ']'; $close_at = stripos( $content, $closing, $end ); $position = $close_at === false ? $end : $close_at + strlen( $closing ); } return $output . substr( $content, $position ); } function velischat_wpbakery_to_text( $content ){ // Plain text of WPBakery content: keeps the text between shortcodes and the text stored in their attributes // Raw HTML blocks are stored as base64 of the url-encoded HTML, raw JS blocks have nothing to read $content = velischat_wpbakery_remove_hidden( $content ); $content = preg_replace( '#\[vc_raw_js[^\]]*\].*?\[/vc_raw_js\]#is', '', $content ); $content = preg_replace_callback( '#\[vc_raw_html[^\]]*\](.*?)\[/vc_raw_html\]#is', function( $match ){ $decoded = base64_decode( trim( wp_strip_all_tags( $match[ 1 ] ) ), true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- WPBakery stores the text of its Raw HTML blocks in base64: decoded only to read it as plain text return $decoded !== false ? "\n\n" . rawurldecode( $decoded ) . "\n\n" : ''; }, $content ); $content = preg_replace_callback( '#\[(/?)([a-z][\w-]*)((?:[^\]"\']|"[^"]*"|\'[^\']*\')*)\]#i', function( $match ){ if( $match[ 1 ] === '/' || trim( $match[ 3 ] ) === '' ){ return "\n\n"; } $atts = shortcode_parse_atts( rtrim( trim( $match[ 3 ] ), '/' ) ); $atts = is_array( $atts ) ? $atts : array(); foreach( $atts as $key => $value ){ if( ! is_string( $value ) ){ continue; } // WPBakery escapes quotes and brackets in attributes; param groups are url-encoded JSON $value = str_replace( array( '`{`', '`}`', '``' ), array( '[', ']', '"' ), $value ); if( strpos( $value, '%5B%7B' ) === 0 ){ $group = json_decode( rawurldecode( $value ), true ); $value = is_array( $group ) ? $group : ''; } elseif( strpos( $value, '%' ) !== false && preg_match( '/%[0-9A-F]{2}/i', $value ) ){ $value = rawurldecode( $value ); } $atts[ $key ] = $value; } $texts = array(); velischat_collect_builder_texts( $atts, $texts ); return "\n\n" . implode( "\n\n", array_map( 'esc_html', $texts ) ) . "\n\n"; }, $content ); return velischat_remove_placeholder_texts( velischat_html_to_text( $content ) ); } function velischat_is_wpbakery_content( $content ){ // WPBakery pages are made of [vc_row] / [vc_section] shortcodes return (bool) preg_match( '#\[vc_(row|section|column)[\s\]]#', $content ); } function velischat_get_salient_portfolio_content( $post_id ){ // Salient saves the WPBakery content of its portfolio projects in this custom field, not in post_content return (string) get_post_meta( $post_id, '_nectar_portfolio_extra_content', true ); } function velischat_post_has_raw_text( $item ){ // Cheap check used by the contents list: Elementor and Salient portfolios keep the text outside post_content if( trim( wp_strip_all_tags( $item->post_content ) ) !== '' ){ return true; } if( trim( wp_strip_all_tags( velischat_get_salient_portfolio_content( $item->ID ) ) ) !== '' ){ return true; } return ! empty( velischat_get_elementor_data( $item->ID ) ); } function velischat_get_post_plain_content( $item ){ // Salient portfolio pages show the post content followed by their own WPBakery content $item = get_post( $item ); if( ! $item ){ return ''; } $extra = velischat_wpbakery_to_text( velischat_get_salient_portfolio_content( $item->ID ) ); return trim( velischat_get_post_main_content( $item ) . "\n\n" . $extra ); } function velischat_get_placeholder_texts(){ // Sample texts that themes and builders put in their elements and that stay saved when the site owner never // replaces them, often in a field the visitors don't see (e.g. the hover text of the Salient fancy box). They // are not about the site: the chatbot must not read them. Lowercase, without the final punctuation $texts = array( "here's some awesome text that would only be shown on hover", ); return array_map( 'velischat_normalize_placeholder_text', (array) apply_filters( 'velischat_placeholder_texts', $texts ) ); } function velischat_normalize_placeholder_text( $text ){ // Same comparison for every line: case, curly apostrophes, spaces and the final punctuation don't count $text = str_replace( array( "\u{2018}", "\u{2019}" ), "'", (string) $text ); $text = strtolower( trim( preg_replace( '/\s+/u', ' ', $text ) ) ); return rtrim( $text, " .!?:;,\u{2026}" ); } function velischat_remove_placeholder_texts( $text ){ // Drop the lines that are only a placeholder text, then the empty lines they leave $placeholders = velischat_get_placeholder_texts(); $lines = array(); foreach( explode( "\n", (string) $text ) as $line ){ if( ! in_array( velischat_normalize_placeholder_text( $line ), $placeholders, true ) ){ $lines[] = $line; } } return trim( preg_replace( "/\n{3,}/", "\n\n", implode( "\n", $lines ) ) ); } function velischat_get_post_main_content( $item ){ // Page builders: read their saved data directly, rendering them outside the frontend is unreliable $text = ''; if( velischat_get_elementor_data( $item->ID ) ){ $text = velischat_elementor_to_text( $item->ID ); } elseif( velischat_is_wpbakery_content( $item->post_content ) ){ $text = velischat_wpbakery_to_text( $item->post_content ); } if( $text !== '' ){ return velischat_remove_placeholder_texts( $text ); } // Render the post content through the usual filters (blocks, shortcodes, builders) and return plain text global $post; $original_post = $post; // WPBakery registers its shortcodes only on the frontend if( class_exists( 'WPBMap' ) && method_exists( 'WPBMap', 'addAllMappedShortcodes' ) ){ WPBMap::addAllMappedShortcodes(); } $post = get_post( $item ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the_content filters read the global post; restored right below setup_postdata( $post ); $html = apply_filters( 'the_content', $post->post_content ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress core filter $post = $original_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the post that was there before if( $original_post ){ setup_postdata( $original_post ); } return velischat_remove_placeholder_texts( velischat_html_to_text( $html ) ); } function velischat_get_context_posts(){ // Selected posts that can be told to the visitors, in the order they were selected $post_ids = velischat_get_context_post_ids(); if( empty( $post_ids ) ){ return array(); } return get_posts( array( 'post_type' => velischat_get_context_post_types(), 'post_status' => 'publish', // Password-protected contents must not be told to every visitor by the chatbot 'has_password' => false, 'post__in' => $post_ids, 'orderby' => 'post__in', 'posts_per_page' => -1, ) ); } function velischat_build_context_text( $free_text, $sections ){ // The contents as plain text, for the AI services that read them directly: free text, then the posts $parts = array(); if( trim( $free_text ) !== '' ){ $parts[] = trim( $free_text ); } foreach( $sections as $section ){ $parts[] = $section[ 'heading' ] . "\n" . $section[ 'url' ] . "\n\n" . $section[ 'text' ]; } return implode( "\n\n", $parts ); } function velischat_get_context_sections(){ // Heading, address and text of each selected post $markers = velischat_get_context_type_markers(); $sections = array(); foreach( velischat_get_context_posts() as $item ){ $sections[] = array( 'heading' => $markers[ $item->post_type ] . ' "' . get_the_title( $item ) . '"', 'url' => get_permalink( $item ), // The text of the content; add-ons can give another version of it (e.g. a summary) 'text' => (string) apply_filters( 'velischat_context_post_text', velischat_get_post_plain_content( $item ), $item ), ); } // Add-ons add content that isn't a post (e.g. the text of other files), with the same keys return array_values( (array) apply_filters( 'velischat_context_sections', $sections ) ); } function velischat_get_context_text(){ // Contents as plain text, or an empty string if there are none. Sites updated from a version that only wrote a // PDF build the text once, at the first message $text = get_option( 'velischat_context_text', false ); if( $text === false ){ $text = velischat_build_context_text( get_option( 'velischat_content_free_text', '' ), velischat_get_context_sections() ); update_option( 'velischat_context_text', $text, false ); update_option( 'velischat_context_markers', velischat_get_context_type_markers(), false ); } return $text; } function velischat_generate_context(){ // Build the contents the assistant reads, as plain text: the free text, then the selected posts if( function_exists( 'set_time_limit' ) ){ @set_time_limit( 300 ); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- reading every selected page can take longer than the default limit on big sites } $sections = velischat_get_context_sections(); $text = velischat_build_context_text( get_option( 'velischat_content_free_text', '' ), $sections ); update_option( 'velischat_context_text', $text, false ); update_option( 'velischat_context_markers', velischat_get_context_type_markers(), false ); if( $text === '' ){ delete_option( 'velischat_context_generated' ); return true; } update_option( 'velischat_context_generated', array( 'time' => time(), 'posts' => count( $sections ), 'size' => strlen( $text ), ), false ); return true; } function velischat_generate_context_pdf(){ // Name used up to 0.9.9, kept for code written for those versions _deprecated_function( __FUNCTION__, '0.9.10', 'velischat_generate_context()' ); return velischat_generate_context(); } function velischat_schedule_context_regeneration(){ // Rebuild the contents once at the end of the request, however many posts were saved static $scheduled = false; if( $scheduled ){ return; } $scheduled = true; add_action( 'shutdown', 'velischat_generate_context' ); } function velischat_maybe_regenerate_on_save( $post_id, $post ){ // Rebuild the contents when a selected post or page is updated if( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ){ return; } if( ! in_array( $post->post_type, velischat_get_context_post_types(), true ) ){ return; } if( ! in_array( (int) $post_id, velischat_get_context_post_ids(), true ) ){ return; } velischat_schedule_context_regeneration(); } add_action( 'save_post', 'velischat_maybe_regenerate_on_save', 20, 2 ); function velischat_maybe_regenerate_on_delete( $post_id ){ // Remove a deleted post from the selection and rebuild the contents $post_ids = velischat_get_context_post_ids(); if( ! in_array( (int) $post_id, $post_ids, true ) ){ return; } update_option( 'velischat_content_post_ids', array_values( array_diff( $post_ids, array( (int) $post_id ) ) ) ); velischat_schedule_context_regeneration(); } add_action( 'deleted_post', 'velischat_maybe_regenerate_on_delete' ); function velischat_regenerate_for_all_types(){ // Since version 0.9 posts and the content types of the theme and plugins can be read too: rebuild the contents once, // so every selected content is read. Only on an admin page, like velischat_save_context_markers() if( wp_doing_ajax() || ! current_user_can( 'manage_options' ) || get_option( 'velischat_all_types_regenerated' ) ){ return; } update_option( 'velischat_all_types_regenerated', 1 ); if( velischat_get_context_post_ids() ){ velischat_schedule_context_regeneration(); } } add_action( 'admin_init', 'velischat_regenerate_for_all_types' ); function velischat_reread_contents(){ // When the way the contents are read changes, rebuild them once, so the chatbot stops telling what the old reading // kept. Version 2 (0.9.6): the sections hidden in WPBakery and Elementor are skipped. Version 3 (0.9.9): the sample // texts of themes are skipped (velischat_get_placeholder_texts()). Only on an admin page, like // velischat_regenerate_for_all_types() $version = 3; if( wp_doing_ajax() || ! current_user_can( 'manage_options' ) || (int) get_option( 'velischat_contents_reader', 1 ) >= $version ){ return; } update_option( 'velischat_contents_reader', $version ); if( velischat_get_context_post_ids() ){ velischat_schedule_context_regeneration(); // Add-ons that keep their own copy of the contents (e.g. summaries) can refresh it do_action( 'velischat_contents_reread' ); } } add_action( 'admin_init', 'velischat_reread_contents' ); function velischat_save_context_markers(){ // Sites updated from a version that didn't save the labels of the content types with the contents: rebuild them // once, so the instructions name the types with the labels written in them. Only on an admin page: the chat // requests also go through admin-ajax.php, in the visitor's language if( wp_doing_ajax() || ! current_user_can( 'manage_options' ) || get_option( 'velischat_context_markers', false ) !== false ){ return; } if( get_option( 'velischat_context_text', false ) === false && get_option( 'velischat_context_file', '' ) === '' ){ update_option( 'velischat_context_markers', velischat_get_context_type_markers(), false ); return; } velischat_schedule_context_regeneration(); } add_action( 'admin_init', 'velischat_save_context_markers' ); function velischat_cleanup_legacy_options(){ // Remove options from the old domains / uploaded PDF modes if( get_option( 'velischat_source_mode', null ) === null && get_option( 'velischat_context_pdf', null ) === null ){ return; } $old_pdf = get_option( 'velischat_context_pdf', '' ); if( $old_pdf ){ delete_transient( 'velischat_gemini_file_' . md5( $old_pdf ) ); } delete_option( 'velischat_source_mode' ); delete_option( 'velischat_reference_domains' ); delete_option( 'velischat_context_pdf' ); } add_action( 'admin_init', 'velischat_cleanup_legacy_options' );