Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/27/2011 10:18:38 AM (11 years ago)
Author:
johnjamesjacoby
Message:

Add _insert_ functions for forums, topics, and replies. Audit (forum/topic/hidden) meta key and updater function names and and normalize them where needed.

(Existing installs will need to run recounts, or let them adjust themselves naturally as new topics and replies come in.)

File:
1 edited

Legend:

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

    r3348 r3349  
    1010// Exit if accessed directly
    1111if ( !defined( 'ABSPATH' ) ) exit;
     12
     13/** Insert ********************************************************************/
     14
     15/**
     16 * A wrapper for wp_insert_post() that also includes the necessary meta values
     17 * for the topic to function properly.
     18 *
     19 * @since bbPress (r3349)
     20 *
     21 * @uses wp_parse_args()
     22 * @uses bbp_get_topic_post_type()
     23 * @uses wp_insert_post()
     24 * @uses update_post_meta()
     25 *
     26 * @param array $topic_data Forum post data
     27 * @param arrap $topic_meta Forum meta data
     28 */
     29function bbp_insert_topic( $topic_data = array(), $topic_meta = array() ) {
     30
     31    // Forum
     32    $default_topic = array(
     33        'post_parent'   => 0, // forum ID
     34        'post_status'   => 'publish',
     35        'post_type'     => bbp_get_topic_post_type(),
     36        'post_author'   => 0,
     37        'post_password' => '',
     38        'post_content'  => '',
     39        'post_title'    => '',
     40        'menu_order'    => 0,
     41    );
     42
     43    // Parse args
     44    $topic_data = wp_parse_args( $topic_data, $default_topic );
     45
     46    // Insert topic
     47    $topic_id   = wp_insert_post( $topic_data );
     48
     49    // Bail if no topic was added
     50    if ( empty( $topic_id ) )
     51        return false;
     52
     53    // Forum meta
     54    $default_meta = array(
     55        'author_ip'          => bbp_current_author_ip(),
     56        'forum_id'           => 0,
     57        'topic_id'           => $topic_id,
     58        'voice_count'        => 1,
     59        'reply_count'        => 0,
     60        'reply_count_hidden' => 0,
     61        'last_reply_id'      => 0,
     62        'last_active_id'     => $topic_id,
     63        'last_active_time'   => get_post_field( 'post_date', $topic_id, 'db' ),
     64    );
     65
     66    // Parse args
     67    $topic_meta = wp_parse_args( $topic_meta, $default_meta );
     68
     69    // Insert topic meta
     70    foreach ( $topic_meta as $meta_key => $meta_value )
     71        update_post_meta( $topic_id, '_bbp_' . $meta_key, $meta_value );
     72
     73    // Update the forum
     74    if ( $forum_id = bbp_get_topic_forum_id( $topic_id ) )
     75        bbp_update_forum( $forum_id );
     76
     77    // Return new topic ID
     78    return $topic_id;
     79}
    1280
    1381/** Post Form Handlers ********************************************************/
     
    589657 * @uses bbp_update_topic_last_active_time() To update the last active topic meta
    590658 * @uses bbp_update_topic_reply_count() To update the topic reply count
    591  * @uses bbp_update_topic_hidden_reply_count() To udpate the topic hidden reply count
     659 * @uses bbp_update_topic_reply_count_hidden() To udpate the topic hidden reply count
    592660 * @uses bbp_update_topic_voice_count() To update the topic voice count
    593661 * @uses bbp_update_topic_walker() To udpate the topic's ancestors
     
    660728        bbp_update_topic_last_active_time   ( $topic_id, $last_active );
    661729        bbp_update_topic_reply_count        ( $topic_id, 0            );
    662         bbp_update_topic_hidden_reply_count ( $topic_id, 0            );
     730        bbp_update_topic_reply_count_hidden ( $topic_id, 0            );
    663731        bbp_update_topic_voice_count        ( $topic_id               );
    664732
     
    10301098 * @uses bbp_update_topic_reply_count() To update the topic reply counts
    10311099 * @uses bbp_update_topic_voice_count() To update the topic voice counts
    1032  * @uses bbp_update_topic_hidden_reply_count() To update the topic hidden reply
     1100 * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
    10331101 *                                              count
    10341102 * @uses do_action() Calls 'bbp_merge_topic_count' with the destination topic
     
    10511119
    10521120    // Topic Hidden Reply Counts
    1053     bbp_update_topic_hidden_reply_count( $destination_topic_id );
     1121    bbp_update_topic_reply_count_hidden( $destination_topic_id );
    10541122
    10551123    // Topic Voice Counts
     
    13391407 * @uses bbp_update_topic_reply_count() To update the topic reply counts
    13401408 * @uses bbp_update_topic_voice_count() To update the topic voice counts
    1341  * @uses bbp_update_topic_hidden_reply_count() To update the topic hidden reply
     1409 * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
    13421410 *                                              count
    13431411 * @uses do_action() Calls 'bbp_split_topic_count' with the from reply id,
     
    13571425
    13581426    // Topic Hidden Reply Counts
    1359     bbp_update_topic_hidden_reply_count( $source_topic_id      );
    1360     bbp_update_topic_hidden_reply_count( $destination_topic_id );
     1427    bbp_update_topic_reply_count_hidden( $source_topic_id      );
     1428    bbp_update_topic_reply_count_hidden( $destination_topic_id );
    13611429
    13621430    // Topic Voice Counts
     
    18961964 * @uses wpdb::get_var() To execute our query and get the var back
    18971965 * @uses update_post_meta() To update the topic hidden reply count meta
    1898  * @uses apply_filters() Calls 'bbp_update_topic_hidden_reply_count' with the
     1966 * @uses apply_filters() Calls 'bbp_update_topic_reply_count_hidden' with the
    18991967 *                        hidden reply count and topic id
    19001968 * @return int Topic hidden reply count
    19011969 */
    1902 function bbp_update_topic_hidden_reply_count( $topic_id = 0, $reply_count = 0 ) {
     1970function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = 0 ) {
    19031971    global $wpdb, $bbp;
    19041972
     
    19131981        $reply_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( '\',\'', array( $bbp->trash_status_id, $bbp->spam_status_id ) ) . "') AND post_type = '%s';", $topic_id, bbp_get_reply_post_type() ) );
    19141982
    1915     update_post_meta( $topic_id, '_bbp_hidden_reply_count', (int) $reply_count );
    1916 
    1917     return apply_filters( 'bbp_update_topic_hidden_reply_count', (int) $reply_count, $topic_id );
     1983    update_post_meta( $topic_id, '_bbp_reply_count_hidden', (int) $reply_count );
     1984
     1985    return apply_filters( 'bbp_update_topic_reply_count_hidden', (int) $reply_count, $topic_id );
    19181986}
    19191987
Note: See TracChangeset for help on using the changeset viewer.