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-reply-functions.php

    r3348 r3349  
    1111if ( !defined( 'ABSPATH' ) ) exit;
    1212
    13 /**
    14  * Update the reply with its forum id it is in
    15  *
    16  * @since bbPress (r2855)
    17  *
    18  * @param int $reply_id Optional. Reply id to update
    19  * @param int $forum_id Optional. Forum id
    20  * @uses bbp_get_reply_id() To get the reply id
    21  * @uses bbp_get_forum_id() To get the forum id
    22  * @uses get_post_ancestors() To get the reply's forum
    23  * @uses get_post_field() To get the post type of the post
    24  * @uses update_post_meta() To update the reply forum id meta
    25  * @uses apply_filters() Calls 'bbp_update_reply_forum_id' with the forum id
    26  *                        and reply id
    27  * @return bool Reply's forum id
    28  */
    29 function bbp_update_reply_forum_id( $reply_id = 0, $forum_id = 0 ) {
    30 
    31     // Validation
    32     $reply_id = bbp_get_reply_id( $reply_id );
    33     $forum_id = bbp_get_forum_id( $forum_id );
    34 
    35     // If no forum_id was passed, walk up ancestors and look for forum type
    36     if ( empty( $forum_id ) ) {
    37 
    38         // Get ancestors
    39         $ancestors = get_post_ancestors( $reply_id );
    40 
    41         // Loop through ancestors
    42         foreach ( $ancestors as $ancestor ) {
    43 
    44             // Get first parent that is a forum
    45             if ( get_post_field( 'post_type', $ancestor ) == bbp_get_forum_post_type() ) {
    46                 $forum_id = $ancestor;
    47 
    48                 // Found a forum, so exit the loop and continue
    49                 continue;
    50             }
    51         }
    52     }
    53 
    54     // Update the forum ID
    55     bbp_update_forum_id( $reply_id, $forum_id );
    56 
    57     return apply_filters( 'bbp_update_reply_forum_id', (int) $forum_id, $reply_id );
    58 }
    59 
    60 /**
    61  * Update the reply with its topic id it is in
    62  *
    63  * @since bbPress (r2855)
    64  *
    65  * @param int $reply_id Optional. Reply id to update
    66  * @param int $topic_id Optional. Topic id
    67  * @uses bbp_get_reply_id() To get the reply id
    68  * @uses bbp_get_topic_id() To get the topic id
    69  * @uses get_post_ancestors() To get the reply's topic
    70  * @uses get_post_field() To get the post type of the post
    71  * @uses update_post_meta() To update the reply topic id meta
    72  * @uses apply_filters() Calls 'bbp_update_reply_topic_id' with the topic id
    73  *                        and reply id
    74  * @return bool Reply's topic id
    75  */
    76 function bbp_update_reply_topic_id( $reply_id = 0, $topic_id = 0 ) {
    77 
    78     // Validation
    79     $reply_id = bbp_get_reply_id( $reply_id );
    80     $topic_id = bbp_get_topic_id( $topic_id );
    81 
    82     // If no topic_id was passed, walk up ancestors and look for topic type
    83     if ( empty( $topic_id ) ) {
    84 
    85         // Get ancestors
    86         $ancestors = get_post_ancestors( $reply_id );
    87 
    88         // Loop through ancestors
    89         foreach ( $ancestors as $ancestor ) {
    90 
    91             // Get first parent that is a forum
    92             if ( get_post_field( 'post_type', $ancestor ) == bbp_get_topic_post_type() ) {
    93                 $topic_id = $ancestor;
    94 
    95                 // Found a forum, so exit the loop and continue
    96                 continue;
    97             }
    98         }
    99     }
    100 
    101     // Update the topic ID
    102     bbp_update_topic_id( $reply_id, $topic_id );
    103 
    104     return apply_filters( 'bbp_update_reply_topic_id', (int) $topic_id, $reply_id );
     13/** Insert ********************************************************************/
     14
     15/**
     16 * A wrapper for wp_insert_post() that also includes the necessary meta values
     17 * for the reply to function properly.
     18 *
     19 * @since bbPress (r3349)
     20 *
     21 * @uses wp_parse_args()
     22 * @uses bbp_get_reply_post_type()
     23 * @uses wp_insert_post()
     24 * @uses update_post_meta()
     25 *
     26 * @param array $reply_data Forum post data
     27 * @param arrap $reply_meta Forum meta data
     28 */
     29function bbp_insert_reply( $reply_data = array(), $reply_meta = array() ) {
     30
     31    // Forum
     32    $default_reply = array(
     33        'post_parent'   => 0, // topic ID
     34        'post_status'   => 'publish',
     35        'post_type'     => bbp_get_reply_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    $reply_data = wp_parse_args( $reply_data, $default_reply );
     45
     46    // Insert reply
     47    $reply_id   = wp_insert_post( $reply_data );
     48
     49    // Bail if no reply was added
     50    if ( empty( $reply_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'  => 0,
     58    );
     59
     60    // Parse args
     61    $reply_meta = wp_parse_args( $reply_meta, $default_meta );
     62
     63    // Insert reply meta
     64    foreach ( $reply_meta as $meta_key => $meta_value )
     65        update_post_meta( $reply_id, '_bbp_' . $meta_key, $meta_value );
     66
     67    // Update the topic
     68    if ( $topic_id = bbp_get_reply_topic_id( $reply_id ) )
     69        bbp_update_topic( $topic_id );
     70
     71    // Return new reply ID
     72    return $reply_id;
    10573}
    10674
     
    688656 * @uses bbp_update_topic_voice_count() To update the topic voice count
    689657 * @uses bbp_update_topic_reply_count() To update the topic reply count
    690  * @uses bbp_update_topic_hidden_reply_count() To update the topic hidden reply
     658 * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
    691659 *                                              count
    692660 * @uses bbp_is_forum() To check if the ancestor is a forum
     
    748716            bbp_update_topic_voice_count       ( $ancestor );
    749717            bbp_update_topic_reply_count       ( $ancestor );
    750             bbp_update_topic_hidden_reply_count( $ancestor );
     718            bbp_update_topic_reply_count_hidden( $ancestor );
    751719
    752720        // Forum meta relating to most recent topic
     
    771739        }
    772740    }
     741}
     742
     743/** Reply Updaters ************************************************************/
     744
     745/**
     746 * Update the reply with its forum id it is in
     747 *
     748 * @since bbPress (r2855)
     749 *
     750 * @param int $reply_id Optional. Reply id to update
     751 * @param int $forum_id Optional. Forum id
     752 * @uses bbp_get_reply_id() To get the reply id
     753 * @uses bbp_get_forum_id() To get the forum id
     754 * @uses get_post_ancestors() To get the reply's forum
     755 * @uses get_post_field() To get the post type of the post
     756 * @uses update_post_meta() To update the reply forum id meta
     757 * @uses apply_filters() Calls 'bbp_update_reply_forum_id' with the forum id
     758 *                        and reply id
     759 * @return bool Reply's forum id
     760 */
     761function bbp_update_reply_forum_id( $reply_id = 0, $forum_id = 0 ) {
     762
     763    // Validation
     764    $reply_id = bbp_get_reply_id( $reply_id );
     765    $forum_id = bbp_get_forum_id( $forum_id );
     766
     767    // If no forum_id was passed, walk up ancestors and look for forum type
     768    if ( empty( $forum_id ) ) {
     769
     770        // Get ancestors
     771        $ancestors = get_post_ancestors( $reply_id );
     772
     773        // Loop through ancestors
     774        foreach ( $ancestors as $ancestor ) {
     775
     776            // Get first parent that is a forum
     777            if ( get_post_field( 'post_type', $ancestor ) == bbp_get_forum_post_type() ) {
     778                $forum_id = $ancestor;
     779
     780                // Found a forum, so exit the loop and continue
     781                continue;
     782            }
     783        }
     784    }
     785
     786    // Update the forum ID
     787    bbp_update_forum_id( $reply_id, $forum_id );
     788
     789    return apply_filters( 'bbp_update_reply_forum_id', (int) $forum_id, $reply_id );
     790}
     791
     792/**
     793 * Update the reply with its topic id it is in
     794 *
     795 * @since bbPress (r2855)
     796 *
     797 * @param int $reply_id Optional. Reply id to update
     798 * @param int $topic_id Optional. Topic id
     799 * @uses bbp_get_reply_id() To get the reply id
     800 * @uses bbp_get_topic_id() To get the topic id
     801 * @uses get_post_ancestors() To get the reply's topic
     802 * @uses get_post_field() To get the post type of the post
     803 * @uses update_post_meta() To update the reply topic id meta
     804 * @uses apply_filters() Calls 'bbp_update_reply_topic_id' with the topic id
     805 *                        and reply id
     806 * @return bool Reply's topic id
     807 */
     808function bbp_update_reply_topic_id( $reply_id = 0, $topic_id = 0 ) {
     809
     810    // Validation
     811    $reply_id = bbp_get_reply_id( $reply_id );
     812    $topic_id = bbp_get_topic_id( $topic_id );
     813
     814    // If no topic_id was passed, walk up ancestors and look for topic type
     815    if ( empty( $topic_id ) ) {
     816
     817        // Get ancestors
     818        $ancestors = get_post_ancestors( $reply_id );
     819
     820        // Loop through ancestors
     821        foreach ( $ancestors as $ancestor ) {
     822
     823            // Get first parent that is a forum
     824            if ( get_post_field( 'post_type', $ancestor ) == bbp_get_topic_post_type() ) {
     825                $topic_id = $ancestor;
     826
     827                // Found a forum, so exit the loop and continue
     828                continue;
     829            }
     830        }
     831    }
     832
     833    // Update the topic ID
     834    bbp_update_topic_id( $reply_id, $topic_id );
     835
     836    return apply_filters( 'bbp_update_reply_topic_id', (int) $topic_id, $reply_id );
    773837}
    774838
Note: See TracChangeset for help on using the changeset viewer.