Skip to:
Content

BuddyPress.org

Changeset 10830


Ignore:
Timestamp:
05/31/2016 06:32:24 AM (4 years ago)
Author:
r-a-y
Message:

In bp_create_excerpt(), introduce 'strip_tags' and 'remove_links' as arguments.

This will help to craft better plain-text excerpts.

To be used in activity embed excerpts. See #6772.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-template.php

    r10825 r10830  
    784784 * http://book.cakephp.org/view/1469/Text#truncate-1625
    785785 *
    786  * ### Options:
    787  *
    788  * - `ending` Will be used as Ending and appended to the trimmed string.
    789  * - `exact` If false, $text will not be cut mid-word.
    790  * - `html` If true, HTML tags would be handled correctly.
    791  * - `filter_shortcodes` If true, shortcodes will be stripped before truncating.
    792  *
    793786 * @since 1.0.0
     787 * @since 2.6.0 Added 'strip_tags' and 'remove_links' as $options args.
    794788 *
    795789 * @param string $text   String to truncate.
     
    806800 *     @type bool   $filter_shortcodes If true, shortcodes will be stripped.
    807801 *                                     Default: true.
     802 *     @type bool   $strip_tags        If true, HTML tags will be stripped. Default: false.
     803 *                                     Only applicable if $html is set to false.
     804 *     @type bool   $remove_links      If true, URLs will be stripped. Default: false.
     805 *                                     Only applicable if $html is set to false.
    808806 * }
    809807 * @return string Trimmed string.
     
    818816        'exact'             => false,
    819817        'html'              => true,
    820         'filter_shortcodes' => $filter_shortcodes_default
     818        'filter_shortcodes' => $filter_shortcodes_default,
     819        'strip_tags'        => false,
     820        'remove_links'      => false,
    821821    ), 'create_excerpt' );
    822822
     
    904904        }
    905905    } else {
     906        // Strip HTML tags if necessary.
     907        if ( ! empty( $r['strip_tags'] ) ) {
     908            $text = strip_tags( $text );
     909        }
     910
     911        // Remove links if necessary.
     912        if ( ! empty( $r['remove_links'] ) ) {
     913            $text = preg_replace( '#^\s*(https?://[^\s"]+)\s*$#im', '', $text );
     914        }
     915
    906916        if ( mb_strlen( $text ) <= $length ) {
    907             return $text;
     917            /**
     918             * Filters the final generated excerpt.
     919             *
     920             * @since 1.1.0
     921             *
     922             * @param string $truncate      Generated excerpt.
     923             * @param string $original_text Original text provided.
     924             * @param int    $length        Length of returned string, including ellipsis.
     925             * @param array  $options       Array of HTML attributes and options.
     926             */
     927            return apply_filters( 'bp_create_excerpt', $text, $original_text, $length, $options );
    908928        } else {
    909929            $truncate = mb_substr( $text, 0, $length - mb_strlen( $ending ) );
     
    9861006    }
    9871007
    988     /**
    989      * Filters the final generated excerpt.
    990      *
    991      * @since 1.1.0
    992      *
    993      * @param string $truncate      Generated excerpt.
    994      * @param string $original_text Original text provided.
    995      * @param int    $length        Length of returned string, including ellipsis.
    996      * @param array  $options       Array of HTML attributes and options.
    997      */
     1008    /** This filter is documented in /bp-core/bp-core-template.php */
    9981009    return apply_filters( 'bp_create_excerpt', $truncate, $original_text, $length, $options );
    9991010}
Note: See TracChangeset for help on using the changeset viewer.