Visualizzazione risultati 1 fino 10 di 10

Discussione: Rendere link linkabile

  1. #1
    Guest

    Predefinito Rendere link linkabile

    Quando nel commento del sito viene inserito anche un URL, non appare linkabile, ma come testo.
    Dovrei renderlo linkabile.
    La parte dei commenti come testo è questa: <span class="comment-excerpt">' . $comment_text . '</span>',

    Codice HTML:
    $comment_content = apply_filters( 'better_recent_comments_comment_content',
                        str_replace(
                            array( '{avatar}', '{author}', '{comment}', '{date}', '{post}' ),
                            array(
                                '<span class="comment-avatar">' . $avatar . '</span>',
                                '<span class="comment-author-link">' . $author . '</span>',
                                '<span class="comment-excerpt">' . $comment_text . '</span>',
                                '<span class="comment-date">' . $date . '</span>',
                                '<span class="comment-post">' . $post . '</span>'
                            ),
                            $format
                        ), $comment );
    Ultima modifica di albertifoto : 10-05-2021 alle ore 03.21.25

  2. #2
    GraphOGLRisorse non è connesso AlterGuru 2500
    Data registrazione
    14-02-2015
    Messaggi
    4,047

    Predefinito

    Salve,
    le ho fatto un semplice esempio che mostra come rendere cliccabli gli url all'interno di una stringa:
    Codice PHP:
    <?php
    function AddLinkToUrl($str) {
    return
    preg_replace('/(http|https|ftp)?:\/\/[\w\-\.!~#?&=+\*\'"(),\/]+/','<a href="$0">$0</a>',$str);
    }

    $comment_text = "Questi sono esempi di url cliccabili all'interno di un testo: 1 - http://www.exemple.com, 2 - https://www.exemple.com, 3 - ftp://exemple.com";
    echo
    '<span class="comment-excerpt">' . AddLinkToUrl($comment_text) . '</span>';
    ?>
    Cordiali saluti.
    Ultima modifica di GraphOGLRisorse : 10-05-2021 alle ore 04.33.11

  3. #3
    Guest

    Predefinito

    Non riesco ad applicarlo.
    E se invece si mettesse un if? Del tipo: se è testo scrivi testo, se invece è url rendilo linkabile.

    La cosa che però non capisco è che nel textarea lo rende linkabile, mentre il plugin (colonna sinistra "Commenti recenti" lo scrive come testo.
    https://albertifoto.altervista.org/gondole/#comment-9
    Ultima modifica di albertifoto : 10-05-2021 alle ore 12.21.24

  4. #4
    GraphOGLRisorse non è connesso AlterGuru 2500
    Data registrazione
    14-02-2015
    Messaggi
    4,047

    Predefinito

    Citazione Originalmente inviato da albertifoto Visualizza messaggio
    Non riesco ad applicarlo.
    E se invece si mettesse un if? Del tipo: se è testo scrivi testo, se invece è url rendilo linkabile
    In quel modo, funzionerebbe soltanto se la scelta fosse solo tra testo e url. Con testo misto ad url non può funzionare.

    Citazione Originalmente inviato da albertifoto Visualizza messaggio
    La cosa che però non capisco è che nel textarea lo rende linkabile, mentre il plugin (colonna sinistra "Commenti recenti" lo scrive come testo.
    https://albertifoto.altervista.org/gondole/#comment-9
    Come ha inserito nel codice della barra laterale la funzione AddLinkToUrl?
    Può mostrare anche la relativa parte di cidice?

    Cordiali saluti.
    Ultima modifica di GraphOGLRisorse : 10-05-2021 alle ore 12.46.10

  5. #5
    Guest

    Predefinito

    Questo è il file originale Util.php del plugin Better Recent Comments

    Codice HTML:
    <?php
    namespace Barn2\Plugin\Better_Recent_Comments;
    
    /**
     * This class provides utility functions for the Better Recent Comments plugin.
     *
     * @package   Barn2\better-recent-comments
     * @author    Barn2 Plugins <support@barn2.co.uk>
     * @license   GPL-3.0
     * @copyright Barn2 Media Ltd
     */
    class Util {
    
        public static function default_shortcode_args() {
            return array(
                'number'      => 5,
                'format'      => self::get_comment_format(),
                'date_format' => 'j M Y, H:i',
                'avatar_size' => 50,
                'post_status' => 'publish',
                'post_type'   => '',
                'excerpts'    => true
            );
        }
    
        public static function get_comment_format( $date = true, $comment = true, $post_link = true, $avatar = false ) {
            $format = '';
    
            if ( $avatar ) {
                $format .= '{avatar} ';
            }
            if ( $post_link ) {
                //* translators: comments widget: 1: comment author, 2: post link */
                $format .= sprintf( _x( '%1$s on %2$s', 'recent comment', 'better-recent-comments' ), '{author}', '{post}' );
            } else {
                $format .= '{author}';
            }
            if ( $comment ) {
                //$format .= '&ldquo;{comment}&rdquo;';
                $format .= '{comment}';
            }
            if ( $date ) {
                $format .= ' {date}';
            }
    
            return apply_filters( 'better_recent_comments_comment_format', $format );
        }
    
        public static function get_recent_comments( $args ) {
            $defaults = self::default_shortcode_args();
            $args     = wp_parse_args( $args, $defaults );
    
            // Sanitize post status used to retrieve comments
            $post_status = array_filter( array_map( 'sanitize_key', explode( ',', $args['post_status'] ) ) );
    
            $comment_args = array(
                'number'      => absint( filter_var( $args['number'], FILTER_VALIDATE_INT ) ),
                'status'      => 'approve',
                'post_status' => $post_status,
                'post_type'   => sanitize_key( $args['post_type'] ),
                'type'        => apply_filters( 'better_recent_comments_comment_type', 'comment' )
            );
    
            if ( class_exists( '\SitePress' ) ) {
                // WPML active - get all published posts & pages in the current language
                $posts_current_lang = get_posts( apply_filters( 'better_recent_comments_post_args_wpml', array(
                    'post_type'        => array( 'post', 'page' ),
                    'posts_per_page'   => 2000,
                    'post_status'      => $post_status,
                    'fields'           => 'ids',
                    'suppress_filters' => false // Ensure WPML filters run on this query
                    ) ) );
    
                if ( $posts_current_lang ) {
                    $comment_args['post__in'] = $posts_current_lang;
                }
            }
    
            // Get recent comments limited to post IDs above
            $comments = get_comments( apply_filters( 'better_recent_comments_comment_args', $comment_args ) );
    
            $output              = $comment_item_style  = '';
            $comments_list_class = 'recent-comments-list';
    
            // Use .recentcomments class on li's to match WP_Recent_Comments widget
            $comment_li_fmt = apply_filters( 'better_recent_comments_li_format', '<li class="recentcomments recent-comment"><div class="comment-wrap"%s>%s</div></li>' );
    
            if ( is_array( $comments ) && $comments ) {
                // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
                $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
                _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
    
                $format      = empty( $args['format'] ) ? self::get_comment_format() : $args['format'];
                $date_format = empty( $args['date_format'] ) ? $defaults['date_format'] : $args['date_format'];
    
                $link_from   = self::comment_link_from( $format );
                $excerpts    = isset( $args['excerpts'] ) ? filter_var( $args['excerpts'], FILTER_VALIDATE_BOOLEAN ) : $defaults['excerpts'];
                $avatar_size = empty( $args['avatar_size'] ) ? false : filter_var( $args['avatar_size'], FILTER_VALIDATE_INT );
                $link_fmt    = apply_filters( 'better_recent_comments_comment_link_format', '<a href="%s">%s</a>' );
    
                if ( ! $avatar_size ) {
                    $avatar_size = $defaults['avatar_size'];
                }
    
                if ( strpos( $format, '{avatar}' ) !== false ) {
                    $comments_list_class .= ' with-avatars';
    
                    if ( apply_filters( 'better_recent_comments_enable_avatar_inline_css', true ) ) {
                        $comment_item_style = sprintf( ' style="padding-left:%1$upx; min-height:%2$upx;"', \round( $avatar_size + ($avatar_size / 4) ), $avatar_size
                            + 4 );
                    }
                }
    
                foreach ( (array) $comments as $comment ) {
                    $avatar = apply_filters( 'better_recent_comments_avatar', get_avatar( $comment, $avatar_size ), $comment );
                    $author = apply_filters( 'better_recent_comments_comment_author_link', get_comment_author_link( $comment->comment_ID ), $comment );
                    $date   = apply_filters( 'better_recent_comments_comment_date', get_comment_date( $date_format, $comment->comment_ID ), $comment, $date_format );
                    $post   = apply_filters( 'better_recent_comments_post_title', get_the_title( $comment->comment_post_ID ), $comment );
    
                    if ( $excerpts ) {
                        $comment_text = get_comment_excerpt( $comment->comment_ID );
                    } else {
                        if ( apply_filters( 'better_recent_comments_strip_formatting', true ) ) {
                            $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
                        } else {
                            //todo: Remove this option and always strip formatting?
                            $comment_text = wpautop( $comment->comment_content );
                        }
                    }
    
                    $comment_text = apply_filters( 'better_recent_comments_comment_text', $comment_text, $comment );
                    $comment_url  = apply_filters( 'better_recent_comments_comment_url', esc_url( get_comment_link( $comment->comment_ID ) ) );
    
                    if ( 'post' === $link_from ) {
                        $post = sprintf( $link_fmt, $comment_url, $post );
                    } elseif ( 'date' === $link_from ) {
                        $date = sprintf( $link_fmt, $comment_url, $date );
                    } elseif ( 'comment' === $link_from ) {
                        $comment_text = sprintf( $link_fmt, $comment_url, $comment_text );
                    }
    
                    $comment_content = apply_filters( 'better_recent_comments_comment_content',
                        str_replace(
                            array( '{avatar}', '{author}', '{comment}', '{date}', '{post}' ),
                            array(
                                '<span class="comment-avatar">' . $avatar . '</span>',
                                '<span class="comment-author-link">' . $author . '</span>',
                                '<span class="comment-excerpt">' . $comment_text . '</span>',
                                '<span class="comment-date">' . $date . '</span>',
                                '<span class="comment-post">' . $post . '</span>'
                            ),
                            $format
                        ), $comment );
    
                    $output .= sprintf( $comment_li_fmt, $comment_item_style, $comment_content );
                } // foreach comment
            } else {
                $output = sprintf( $comment_li_fmt, '', __( 'No recent comments available.', 'better-recent-comments' ) );
            } // if comments
    
            return apply_filters( 'better_recent_comments_list', sprintf( '<ul id="better-recent-comments" class="%s">%s</ul>', $comments_list_class, $output ) );
        }
    
        private static function comment_link_from( $format ) {
            $link_from = 'post';
            if ( false === strpos( $format, 'post' ) ) {
                $link_from = 'date';
                if ( false === strpos( $format, 'date' ) ) {
                    $link_from = 'comment';
                }
            }
            return apply_filters( 'better_recent_comments_link_from', $link_from );
        }
    
    }

  6. #6
    darbula non è connesso AlterGuru 2500
    Data registrazione
    24-04-2011
    Messaggi
    2,894

    Predefinito

    Se è presente una url dovrai modificare in '<a href="url">url<span class="comment-excerpt">' . $comment_text . '</span></a>'
    Quindi per prima cosa dovrai filtrare il commento poi giustamente usi esc_url() (wordpress). Sanificazione dati sempre e ovunque. https://developer.wordpress.org/refe...tions/esc_url/
    Comunque se guardi il sorgente html dal browser il commento è santificato per output html? Altrimenti esc_html().
    Giustamente per iniziare va bene l'espressione regolare di GraphOGLRisorse ma poi dovrai studiare la codifica WordPress e renderla conforme ad essa. Un buon punto di partenza per creare una sintassi è prendere spunto da esc_url().
    Ultima modifica di darbula : 10-05-2021 alle ore 13.29.46

  7. #7
    Guest

    Predefinito

    Dov'è questo filtro attivo che provo a rimuoverlo?

  8. #8
    GraphOGLRisorse non è connesso AlterGuru 2500
    Data registrazione
    14-02-2015
    Messaggi
    4,047

    Predefinito

    Il filtro dovrebbe essere questo:
    Codice PHP:
    $comment_url = apply_filters( 'better_recent_comments_comment_url', esc_url( get_comment_link( $comment->comment_ID ) ) );
    Provi a rimuovere $comment_url da:
    Codice PHP:
    $comment_text = sprintf( $link_fmt, $comment_url, $comment_text );
    Ho rimosso il mio precedente messaggio, perchè mi sembra potesse creare confusione con il messaggio di darbula.

    Cordiali saluti.
    Ultima modifica di GraphOGLRisorse : 10-05-2021 alle ore 13.57.35

  9. #9
    Guest

    Predefinito

    Ho rimosso $comment_url ma non si linka.
    Nel frattempo ho scritto al creatore del plugin, vediamo cosa dice.

  10. #10
    Guest

    Predefinito

    Mi hanno risposto che il plugin non ha questa opzione, ma vedranno di inserirla nelle prossime versioni.

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •