Skip to:
Content

bbPress.org


Ignore:
Timestamp:
07/14/2015 12:31:42 AM (11 years ago)
Author:
johnjamesjacoby
Message:

Abstraction: Use bbp_db(), bbp_rewrite() & friends, introduced in r5823 & r5826.

This commit improves the stability of bbPress in the WordPress environment by reducing global variable exposure. It also comes with minimal opcode improvements in some circumstances where $GLOBALS is preferred over defining via global statements.

Some additional surrounding cleanup directly related to functions & methods being altered is also being performed here.

Fixes #2786.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/topics/functions.php

    r5778 r5827  
    14721472    }
    14731473
    1474     global $wpdb;
    1475 
    14761474    // Prevent debug notices
    14771475    $from_reply_id = $destination_topic_id = 0;
     
    16781676    // get_posts() is not used because it doesn't allow us to use '>='
    16791677    // comparision without a filter.
    1680     $replies = (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_date >= %s AND {$wpdb->posts}.post_parent = %d AND {$wpdb->posts}.post_type = %s ORDER BY {$wpdb->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type() ) );
     1678    $bbp_db  = bbp_db();
     1679    $query   = $bbp_db->prepare( "SELECT * FROM {$bbp_db->posts} WHERE {$bbp_db->posts}.post_date >= %s AND {$bbp_db->posts}.post_parent = %d AND {$bbp_db->posts}.post_type = %s ORDER BY {$bbp_db->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type() );
     1680    $replies = (array) $bbp_db->get_results( $query );
    16811681
    16821682    // Make sure there are replies to loop through
     
    25092509 */
    25102510function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = 0 ) {
    2511     global $wpdb;
    25122511
    25132512    // If it's a reply, then get the parent (topic id)
     
    25202519    // Get replies of topic
    25212520    if ( empty( $reply_count ) ) {
     2521        $bbp_db      = bbp_db();
    25222522        $post_status = "'" . implode( "','", array( bbp_get_trash_status_id(), bbp_get_spam_status_id(), bbp_get_pending_status_id() ) ) . "'";
    2523         $reply_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';", $topic_id, bbp_get_reply_post_type() ) );
     2523        $query       = $bbp_db->prepare( "SELECT COUNT(ID) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $topic_id, bbp_get_reply_post_type() );
     2524        $reply_count = $bbp_db->get_var( $query );
    25242525    }
    25252526
     
    26732674 * @uses bbp_get_topic_post_type() To get the topic post type
    26742675 * @uses wpdb::prepare() To prepare our sql query
    2675  * @uses wpdb::get_col() To execute our query and get the column back
     2676 * @uses wpdb::get_var() To execute our query and get the column back
    26762677 * @uses update_post_meta() To update the topic voice count meta
    26772678 * @uses apply_filters() Calls 'bbp_update_topic_voice_count' with the voice
     
    26802681 */
    26812682function bbp_update_topic_voice_count( $topic_id = 0 ) {
    2682     global $wpdb;
    26832683
    26842684    // If it's a reply, then get the parent (topic id)
     
    26922692
    26932693    // Query the DB to get voices in this topic
    2694     $voices = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' ) OR ( ID = %d AND post_type = '%s' );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) );
     2694    $bbp_db = bbp_db();
     2695    $query  = $bbp_db->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' ) OR ( ID = %d AND post_type = '%s' );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
     2696    $voices = (int) $bbp_db->get_var( $query );
    26952697
    26962698    // Update the voice count for this topic id
     
    27132715 * @uses bbp_get_topic_post_type() To get the topic post type
    27142716 * @uses wpdb::prepare() To prepare our sql query
    2715  * @uses wpdb::get_col() To execute our query and get the column back
     2717 * @uses wpdb::get_var() To execute our query and get the column back
    27162718 * @uses update_post_meta() To update the topic anonymous reply count meta
    27172719 * @uses apply_filters() Calls 'bbp_update_topic_anonymous_reply_count' with the
     
    27202722 */
    27212723function bbp_update_topic_anonymous_reply_count( $topic_id = 0 ) {
    2722     global $wpdb;
    27232724
    27242725    // If it's a reply, then get the parent (topic id)
     
    27312732    }
    27322733
    2733     $anonymous_replies = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( ID ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' AND post_author = 0 ) OR ( ID = %d AND post_type = '%s' AND post_author = 0 );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) );
    2734 
    2735     update_post_meta( $topic_id, '_bbp_anonymous_reply_count', $anonymous_replies );
    2736 
    2737     return (int) apply_filters( 'bbp_update_topic_anonymous_reply_count', $anonymous_replies, $topic_id );
     2734    // Query the DB to get anonymous replies in this topic
     2735    $bbp_db  = bbp_db();
     2736    $query   = $bbp_db->prepare( "SELECT COUNT( ID ) FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' AND post_author = 0 ) OR ( ID = %d AND post_type = '%s' AND post_author = 0 );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
     2737    $replies = (int) $bbp_db->get_var( $query );
     2738
     2739    update_post_meta( $topic_id, '_bbp_anonymous_reply_count', $replies );
     2740
     2741    return (int) apply_filters( 'bbp_update_topic_anonymous_reply_count', $replies, $topic_id );
    27382742}
    27392743
Note: See TracChangeset for help on using the changeset viewer.