'X (Twitter)', 'linkedin' => 'LinkedIn', 'facebook' => 'Facebook', 'telegram' => 'Telegram', 'whatsapp' => 'WhatsApp', 'email' => 'Email', 'raindrop' => 'Raindrop', 'reddit' => 'Reddit', 'bluesky' => 'Bluesky', 'line' => 'LINE', 'mastodon' => 'Mastodon', 'threads' => 'Threads', 'pinterest' => 'Pinterest', 'claude' => 'Claude AI', 'chatgpt' => 'ChatGPT', 'google_ai' => 'Google AI', 'gemini' => 'Gemini', 'grok' => 'Grok', 'perplexity' => 'Perplexity', 'deepseek' => 'DeepSeek', 'mistral' => 'Mistral AI', 'copilot' => 'Microsoft Copilot', 'qwen' => 'Qwen', 'meta_ai' => 'Meta AI', // Browser actions (v2.5.0): no destination site, they act on the page. // Translatable, unlike the platform names above, which are brands. 'copy_link' => __( 'Copy link', 'ai-share-summarize' ), 'web_share' => __( 'Native share', 'ai-share-summarize' ), // Google preferred source (v2.6.0): a link to Google's own tool, not a // share. Translatable for the same reason as the browser actions. 'google_preferred' => __( 'Google preferred source', 'ai-share-summarize' ), ); } /** * Get social media buttons list * * @return array Social media button keys. */ function ayudawp_aiss_get_social_buttons() { return array( 'twitter', 'linkedin', 'facebook', 'telegram', 'whatsapp', 'email', 'raindrop', 'reddit', 'bluesky', 'line', 'mastodon', 'threads', 'pinterest', 'copy_link', 'web_share', 'google_preferred' ); } /** * Get AI buttons list * * @return array AI button keys. */ function ayudawp_aiss_get_ai_buttons() { return array( 'claude', 'chatgpt', 'google_ai', 'gemini', 'grok', 'perplexity', 'deepseek', 'mistral', 'copilot', 'qwen', 'meta_ai' ); } /** * Sanitize a title_style value against an allowlist of HTML tag names. * * The value is emitted in tag-name position (`<{$tag}>`), where `esc_attr()` * is not enough: it does not encode spaces or `=`, so an attacker could turn * the opening tag into an arbitrary element with event-handler attributes. * Anything outside the allowlist falls back to `span`. * * @since 2.0.4 * @param string $value Untrusted tag name. * @return string Safe tag name from the allowlist. */ function ayudawp_aiss_sanitize_title_style( $value ) { $allowed = array( 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'div' ); $value = is_string( $value ) ? strtolower( trim( $value ) ) : ''; return in_array( $value, $allowed, true ) ? $value : 'span'; } /** * Build an opening title/section tag from a safe tag name (v2.1.0) * * The tag name is validated against the allowlist *inside* this helper, via * ayudawp_aiss_sanitize_title_style(), so the allowlist can never be bypassed * by a caller that builds the tag itself. This replaces the inline * `'<' . esc_attr( $tag ) . ' class=...>'` pattern: esc_attr() does not encode * spaces or `=`, so emitting an unsanitized value in tag-name position was the * class of bug behind the 2.0.x stored XSS. Keeping the allowlist here removes * that recurring "looks vulnerable" pattern from the codebase. * * @since 2.1.0 * @since 2.5.0 Optional id, used to wire section titles to their button group * with aria-labelledby. * @param string $style Untrusted tag name (validated against the allowlist). * @param string $class CSS class(es) for the element (escaped here). * @param string $id Optional element id (escaped here). * @return string Opening tag, e.g. '

'. */ function ayudawp_aiss_open_tag( $style, $class = '', $id = '' ) { $tag = ayudawp_aiss_sanitize_title_style( $style ); $class = trim( (string) $class ); $id = trim( (string) $id ); $attributes = ''; if ( '' !== $id ) { $attributes .= ' id="' . esc_attr( $id ) . '"'; } if ( '' !== $class ) { $attributes .= ' class="' . esc_attr( $class ) . '"'; } return '<' . $tag . $attributes . '>'; } /** * Build the matching closing tag for ayudawp_aiss_open_tag() (v2.1.0) * * @since 2.1.0 * @param string $style Untrusted tag name (validated against the allowlist). * @return string Closing tag, e.g. '

'. */ function ayudawp_aiss_close_tag( $style ) { return ''; } /** * Validate and sanitize plugin options * * Handles the buttons/general option only. The AI-summary settings moved to * their own option (`ayudawp_aiss_summary_options`) in 2.3.0 — see * ayudawp_aiss_validate_summary_options(). Any `ai_summary_*` keys still * present in the stored value are a frozen pre-2.3.0 snapshot kept for * rollback safety; they are carried over untouched, never re-validated. * * @param array $input Raw input data. * @return array Validated and sanitized options. */ function ayudawp_aiss_validate_options( $input ) { $validated = array(); // Text fields. $text_fields = array( 'twitter_handle', 'title_text', 'title_style', 'button_order', 'button_alignment', 'button_colors', 'icon_style', 'seo_button_type', 'data_retention', 'button_size', 'ai_section_title', 'social_section_title', 'mastodon_instance', 'custom_color_bg', 'custom_color_text', 'dark_mode_adaptation', // Mobile compact mode and UTM tagging (v2.5.0). 'mobile_compact', 'utm_campaign', // Google preferred source button (v2.6.0). 'google_preferred_placement', 'google_section_title', ); $textarea_fields = array( 'default_prompt', 'custom_text' ); $array_fields = array( 'enabled_buttons', 'button_custom_order_ai', 'button_custom_order_social' ); $boolean_fields = array( 'show_icons', 'delete_data_on_uninstall', 'exclude_noindex', 'separator_top', 'separator_bottom', 'floating_bar_autohide', 'floating_bar_hide_mobile', 'floating_bar_hide_with_inline', 'utm_enabled', ); foreach ( $text_fields as $field ) { if ( isset( $input[ $field ] ) ) { $validated[ $field ] = sanitize_text_field( $input[ $field ] ); } } // title_style is emitted in tag-name position; restrict it to an allowlist. if ( isset( $validated['title_style'] ) ) { $validated['title_style'] = ayudawp_aiss_sanitize_title_style( $validated['title_style'] ); } foreach ( $textarea_fields as $field ) { if ( isset( $input[ $field ] ) ) { $validated[ $field ] = sanitize_textarea_field( $input[ $field ] ); } } foreach ( $array_fields as $field ) { if ( isset( $input[ $field ] ) && is_array( $input[ $field ] ) ) { $validated[ $field ] = array_map( 'sanitize_text_field', $input[ $field ] ); } else { $validated[ $field ] = array(); } } foreach ( $boolean_fields as $field ) { $validated[ $field ] = isset( $input[ $field ] ) ? (bool) $input[ $field ] : false; } // Validate seo_button_type. if ( isset( $validated['seo_button_type'] ) && ! in_array( $validated['seo_button_type'], array( 'link', 'button' ), true ) ) { $validated['seo_button_type'] = 'link'; } // Validate button_alignment. if ( isset( $validated['button_alignment'] ) && ! in_array( $validated['button_alignment'], array( 'left', 'center' ), true ) ) { $validated['button_alignment'] = 'left'; } // Validate data_retention. $valid_retention = array( '1_month', '3_months', '6_months', '1_year', 'forever' ); if ( isset( $validated['data_retention'] ) && ! in_array( $validated['data_retention'], $valid_retention, true ) ) { $validated['data_retention'] = 'forever'; } // Validate button_size. $valid_sizes = array( 'compact', 'normal', 'large', 'fluid' ); if ( isset( $validated['button_size'] ) && ! in_array( $validated['button_size'], $valid_sizes, true ) ) { $validated['button_size'] = 'normal'; } // Validate button_colors. $valid_styles = array( 'minimal', 'brand', 'outline', 'dark', 'icons-only', 'custom' ); if ( isset( $validated['button_colors'] ) && ! in_array( $validated['button_colors'], $valid_styles, true ) ) { $validated['button_colors'] = 'minimal'; } /* * Placements (v2.5.0): a map of destination => position, where a key that * is not present means "not there". Both the destination and the position * are checked against the lists the screen paints, so a form that posts a * post type that no longer exists cannot write it. */ $targets = ayudawp_aiss_get_placement_targets(); foreach ( array( 'placements', 'floating_bar_placements' ) as $map_key ) { $positions = ayudawp_aiss_get_placement_positions( $map_key ); $fallback = ayudawp_aiss_get_default_placement_position( $map_key ); $validated[ $map_key ] = array(); // The form posts two halves: which destinations are ticked, and the // position chosen for every row (ticked or not, so unticking and // reticking a row does not lose the position). Only the intersection // is stored. $enabled = isset( $input[ $map_key . '_on' ] ) && is_array( $input[ $map_key . '_on' ] ) ? array_map( 'sanitize_key', $input[ $map_key . '_on' ] ) : array(); $chosen = isset( $input[ $map_key ] ) && is_array( $input[ $map_key ] ) ? $input[ $map_key ] : array(); foreach ( $enabled as $target ) { if ( ! isset( $targets[ $target ] ) ) { continue; } $position = isset( $chosen[ $target ] ) ? sanitize_key( $chosen[ $target ] ) : $fallback; $validated[ $map_key ][ $target ] = isset( $positions[ $position ] ) ? $position : $fallback; } } // Where the Google preferred source button goes (v2.6.0). if ( isset( $validated['google_preferred_placement'] ) && ! in_array( $validated['google_preferred_placement'], array( 'social', 'section', 'outside' ), true ) ) { $validated['google_preferred_placement'] = 'social'; } // Validate mobile_compact (v2.5.0). $valid_compact_modes = array( 'disabled', 'single', 'partial' ); if ( isset( $validated['mobile_compact'] ) && ! in_array( $validated['mobile_compact'], $valid_compact_modes, true ) ) { $validated['mobile_compact'] = 'disabled'; } /* * How many buttons stay visible in the "N visible + more" compact mode. * Clamped rather than rejected: the field is a number input, so an out of * range value is a typo, not an attack, and falling back to the default * would silently discard what the admin meant. */ if ( isset( $input['mobile_compact_visible'] ) ) { $visible = (int) $input['mobile_compact_visible']; $validated['mobile_compact_visible'] = max( 1, min( 5, $visible ) ); } // Validate dark_mode_adaptation. $valid_dark_modes = array( 'disabled', 'auto' ); if ( isset( $validated['dark_mode_adaptation'] ) && ! in_array( $validated['dark_mode_adaptation'], $valid_dark_modes, true ) ) { $validated['dark_mode_adaptation'] = 'disabled'; } // Sanitize hex colors. if ( isset( $validated['custom_color_bg'] ) ) { $validated['custom_color_bg'] = sanitize_hex_color( $validated['custom_color_bg'] ); } if ( isset( $validated['custom_color_text'] ) ) { $validated['custom_color_text'] = sanitize_hex_color( $validated['custom_color_text'] ); } // Sanitize mastodon instance URL. if ( isset( $validated['mastodon_instance'] ) ) { $validated['mastodon_instance'] = sanitize_text_field( wp_unslash( $validated['mastodon_instance'] ) ); // Remove protocol and trailing slashes. $validated['mastodon_instance'] = preg_replace( '#^https?://#', '', $validated['mastodon_instance'] ); $validated['mastodon_instance'] = rtrim( $validated['mastodon_instance'], '/' ); } /* * The frozen pre-2.3.0 `ai_summary_*` snapshot was carried over here on * every save until 2.6.0, so a rollback to 2.2.x found its configuration. * The 2.6.0 migration deletes it, and without this carry-over the next * save does not bring it back. A rollback is safe to 2.3.0 or later, where * the summary has its own option. */ $stored = get_option( 'ayudawp_aiss_options', array() ); if ( is_array( $stored ) ) { /* * Rollback snapshot of the three keys `placements` replaced in 2.5.0 * (`display_locations`, `auto_insert_content_types` and * `auto_insert_position`). The form no longer posts them, so without * this they would disappear on the first save and a rollback to 2.4.x * would land on a site with no buttons anywhere. Kept when the 2.3.0 * snapshot went in 2.6.0: 2.5.0 is recent, and its removal has its own * release once the 2.5 series has settled (roadmap, entry 38). */ foreach ( array( 'display_locations', 'auto_insert_content_types', 'auto_insert_position' ) as $legacy_key ) { if ( isset( $stored[ $legacy_key ] ) && ! isset( $validated[ $legacy_key ] ) ) { $validated[ $legacy_key ] = $stored[ $legacy_key ]; } } } /* * A prompt identical to the bundled default is not stored at all (v2.4.1), * so it keeps being resolved from the language pack on every request and * the AI is asked for the summary in the language of each reader. An empty * prompt is a different thing and is stored: it means send no instructions, * only the source URL. */ if ( isset( $validated['default_prompt'] ) && ayudawp_aiss_is_default_prompt( $validated['default_prompt'] ) ) { unset( $validated['default_prompt'] ); } return $validated; } /** * Whether a stored prompt is just a copy of the bundled default. * * Until 2.4.1 the prompt was stored verbatim: activation wrote it, and the * settings field came pre-filled, so the first save on the Share Buttons tab * stored whatever the admin was seeing. From then on the buttons sent that * exact text to the AI, so the prompt stopped going through translation and * stayed in one language. On a multilingual site that means every reader gets * the summary requested in the language of whoever saved the settings. * * Every locale installed on the site is checked, plus the original English, * which covers the language the admin could have been using. A prompt the site * actually wrote never matches and is left untouched. * * @since 2.4.1 * @param string $prompt Stored prompt. * @return bool True when the prompt is a bundled default in some locale. */ function ayudawp_aiss_is_default_prompt( $prompt ) { $locales = array_unique( array_merge( array( 'en_US', get_locale() ), get_available_languages() ) ); foreach ( $locales as $locale ) { $switched = switch_to_locale( $locale ); $default = ayudawp_aiss_get_default_prompt(); if ( $switched ) { restore_previous_locale(); } if ( $prompt === $default ) { return true; } } return false; } /** * Validate and sanitize the AI-summary options (v2.3.0) * * Sanitize callback for `ayudawp_aiss_summary_options`, the dedicated option * behind the AI Summary settings tab. Every key is always emitted with an * explicit value, so the stored option is complete and unambiguous: an empty * `ai_summary_content_types` list genuinely means "no content types" (the * form ships a hidden `ai_summary_content_types_submitted` marker so an * all-unchecked group can be told apart from a request that did not include * the field at all). * * @since 2.3.0 * @param array $input Raw input data. * @return array Validated and sanitized summary options. */ function ayudawp_aiss_validate_summary_options( $input ) { if ( ! is_array( $input ) ) { $input = array(); } $validated = array(); // Generation mode — explicit allowlist (v2.2.0). $valid_summary_modes = array( 'ai_fallback', 'ai_only', 'extractive_only', 'disabled' ); $validated['ai_summary_mode'] = isset( $input['ai_summary_mode'] ) && in_array( $input['ai_summary_mode'], $valid_summary_modes, true ) ? $input['ai_summary_mode'] : 'ai_fallback'; // Model — '' (automatic) or a "provider:model" pair restricted to a safe // charset. When the live model list is available the pair must exist in // it; if enumeration is momentarily unavailable we keep the sanitized // value rather than wiping the admin's choice. $validated['ai_summary_model'] = ''; if ( isset( $input['ai_summary_model'] ) && '' !== $input['ai_summary_model'] ) { $candidate = preg_replace( '/[^a-zA-Z0-9:._-]/', '', (string) $input['ai_summary_model'] ); if ( '' !== $candidate && 1 === substr_count( $candidate, ':' ) ) { list( $cand_provider, $cand_model ) = explode( ':', $candidate, 2 ); if ( '' !== $cand_provider && '' !== $cand_model ) { $available = ayudawp_aiss_get_available_models(); $is_known = false; if ( isset( $available[ $cand_provider ]['models'] ) ) { foreach ( $available[ $cand_provider ]['models'] as $known_model ) { if ( $known_model['id'] === $cand_model ) { $is_known = true; break; } } } if ( $is_known || empty( $available ) ) { $validated['ai_summary_model'] = $candidate; } } } } // Content types. The hidden marker distinguishes "submitted with every // checkbox unchecked" (a deliberate empty list) from input that simply // did not include the field (programmatic partial update: keep stored). if ( isset( $input['ai_summary_content_types'] ) && is_array( $input['ai_summary_content_types'] ) ) { $validated['ai_summary_content_types'] = array_values( array_map( 'sanitize_text_field', $input['ai_summary_content_types'] ) ); } elseif ( isset( $input['ai_summary_content_types_submitted'] ) ) { $validated['ai_summary_content_types'] = array(); } else { $stored = ayudawp_aiss_get_summary_options(); $validated['ai_summary_content_types'] = isset( $stored['ai_summary_content_types'] ) && is_array( $stored['ai_summary_content_types'] ) ? array_values( array_map( 'strval', $stored['ai_summary_content_types'] ) ) : array( 'post' ); } /* * Placements (v2.5.0): one row per content type with its own position, * same table and same sanitizing as the buttons. `ai_summary_content_types` * and `ai_summary_position` are kept below as a rollback snapshot. */ $summary_targets = ayudawp_aiss_get_placement_targets( 'ai_summary_placements' ); $summary_positions = ayudawp_aiss_get_placement_positions( 'ai_summary_placements' ); $summary_fallback = ayudawp_aiss_get_default_placement_position( 'ai_summary_placements' ); $validated['ai_summary_placements'] = array(); $summary_enabled = isset( $input['ai_summary_placements_on'] ) && is_array( $input['ai_summary_placements_on'] ) ? array_map( 'sanitize_key', $input['ai_summary_placements_on'] ) : array(); $summary_chosen = isset( $input['ai_summary_placements'] ) && is_array( $input['ai_summary_placements'] ) ? $input['ai_summary_placements'] : array(); foreach ( $summary_enabled as $target ) { if ( ! isset( $summary_targets[ $target ] ) ) { continue; } $position = isset( $summary_chosen[ $target ] ) ? sanitize_key( $summary_chosen[ $target ] ) : $summary_fallback; $validated['ai_summary_placements'][ $target ] = isset( $summary_positions[ $position ] ) ? $position : $summary_fallback; } // Position — allowlist. Legacy key, carried over for rollback. $valid_summary_positions = array( 'before_buttons', 'after_buttons', 'before_content', 'disabled' ); $validated['ai_summary_position'] = isset( $input['ai_summary_position'] ) && in_array( $input['ai_summary_position'], $valid_summary_positions, true ) ? $input['ai_summary_position'] : 'before_buttons'; // Block style — allowlist. $valid_summary_styles = array( 'minimal', 'outline', 'brand', 'dark', 'custom' ); $validated['ai_summary_style'] = isset( $input['ai_summary_style'] ) && in_array( $input['ai_summary_style'], $valid_summary_styles, true ) ? $input['ai_summary_style'] : 'minimal'; // Icon position — allowlist. $valid_icon_positions = array( 'left', 'right', 'hidden' ); $validated['ai_summary_icon_position'] = isset( $input['ai_summary_icon_position'] ) && in_array( $input['ai_summary_icon_position'], $valid_icon_positions, true ) ? $input['ai_summary_icon_position'] : 'left'; // Sentence count — integer in 1..5. $validated['ai_summary_sentences'] = isset( $input['ai_summary_sentences'] ) ? max( 1, min( 5, absint( $input['ai_summary_sentences'] ) ) ) : 3; // Free-text labels. $validated['ai_summary_label'] = isset( $input['ai_summary_label'] ) ? sanitize_text_field( $input['ai_summary_label'] ) : ''; $validated['ai_summary_generate_button_label'] = isset( $input['ai_summary_generate_button_label'] ) ? sanitize_text_field( $input['ai_summary_generate_button_label'] ) : ''; // Custom colors. $validated['ai_summary_custom_bg'] = isset( $input['ai_summary_custom_bg'] ) ? sanitize_hex_color( sanitize_text_field( $input['ai_summary_custom_bg'] ) ) : '#ffffff'; $validated['ai_summary_custom_text'] = isset( $input['ai_summary_custom_text'] ) ? sanitize_hex_color( sanitize_text_field( $input['ai_summary_custom_text'] ) ) : '#1a1a1a'; // Booleans (unchecked checkboxes are simply absent from the POST). $validated['ai_summary_collapsed_default'] = ! empty( $input['ai_summary_collapsed_default'] ); $validated['ai_summary_bullets'] = ! empty( $input['ai_summary_bullets'] ); $validated['ai_summary_frontend_button'] = ! empty( $input['ai_summary_frontend_button'] ); $validated['ai_summary_frontend_force_extractive'] = ! empty( $input['ai_summary_frontend_force_extractive'] ); return $validated; } /** * Read the AI-summary options (v2.3.0) * * Single accessor for `ayudawp_aiss_summary_options`, the dedicated option * that holds every `ai_summary_*` setting since 2.3.0. When the option does * not exist yet (the first request after updating, before the init-time * migration has run) the value is derived on the fly from the legacy * combined option, so readers never see a half-migrated state. * * @since 2.3.0 * @return array Summary options. */ function ayudawp_aiss_get_summary_options() { $options = get_option( 'ayudawp_aiss_summary_options', false ); if ( is_array( $options ) ) { return $options; } return ayudawp_aiss_derive_summary_options_from_legacy(); } /** * Derive the AI-summary options from the legacy combined option (v2.3.0) * * Builds the `ayudawp_aiss_summary_options` payload out of the pre-2.3.0 * `ayudawp_aiss_options` keys. Used by the 2.3.0 migration to materialize * the new option, and by ayudawp_aiss_get_summary_options() as a live * fallback until that migration runs. Two implicit legacy behaviours are * made explicit here, once and for all: * * - the generation mode is resolved from the pre-2.2.0 boolean pair when * `ai_summary_mode` was never materialized, and * - the content-type list inherits the buttons' list when the dedicated * selector was never set (the old cross-option fallback this release * removes from the runtime resolver). * * @since 2.3.0 * @return array Derived summary options. */ function ayudawp_aiss_derive_summary_options_from_legacy() { $legacy = get_option( 'ayudawp_aiss_options', array() ); if ( ! is_array( $legacy ) ) { $legacy = array(); } $keys = array( 'ai_summary_mode', 'ai_summary_model', 'ai_summary_content_types', 'ai_summary_position', 'ai_summary_collapsed_default', 'ai_summary_label', 'ai_summary_style', 'ai_summary_custom_bg', 'ai_summary_custom_text', 'ai_summary_icon_position', 'ai_summary_sentences', 'ai_summary_frontend_button', 'ai_summary_generate_button_label', 'ai_summary_frontend_force_extractive', ); $derived = array(); foreach ( $keys as $key ) { if ( isset( $legacy[ $key ] ) ) { $derived[ $key ] = $legacy[ $key ]; } } // Pre-2.2.0 sites never materialized the explicit mode: resolve it from // the legacy boolean pair (same rules as the 2.2.0 migration). if ( ! isset( $derived['ai_summary_mode'] ) ) { $enabled = ! isset( $legacy['ai_summary_enabled'] ) || ! empty( $legacy['ai_summary_enabled'] ); if ( ! $enabled ) { $derived['ai_summary_mode'] = 'disabled'; } else { $fallback = ! isset( $legacy['ai_summary_use_extractive_fallback'] ) || ! empty( $legacy['ai_summary_use_extractive_fallback'] ); $derived['ai_summary_mode'] = $fallback ? 'ai_fallback' : 'ai_only'; } } // Sites where the dedicated selector was never set inherited the buttons' // content-type list at read time. Materialize that inheritance so the new // option starts from the exact behaviour the site had. if ( ! isset( $derived['ai_summary_content_types'] ) || ! is_array( $derived['ai_summary_content_types'] ) ) { $derived['ai_summary_content_types'] = isset( $legacy['auto_insert_content_types'] ) && is_array( $legacy['auto_insert_content_types'] ) ? array_values( array_map( 'strval', $legacy['auto_insert_content_types'] ) ) : array( 'post' ); } return $derived; } /** * The places the buttons can be inserted into (v2.5.0) * * One row per public post type, plus the blog home and the archive listings. * This is the list the settings screen paints and the list the resolver walks. * * @since 2.5.0 * @return array Destination key => label. */ function ayudawp_aiss_get_placement_targets( $key = 'placements' ) { $targets = array(); /* * Media is a public post type but not a place anybody shares from, and on a * site with a few plugins the list is long enough already. It is only left * out when it is not already configured: a site that had it ticked before * keeps seeing its row instead of losing the setting silently. */ $configured = array(); $options = get_option( 'ayudawp_aiss_options' ); foreach ( array( 'placements', 'floating_bar_placements' ) as $map_key ) { if ( isset( $options[ $map_key ] ) && is_array( $options[ $map_key ] ) ) { $configured = array_merge( $configured, array_keys( $options[ $map_key ] ) ); } } foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $post_type ) { if ( 'attachment' === $post_type->name && ! in_array( 'attachment', $configured, true ) ) { continue; } $targets[ $post_type->name ] = $post_type->label; } /* * The summary belongs to one post, so the listings are not destinations for * it; the buttons and the bar do work there. */ if ( 'ai_summary_placements' !== $key ) { $targets['home'] = __( 'Blog home', 'ai-share-summarize' ); $targets['archive'] = __( 'Archives, categories and tags', 'ai-share-summarize' ); } /** * Filter the destinations the buttons can be placed in. * * Useful on sites where plugins register public post types that never show * share buttons, to keep the settings table short. * * @since 2.5.0 * @param array $targets Destination key => label. * @param string $key Which placements map is being painted. */ return apply_filters( 'ayudawp_aiss_placement_targets', $targets, $key ); } /** * Positions a destination can use, per feature (v2.5.0) * * The three features place themselves in their own terms: the inline block * relative to the content, the floating bar against an edge of the viewport, * and the summary relative to the buttons. Same table, same interaction, its * own vocabulary in each. * * @since 2.5.0 * @param string $key Placements option key. * @return array Position key => label. */ function ayudawp_aiss_get_placement_positions( $key = 'placements' ) { if ( 'floating_bar_placements' === $key ) { return array( 'left' => __( 'Pinned to the left edge', 'ai-share-summarize' ), 'right' => __( 'Pinned to the right edge', 'ai-share-summarize' ), 'bottom' => __( 'Along the bottom', 'ai-share-summarize' ), ); } if ( 'ai_summary_placements' === $key ) { return array( 'before_buttons' => __( 'Right before the share buttons', 'ai-share-summarize' ), 'after_buttons' => __( 'Right after the share buttons', 'ai-share-summarize' ), 'before_content' => __( 'Before the content', 'ai-share-summarize' ), // Keeps what the old "Do not display" position did: the summary is // generated and available to the shortcode and the REST API, but // the post does not print it. 'hidden' => __( 'Generate it, but do not display it', 'ai-share-summarize' ), ); } return array( 'before_content' => __( 'Before the content', 'ai-share-summarize' ), 'after_content' => __( 'After the content', 'ai-share-summarize' ), 'both' => __( 'Before and after', 'ai-share-summarize' ), ); } /** * Default position for a placements map, used when a stored value is unusable * * @since 2.5.0 * @param string $key Placements option key. * @return string */ function ayudawp_aiss_get_default_placement_position( $key = 'placements' ) { if ( 'floating_bar_placements' === $key ) { return 'right'; } if ( 'ai_summary_placements' === $key ) { return 'before_buttons'; } return 'after_content'; } /** * Stored placements, cleaned of anything that is not a valid pair (v2.5.0) * * @since 2.5.0 * @param string $key Option key holding the placements. * @return array Destination key => position. */ function ayudawp_aiss_get_placements( $key = 'placements' ) { // The summary keeps its settings in its own option since 2.3.0. $options = ( 'ai_summary_placements' === $key ) ? ayudawp_aiss_get_summary_options() : get_option( 'ayudawp_aiss_options' ); $stored = isset( $options[ $key ] ) && is_array( $options[ $key ] ) ? $options[ $key ] : array(); $targets = ayudawp_aiss_get_placement_targets(); $positions = ayudawp_aiss_get_placement_positions( $key ); $clean = array(); foreach ( $stored as $target => $position ) { /* * A post type that no longer exists (its plugin was deactivated) is * skipped rather than dropped from the option: reactivating the plugin * brings the setting back instead of silently losing it. */ if ( ! isset( $targets[ $target ] ) ) { continue; } $clean[ $target ] = isset( $positions[ $position ] ) ? $position : ayudawp_aiss_get_default_placement_position( $key ); } return $clean; } /** * Which position applies to the request being rendered (v2.5.0) * * Replaces the three-way condition that governed this until 2.4.4 * (`display_locations` AND `auto_insert_content_types` AND * `auto_insert_position`), where the first two listed overlapping things and * nothing on screen said the condition was an AND. One row per destination * now carries both the "whether" and the "where". * * @since 2.5.0 * @param string $key Option key holding the placements. * @return string 'before_content', 'after_content', 'both', or '' for nowhere. */ function ayudawp_aiss_get_placement_for_context( $key = 'placements' ) { $placements = ayudawp_aiss_get_placements( $key ); if ( empty( $placements ) ) { return ''; } if ( is_singular() ) { // The queried post, not the global one (v2.6.0): a theme action, a // widget or a related-posts module can leave another post in $post, // and its type would decide where this page's blocks go. $post_type = get_post_type( get_queried_object_id() ); return isset( $placements[ $post_type ] ) ? $placements[ $post_type ] : ''; } if ( is_home() ) { return isset( $placements['home'] ) ? $placements['home'] : ''; } if ( is_archive() || is_category() || is_tag() ) { return isset( $placements['archive'] ) ? $placements['archive'] : ''; } return ''; } /** * Check if buttons should be displayed on current page * * Kept as a boolean helper for the callers that only need a yes or no; the * position itself comes from ayudawp_aiss_get_placement_for_context(). * * @since 1.2.0 * @since 2.5.0 Resolved from the unified `placements` option. * @return bool True when the buttons block belongs on this request. */ function ayudawp_aiss_should_display_buttons() { return '' !== ayudawp_aiss_get_placement_for_context(); } /** * Check if a specific post is excluded from showing the share buttons * * Checks both the meta box exclusion and SEO noindex status. Since 2.3.0 * this governs the share buttons only; the AI summary has its own per-post * control — see ayudawp_aiss_is_summary_excluded(). * * @since 1.4.0 * @param int $post_id Post ID to check. * @return bool True if post is excluded, false otherwise. */ function ayudawp_aiss_is_post_excluded( $post_id ) { if ( ! $post_id ) { return false; } // Check meta box exclusion. if ( class_exists( 'AyudaWP_AISS_Meta_Box' ) && AyudaWP_AISS_Meta_Box::ayudawp_is_post_excluded( $post_id ) ) { return true; } return ayudawp_aiss_is_noindex_excluded( $post_id ); } /** * Check if a post is excluded from showing (and generating) the AI summary (v2.3.0) * * Per-post summary control, independent from the buttons' exclusion since * 2.3.0 (the single pre-2.3.0 checkbox silently switched off both; the * migration materializes the new meta on every post that had it checked so * existing exclusions keep working). The noindex rule stays common to both * features: noindexed content should not spend AI tokens either. * * @since 2.3.0 * @param int $post_id Post ID to check. * @return bool True if the summary is excluded for this post. */ function ayudawp_aiss_is_summary_excluded( $post_id ) { if ( ! $post_id ) { return false; } if ( class_exists( 'AyudaWP_AISS_Meta_Box' ) && AyudaWP_AISS_Meta_Box::ayudawp_is_summary_excluded( $post_id ) ) { return true; } return ayudawp_aiss_is_noindex_excluded( $post_id ); } /** * Whether a post is excluded because it is noindexed (v2.3.0) * * Shared rule between the buttons and the AI summary: when the * "Exclude noindex content" setting is on and the SEO integration reports * the post as noindex, both features stay away from it. * * @since 2.3.0 * @param int $post_id Post ID to check. * @return bool True when the noindex exclusion applies to this post. */ function ayudawp_aiss_is_noindex_excluded( $post_id ) { $options = get_option( 'ayudawp_aiss_options' ); $exclude_noindex = isset( $options['exclude_noindex'] ) ? $options['exclude_noindex'] : true; if ( $exclude_noindex && class_exists( 'AyudaWP_AISS_SEO_Integration' ) ) { if ( AyudaWP_AISS_SEO_Integration::ayudawp_is_post_noindex( $post_id ) ) { return true; } } return false; } /** * Detect whether the canonical "AI" plugin (wordpress.org/plugins/ai/) is active * * Used to surface a hint when AI generation starts failing silently, * because that plugin ships an opt-in Connector approval system * (Tools > Connector Approvals) that intercepts and blocks calls to * `wp_ai_client_prompt()` from plugins it hasn't approved yet — without * exposing a programmatic filter for plugins to request approval, so the * only signal we have is a generic call failure when the canonical plugin * is around. * * @since 2.0.0 * @return bool */ function ayudawp_aiss_is_canonical_ai_plugin_active() { if ( ! function_exists( 'is_plugin_active' ) ) { include_once ABSPATH . 'wp-admin/includes/plugin.php'; } return is_plugin_active( 'ai/ai.php' ); } /** * Resolve the post types where the AI summary feature applies (v2.0.0) * * Truly independent from the buttons' `auto_insert_content_types` since * 2.3.0: the old read-time fallback to the buttons list is gone. The 2.3.0 * migration materialized the inherited value into the dedicated option, so * an empty list here genuinely means "no content types" (a state the * settings tab flags with a visible notice). * * @since 2.0.0 * @since 2.3.0 Reads the dedicated summary option; no buttons-list fallback. * @return array List of post type slugs. */ function ayudawp_aiss_get_summary_post_types() { $placements = ayudawp_aiss_get_placements( 'ai_summary_placements' ); if ( ! empty( $placements ) ) { return array_keys( $placements ); } /* * Pre-2.5.0 shape, still read directly so a site whose migration has not * run yet (an update in flight, a partial restore) keeps generating. */ $options = ayudawp_aiss_get_summary_options(); if ( isset( $options['ai_summary_content_types'] ) && is_array( $options['ai_summary_content_types'] ) ) { return array_values( array_filter( array_map( 'strval', $options['ai_summary_content_types'] ) ) ); } return array(); } /** * Where the summary goes on this request (v2.5.0) * * @since 2.5.0 * @return string Position key, or '' when the summary is not shown here. */ function ayudawp_aiss_get_summary_position_for_context() { $position = ayudawp_aiss_get_placement_for_context( 'ai_summary_placements' ); if ( '' !== $position ) { return ( 'hidden' === $position ) ? '' : $position; } // Pre-2.5.0 fallback, as above. $placements = ayudawp_aiss_get_placements( 'ai_summary_placements' ); if ( ! empty( $placements ) ) { return ''; } $options = ayudawp_aiss_get_summary_options(); $legacy = isset( $options['ai_summary_position'] ) ? $options['ai_summary_position'] : 'before_buttons'; return ( 'disabled' === $legacy ) ? '' : $legacy; } /** * Check whether the AI summary feature applies to a given post (v2.0.0) * * Combines the per-post summary exclusion (meta box / noindex) with the * `ai_summary_content_types` post-type whitelist. Centralised here so * the meta registration, save-post handler, frontend renderer and REST * endpoint all agree on the rule. * * @since 2.0.0 * @since 2.3.0 Uses the summary-specific per-post exclusion. * @param int $post_id Post ID to check. * @return bool True when the summary should be generated and shown. */ function ayudawp_aiss_should_apply_summary( $post_id ) { $post_id = (int) $post_id; if ( ! $post_id ) { return false; } if ( ayudawp_aiss_is_summary_excluded( $post_id ) ) { return false; } $post_type = get_post_type( $post_id ); if ( ! $post_type ) { return false; } $allowed = ayudawp_aiss_get_summary_post_types(); return in_array( $post_type, $allowed, true ); } /** * Resolve the AI summary generation mode (v2.2.0) * * Single explicit mode that replaced the legacy boolean pair in 2.2.0. * Reads the dedicated summary option; pre-migration states (including the * pre-2.2.0 booleans) are resolved by the legacy derivation inside * ayudawp_aiss_get_summary_options(). * * @since 2.2.0 * @since 2.3.0 Reads the dedicated summary option. * @return string One of: ai_fallback, ai_only, extractive_only, disabled. */ function ayudawp_aiss_get_summary_mode() { $options = ayudawp_aiss_get_summary_options(); if ( isset( $options['ai_summary_mode'] ) ) { $mode = (string) $options['ai_summary_mode']; if ( in_array( $mode, array( 'ai_fallback', 'ai_only', 'extractive_only', 'disabled' ), true ) ) { return $mode; } } return 'ai_fallback'; } /** * Whether the AI summary feature is active (any mode other than disabled) (v2.2.0) * * @since 2.2.0 * @return bool True when summaries should be generated and displayed. */ function ayudawp_aiss_is_summary_active() { return 'disabled' !== ayudawp_aiss_get_summary_mode(); } /** * Enumerate the text-generation models available from the configured AI providers (v2.2.0) * * Walks the WordPress 7.0 AI Client provider registry, keeps the providers whose * companion plugin is active AND whose credentials are configured, and lists each * one's text-generation models. The AI Client caches the live model lookup for * 24h, so repeated calls are cheap. Used to build the model selector in Settings * and to resolve the "Automatic" default at generation time. * * Note: providers ship as separate plugins (ai-provider-for-anthropic, etc.); the * registry only lists those whose plugin is active, which is exactly the set that * can actually generate. Wrapped in try/catch \Throwable to survive any signature * drift in the AI Client builder API. * * @since 2.2.0 * @return array}> * Map of provider ID to its display name and model list. Empty when * the AI Client is unavailable or no provider is configured. */ function ayudawp_aiss_get_available_models() { if ( ! class_exists( '\WordPress\AiClient\AiClient' ) ) { return array(); } $out = array(); try { $registry = \WordPress\AiClient\AiClient::defaultRegistry(); $ids = $registry->getRegisteredProviderIds(); if ( empty( $ids ) ) { return array(); } // Friendly provider display names for the UI. We deliberately avoid // wp_get_connectors() (WP 7.0) so the plugin stays within its declared // minimum WP version; unknown providers fall back to a title-cased ID. $provider_names = array( 'anthropic' => 'Anthropic', 'google' => 'Google', 'openai' => 'OpenAI', ); $requirements = new \WordPress\AiClient\Providers\Models\DTO\ModelRequirements( array( \WordPress\AiClient\Providers\Models\Enums\CapabilityEnum::textGeneration() ), array() ); foreach ( $ids as $provider_id ) { if ( ! $registry->isProviderConfigured( $provider_id ) ) { continue; } try { $models_metadata = $registry->findProviderModelsMetadataForSupport( $provider_id, $requirements ); } catch ( \Throwable $e ) { // One provider's live lookup failing must not break the others. continue; } $models = array(); foreach ( $models_metadata as $model_metadata ) { $models[] = array( 'id' => $model_metadata->getId(), 'name' => $model_metadata->getName(), ); } if ( empty( $models ) ) { continue; } $out[ $provider_id ] = array( 'name' => isset( $provider_names[ $provider_id ] ) ? $provider_names[ $provider_id ] : ucfirst( $provider_id ), 'models' => $models, ); } } catch ( \Throwable $e ) { return array(); } return $out; } /** * Resolve the ordered "Automatic" model candidate chain for summary generation * * When the admin leaves the model selector on "Automatic", we must not let the * WP AI Client fall back to its first discovered candidate, which is the newest * (and usually most expensive, sometimes access-restricted) flagship — exactly * what broke summaries when Claude Fable 5 / Sonnet 5 shipped and became the * first Anthropic candidates while being unavailable to, or pricier for, most * accounts. * * Instead we bias toward the cheapest tier of each provider (Haiku, Flash-Lite, * Nano...), matched by ID substring so the choice survives model-ID changes * (date suffixes, new minor versions). We return the FULL ordered chain — every * matching model, cheapest tier first, then any leftover models the patterns did * not match — as [providerId, modelId] tuples. Emitting tuples lets the builder * resolve the provider directly, and returning the whole chain (not a single * pick) gives generate_with_wp_ai_client() real fallbacks to degrade through * when a listed model turns out not to be usable. * * @since 2.2.0 * @since 2.2.1 Returns the full ordered chain as [providerId, modelId] tuples, * cheapest tier first, with a catch-all so a renamed catalog never * yields an empty list. * @param array|null $available Optional pre-fetched ayudawp_aiss_get_available_models() output. * @return array Ordered [providerId, modelId] tuples. May be empty. */ function ayudawp_aiss_resolve_default_model_preference( $available = null ) { if ( null === $available ) { $available = ayudawp_aiss_get_available_models(); } if ( empty( $available ) ) { return array(); } /** * Filters the per-provider model preference patterns for "Automatic" mode. * * Each provider maps to an ordered list of case-insensitive substrings, from * the cheapest/smallest tier to the priciest. Every model whose ID contains a * pattern is added in that order; models matching no pattern are appended * afterwards so the chain is never empty. Add entries for other providers to * bias their defaults too. * * @since 2.2.0 * @since 2.2.1 Ordered cheapest tier first; all matches are kept (no longer * just the first), and unmatched models are appended as a * catch-all. * @param array> $patterns Provider ID => ordered ID substrings. */ $patterns = apply_filters( 'ayudawp_aiss_default_models', array( 'anthropic' => array( 'haiku', 'sonnet', 'opus' ), 'google' => array( 'flash-lite', 'flash', 'pro' ), 'openai' => array( 'nano', 'mini' ), ) ); $preference = array(); foreach ( $available as $provider_id => $data ) { if ( empty( $data['models'] ) ) { continue; } $ordered = array(); $seen = array(); // 1) Add models matching the known tiers, cheapest first. if ( ! empty( $patterns[ $provider_id ] ) ) { foreach ( $patterns[ $provider_id ] as $needle ) { $needle = strtolower( (string) $needle ); if ( '' === $needle ) { continue; } foreach ( $data['models'] as $model ) { $id = (string) $model['id']; if ( ! isset( $seen[ $id ] ) && false !== strpos( strtolower( $id ), $needle ) ) { $ordered[] = $id; $seen[ $id ] = true; } } } } // 2) Catch-all: append any remaining models in provider order, so a // renamed catalog (no pattern matches) still yields candidates. foreach ( $data['models'] as $model ) { $id = (string) $model['id']; if ( ! isset( $seen[ $id ] ) ) { $ordered[] = $id; $seen[ $id ] = true; } } foreach ( $ordered as $id ) { $preference[] = array( $provider_id, $id ); } } return $preference; } /** * Get platform brand colors configuration * * Centralized color definitions used across CSS, JS, and admin. * * @since 1.6.0 * @return array Platform colors as key => hex pairs. */ function ayudawp_aiss_get_platform_colors() { return array( 'twitter' => '#000000', 'linkedin' => '#0077b5', 'facebook' => '#1877f2', 'telegram' => '#26A5E4', 'whatsapp' => '#25d366', 'email' => '#6c757d', 'raindrop' => '#0F66E3', 'reddit' => '#FF4500', 'bluesky' => '#0085FF', 'line' => '#00C300', 'mastodon' => '#6364FF', 'threads' => '#000000', 'pinterest' => '#BD081C', 'claude' => '#D97757', 'chatgpt' => '#10a37f', 'google_ai' => '#E84430', 'gemini' => '#8E75B2', 'grok' => '#000000', 'perplexity' => '#20808D', 'deepseek' => '#4D6BFE', 'mistral' => '#F7D046', 'copilot' => '#6264A7', 'qwen' => '#615CED', 'meta_ai' => '#0081FB', 'copy_link' => '#5F6C7B', 'web_share' => '#2F6FED', // Google blue; white text on it measures 4.5:1, the AA minimum. 'google_preferred' => '#1A73E8', ); } /** * Colour, type and name of every platform, for the analytics screens (v2.6.0) * * One place for what the analytics page and the dashboard widget used to keep * in maps of their own, which fell behind every time a button was added. * Platforms no longer offered keep their raw key on screen, as before. * * @since 2.6.0 * @return array Platform key => array( name, color, type ). */ function ayudawp_aiss_get_platforms_meta() { $colors = ayudawp_aiss_get_platform_colors(); $ai = ayudawp_aiss_get_ai_buttons(); $meta = array(); foreach ( ayudawp_aiss_get_available_buttons() as $key => $name ) { $meta[ $key ] = array( 'name' => $name, 'color' => isset( $colors[ $key ] ) ? $colors[ $key ] : '#999999', 'type' => in_array( $key, $ai, true ) ? 'ai' : 'social', ); } return $meta; } /** * Allowed tags for the plugin's own inline SVG icons (v2.5.0) * * The icons are literal strings in AyudaWP_AISS_Icons with no user input in * them, so they were printed raw behind a `phpcs:ignore`. Running them through * wp_kses() instead costs nothing measurable, removes two security-sniff * suppressions, and means a future icon carrying something it should not is * stripped rather than printed. * * @since 2.5.0 * @return array Allowlist for wp_kses(). */ function ayudawp_aiss_kses_svg() { return array( 'svg' => array( 'width' => array(), 'height' => array(), 'viewbox' => array(), 'fill' => array(), 'role' => array(), 'class' => array(), 'aria-hidden' => array(), 'xmlns' => array(), ), 'path' => array( 'd' => array(), 'fill' => array(), 'stroke' => array(), 'fill-rule' => array(), 'clip-rule' => array(), ), 'circle' => array( 'cx' => array(), 'cy' => array(), 'r' => array(), 'fill' => array(), 'stroke' => array(), ), 'rect' => array( 'x' => array(), 'y' => array(), 'width' => array(), 'height' => array(), 'rx' => array(), 'ry' => array(), 'fill' => array(), ), 'polyline' => array( 'points' => array() ), 'polygon' => array( 'points' => array(), 'fill' => array() ), 'g' => array( 'fill' => array() ), 'span' => array( 'class' => array() ), ); } /** * Allowed tags for a rendered block of share buttons (v2.5.0) * * Used by the floating bar, the one place that echoes a composed buttons * string instead of returning it through the_content. Every attribute the * button markup can carry is listed here; anything else is dropped, so a * regression in the builder cannot print an attribute nobody reviewed. * * @since 2.5.0 * @return array Allowlist for wp_kses(). */ function ayudawp_aiss_kses_buttons() { $button_attributes = array( 'class' => array(), 'id' => array(), 'data-url' => array(), 'data-platform' => array(), 'data-title' => array(), 'data-post-id' => array(), 'data-autohide' => array(), 'aria-label' => array(), 'aria-labelledby' => array(), 'aria-controls' => array(), 'aria-expanded' => array(), 'aria-hidden' => array(), 'role' => array(), 'hidden' => array(), 'style' => array(), ); $link_attributes = array_merge( $button_attributes, array( 'href' => array(), 'target' => array(), 'rel' => array(), ) ); return array_merge( ayudawp_aiss_kses_svg(), array( 'div' => $button_attributes, 'a' => $link_attributes, 'button' => array_merge( $button_attributes, array( 'type' => array() ) ), 'span' => $button_attributes, ) ); } /** * Allowed tags for the blocks printed in a theme action (v2.6.0) * * What ayudawp_aiss_kses_buttons() allows, plus the tags the inline block * uses for its titles (headings and paragraphs, which the floating bar never * prints) and everything the summary box and its "Generate" button use, since * the summary can be glued to the buttons. Tags present in both lists get the * attributes of both. * * @since 2.6.0 * @return array Allowed HTML for wp_kses(). */ function ayudawp_aiss_kses_content_blocks() { $allowed = ayudawp_aiss_kses_buttons(); $title = array( 'id' => array(), 'class' => array(), ); $class = array( 'class' => array() ); $extra = array( 'h2' => $title, 'h3' => $title, 'h4' => $title, 'h5' => $title, 'h6' => $title, 'p' => $title, 'aside' => array( 'class' => array(), 'style' => array(), 'role' => array(), 'aria-label' => array(), 'data-provider' => array(), 'data-post-id' => array(), 'itemscope' => array(), 'itemtype' => array(), ), 'div' => array( 'class' => array(), 'data-nosnippet' => array(), 'itemprop' => array(), ), 'meta' => array( 'itemprop' => array(), 'content' => array(), ), 'details' => array( 'class' => array(), 'open' => array(), ), 'button' => array( 'type' => array(), 'class' => array(), ), 'span' => array( 'class' => array(), 'aria-hidden' => array(), ), 'summary' => $class, 'ul' => $class, 'li' => $class, 'small' => $class, ); foreach ( $extra as $tag => $attributes ) { $allowed[ $tag ] = isset( $allowed[ $tag ] ) ? array_merge( $allowed[ $tag ], $attributes ) : $attributes; } return $allowed; } /** * Buttons that act on the current page instead of opening a destination (v2.5.0) * * "Copy link" writes to the clipboard and "Native share" opens the operating * system share sheet. They are rendered as