context['postId'] ?? get_the_ID() ); if ( $post_id <= 0 ) { return; } $url = (string) get_permalink( $post_id ); $title = (string) get_the_title( $post_id ); if ( '' === $url ) { return; } $heading = trim( (string) ( $attributes['heading'] ?? '' ) ); $heading = '' === $heading ? _x( 'Share', 'the share box: its button and its heading', 'ai-eshop-optimizer' ) : $heading; $show_heading = isset( $attributes['showHeading'] ) ? (bool) $attributes['showHeading'] : 'popover' !== ( $attributes['display'] ?? 'inline' ); $heading_html = $show_heading ? '' . esc_html( $heading ) . '' : ''; $wanted = array_filter( array_map( 'trim', explode( ',', strtolower( (string) ( $attributes['channels'] ?? '' ) ) ) ) ); $url_e = rawurlencode( $url ); $title_e = rawurlencode( $title ); $channels = [ 'facebook' => [ __( 'Share on Facebook', 'ai-eshop-optimizer' ), 'https://www.facebook.com/sharer/sharer.php?u=' . $url_e, 'facebook' ], 'x' => [ __( 'Share on X', 'ai-eshop-optimizer' ), 'https://twitter.com/intent/tweet?url=' . $url_e . '&text=' . $title_e, 'x-social' ], 'pinterest' => [ __( 'Pin on Pinterest', 'ai-eshop-optimizer' ), 'https://pinterest.com/pin/create/button/?url=' . $url_e . '&description=' . $title_e, 'pinterest' ], 'whatsapp' => [ __( 'Share on WhatsApp', 'ai-eshop-optimizer' ), 'https://api.whatsapp.com/send?text=' . $title_e . '%20' . $url_e, 'whatsapp' ], 'email' => [ __( 'Share by email', 'ai-eshop-optimizer' ), 'mailto:?subject=' . $title_e . '&body=' . $url_e, 'mail' ], ]; $items = ''; foreach ( $wanted as $key ) { if ( 'copy' === $key ) { $items .= sprintf( '
  • ', esc_attr__( 'Copy link', 'ai-eshop-optimizer' ), rooster_icon( 'link', [ 'width' => 20, 'height' => 20 ] ) ); continue; } if ( ! isset( $channels[ $key ] ) ) { continue; } [ $label, $href, $icon ] = $channels[ $key ]; $item = sprintf( '
  • %4$s
  • ', esc_attr( $key ), esc_url( $href ), esc_attr( $label ), rooster_icon( $icon, [ 'width' => 20, 'height' => 20 ] ) ?: '' . esc_html( ucfirst( $key ) ) . '' ); $items .= 'email' === $key ? '' . $item . '' : $item; } if ( '' === $items ) { return; } if ( 'popover' === ( $attributes['display'] ?? 'inline' ) ) { $panel = wp_unique_id( 'rooster-share-' ); $wrapper = get_block_wrapper_attributes( [ 'class' => 'rooster-share-box rooster-share-box--popover', 'data-wp-interactive' => 'rooster/share', 'data-wp-context' => wp_json_encode( [ 'url' => $url, 'copied' => false, 'copiedLabel' => __( 'Copied', 'ai-eshop-optimizer' ) ] ), ] ); AIEO_Engine_Blocks::out( sprintf( '
    %5$s
    ', $wrapper, esc_attr( $panel ), esc_attr( $heading ), rooster_icon( 'share-2', [ 'width' => 20, 'height' => 20 ] ), $heading_html, $items ) ); return; } $wrapper = get_block_wrapper_attributes( [ 'class' => 'rooster-share-box', 'data-wp-interactive' => 'rooster/share', 'data-wp-context' => wp_json_encode( [ 'url' => $url, 'copied' => false, 'copiedLabel' => __( 'Copied', 'ai-eshop-optimizer' ) ] ), ] ); AIEO_Engine_Blocks::out( sprintf( '
    %s
    ', $wrapper, $heading_html, $items ) );