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; } $display = in_array( $attributes['display'] ?? 'inline', [ 'inline', 'popover', 'column' ], true ) ? (string) ( $attributes['display'] ?? 'inline' ) : 'inline'; $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'] : 'inline' === $display; $heading_html = $show_heading ? '' . esc_html( $heading ) . '' : ''; $wanted = array_filter( array_map( 'trim', explode( ',', strtolower( (string) ( $attributes['channels'] ?? '' ) ) ) ) ); if ( 'column' === $display ) { $wanted = array_merge( [ 'copy' ], array_slice( array_values( array_diff( $wanted, [ 'copy' ] ) ), 0, 2 ) ); } $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' ], ]; $heart = ''; if ( ! empty( $attributes['favourite'] ) && WP_Block_Type_Registry::get_instance()->is_registered( 'aieo/favourite' ) ) { $heart = trim( (string) ( new WP_Block( [ 'blockName' => 'aieo/favourite', 'attrs' => [ 'look' => 'icon' ], 'innerBlocks' => [], 'innerHTML' => '', 'innerContent' => [] ], [ 'postId' => $post_id, 'postType' => (string) get_post_type( $post_id ) ] ) )->render() ); } $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 ( '' !== $heart ) { $items = '
  • ' . $heart . '
  • ' . $items; } if ( 'popover' === $display ) { $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; } if ( 'column' === $display ) { $wrapper = get_block_wrapper_attributes( [ 'class' => 'rooster-share-box rooster-share-box--column', 'data-wp-interactive' => 'rooster/share', 'data-wp-context' => wp_json_encode( [ 'url' => $url, 'copied' => false, 'copiedLabel' => __( 'Copied', 'ai-eshop-optimizer' ) ] ), 'data-rooster-ui' => 'rooster-zero/share-box', ] ); AIEO_Engine_Blocks::out( sprintf( '
    %2$s
    ', $wrapper, $heading_html, esc_attr( $heading ), $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 ) );