Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/16/2011 11:33:09 AM (15 years ago)
Author:
boonebgorges
Message:

Modifies bp_create_excerpt() to take account of HTML tag length when truncating long texts. Thanks to CakePHP for the function. Refactors BP use of bp_create_excerpt(), where necessary, to account for new function argument structure. Reworks bp_activity_truncate_entry() so that already-truncated activity items (like blog posts) do not get a Read More link when they contain HTML. Introduces fallback functions for some multibyte string functions, to make bp_create_excerpt() work properly. Thanks to MediaWiki for these fallbacks. Fixes #3452. Props DJPaul for all the help with this patch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-wpabstraction.php

    r4820 r4989  
    2727        }
    2828    }
    29    
     29
    3030    if ( !function_exists( 'update_blog_option' ) ) {
    3131        function update_blog_option( $blog_id, $option_name, $value ) {
     
    7474        return "{$prefix}spam = 0 AND {$prefix}deleted = 0 AND {$prefix}user_status = 0";
    7575}
     76
     77/**
     78 * Multibyte encoding fallback functions
     79 *
     80 * The PHP multibyte encoding extension is not enabled by default. In cases where it is not enabled,
     81 * these functions provide a fallback.
     82 *
     83 * Borrowed from MediaWiki, under the GPLv2. Thanks!
     84 */
     85if ( !function_exists( 'mb_strlen' ) ) {
     86    /**
     87     * Fallback implementation of mb_strlen, hardcoded to UTF-8.
     88     * @param string $str
     89     * @param string $enc optional encoding; ignored
     90     * @return int
     91     */
     92    function mb_strlen( $str, $enc = '' ) {
     93        $counts = count_chars( $str );
     94        $total = 0;
     95
     96        // Count ASCII bytes
     97        for( $i = 0; $i < 0x80; $i++ ) {
     98            $total += $counts[$i];
     99        }
     100
     101        // Count multibyte sequence heads
     102        for( $i = 0xc0; $i < 0xff; $i++ ) {
     103            $total += $counts[$i];
     104        }
     105        return $total;
     106    }
     107}
     108
     109if ( !function_exists( 'mb_strpos' ) ) {
     110    /**
     111     * Fallback implementation of mb_strpos, hardcoded to UTF-8.
     112     * @param $haystack String
     113     * @param $needle String
     114     * @param $offset String: optional start position
     115     * @param $encoding String: optional encoding; ignored
     116     * @return int
     117     */
     118    function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
     119        $needle = preg_quote( $needle, '/' );
     120
     121        $ar = array();
     122        preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
     123
     124        if( isset( $ar[0][1] ) ) {
     125            return $ar[0][1];
     126        } else {
     127            return false;
     128        }
     129    }
     130}
     131
     132if ( !function_exists( 'mb_strrpos' ) ) {
     133    /**
     134     * Fallback implementation of mb_strrpos, hardcoded to UTF-8.
     135     * @param $haystack String
     136     * @param $needle String
     137     * @param $offset String: optional start position
     138     * @param $encoding String: optional encoding; ignored
     139     * @return int
     140     */
     141    function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
     142        $needle = preg_quote( $needle, '/' );
     143
     144        $ar = array();
     145        preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
     146
     147        if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
     148            isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
     149            return $ar[0][count( $ar[0] ) - 1][1];
     150        } else {
     151            return false;
     152        }
     153    }
     154}
     155
    76156?>
Note: See TracChangeset for help on using the changeset viewer.