Skip to:
Content

bbPress.org

Changeset 2659


Ignore:
Timestamp:
11/29/2010 02:43:33 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Move ajax handling of favorites into theme. Clean up other favorites related code. More to do.

Location:
branches/plugin
Files:
2 added
5 edited
1 copied

Legend:

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

    r2652 r2659  
    11<?php
    2 
    3 /**
    4  * bbp_has_access()
    5  *
    6  * Make sure user can perform special tasks
    7  *
    8  * @package bbPress
    9  * @subpackage Functions
    10  * @since bbPress (r2464)
    11  *
    12  * @uses is_super_admin ()
    13  * @uses apply_filters
    14  *
    15  * @todo bbPress port of existing roles/caps
    16  * @return bool $has_access
    17  */
    18 function bbp_has_access () {
    19 
    20     if ( is_super_admin () )
    21         $has_access = true;
    22     else
    23         $has_access = false;
    24 
    25     return apply_filters( 'bbp_has_access', $has_access );
    26 }
    27 
    28 /**
    29  * bbp_allow_anonymous ()
    30  *
    31  * Returns true|false if anonymous topic creation and replies are allowed
    32  *
    33  * @since bbPress (r2596)
    34  * @return bool
    35  */
    36 function bbp_allow_anonymous () {
    37     return apply_filters( 'bbp_allow_anonymous', get_option( 'bbp_allow_anonymous', false ) );
    38 }
    392
    403/**
     
    365328
    366329/**
    367  * bbp_dim_favorite ()
    368  *
    369  * Add or remove a topic from a user's favorites
     330 * bbp_remove_topic_from_all_favorites ()
     331 *
     332 * Remove a deleted topic from all users' favorites
    370333 *
    371334 * @package bbPress
     
    373336 * @since bbPress (r2652)
    374337 *
    375  * @return void
    376  */
    377 function bbp_dim_favorite () {
    378     global $current_user;
    379 
    380     wp_get_current_user();
    381 
    382     $user_id = $current_user->ID;
    383     $id = intval( $_POST['id'] );
    384 
    385     if ( !$topic = get_post( $id ) )
    386         die( '0' );
    387 
    388     if ( !current_user_can( 'edit_user', $user_id ) )
    389         die( '-1' );
    390 
    391     check_ajax_referer( "toggle-favorite_$topic->ID" );
    392 
    393     if ( bbp_is_user_favorite( $user_id, $topic->ID ) ) {
    394         if ( bbp_remove_user_favorite( $user_id, $topic->ID ) )
    395             die( '1' );
    396     } else {
    397         if ( bbp_add_user_favorite( $user_id, $topic->ID ) )
    398             die( '1' );
    399     }
    400 
    401     die( '0' );
    402 
    403 }
    404 add_action( 'wp_ajax_dim-favorite', 'bbp_dim_favorite' );
    405 
    406 /**
    407  * bbp_remove_topic_from_all_favorites ()
    408  *
    409  * Remove a deleted topic from all users' favorites
    410  *
    411  * @package bbPress
    412  * @subpackage Template Tags
    413  * @since bbPress (r2652)
    414  *
    415338 * @param int $topic_id Topic ID to remove
    416339 * @return void
     
    419342    global $wpdb;
    420343
    421     if ( $ids = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '_bbp_favorites' and FIND_IN_SET('{$topic_id}', meta_value) > 0" ) )
    422         foreach ( $ids as $id )
    423             bbp_remove_user_favorite( $id, $topic_id );
    424 }
    425 add_action( 'trash_post', 'bbp_remove_topic_from_all_favorites' );
     344    if ( $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '_bbp_favorites' and FIND_IN_SET('{$topic_id}', meta_value) > 0" ) )
     345        foreach ( $users as $user )
     346            bbp_remove_user_favorite( $user, $topic_id );
     347}
     348add_action( 'trash_post',  'bbp_remove_topic_from_all_favorites' );
    426349add_action( 'delete_post', 'bbp_remove_topic_from_all_favorites' );
    427350
    428 /**
    429  * bbp_enqueue_topic_script ()
    430  *
    431  * Enqueue the topic page Javascript file
    432  *
    433  * @package bbPress
    434  * @subpackage Template Tags
    435  * @since bbPress (r2652)
    436  *
    437  * @return void
    438  */
    439 function bbp_enqueue_topic_script () {
    440     if ( !bbp_is_topic() )
    441         return;
    442 
    443     global $bbp;
    444 
    445     wp_enqueue_script( 'bbp_topic', $bbp->plugin_url . 'bbp-includes/js/topic.js', array( 'wp-lists' ), '20101124' );
    446 }
    447 add_filter( 'wp_enqueue_scripts', 'bbp_enqueue_topic_script' );
    448 
    449 /**
    450  * bbp_scripts ()
    451  *
    452  * Put some scripts in the header, like AJAX url for wp-lists
    453  *
    454  * @package bbPress
    455  * @subpackage Template Tags
    456  * @since bbPress (r2652)
    457  *
    458  * @return void
    459  */
    460 function bbp_scripts () {
    461     if ( !bbp_is_topic() )
    462         return;
    463 
    464     echo "<script type='text/javascript'>
    465 /* <![CDATA[ */
    466 var ajaxurl = '" . admin_url( 'admin-ajax.php' ) . "';
    467 /* ]]> */
    468 </script>\n";
    469 }
    470 add_filter( 'wp_head', 'bbp_scripts', -1 );
    471 
    472 /**
    473  * bbp_topic_script_localization ()
    474  *
    475  * Load localizations for topic script.
    476  *
    477  * These localizations require information that may not be loaded even by init.
    478  *
    479  * @package bbPress
    480  * @subpackage Template Tags
    481  * @since bbPress (r2652)
    482  *
    483  * @return void
    484  */
    485 function bbp_topic_script_localization () {
    486     if ( !bbp_is_topic() )
    487         return;
    488 
    489     global $current_user;
    490 
    491     wp_get_current_user();
    492     $user_id = $current_user->ID;
    493 
    494     wp_localize_script( 'bbp_topic', 'bbpTopicJS', array(
    495         'currentUserId' => $user_id,
    496         'topicId'       => bbp_get_topic_id(),
    497         'favoritesLink' => bbp_get_favorites_link( $user_id ),
    498         'isFav'         => (int) bbp_is_user_favorite( $user_id ),
    499         'favLinkYes'    => __( 'favorites', 'bbpress' ),
    500         'favLinkNo'     => __( '?', 'bbpress' ),
    501         'favYes'        => __( 'This topic is one of your %favLinkYes% [%favDel%]', 'bbpress' ),
    502         'favNo'         => __( '%favAdd% (%favLinkNo%)', 'bbpress' ),
    503         'favDel'        => __( '&times;', 'bbpress' ),
    504         'favAdd'        => __( 'Add this topic to your favorites', 'bbpress' )
    505     ));
    506 }
    507 add_filter( 'wp_enqueue_scripts', 'bbp_topic_script_localization' );
    508 
    509351?>
  • branches/plugin/bbp-includes/bbp-template.php

    r2657 r2659  
    26372637        wp_get_current_user();
    26382638
    2639         if ( !$user_id && !$user_id = $current_user->ID )
    2640             return;
    2641 
    2642         $topic = get_post( bbp_get_topic_id() );
    2643 
    2644         if ( empty( $add ) || !is_array( $add ) )
    2645             $add = array( 'mid' => __( 'Add this topic to your favorites', 'bbpress' ), 'post' => __( ' (%?%)', 'bbpress' ) );
    2646 
    2647         if ( empty( $rem ) || !is_array( $rem ) )
    2648             $rem = array( 'pre' => __( 'This topic is one of your %favorites% [', 'bbpress' ), 'mid' => __( '&times;', 'bbpress' ), 'post' => __( ']', 'bbpress' ) );
     2639        if ( empty( $user_id ) && !$user_id = $current_user->ID )
     2640            return false;
    26492641
    26502642        if ( !current_user_can( 'edit_user', (int) $user_id ) )
    26512643            return false;
    26522644
    2653         $url = esc_url( bbp_get_favorites_link( $user_id ) );
    2654 
    2655         if ( bbp_is_user_favorite( $user_id, $topic->ID ) ) {
     2645        if ( !$topic_id = bbp_get_topic_id() )
     2646            return false;
     2647
     2648        if ( empty( $add ) || !is_array( $add ) ) {
     2649            $add = array(
     2650                'mid'  => __( 'Add this topic to your favorites', 'bbpress' ),
     2651                'post' => __( ' (%?%)', 'bbpress' )
     2652            );
     2653            $url = esc_url( bbp_get_topic_permalink( $topic_id ) );
     2654        }
     2655
     2656        if ( empty( $rem ) || !is_array( $rem ) ) {
     2657            $rem = array(
     2658                'pre'  => __( 'This topic is one of your %favorites% [', 'bbpress' ),
     2659                'mid'  => __( '&times;', 'bbpress' ),
     2660                'post' => __( ']', 'bbpress' )
     2661            );
     2662            $url = esc_url( bbp_get_favorites_link( $user_id ) );
     2663        }
     2664
     2665        if ( bbp_is_user_favorite( $user_id, $topic_id ) ) {
    26562666            $rem  = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $rem );
    2657             $favs = array( 'fav' => '0', 'topic_id' => $topic->ID );
     2667            $favs = array( 'fav' => '0', 'topic_id' => $topic_id );
    26582668            $pre  = ( is_array( $rem ) && isset( $rem['pre']  ) ) ? $rem['pre']  : '';
    26592669            $mid  = ( is_array( $rem ) && isset( $rem['mid']  ) ) ? $rem['mid']  : ( is_string( $rem ) ? $rem : '' );
     
    26612671        } else {
    26622672            $add  = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $add );
    2663             $favs = array( 'fav' => '1', 'topic_id' => $topic->ID );
     2673            $favs = array( 'fav' => '1', 'topic_id' => $topic_id );
    26642674            $pre  = ( is_array( $add ) && isset( $add['pre']  ) ) ? $add['pre']  : '';
    26652675            $mid  = ( is_array( $add ) && isset( $add['mid']  ) ) ? $add['mid']  : ( is_string( $add ) ? $add : '' );
     
    26672677        }
    26682678
    2669         $url = esc_url( wp_nonce_url( add_query_arg( $favs, bbp_get_favorites_link( $user_id ) ), 'toggle-favorite_' . $topic->ID ) );
    2670 
    2671         return apply_filters( 'bbp_get_user_favorites_link', "<span id='favorite-toggle'><span id='favorite-$topic->ID'>$pre<a href='$url' class='dim:favorite-toggle:favorite-$topic->ID:is-favorite'>$mid</a>$post</span></span>" );
     2679        $url = esc_url( wp_nonce_url( add_query_arg( $favs, bbp_get_topic_permalink( $topic_id ) ), 'toggle-favorite_' . $topic_id ) );
     2680
     2681        return apply_filters( 'bbp_get_user_favorites_link', "<span id='favorite-toggle'><span id='favorite-$topic_id'>$pre<a href='$url' class='dim:favorite-toggle:favorite-$topic_id:is-favorite'>$mid</a>$post</span></span>" );
    26722682    }
    26732683
  • branches/plugin/bbp-includes/bbp-users.php

    r2652 r2659  
    11<?php
    22
    3 /* Favorites */
     3/**
     4 * bbp_has_access()
     5 *
     6 * Make sure user can perform special tasks
     7 *
     8 * @package bbPress
     9 * @subpackage Functions
     10 * @since bbPress (r2464)
     11 *
     12 * @uses is_super_admin ()
     13 * @uses apply_filters
     14 *
     15 * @todo bbPress port of existing roles/caps
     16 * @return bool $has_access
     17 */
     18function bbp_has_access () {
     19
     20    if ( is_super_admin () )
     21        $has_access = true;
     22    else
     23        $has_access = false;
     24
     25    return apply_filters( 'bbp_has_access', $has_access );
     26}
     27
     28/**
     29 * bbp_allow_anonymous ()
     30 *
     31 * Returns true|false if anonymous topic creation and replies are allowed
     32 *
     33 * @since bbPress (r2596)
     34 * @return bool
     35 */
     36function bbp_allow_anonymous () {
     37    return apply_filters( 'bbp_allow_anonymous', get_option( 'bbp_allow_anonymous', false ) );
     38}
     39
     40/** START - Favorites *********************************************************/
    441
    542/**
     
    1552 * @return array|bool Results if user has favorites, otherwise false
    1653 */
    17 function bbp_get_user_favorites_topic_ids ( $user_id ) {
    18     if ( !$user_id )
     54function bbp_get_user_favorites_topic_ids ( $user_id = 0 ) {
     55    if ( empty( $user_id ) )
    1956        return;
    2057
     
    4380 * @return array|bool Results if user has favorites, otherwise false
    4481 */
    45 function bbp_get_user_favorites ( $user_id ) {
     82function bbp_get_user_favorites ( $user_id = 0 ) {
     83    if ( empty( $user_id ) )
     84        return;
     85
    4686    $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    4787
     
    68108 */
    69109function bbp_is_user_favorite ( $user_id = 0, $topic_id = 0 ) {
    70     if ( !$user_id ) {
    71         global $current_user;
     110    global $post, $current_user;
     111
     112    if ( empty( $user_id ) ) {
    72113        wp_get_current_user();
    73114        $user_id = $current_user->ID;
    74115    }
    75116
    76     if ( !$user_id )
     117    if ( empty( $user_id ) )
    77118        return false;
    78119
    79120    $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    80121
    81     if ( $topic_id ) {
     122    if ( !empty( $topic_id ) ) {
    82123        $post = get_post( $topic_id );
    83124        $topic_id = $post->ID;
    84125    } elseif ( !$topic_id = bbp_get_topic_id() ) {
    85         global $post;
    86         if ( !$post )
     126        if ( empty( $post ) )
    87127            return false;
    88128
     
    90130    }
    91131
    92     if ( !$favorites || !$topic_id )
     132    if ( empty( $favorites ) || empty( $topic_id ) )
    93133        return false;
    94134
    95135    if ( isset( $favorites ) )
    96             return in_array( $topic_id, $favorites );
     136        return in_array( $topic_id, $favorites );
    97137
    98138    return false;
     
    112152 * @return bool True
    113153 */
    114 function bbp_add_user_favorite ( $user_id, $topic_id ) {
    115     $user_id   = (int) $user_id;
    116     $topic_id  = (int) $topic_id;
     154function bbp_add_user_favorite ( $user_id = 0, $topic_id = 0 ) {
     155    if ( empty( $user_id ) || empty( $topic_id ) )
     156        return false;
     157
    117158    $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id );
    118159    $topic     = get_post( $topic_id );
    119160
    120     if ( !$favorites || !$topic )
     161    if ( empty( $favorites ) || empty( $topic ) )
    121162        return false;
    122163
     
    129170
    130171    do_action( 'bbp_add_user_favorite', $user_id, $topic_id );
     172
    131173    return true;
    132174}
     
    146188 */
    147189function bbp_remove_user_favorite ( $user_id, $topic_id ) {
    148     $user_id   = (int) $user_id;
    149     $topic_id  = (int) $topic_id;
    150     $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id );
    151 
    152     if ( !$favorites || !$topic_id )
     190    if ( empty( $user_id ) || empty( $topic_id ) )
     191        return false;
     192
     193    if ( !$favorites = (array) bbp_get_user_favorites_topic_ids( $user_id ) )
    153194        return false;
    154195
     
    156197        array_splice( $favorites, $pos, 1 );
    157198        $favorites = array_filter( $favorites );
     199
    158200        if ( !empty( $favorites ) ) {
    159201            $favorites = implode( ',', $favorites );
     
    165207
    166208    do_action( 'bbp_remove_user_favorite', $user_id, $topic_id );
     209
    167210    return true;
    168211}
     212
     213/** END - Favorites ***********************************************************/
     214
     215?>
  • branches/plugin/bbp-themes/bbp-twentyten/single-bbp_topic.php

    r2652 r2659  
    2121                            <?php bbp_topic_tag_list(); ?>
    2222
    23                             <?php bbp_user_favorites_link(); ?>
    24 
    2523                            <div id="ajax-response"></div>
    2624
     
    2927                                    <tr>
    3028                                        <th><?php _e( 'Creator', 'bbpress' ); ?></th>
    31                                         <th><?php _e( 'Topic', 'bbpress' ); ?></th>
     29                                        <th>
     30                                            <?php _e( 'Topic', 'bbpress' ); ?>
     31                                            <?php bbp_user_favorites_link(); ?>
     32                                        </th>
    3233                                    </tr>
    3334                                </thead>
  • branches/plugin/bbp-themes/bbp-twentyten/style.css

    r2638 r2659  
    13321332    background-color: #f5f5f5;
    13331333}
     1334table.bbp-forums th span, table.bbp-topics th span,
     1335table.bbp-topic th span, table.bbp-replies th span {
     1336    float: right;
     1337}
    13341338table.bbp-forums tfoot td, table.bbp-topics tfoot td,
    13351339table.bbp-topic tfoot td, table.bbp-replies tfoot td {
    13361340    background-color: #fafafa;
     1341    color: #888;
     1342    font-size: 12px;
     1343    font-weight: bold;
     1344    font-family: 'Helvetica Neue', Arial, Helvetica, 'Nimbus Sans L', sans-serif;
    13371345}
    13381346
Note: See TracChangeset for help on using the changeset viewer.