'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',
);
}
/**
* 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' );
}
/**
* 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
* @param string $style Untrusted tag name (validated against the allowlist).
* @param string $class CSS class(es) for the element (escaped here).
* @return string Opening tag, e.g. '
List of post type slugs.
*/
function ayudawp_aiss_get_summary_post_types() {
$options = get_option( 'ayudawp_aiss_options', array() );
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'] ) ) );
}
if ( isset( $options['auto_insert_content_types'] ) && is_array( $options['auto_insert_content_types'] ) ) {
return array_values( array_filter( array_map( 'strval', $options['auto_insert_content_types'] ) ) );
}
return array( 'post' );
}
/**
* Check whether the AI summary feature applies to a given post (v2.0.0)
*
* Combines the per-post exclusion (meta box / noindex) with the new
* `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
* @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_post_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 );
}
/**
* 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',
);
}
/**
* Enqueue frontend CSS, JS and localized data
*
* Centralised helper so both auto-insert (class-frontend) and
* shortcode (class-shortcode) share the same enqueue logic.
* Safe to call multiple times — assets are only loaded once.
*
* @since 1.7.2
*/
function ayudawp_aiss_enqueue_frontend_assets() {
if ( wp_style_is( 'ayudawp-aiss-styles', 'enqueued' ) ) {
return;
}
wp_enqueue_style(
'ayudawp-aiss-styles',
AYUDAWP_AISS_PLUGIN_URL . 'assets/css/ai-share-summarize.css',
array(),
AYUDAWP_AISS_VERSION
);
wp_enqueue_script(
'ayudawp-aiss-scripts',
AYUDAWP_AISS_PLUGIN_URL . 'assets/js/ai-share-summarize.js',
array( 'jquery' ),
AYUDAWP_AISS_VERSION,
true
);
$options = get_option( 'ayudawp_aiss_options', array() );
$dark_adapt_mode = isset( $options['dark_mode_adaptation'] ) ? $options['dark_mode_adaptation'] : 'disabled';
wp_localize_script( 'ayudawp-aiss-scripts', 'ayudawpAissL10n', array(
'promptCopied' => __( 'Prompt copied to clipboard!', 'ai-share-summarize' ),
// Generic copy-prompt tooltips.
'copyPromptShort' => __( 'Copy prompt & open', 'ai-share-summarize' ),
'copyPromptLong' => __( 'Copy prompt and open', 'ai-share-summarize' ),
// Gemini specific tooltips.
'geminiTooltipShort' => __( 'Copy prompt & open', 'ai-share-summarize' ),
'geminiTooltipLong' => __( 'Copy prompt and open Gemini', 'ai-share-summarize' ),
// DeepSeek specific tooltips.
'deepseekTooltipShort' => __( 'Copy prompt & open', 'ai-share-summarize' ),
'deepseekTooltipLong' => __( 'Copy prompt and open DeepSeek', 'ai-share-summarize' ),
// Copilot specific tooltips.
'copilotTooltipShort' => __( 'Copy prompt & open', 'ai-share-summarize' ),
'copilotTooltipLong' => __( 'Copy prompt and open Copilot', 'ai-share-summarize' ),
// Platform names for tooltips.
'platformNames' => array(
'twitter' => 'X (Twitter)',
'linkedin' => 'LinkedIn',
'facebook' => 'Facebook',
'telegram' => 'Telegram',
'whatsapp' => 'WhatsApp',
'email' => __( 'Email', 'ai-share-summarize' ),
'raindrop' => 'Raindrop',
'reddit' => 'Reddit',
'bluesky' => 'Bluesky',
'line' => 'LINE',
'claude' => 'Claude AI',
'chatgpt' => 'ChatGPT',
'google_ai' => 'Google AI',
'gemini' => 'Gemini',
'grok' => 'Grok',
'perplexity' => 'Perplexity',
'deepseek' => 'DeepSeek',
'mistral' => 'Mistral AI',
'copilot' => 'Microsoft Copilot',
),
// Analytics click tracking (v1.5.0) - no nonce needed for public counter.
'trackUrl' => esc_url_raw( rest_url( 'aiss/v1/track' ) ),
// Dark-mode adaptation: 'disabled' (default) or 'auto'
// ('auto' enables the JS background detector that toggles .is-on-dark).
'darkAdaptMode' => $dark_adapt_mode,
// Public AI summary generation (v2.0.0).
'publicGenerateUrl' => esc_url_raw( rest_url( 'aiss/v1/summary/generate' ) ),
'summaryGenerating' => __( 'Generating…', 'ai-share-summarize' ),
'summaryGenerateError' => __( 'Generation failed. Try again.', 'ai-share-summarize' ),
) );
}
/**
* Clean HTML/block content for summarization (v2.0.0)
*
* `wp_strip_all_tags()` on raw post content concatenates table cells and
* code blocks without separators, producing nonsense like:
* "NombreDescripcionCategoriaJardinBilbaoRevisado..."
*
* This helper preprocesses the HTML so the sentence splitter has natural
* breakpoints to work with:
*
* 1. Strips WordPress block comments (``).
* 2. Removes entire blocks that are usually noise for prose summarization
* (tables, code, captions, forms, navigation, embeds).
* 3. Replaces block-level tag boundaries with ". " so sentences from
* different paragraphs/list-items don't get concatenated.
* 4. Strips remaining inline tags via wp_strip_all_tags().
* 5. Normalizes whitespace and collapses runs of dots.
*
* Applied by both the extractive summarizer and the AI Client prompt so
* the LLM also receives clean text instead of table soup.
*
* @since 2.0.0
* @param string $html Raw HTML / block content.
* @return string Cleaned plain text ready to summarize.
*/
function ayudawp_aiss_clean_html_for_summary( $html ) {
$html = (string) $html;
if ( '' === $html ) {
return '';
}
// 0. Remove shortcodes wholesale BEFORE anything else, so the summary
// never includes raw shortcode markup like `[gallery ids="1,2,3"]`,
// `[caption ...]...[/caption]` or any plugin-defined shortcode. We
// cannot `do_shortcode()` here because that would inject the whole
// rendered gallery/embed back into the prose we want to summarize.
$html = strip_shortcodes( $html );
// 1. Strip block comments (Gutenberg leaves them in post_content).
$html = preg_replace( '##s', '', $html );
// 2. Remove noisy elements wholesale. Filterable so themes/plugins
// can add custom ones (e.g. an "advertisement" shortcode wrapper).
$noisy = apply_filters(
'ayudawp_aiss_summary_noisy_tags',
array( 'table', 'pre', 'code', 'figcaption', 'figure', 'form', 'button', 'nav', 'script', 'style', 'iframe', 'svg', 'audio', 'video' )
);
if ( ! empty( $noisy ) ) {
$pattern = '#<(' . implode( '|', array_map( 'preg_quote', $noisy ) ) . ')\b[^>]*>.*?\1>#is';
$html = preg_replace( $pattern, ' ', $html );
}
// 3. Replace block-level tag boundaries (open AND close) with a
// sentence separator so prose from different blocks doesn't get
// concatenated by wp_strip_all_tags().
$boundaries = 'p|h[1-6]|li|div|td|th|tr|br|hr|blockquote|aside|section|article|header|footer|dd|dt';
$html = preg_replace( '#?(?:' . $boundaries . ')(?:\s[^>]*)?/?>#i', '. ', $html );
// 4. Strip the rest.
$text = wp_strip_all_tags( $html );
// 5. Whitespace normalization + collapse repeated punctuation runs.
$text = preg_replace( '/\s+/u', ' ', $text );
$text = preg_replace( '/(\s*\.\s*){2,}/', '. ', $text ); // ". . . " → ". "
$text = preg_replace( '/\s+\./', '.', $text ); // " ." → "."
$text = preg_replace( '/\.{2,}/', '.', $text ); // ".." → "."
return trim( $text );
}
/**
* Split a block of text into sentences (v2.1.0)
*
* Single source of truth for sentence segmentation, shared by the extractive
* summarizer and the frontend renderer (which prints one per sentence for
* readability). Splits on sentence-final punctuation followed by whitespace and
* an uppercase/opening character, so decimals and most abbreviations inside a
* sentence are not split.
*
* @since 2.1.0
* @param string $text Plain text (already stripped of HTML).
* @return array Trimmed, non-empty sentences in original order.
*/
function ayudawp_aiss_split_sentences( $text ) {
$text = trim( (string) $text );
if ( '' === $text ) {
return array();
}
$parts = preg_split( '/(?<=[.!?])\s+(?=[A-ZÁÉÍÓÚÑ¿¡"\'])/u', $text );
if ( ! is_array( $parts ) ) {
return array( $text );
}
$parts = array_map( 'trim', $parts );
return array_values( array_filter( $parts, 'strlen' ) );
}
/**
* Generate an extractive summary from post content (v2.0.0)
*
* Picks the top N sentences scored by token-frequency (excluding stopwords),
* with a bonus for title words and a lead-position bonus for the first two
* sentences. Returns sentences in their original order so the summary reads
* naturally.
*
* Used as a fallback when WP AI Client is not available, or when the call
* to wp_ai_client_prompt() fails.
*
* @since 2.0.0
* @param string $content Raw post content (HTML accepted, will be stripped).
* @param string $title Post title — its words get a 2x weight bonus.
* @param int $n Target sentence count (clamped to 1..5).
* @return string Summary text, or '' if the content is too short to summarize.
*/
function ayudawp_aiss_extractive_summary( $content, $title, $n = 3 ) {
$text = ayudawp_aiss_clean_html_for_summary( (string) $content );
if ( '' === $text ) {
return '';
}
$sentences = ayudawp_aiss_split_sentences( $text );
if ( count( $sentences ) < 3 ) {
return '';
}
$stopwords = apply_filters( 'ayudawp_aiss_extractive_stopwords', ayudawp_aiss_get_stopwords() );
/**
* Tunable weights. Filterable so themes/plugins can adjust them without
* forking the algorithm.
*
* - lead_bonus: multiplier applied to the first two sentences. Lowered
* from the previous 1.3 because it made the first sentence always
* "win", giving the impression the summarizer hadn't done anything.
* - title_weight: how much extra each title word contributes to its
* frequency score. Raised from 2x to 3x so title-relevant sentences
* stand out more reliably.
* - min_words: discard sentences shorter than this (likely headings
* leaked from the markup, navigation labels, etc).
* - similarity_threshold: Jaccard similarity above which a candidate
* sentence is considered redundant with one already selected.
*
* @since 2.0.0
*/
$weights = apply_filters( 'ayudawp_aiss_extractive_weights', array(
'lead_bonus' => 1.1,
'title_weight' => 3,
'min_words' => 5,
'similarity_threshold' => 0.5,
) );
// Tokenize once per sentence and reuse — Jaccard needs token sets too.
$sentence_tokens = array();
foreach ( $sentences as $i => $sentence ) {
$tokens = preg_split( '/\W+/u', mb_strtolower( $sentence, 'UTF-8' ) );
$tokens = is_array( $tokens ) ? array_values( array_filter( $tokens, 'strlen' ) ) : array();
$sentence_tokens[ $i ] = $tokens;
}
// Build word-frequency table over content-bearing tokens.
$freq = array();
foreach ( $sentence_tokens as $tokens ) {
foreach ( $tokens as $token ) {
if ( isset( $stopwords[ $token ] ) || mb_strlen( $token, 'UTF-8' ) < 3 ) {
continue;
}
$freq[ $token ] = isset( $freq[ $token ] ) ? $freq[ $token ] + 1 : 1;
}
}
// Title words boost their frequency weight.
$title_tokens = preg_split( '/\W+/u', mb_strtolower( (string) $title, 'UTF-8' ) );
if ( is_array( $title_tokens ) ) {
foreach ( $title_tokens as $token ) {
if ( isset( $freq[ $token ] ) ) {
$freq[ $token ] *= (float) $weights['title_weight'];
}
}
}
// Score sentences and remember which are eligible (have enough words).
$scored = array();
$eligible = array();
$jaccard_set = array(); // Per-sentence content-token set used for similarity comparison.
foreach ( $sentence_tokens as $i => $tokens ) {
if ( count( $tokens ) < (int) $weights['min_words'] ) {
continue;
}
$score = 0.0;
$set = array();
foreach ( $tokens as $token ) {
if ( isset( $stopwords[ $token ] ) || mb_strlen( $token, 'UTF-8' ) < 3 ) {
continue;
}
if ( isset( $freq[ $token ] ) ) {
$score += $freq[ $token ];
}
$set[ $token ] = 1;
}
if ( $i < 2 ) {
$score *= (float) $weights['lead_bonus'];
}
$scored[ $i ] = $score;
$eligible[ $i ] = true;
$jaccard_set[ $i ] = $set;
}
if ( empty( $scored ) ) {
return '';
}
arsort( $scored );
$n = max( 1, min( 5, (int) $n ) );
// Greedy MMR-style selection: walk candidates by score descending and
// skip any that are too similar (Jaccard >= threshold) to a sentence
// already picked. Falls back to plain top-N if Jaccard filtering would
// leave us with fewer than N selections.
$threshold = (float) $weights['similarity_threshold'];
$selected = array();
foreach ( array_keys( $scored ) as $idx ) {
if ( count( $selected ) >= $n ) {
break;
}
$is_redundant = false;
foreach ( $selected as $picked ) {
$intersection = count( array_intersect_key( $jaccard_set[ $idx ], $jaccard_set[ $picked ] ) );
$union = count( $jaccard_set[ $idx ] + $jaccard_set[ $picked ] );
$similarity = $union > 0 ? ( $intersection / $union ) : 0;
if ( $similarity >= $threshold ) {
$is_redundant = true;
break;
}
}
if ( ! $is_redundant ) {
$selected[] = $idx;
}
}
// Backfill if the similarity filter was too aggressive.
if ( count( $selected ) < $n ) {
foreach ( array_keys( $scored ) as $idx ) {
if ( count( $selected ) >= $n ) {
break;
}
if ( ! in_array( $idx, $selected, true ) ) {
$selected[] = $idx;
}
}
}
// Restore original sentence order in the output.
sort( $selected );
$pieces = array();
foreach ( $selected as $index ) {
if ( isset( $sentences[ $index ] ) ) {
$pieces[] = $sentences[ $index ];
}
}
return trim( implode( ' ', $pieces ) );
}
/**
* Get the stopwords lookup table for the extractive summarizer (v2.0.0)
*
* Returns an associative array (`'the' => 1`) so callers can use
* `isset( $stopwords[ $token ] )` for O(1) membership checks.
*
* Covers English and Spanish — extend via the `ayudawp_aiss_extractive_stopwords`
* filter to support additional languages.
*
* @since 2.0.0
* @return array Associative array of stopwords.
*/
function ayudawp_aiss_get_stopwords() {
$words = array(
// English.
'the', 'and', 'for', 'are', 'but', 'not', 'you', 'all', 'can', 'her', 'was', 'one', 'our', 'out',
'day', 'get', 'has', 'him', 'his', 'how', 'man', 'new', 'now', 'old', 'see', 'two', 'way', 'who',
'boy', 'did', 'its', 'let', 'put', 'say', 'she', 'too', 'use', 'with', 'this', 'that', 'from',
'they', 'have', 'were', 'been', 'will', 'your', 'what', 'when', 'make', 'like', 'time', 'just',
'know', 'take', 'into', 'some', 'them', 'than', 'then', 'look', 'only', 'come', 'over', 'also',
'back', 'after', 'work', 'first', 'well', 'even', 'want', 'because', 'these', 'give', 'most',
'about', 'which', 'their', 'would', 'there', 'could', 'other', 'should', 'where', 'while',
'being', 'does', 'each', 'such', 'very', 'much', 'many', 'more', 'made', 'said', 'here',
// Spanish.
'los', 'las', 'una', 'uno', 'del', 'que', 'por', 'con', 'para', 'pero', 'sus', 'sin', 'son',
'mas', 'muy', 'ese', 'esa', 'eso', 'este', 'esta', 'esto', 'hay', 'fue', 'ser', 'han', 'has',
'como', 'cuando', 'donde', 'porque', 'sobre', 'entre', 'desde', 'hasta', 'también', 'tambien',
'cada', 'todo', 'todos', 'todas', 'algo', 'algun', 'alguna', 'mucho', 'mucha', 'muchos', 'muchas',
'otro', 'otra', 'otros', 'otras', 'mismo', 'misma', 'tanto', 'tanta', 'poco', 'poca', 'nada',
'ante', 'bajo', 'cabe', 'contra', 'segun', 'según', 'durante', 'mediante', 'hacia',
'aqui', 'aquí', 'alli', 'allí', 'ahi', 'ahí', 'asi', 'así', 'aun', 'aún', 'ya', 'si', 'sí',
'siempre', 'nunca', 'jamás', 'jamas', 'antes', 'despues', 'después', 'luego', 'entonces',
'estaba', 'estaban', 'estamos', 'estar', 'estado', 'estos', 'estas', 'eran', 'era', 'eres',
'haber', 'había', 'habia', 'habían', 'habian', 'hace', 'hacer', 'hecho', 'puede', 'pueden',
'porqué', 'porque', 'cual', 'cuál', 'quien', 'quién', 'cuanto', 'cuánto',
);
return array_fill_keys( $words, 1 );
}