Skip to:
Content

bbPress.org

Changeset 6440


Ignore:
Timestamp:
05/27/2017 07:53:30 PM (4 years ago)
Author:
johnjamesjacoby
Message:

Common: introduce bbp_has_shortcode() to check if some text contains a bbPress shortcode.

If a shortcode was found in the global post content, a few extra things will happen:

  • The bbp-shortcode class is added to the body tag
  • is_bbpress() will now return true, to help enqueue additional styles & scripts on those pages in themes that want to support bbPress natively

Fixes #2996.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/common/template.php

    r6438 r6440  
    11111111        $bbp_classes[] = 'bbp-search-results';
    11121112        $bbp_classes[] = 'forum-search-results';
     1113
     1114    /** Shortcodes ************************************************************/
     1115
     1116    } elseif ( bbp_has_shortcode() ) {
     1117        $bbp_classes[] = 'bbp-shortcode';
    11131118    }
    11141119
     
    11281133    // Filter & return
    11291134    return (array) apply_filters( 'bbp_body_class', $classes, $bbp_classes, $wp_classes, $custom_classes );
     1135}
     1136
     1137/**
     1138 * Check if text contains a bbPress shortcode.
     1139 *
     1140 * Loops through registered bbPress shortcodes and keeps track of which ones
     1141 * were used in a blob of text. If no text is passed, the current global post
     1142 * content is assumed.
     1143 *
     1144 * A preliminary strpos() is performed before looping through each shortcode, to
     1145 * prevent unnecessarily processing.
     1146 *
     1147 * @since 2.6.0
     1148 *
     1149 * @uses is_singular()
     1150 * @uses bbp_get_global_post_field()
     1151 * @uses has_shortcode();
     1152 *
     1153 * @param string $text
     1154 * @return bool
     1155 */
     1156function bbp_has_shortcode( $text = '' ) {
     1157
     1158    // Default return value
     1159    $retval = false;
     1160    $found  = array();
     1161
     1162    // Fallback to global post_content
     1163    if ( empty( $text ) && is_singular() ) {
     1164        $text = bbp_get_global_post_field( 'post_content', 'raw' );
     1165    }
     1166
     1167    // Skip if empty, or string doesn't contain the bbPress shortcode prefix
     1168    if ( ! empty( $text ) && ( false !== strpos( $text, '[bbp' ) ) ) {
     1169
     1170        // Get possible shortcodes
     1171        $codes = array_keys( bbpress()->shortcodes->codes );
     1172
     1173        // Loop through codes
     1174        foreach ( $codes as $code ) {
     1175
     1176            // Looking for shortcode in text
     1177            if ( has_shortcode( $text, $code ) ) {
     1178                $retval  = true;
     1179                $found[] = $code;
     1180            }
     1181        }
     1182    }
     1183
     1184    // Filter & return
     1185    return (bool) apply_filters( 'bbp_has_shortcode', $retval, $found, $text );
    11301186}
    11311187
     
    11511207 * @uses bbp_is_topics_created()
    11521208 * @uses bbp_is_replies_created()
     1209 * @uses bbp_has_shortcode()
     1210 *
    11531211 * @return bool In a bbPress page
    11541212 */
     
    12361294    } elseif ( bbp_is_search_results() ) {
    12371295        $retval = true;
     1296
     1297    /** Shortcodes ************************************************************/
     1298
     1299    } elseif ( bbp_has_shortcode() ) {
     1300        $retval = true;
    12381301    }
    12391302
    12401303    /** Done ******************************************************************/
    12411304
     1305    // Filter & return
    12421306    return (bool) apply_filters( 'is_bbpress', $retval );
    12431307}
Note: See TracChangeset for help on using the changeset viewer.