Making WordPress.org

Changeset 3931


Ignore:
Timestamp:
09/02/2016 08:59:31 PM (8 years ago)
Author:
coffee2code
Message:

Profiles Activity Notifier: Improve reply excerpt generation.

  • Use newly introduced get_reply_excerpt() instead of bbp_get_reply_excerpt().
  • Strip all blockquoted text from excerpts since that text is not original to the reply.
  • Trim reply excerpts by words (15) instead of by characters.
  • Add trim_text() to handle trimming text by words or by characters.
  • If trimming by characters, honor requested excerpt length for multibyte strings.

Fixes #1953.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-profiles-wp-activity-notifier/wporg-profiles-wp-activity-notifier.php

    r3920 r3931  
    307307                'title'     => bbp_get_reply_topic_title( $reply_id ) ,
    308308                'url'       => bbp_get_reply_url( $reply_id ),
    309                 'message'   => bbp_get_reply_excerpt( $reply_id, 55 ),
     309                'message'   => $this->get_reply_excerpt( $reply_id, 15 ),
    310310                'site'      => get_bloginfo( 'name' ),
    311311                'site_url'  => site_url(),
     
    335335    }
    336336
     337    /**
     338     * Returns the excerpt of the reply.
     339     *
     340     * This is similar to `bbp_get_reply_excerpt()` except:
     341     *
     342     * - Excerpt length is by number of words and not number of characters.
     343     * - Omits inclusion of any blockquoted text.
     344     *
     345     * @param int $reply_id Optional. The reply id.
     346     * @param int $words    Optional. The number of words for the excerpt. Default 15.
     347     * @return string
     348     */
     349    public function get_reply_excerpt( $reply_id = 0, $words = 15 ) {
     350        $reply_id = bbp_get_reply_id( $reply_id );
     351        $excerpt  = get_post_field( 'post_excerpt', $reply_id );
     352
     353        if ( ! $excerpt ) {
     354            $excerpt = bbp_get_reply_content( $reply_id );
     355        }
     356
     357        $excerpt = $this->trim_text( $excerpt, $words, 'words' );
     358        return apply_filters( 'bbp_get_reply_excerpt', $excerpt, $reply_id, $words );
     359    }
     360
     361    /**
     362     * Trims text by words or characters.
     363     *
     364     * @param string $text       The text to trim.
     365     * @param int    $length     Optional. The number of words or characters to try down to. Default 15.
     366     * @param string $trim_style Optional. The manner in which the text should be trimmed. Either 'chars' or 'words'. Default 'words'.
     367     * @return string
     368     */
     369    public function trim_text( $text, $length = 15, $trim_style = 'words' ) {
     370        $length     = (int) $length;
     371        $trim_style = in_array( $trim_style, array( 'chars', 'words' ) ) ? $trim_style : 'words';
     372
     373        // Remove blockquoted text since the text isn't original.
     374        $text = preg_replace( '/<blockquote>.+<\/blockquote>/', '', $text );
     375
     376        // Strip tags and surrounding whitespace.
     377        $text = trim ( strip_tags( $text ) );
     378
     379        // If trimming by chars, behave like a more multibyte-aware
     380        // bbp_get_reply_excerp().
     381        if ( 'chars' === $trim_style ) {
     382            // Multibyte support
     383            if ( function_exists( 'mb_strlen' ) ) {
     384                $text_length = mb_strlen( $text );
     385            } else {
     386                $text_length = strlen( $text );
     387            }
     388
     389            if ( $length && ( $text_length > $length ) ) {
     390                if ( function_exists( 'mb_strlen' ) ) {
     391                    $text = mb_substr( $text, 0, $length - 1 );
     392                } else {
     393                    $text = substr( $text, 0, $length - 1 );
     394                }
     395                $text .= '&hellip;';
     396            }
     397        }
     398        // Else trim by words.
     399        else {
     400            $text = wp_trim_words( $text, $length );
     401        }
     402
     403        return $text;
     404    }
     405
    337406}
    338407
Note: See TracChangeset for help on using the changeset viewer.