Skip to:
Content

bbPress.org


Ignore:
Timestamp:
04/01/2011 01:33:09 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Adjustments to login and register forms to improve behavior and functionality.
Various documentation fixes.
Rename _bbp_topic_status meta to _bbp_status, and add migration routine to updater.
Sanity checks on $wp_query in bbp_pre_get_posts.

Fixes #1476, #1493. Props GautamGupta for original diff.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-includes/bbp-general-functions.php

    r2957 r2970  
    116116/**
    117117 * The plugin version of bbPress comes with two topic display options:
    118  *     ~ Traditional - Topics are included in the reply loop (default)
    119  *     ~ New Style   - Topics appear as "lead" posts, ahead of replies
     118 * - Traditional: Topics are included in the reply loop (default)
     119 * - New Style: Topics appear as "lead" posts, ahead of replies
    120120 *
    121121 * @since bbPress (r2954)
    122122 *
    123  * @global obj $bbp
    124123 * @param $show_lead Optional. Default false
    125  * @return bool
     124 * @return bool Yes if the topic appears as a lead, otherwise false
    126125 */
    127126function bbp_show_lead_topic( $show_lead = false ) {
     
    158157 * Append 'view=all' to query string if it's already there from referer
    159158 *
    160  * @param string $original_link
    161  * @return <type>
     159 * @param string $original_link Original Link to be modified
     160 * @uses current_user_can() To check if the current user can moderate
     161 * @uses add_query_arg() To add args to the url
     162 * @uses apply_filters() Calls 'bbp_add_view_all' with the link and original link
     163 * @return string The link with 'view=all' appended if necessary
    162164 */
    163165function bbp_add_view_all( $original_link ) {
     
    202204 * @param array $data Post data
    203205 * @param array $postarr Original post array (includes post id)
     206 * @uses bbp_get_topic_post_type() To get the topic post type
     207 * @uses bbp_get_reply_post_type() To get the reply post type
    204208 * @uses bbp_is_topic_anonymous() To check if the topic is by an anonymous user
    205209 * @uses bbp_is_reply_anonymous() To check if the reply is by an anonymous user
     
    259263 *  - count_empty_tags: Count empty tags?
    260264 * @uses bbp_count_users() To count the number of registered users
     265 * @uses bbp_get_forum_post_type() To get the forum post type
     266 * @uses bbp_get_topic_post_type() To get the topic post type
     267 * @uses bbp_get_reply_post_type() To get the reply post type
    261268 * @uses wp_count_posts() To count the number of forums, topics and replies
    262269 * @uses wp_count_terms() To count the number of topic tags
     
    537544    // Assign variables
    538545    $defaults = array (
    539         'bbp_anonymous_name'    => $_POST['bbp_anonymous_name'],
    540         'bbp_anonymous_email'   => $_POST['bbp_anonymous_email'],
    541         'bbp_anonymous_website' => $_POST['bbp_anonymous_website'],
    542         'bbp_anonymous_ip'      => $_SERVER['REMOTE_ADDR']
     546        'bbp_anonymous_name'    => !empty( $_POST['bbp_anonymous_name']    ) ? $_POST['bbp_anonymous_name']    : false,
     547        'bbp_anonymous_email'   => !empty( $_POST['bbp_anonymous_email']   ) ? $_POST['bbp_anonymous_email']   : false,
     548        'bbp_anonymous_website' => !empty( $_POST['bbp_anonymous_website'] ) ? $_POST['bbp_anonymous_website'] : false,
     549        'bbp_anonymous_ip'      => !empty( $_SERVER['REMOTE_ADDR']         ) ? $_SERVER['REMOTE_ADDR']         : false
    543550    );
    544551
     
    680687 *
    681688 * @uses bbp_is_user_profile_page() To check if it's a profile page
     689 * @uses apply_filters() Calls 'bbp_profile_templates' with the profile
     690 *                        templates array
    682691 * @uses bbp_is_user_profile_edit() To check if it's a profile edit page
     692 * @uses apply_filters() Calls 'bbp_profile_edit_templates' with the profile
     693 *                        edit templates array
     694 * @uses bbp_is_view() To check if it's a view page
     695 * @uses bbp_get_view_id() To get the view id
     696 * @uses apply_filters() Calls 'bbp_view_templates' with the view templates array
    683697 * @uses bbp_is_topic_edit() To check if it's a topic edit page
     698 * @uses bbp_get_topic_post_type() To get the topic post type
     699 * @uses apply_filters() Calls 'bbp_topic_edit_templates' with the topic edit
     700 *                        templates array
    684701 * @uses bbp_is_reply_edit() To check if it's a reply edit page
     702 * @uses bbp_get_reply_post_type() To get the reply post type
     703 * @uses apply_filters() Calls 'bbp_reply_edit_templates' with the reply edit
     704 *                        templates array
    685705 * @uses apply_filters() Calls 'bbp_custom_template' with the template array
    686706 * @uses bbp_load_template() To load the template
     
    707727            'bbpress/user-edit.php',
    708728            'user-edit.php',
     729            'forums/user.php',
    709730            'bbpress/user.php',
    710731            'user.php',
     
    717738        $template = apply_filters( 'bbp_view_templates', array(
    718739            'forums/view-' . bbp_get_view_id(),
     740            'bbpress/view-' . bbp_get_view_id(),
    719741            'forums/view.php',
    720             'bbpress/view-' . bbp_get_view_id(),
    721742            'bbpress/view.php',
    722743            'view-' . bbp_get_view_id(),
     
    729750        $template = array(
    730751            'forums/action-edit.php',
     752            'bbpress/action-edit.php',
    731753            'forums/single-' . bbp_get_topic_post_type(),
    732             'bbpress/action-edit.php',
    733754            'bbpress/single-' . bbp_get_topic_post_type(),
    734755            'action-bbp-edit.php',
     
    740761        // Add split/merge to front of array if present in _GET
    741762        if ( !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'merge', 'split' ) ) ) {
    742             array_unshift( $template, array(
     763            array_unshift( $template,
    743764                'forums/action-split-merge.php',
    744765                'bbpress/action-split-merge.php',
    745                 'action-bbp-split-merge.php'
    746             ) );
     766                'action-split-merge.php'
     767            );
    747768        }
    748769
     
    753774        $template = apply_filters( 'bbp_reply_edit_templates', array(
    754775            'forums/action-edit.php',
     776            'bbpress/action-edit.php',
    755777            'forums/single-' . bbp_get_reply_post_type(),
    756             'bbpress/action-edit.php',
    757778            'bbpress/single-' . bbp_get_reply_post_type(),
    758779            'action-bbp-edit.php',
     
    778799 */
    779800function bbp_load_template( $files ) {
     801
     802    // Bail if nothing passed
    780803    if ( empty( $files ) )
    781804        return;
     
    788811    if ( locate_template( $files, true ) )
    789812        exit();
    790 
    791     return;
    792813}
    793814
     
    811832 *
    812833 * @uses get_query_var() To get {@link WP_Query} query var
     834 * @uses is_email() To check if the string is an email
     835 * @uses get_user_by() To try to get the user by email and nicename
    813836 * @uses WP_User to get the user data
    814837 * @uses WP_Query::set_404() To set a 404 status
    815  * @uses is_multisite() To check if it's a multisite
    816838 * @uses current_user_can() To check if the current user can edit the user
    817839 * @uses apply_filters() Calls 'enable_edit_any_user_configuration' with true
    818840 * @uses wp_die() To die
     841 * @uses bbp_get_query_name() To get the query name and check if it's 'bbp_widget'
     842 * @uses bbp_get_view_query_args() To get the view query args
     843 * @uses bbp_get_topic_post_type() To get the topic post type
     844 * @uses bbp_get_reply_post_type() To get the reply post type
     845 * @uses is_multisite() To check if it's a multisite
     846 * @uses remove_action() To remove the auto save post revision action
    819847 */
    820848function bbp_pre_get_posts( $wp_query ) {
    821849    global $bbp, $wp_version;
    822850
     851    // Bail if $wp_query is empty or of incorrect class
     852    if ( empty( $wp_query ) || ( 'WP_Query' != get_class( $wp_query ) ) )
     853        return $wp_query;
     854
     855    // Get query variables
    823856    $bbp_user = get_query_var( 'bbp_user' );
    824857    $bbp_view = get_query_var( 'bbp_view' );
     
    930963 * @uses bbp_is_user_profile_page() To check if it's a user profile page
    931964 * @uses bbp_is_user_profile_edit() To check if it's a user profile edit page
     965 * @uses bbp_is_user_home() To check if the profile page is of the current user
    932966 * @uses get_query_var() To get the user id
    933967 * @uses get_userdata() To get the user data
     968 * @uses bbp_is_forum() To check if it's a forum
     969 * @uses bbp_get_forum_title() To get the forum title
     970 * @uses bbp_is_topic() To check if it's a topic
     971 * @uses bbp_get_topic_title() To get the topic title
     972 * @uses bbp_is_reply() To check if it's a reply
     973 * @uses bbp_get_reply_title() To get the reply title
     974 * @uses is_tax() To check if it's the tag page
     975 * @uses get_queried_object() To get the queried object
     976 * @uses bbp_is_view() To check if it's a view
     977 * @uses bbp_get_view_title() To get the view title
    934978 * @uses apply_filters() Calls 'bbp_raw_title' with the title
    935979 * @uses apply_filters() Calls 'bbp_profile_page_wp_title' with the title,
     
    10151059    }
    10161060
    1017     $title = apply_filters( 'bbp_title', $title, $sep, $seplocation );
    1018 
    1019     return $title;
     1061    // Filter and return
     1062    return apply_filters( 'bbp_title', $title, $sep, $seplocation );
    10201063}
    10211064
     
    10701113        return false;
    10711114
    1072     $reply_id = $reply->ID;
    1073     $topic_id = $topic->ID;
    1074 
    1075     do_action( 'bbp_pre_notify_subscribers', $reply_id, $topic_id );
     1115    do_action( 'bbp_pre_notify_subscribers', $reply->ID, $topic->ID );
    10761116
    10771117    // Get the users who have favorited the topic and have subscriptions on
    1078     if ( !$user_ids = bbp_get_topic_subscribers( $topic_id, true ) )
    1079         return false;
    1080 
     1118    if ( !$user_ids = bbp_get_topic_subscribers( $topic->ID, true ) )
     1119        return false;
     1120
     1121    // Loop through users
    10811122    foreach ( (array) $user_ids as $user_id ) {
    10821123
     
    10861127
    10871128        // For plugins
    1088         if ( !$message = apply_filters( 'bbp_subscription_mail_message', __( "%1\$s wrote:\n\n%2\$s\n\nPost Link: %3\$s\n\nYou're getting this mail because you subscribed to the topic, visit the topic and login to unsubscribe." ), $reply_id, $topic_id, $user_id ) )
     1129        if ( !$message = apply_filters( 'bbp_subscription_mail_message', __( "%1\$s wrote:\n\n%2\$s\n\nPost Link: %3\$s\n\nYou're getting this mail because you subscribed to the topic, visit the topic and login to unsubscribe." ), $reply->ID, $topic->ID, $user_id ) )
    10891130            continue;
    10901131
     1132        // Get user data of this user
    10911133        $user = get_userdata( $user_id );
    10921134
     1135        // Send notification email
    10931136        wp_mail(
    10941137            $user->user_email,
    1095             apply_filters( 'bbp_subscription_mail_title', '[' . get_option( 'blogname' ) . '] ' . $topic->post_title, $reply_id, $topic_id ),
    1096             sprintf( $message, $poster_name, strip_tags( $reply->post_content ), bbp_get_reply_permalink( $reply_id ) )
     1138            apply_filters( 'bbp_subscription_mail_title', '[' . get_option( 'blogname' ) . '] ' . $topic->post_title, $reply->ID, $topic->ID ),
     1139            sprintf( $message, $poster_name, strip_tags( $reply->post_content ), bbp_get_reply_permalink( $reply->ID ) )
    10971140        );
    10981141    }
    10991142
    1100     do_action( 'bbp_post_notify_subscribers', $reply_id, $topic_id );
     1143    do_action( 'bbp_post_notify_subscribers', $reply->ID, $topic->ID );
    11011144
    11021145    return true;
     
    11331176 */
    11341177function bbp_logout_url( $url = '', $redirect_to = '' ) {
    1135     if ( !isset( $_SERVER['REDIRECT_URL'] ) || !$redirect_to = home_url( $_SERVER['REDIRECT_URL'] ) )
     1178
     1179    // Rejig the $redirect_to
     1180    if ( !isset( $_SERVER['REDIRECT_URL'] ) || ( !$redirect_to = home_url( $_SERVER['REDIRECT_URL'] ) ) )
    11361181        $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
    11371182
     1183    // Make sure we are directing somewhere
    11381184    if ( empty( $redirect_to ) )
    11391185        $redirect_to = home_url( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
    11401186
    1141     $url = add_query_arg( array( 'redirect_to' => esc_url( $redirect_to ) ), $url );
    1142 
     1187    // Sanitize $redirect_to and add it to full $url
     1188    $redirect_to = esc_url( add_query_arg( array( 'loggedout'   => 'true'       ), $redirect_to ) );
     1189    $url         =          add_query_arg( array( 'redirect_to' => $redirect_to ), $url           );
     1190
     1191    // Filter and return
    11431192    return apply_filters( 'bbp_logout_url', $url, $redirect_to );
    11441193}
     
    11491198 * Query the DB and get the last public post_id that has parent_id as post_parent
    11501199 *
    1151  * @global db $wpdb
    1152  * @param int $parent_id
    1153  * @param string $post_type
     1200 * @param int $parent_id Parent id
     1201 * @param string $post_type Post type. Defaults to 'post'
     1202 * @uses bbp_get_topic_post_type() To get the topic post type
     1203 * @uses wp_cache_get() To check if there is a cache of the last child id
     1204 * @uses wpdb::prepare() To prepare the query
     1205 * @uses wpdb::get_var() To get the result of the query in a variable
     1206 * @uses wp_cache_set() To set the cache for future use
     1207 * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child
     1208 *                        id, parent id and post type
    11541209 * @return int The last active post_id
    11551210 */
     
    11571212    global $wpdb;
    11581213
     1214    // Bail if nothing passed
    11591215    if ( empty( $parent_id ) )
    11601216        return false;
    11611217
    11621218    // The ID of the cached query
    1163     $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id';
    1164 
     1219    $cache_id    = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id';
     1220    $post_status = array( 'publish' );
     1221
     1222    // Add closed status if topic post type
     1223    if ( $post_type == bbp_get_topic_post_type() )
     1224        $post_status[] = $bbp->closed_status_id;
     1225
     1226    // Join post statuses together
     1227    $post_status = "'" . join( "', '", $post_status ) . "'";
     1228
     1229    // Check for cache and set if needed
    11651230    if ( !$child_id = wp_cache_get( $cache_id, 'bbpress' ) ) {
    1166         $child_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", $parent_id, $post_type ) );
     1231        $child_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", $parent_id, $post_type ) );
    11671232        wp_cache_set( $cache_id, $child_id, 'bbpress' );
    11681233    }
    11691234
     1235    // Filter and return
    11701236    return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, (int) $parent_id, $post_type );
    11711237}
     
    11741240 * Query the DB and get a count of public children
    11751241 *
    1176  * @global db $wpdb
    1177  * @param int $parent_id
    1178  * @param string $post_type
     1242 * @param int $parent_id Parent id
     1243 * @param string $post_type Post type. Defaults to 'post'
     1244 * @uses bbp_get_topic_post_type() To get the topic post type
     1245 * @uses wp_cache_get() To check if there is a cache of the children count
     1246 * @uses wpdb::prepare() To prepare the query
     1247 * @uses wpdb::get_var() To get the result of the query in a variable
     1248 * @uses wp_cache_set() To set the cache for future use
     1249 * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child
     1250 *                        count, parent id and post type
    11791251 * @return int The number of children
    11801252 */
     
    11821254    global $wpdb, $bbp;
    11831255
     1256    // Bail if nothing passed
    11841257    if ( empty( $parent_id ) )
    11851258        return false;
    11861259
    11871260    // The ID of the cached query
    1188     $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count';
    1189 
     1261    $cache_id    = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count';
     1262    $post_status = array( 'publish' );
     1263
     1264    // Add closed status if topic post type
     1265    if ( $post_type == bbp_get_topic_post_type() )
     1266        $post_status[] = $bbp->closed_status_id;
     1267
     1268    // Join post statuses together
     1269    $post_status = "'" . join( "', '", $post_status ) . "'";
     1270
     1271    // Check for cache and set if needed
    11901272    if ( !$child_count = wp_cache_get( $cache_id, 'bbpress' ) ) {
    1191         $child_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( "', '", array( 'publish', $bbp->closed_status_id ) ) . "' ) AND post_type = '%s';", $parent_id, $post_type ) );
     1273        $child_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $parent_id, $post_type ) );
    11921274        wp_cache_set( $cache_id, $child_count, 'bbpress' );
    11931275    }
    11941276
     1277    // Filter and return
    11951278    return apply_filters( 'bbp_get_public_child_count', (int) $child_count, (int) $parent_id, $post_type );
    11961279}
    11971280
    11981281/**
    1199  * Query the DB and get a the child ID's of public children
    1200  *
    1201  * @global db $wpdb
    1202  * @param int $parent_id
    1203  * @param string $post_type
    1204  * @return int The number of children
     1282 * Query the DB and get a the child id's of public children
     1283 *
     1284 * @param int $parent_id Parent id
     1285 * @param string $post_type Post type. Defaults to 'post'
     1286 * @uses bbp_get_topic_post_type() To get the topic post type
     1287 * @uses wp_cache_get() To check if there is a cache of the children
     1288 * @uses wpdb::prepare() To prepare the query
     1289 * @uses wpdb::get_col() To get the result of the query in an array
     1290 * @uses wp_cache_set() To set the cache for future use
     1291 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids,
     1292 *                        parent id and post type
     1293 * @return array The array of children
    12051294 */
    12061295function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) {
    12071296    global $wpdb, $bbp;
    12081297
     1298    // Bail if nothing passed
    12091299    if ( empty( $parent_id ) )
    12101300        return false;
    12111301
    12121302    // The ID of the cached query
    1213     $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_ids';
    1214 
     1303    $cache_id    = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_ids';
     1304    $post_status = array( 'publish' );
     1305
     1306    // Add closed status if topic post type
     1307    if ( $post_type == bbp_get_topic_post_type() )
     1308        $post_status[] = $bbp->closed_status_id;
     1309
     1310    // Join post statuses together
     1311    $post_status = "'" . join( "', '", $post_status ) . "'";
     1312
     1313    // Check for cache and set if needed
    12151314    if ( !$child_ids = wp_cache_get( $cache_id, 'bbpress' ) ) {
    1216         $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( "', '", array( 'publish', $bbp->closed_status_id ) ) . "' ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
     1315        $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
    12171316        wp_cache_set( $cache_id, $child_ids, 'bbpress' );
    12181317    }
    12191318
     1319    // Filter and return
    12201320    return apply_filters( 'bbp_get_public_child_ids', $child_ids, (int) $parent_id, $post_type );
    12211321}
Note: See TracChangeset for help on using the changeset viewer.