Skip to:
Content

bbPress.org


Ignore:
Timestamp:
01/09/2011 10:06:53 PM (15 years ago)
Author:
johnjamesjacoby
Message:

Introduce views API into plugin. Allows for creating specific topic views with code rather than with templates. The 'no-replies' view is included as default. Props GautamGupta via Google Code-in

File:
1 edited

Legend:

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

    r2788 r2789  
    275275
    276276    return $reason;
     277}
     278
     279/** Views *********************************************************************/
     280
     281/**
     282 * Get the registered views
     283 *
     284 * Does nothing much other than return the {@link $bbp->views} variable
     285 *
     286 * @since bbPress (r2789)
     287 *
     288 * @return array Views
     289 */
     290function bbp_get_views() {
     291    global $bbp;
     292
     293    return $bbp->views;
     294}
     295
     296/**
     297 * Register a bbPress view
     298 *
     299 * @todo Implement feeds - See {@link http://trac.bbpress.org/ticket/1422}
     300 *
     301 * @since bbPress (r2789)
     302 *
     303 * @param string $view View name
     304 * @param string $title View title
     305 * @param mixed $query_args {@link bbp_has_topics()} arguments.
     306 * @param bool $feed Have a feed for the view? Defaults to true. NOT IMPLEMENTED
     307 * @uses sanitize_title() To sanitize the view name
     308 * @uses esc_html() To sanitize the view title
     309 * @return array The just registered (but processed) view
     310 */
     311function bbp_register_view( $view, $title, $query_args = '', $feed = true ) {
     312    global $bbp;
     313
     314    $view  = sanitize_title( $view );
     315    $title = esc_html( $title );
     316
     317    if ( empty( $view ) || empty( $title ) )
     318        return false;
     319
     320    $query_args = wp_parse_args( $query_args );
     321
     322    // Set ignore_sticky_topics to true if it wasn't supplied
     323    if ( !isset( $query_args['ignore_sticky_topics'] ) )
     324        $query_args['ignore_sticky_topics'] = true;
     325
     326    $bbp->views[$view]['title'] = $title;
     327    $bbp->views[$view]['query'] = $query_args;
     328    $bbp->views[$view]['feed']  = $feed;
     329
     330    return $bbp->views[$view];
     331}
     332
     333/**
     334 * Deregister a bbPress view
     335 *
     336 * @since bbPress (r2789)
     337 *
     338 * @param string $view View name
     339 * @uses sanitize_title() To sanitize the view name
     340 * @return bool False if the view doesn't exist, true on success
     341 */
     342function bbp_deregister_view( $view ) {
     343    global $bbp;
     344
     345    $view = sanitize_title( $view );
     346
     347    if ( !isset( $bbp->views[$view] ) )
     348        return false;
     349
     350    unset( $bbp->views[$view] );
     351
     352    return true;
     353}
     354
     355/**
     356 * Run the view's query
     357 *
     358 * @since bbPress (r2789)
     359 *
     360 * @param string $view Optional. View id
     361 * @param mixed $new_args New arguments. See {@link bbp_has_topics()}
     362 * @uses bbp_get_view_id() To get the view id
     363 * @uses bbp_get_view_query_args() To get the view query args
     364 * @uses sanitize_title() To sanitize the view name
     365 * @uses bbp_has_topics() To make the topics query
     366 * @return bool False if the view doesn't exist, otherwise if topics are there
     367 */
     368function bbp_view_query( $view = '', $new_args = '' ) {
     369    global $bbp;
     370
     371    if ( !$view = bbp_get_view_id( $view ) )
     372        return false;
     373
     374    $query_args = bbp_get_view_query_args( $view );
     375
     376    if ( !empty( $new_args ) ) {
     377        $new_args   = wp_parse_args( $new_args );
     378        $query_args = array_merge( $query_args, $new_args );
     379    }
     380
     381    return bbp_has_topics( $query_args );
     382}
     383
     384/**
     385 * Run the view's query's arguments
     386 *
     387 * @since bbPress (r2789)
     388 *
     389 * @param string $view View name
     390 * @uses bbp_get_view_id() To get the view id
     391 * @uses sanitize_title() To sanitize the view name
     392 * @return array Query arguments
     393 */
     394function bbp_get_view_query_args( $view ) {
     395    global $bbp;
     396
     397    if ( !$views = bbp_get_view_id( $view ) )
     398        return false;
     399
     400    return $bbp->views[$view]['query'];
    277401}
    278402
     
    18581982 * Load bbPress custom templates
    18591983 *
    1860  * Loads custom templates for bbPress user profile, user edit, topic edit and
    1861  * reply edit pages.
     1984 * Loads custom templates for bbPress view page, user profile, user edit, topic
     1985 * edit and reply edit pages.
    18621986 *
    18631987 * @since bbPress (r2753)
     
    18832007        $template = array( 'user-edit.php', 'user.php', 'author.php', 'index.php' );
    18842008
     2009    // View page
     2010    } elseif ( bbp_is_view() ) {
     2011        $template = array( 'view-' . bbp_get_view_id(), 'view.php', 'index.php' );
     2012
    18852013    // Editing a topic
    18862014    } elseif ( bbp_is_topic_edit() ) {
     
    19032031
    19042032/**
    1905  * Add checks for user page, user edit, topic edit and reply edit pages.
     2033 * Add checks for view page, user page, user edit, topic edit and reply edit
     2034 * pages.
    19062035 *
    19072036 * If it's a user page, WP_Query::bbp_is_user_profile_page is set to true.
     
    19142043 * If it's a topic edit, WP_Query::bbp_is_topic_edit is set to true and
    19152044 * similarly, if it's a reply edit, WP_Query::bbp_is_reply_edit is set to true.
     2045 *
     2046 * If it's a view page, WP_Query::bbp_is_view is set to true
    19162047 *
    19172048 * @since bbPress (r2688)
     
    19292060
    19302061    $bbp_user = get_query_var( 'bbp_user' );
     2062    $bbp_view = get_query_var( 'bbp_view' );
    19312063    $is_edit  = get_query_var( 'edit'     );
    19322064
     2065    // Profile page
    19332066    if ( !empty( $bbp_user ) ) {
    19342067
     
    19722105        // Set the displayed user global to this user
    19732106        $bbp->displayed_user = $user;
     2107
     2108    // View Page
     2109    } elseif ( !empty( $bbp_view ) ) {
     2110
     2111        // Check if the view exists by checking if there are query args are set or not
     2112        $view_args = bbp_get_view_query_args( $bbp_view );
     2113
     2114        // Stop if view args is false - means the view isn't registered
     2115        if ( false === $view_args ) {
     2116            $wp_query->set_404();
     2117            return;
     2118        }
     2119
     2120        $wp_query->bbp_is_view = true;
     2121
     2122    // Topic/Reply Edit Page
    19742123    } elseif ( !empty( $is_edit ) ) {
    19752124
     
    20462195        $title = bbp_get_reply_title();
    20472196
     2197    // Topic tag page
    20482198    } elseif ( is_tax( $bbp->topic_tag_id ) ) {
    20492199
    20502200        $term  = get_queried_object();
    20512201        $title = sprintf( __( 'Topic Tag: %s', 'bbpress' ), $term->name );
     2202
     2203    // Views
     2204    } elseif ( bbp_is_view() ) {
     2205
     2206        $title = sprintf( __( 'View: %s', 'bbpress' ), bbp_get_view_title() );
    20522207
    20532208    }
Note: See TracChangeset for help on using the changeset viewer.