1 ) {
$enabled_buttons = array_values( array_diff( $enabled_buttons, array( 'google_preferred' ) ) );
$google_section = true;
}
}
if ( empty( $enabled_buttons ) ) {
return '';
}
/*
* Compact mode needs something to compact (v2.6.0). With a single
* button, "one Share button that opens the rest" hid that button
* behind a toggle that opened nothing else.
*/
if ( count( $enabled_buttons ) + ( $google_section ? 1 : 0 ) < 2 ) {
$compact_mode = 'disabled';
}
// Determine if we use section titles (override global title).
// Mixed order always uses the general title since groups are interleaved.
$use_section_titles = ( ! empty( $ai_section_title ) || ! empty( $social_section_title ) ) && 'ayudawp_mixed' !== $button_order;
// Determine CSS classes.
$container_classes = array( 'ayudawp-share-buttons' );
if ( 'center' === $button_alignment ) {
$container_classes[] = 'ayudawp-aiss-centered';
}
if ( ! $separator_top ) {
$container_classes[] = 'no-separator-top';
}
if ( ! $separator_bottom ) {
$container_classes[] = 'no-separator-bottom';
}
// Size, content, colours and corners (v2.8.0), shared with the bar.
$container_classes = array_merge( $container_classes, ayudawp_aiss_get_look_classes( $look ) );
/*
* Mobile compact mode (v2.5.0). Everything is decided here in PHP and
* applied by CSS inside the mobile breakpoint, so the desktop render is
* byte-for-byte what it was and the extra markup costs one button. The
* JS only flips `is-expanded` and the aria state.
*/
if ( 'disabled' !== $compact_mode ) {
$container_classes[] = 'is-mobile-compact';
$container_classes[] = 'compact-' . $compact_mode;
}
$style_class = implode( ' ', $container_classes );
self::$uid++;
$block_id = 'ayudawp-aiss-share-' . self::$uid;
// Custom color inline style, escaped where it is built.
$custom_style = ayudawp_aiss_get_custom_colors_attr( $look, $options );
/*
* data-post-id (v2.5.0) so click tracking knows which post it is
* counting without guessing from the theme's `article id="post-N"`,
* which only some themes emit.
*/
$block_post_id = null === $post_id ? get_the_ID() : (int) $post_id;
$post_id_attr = $block_post_id ? ' data-post-id="' . esc_attr( (string) $block_post_id ) . '"' : '';
$html = '
';
// Global title (only if no section titles). Carries an id so the
// buttons group can point at it with aria-labelledby (v2.5.0).
$global_title_id = '';
if ( ! $use_section_titles && ! empty( trim( $title_text ) ) ) {
$global_title_id = $block_id . '-title';
$html .= ayudawp_aiss_open_tag( $title_style, 'ayudawp-title', $global_title_id ) . esc_html( $title_text ) . ayudawp_aiss_close_tag( $title_style );
}
// Running index across every group, so "N visible + more" counts
// buttons in render order and not per section.
$rendered = 0;
// Split buttons into groups for section titles.
if ( $use_section_titles ) {
$social_buttons_list = ayudawp_aiss_get_social_buttons();
$ai_buttons_list = ayudawp_aiss_get_ai_buttons();
$social_group = array_values( array_intersect( $enabled_buttons, $social_buttons_list ) );
$ai_group = array_values( array_intersect( $enabled_buttons, $ai_buttons_list ) );
// Determine display order (mixed never reaches here due to $use_section_titles check).
$groups = array();
if ( 'ayudawp_ai_first' === $button_order ) {
if ( ! empty( $ai_group ) ) {
$groups[] = array( 'title' => $ai_section_title, 'buttons' => $ai_group, 'kind' => 'ai' );
}
if ( ! empty( $social_group ) ) {
$groups[] = array( 'title' => $social_section_title, 'buttons' => $social_group, 'kind' => 'social' );
}
} else {
if ( ! empty( $social_group ) ) {
$groups[] = array( 'title' => $social_section_title, 'buttons' => $social_group, 'kind' => 'social' );
}
if ( ! empty( $ai_group ) ) {
$groups[] = array( 'title' => $ai_section_title, 'buttons' => $ai_group, 'kind' => 'ai' );
}
}
foreach ( $groups as $index => $group ) {
$group_id = $block_id . '-group-' . $index;
$title_id = '';
/*
* A section whose every button falls past the visible count is
* hidden along with its title, so the compact render never
* leaves a heading standing over nothing.
*/
$group_hidden = ( 'partial' === $compact_mode && $rendered >= $compact_visible );
if ( ! empty( $group['title'] ) ) {
$title_id = $group_id . '-title';
$section_class = $index > 0 ? ' ayudawp-section-title' : '';
if ( $group_hidden ) {
$section_class .= ' aiss-overflow';
}
$html .= ayudawp_aiss_open_tag( $title_style, 'ayudawp-title' . $section_class, $title_id ) . esc_html( $group['title'] ) . ayudawp_aiss_close_tag( $title_style );
} elseif ( $index > 0 ) {
$html .= '';
}
$html .= '
';
$rendered++;
}
// The compact toggle is rendered whenever the mode is on, but CSS only
// shows it inside the mobile breakpoint — and, in "N visible" mode,
// only when there is something hidden behind it.
if ( 'disabled' !== $compact_mode && ( 'single' === $compact_mode || $rendered > $compact_visible ) ) {
$html .= self::ayudawp_compact_toggle_html( $compact_mode, $block_id );
}
$html .= '
';
return $html;
}
/**
* Accessible name for a buttons group (v2.5.0)
*
* Points at the section title when there is one, so screen readers announce
* the group with the same words sighted visitors read. Without a title the
* group still needs a name, or `role="group"` announces an unnamed group.
*
* @since 2.5.0
* @param string $title_id Id of the title element, or '' when there is none.
* @param string $kind 'ai', 'social', 'mixed' or 'google' (v2.6.0).
* @return string Attribute string, starting with a space.
*/
private static function ayudawp_group_label_attr( $title_id, $kind ) {
if ( '' !== $title_id ) {
return ' aria-labelledby="' . esc_attr( $title_id ) . '"';
}
if ( 'ai' === $kind ) {
$label = __( 'Ask an AI assistant about this content', 'ai-share-summarize' );
} elseif ( 'social' === $kind ) {
$label = __( 'Share this content on social networks', 'ai-share-summarize' );
} elseif ( 'google' === $kind ) {
$label = __( 'Google preferred source', 'ai-share-summarize' );
} else {
$label = __( 'Share this content', 'ai-share-summarize' );
}
return ' aria-label="' . esc_attr( $label ) . '"';
}
/**
* Toggle that expands the compact mobile render (v2.5.0)
*
* Server-rendered so it works with page caching and needs no JS to exist;
* the script only toggles `is-expanded` on the block and keeps
* `aria-expanded` in sync. Hidden on desktop by CSS.
*
* @since 2.5.0
* @param string $mode 'single' or 'partial'.
* @param string $block_id Id of the share block it controls.
* @return string Toggle HTML.
*/
private static function ayudawp_compact_toggle_html( $mode, $block_id ) {
$label = 'single' === $mode
? __( 'Share', 'ai-share-summarize' )
: __( 'More', 'ai-share-summarize' );
/*
* No icon of its own: the caret comes from CSS, the same one the summary
* block uses to say "there is more behind this", and it rotates when the
* block opens. A share glyph here was indistinguishable from the native
* Share button, and an ellipsis was one more symbol for a gesture the
* interface already had a sign for.
*/
return '