Skip to:
Content

bbPress.org

Changeset 2652


Ignore:
Timestamp:
11/27/2010 09:05:29 AM (16 years ago)
Author:
johnjamesjacoby
Message:

First pass at user favorites. Props GautamGupta via Google Code In

Location:
branches/plugin
Files:
4 added
7 edited

Legend:

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

    r2633 r2652  
    3838function bbp_recount_list () {
    3939        $recount_list = array(
    40                 5  => array( 'bbp-forum-topics',          __( 'Count topics in each forum',           'bbpress' ), 'bbp_recount_forum_topics'          ),
    41                 10 => array( 'bbp-forum-replies',         __( 'Count replies in each forum',          'bbpress' ), 'bbp_recount_forum_replies'         ),
    42                 15 => array( 'bbp-topic-replies',         __( 'Count replies in each topic',          'bbpress' ), 'bbp_recount_topic_replies'         ),
    43                 20 => array( 'bbp-topic-voices',          __( 'Count voices in each topic',           'bbpress' ), 'bbp_recount_topic_voices'          ),
    44                 25 => array( 'bbp-topic-trashed-replies', __( 'Count trashed replies in each topic',  'bbpress' ), 'bbp_recount_topic_trashed_replies' ),
    45                 30 => array( 'bbp-topics-replied',        __( 'Count replies for each user',          'bbpress' ), 'bbp_recount_user_topics_replied'   ),
    46                 //35 => array( 'bbp-topic-tag-count',       __( 'Count tags for every topic',                  'bbpress' ), 'bbp_recount_topic_tags'            ),
    47                 //40 => array( 'bbp-tags-tag-count',        __( 'Count topics for every tag',                  'bbpress' ), 'bbp_recount_tag_topics'            ),
    48                 //45 => array( 'bbp-tags-delete-empty',     __( 'Delete tags with no topics',                  'bbpress' ), 'bbp_recount_tag_delete_empty'      ),
    49                 //50 => array( 'bbp-clean-favorites',       __( 'Remove deleted topics from user favorites',   'bbpress' ), 'bbp_recount_clean_favorites'       )
     40                5  => array( 'bbp-forum-topics',          __( 'Count topics in each forum',                'bbpress' ), 'bbp_recount_forum_topics'          ),
     41                10 => array( 'bbp-forum-replies',         __( 'Count replies in each forum',               'bbpress' ), 'bbp_recount_forum_replies'         ),
     42                15 => array( 'bbp-topic-replies',         __( 'Count replies in each topic',               'bbpress' ), 'bbp_recount_topic_replies'         ),
     43                20 => array( 'bbp-topic-voices',          __( 'Count voices in each topic',                'bbpress' ), 'bbp_recount_topic_voices'          ),
     44                25 => array( 'bbp-topic-trashed-replies', __( 'Count trashed replies in each topic',       'bbpress' ), 'bbp_recount_topic_trashed_replies' ),
     45                30 => array( 'bbp-topics-replied',        __( 'Count replies for each user',               'bbpress' ), 'bbp_recount_user_topics_replied'   ),
     46                35 => array( 'bbp-clean-favorites',       __( 'Remove deleted topics from user favorites', 'bbpress' ), 'bbp_recount_clean_favorites'       )
     47                //40 => array( 'bbp-topic-tag-count',       __( 'Count tags for every topic',                'bbpress' ), 'bbp_recount_topic_tags'            ),
     48                //45 => array( 'bbp-tags-tag-count',        __( 'Count topics for every tag',                'bbpress' ), 'bbp_recount_tag_topics'            ),
     49                //50 => array( 'bbp-tags-delete-empty',     __( 'Delete tags with no topics',                'bbpress' ), 'bbp_recount_tag_delete_empty'      )
    5050        );
    5151
    … …  
    447447
    448448function bbp_recount_clean_favorites () {
    449         global $wpdb;
     449        global $wpdb, $bbp;
    450450
    451451        $statement = __( 'Removing deleted topics from user favorites… %s', 'bbpress' );
    452452        $result    = __( 'Failed!', 'bbpress' );
    453453
    454 //      $meta_key  = $wpdb->prefix . 'favorites';
    455 //
    456 //      $users = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `$wpdb->usermeta` WHERE `meta_key` = '$meta_key';" );
    457 //      if ( is_wp_error( $users ) )
    458 //              return array( 1, sprintf( $statement, $result ) );
    459 //
    460 //      $topics = $wpdb->get_col( "SELECT `topic_id` FROM `$wpdb->topics` WHERE `topic_status` = '0';" );
    461 //
    462 //      if ( is_wp_error( $topics ) )
    463 //              return array( 2, sprintf( $statement, $result ) );
    464 //
    465 //      $values = array( );
    466 //      foreach ( $users as $user ) {
    467 //              if ( empty( $user->favorites ) || !is_string( $user->favorites ) ) {
    468 //                      continue;
    469 //              }
    470 //              $favorites = explode( ',', $user->favorites );
    471 //              if ( empty( $favorites ) || !is_array( $favorites ) ) {
    472 //                      continue;
    473 //              }
    474 //              $favorites = join( ',', array_intersect( $topics, $favorites ) );
    475 //              $values[] = "('$user->user_id', '$meta_key', '$favorites')";
    476 //      }
    477 //
    478 //      if ( !count( $values ) ) {
    479 //              $result = __( 'Nothing to remove!', 'bbpress' );
    480 //              return array( 0, sprintf( $statement, $result ) );
    481 //      }
    482 //
    483 //      $sql_delete = "DELETE FROM `$wpdb->usermeta` WHERE `meta_key` = '$meta_key';";
    484 //      if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    485 //              return array( 4, sprintf( $statement, $result ) );
    486 //
    487 //      $values = array_chunk( $values, 10000 );
    488 //      foreach ( $values as $chunk ) {
    489 //              $chunk = "\n" . join( ",\n", $chunk );
    490 //              $sql_insert = "INSERT INTO `$wpdb->usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
    491 //              if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) {
    492 //                      return array( 5, sprintf( $statement, $result ) );
    493 //              }
    494 //      }
    495 //
    496 //      $result = __( 'Complete!', 'bbpress' );
     454        $users = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `$wpdb->usermeta` WHERE `meta_key` = '_bbp_favorites';" );
     455        if ( is_wp_error( $users ) )
     456                return array( 1, sprintf( $statement, $result ) );
     457
     458        $topics = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = '$bbp->topic_id' AND `post_status` = 'publish';" );
     459
     460        if ( is_wp_error( $topics ) )
     461                return array( 2, sprintf( $statement, $result ) );
     462
     463        $values = array();
     464        foreach ( $users as $user ) {
     465                if ( empty( $user->favorites ) || !is_string( $user->favorites ) )
     466                        continue;
     467
     468                $favorites = array_intersect( $topics, (array) explode( ',', $user->favorites ) );
     469                if ( empty( $favorites ) || !is_array( $favorites ) )
     470                        continue;
     471
     472                $favorites = join( ',', $favorites );
     473                $values[] = "('$user->user_id', '_bbp_favorites', '$favorites')";
     474        }
     475
     476        if ( !count( $values ) ) {
     477                $result = __( 'Nothing to remove!', 'bbpress' );
     478                return array( 0, sprintf( $statement, $result ) );
     479        }
     480
     481        $sql_delete = "DELETE FROM `$wpdb->usermeta` WHERE `meta_key` = '_bbp_favorites';";
     482        if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
     483                return array( 4, sprintf( $statement, $result ) );
     484
     485        $values = array_chunk( $values, 10000 );
     486        foreach ( $values as $chunk ) {
     487                $chunk = "\n" . join( ",\n", $chunk );
     488                $sql_insert = "INSERT INTO `$wpdb->usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
     489                if ( is_wp_error( $wpdb->query( $sql_insert ) ) )
     490                        return array( 5, sprintf( $statement, $result ) );
     491        }
     492
     493        $result = __( 'Complete!', 'bbpress' );
    497494        return array( 0, sprintf( $statement, $result ) );
    498495}
  • branches/plugin/bbp-admin/bbp-tools.php

    r2649 r2652  
    2727        <div class="wrap">
    2828
     29                <?php do_action( 'admin_notices' ); ?>
    2930                <?php screen_icon( 'tools' ); ?>
    3031
  • branches/plugin/bbp-includes/bbp-functions.php

    r2634 r2652  
    364364}
    365365
     366/**
     367 * bbp_dim_favorite ()
     368 *
     369 * Add or remove a topic from a user's favorites
     370 *
     371 * @package bbPress
     372 * @subpackage Template Tags
     373 * @since bbPress (r2652)
     374 *
     375 * @return void
     376 */
     377function 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}
     404add_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 *
     415 * @param int $topic_id Topic ID to remove
     416 * @return void
     417 */
     418function bbp_remove_topic_from_all_favorites ( $topic_id = 0 ) {
     419        global $wpdb;
     420
     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}
     425add_action( 'trash_post', 'bbp_remove_topic_from_all_favorites' );
     426add_action( 'delete_post', 'bbp_remove_topic_from_all_favorites' );
     427
     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 */
     439function 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}
     447add_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 */
     460function bbp_scripts () {
     461        if ( !bbp_is_topic() )
     462                return;
     463
     464        echo "<script type='text/javascript'>
     465/* <![CDATA[ */
     466var ajaxurl = '" . admin_url( 'admin-ajax.php' ) . "';
     467/* ]]> */
     468</script>\n";
     469}
     470add_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 */
     485function 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}
     507add_filter( 'wp_enqueue_scripts', 'bbp_topic_script_localization' );
     508
    366509?>
  • branches/plugin/bbp-includes/bbp-template.php

    r2634 r2652  
    567567 * @since bbPress (r2625)
    568568 *
    569  * @param int $forum_id 
     569 * @param int $forum_id
    570570 */
    571571function bbp_forum_freshness_link ( $forum_id = 0) {
    … …  
    25462546}
    25472547
     2548/**
     2549 * bbp_is_favorites ()
     2550 *
     2551 * Check if current page is a bbPress user's favorites page (author page)
     2552 *
     2553 * @since bbPress (r2652)
     2554 *
     2555 * @return bool
     2556 */
     2557function bbp_is_favorites () {
     2558        return (bool) is_author();
     2559}
     2560
    25482561/** END is_ Functions *********************************************************/
     2562
     2563/** START Favorites Functions *************************************************/
     2564
     2565/**
     2566 * bbp_favorites_link ()
     2567 *
     2568 * Output the link to the user's favorites page (author page)
     2569 *
     2570 * @package bbPress
     2571 * @subpackage Template Tags
     2572 * @since bbPress (r2652)
     2573 *
     2574 * @param int $user_id optional
     2575 * @uses bbp_get_favorites_link()
     2576 */
     2577function bbp_favorites_link ( $user_id = 0 ) {
     2578        echo bbp_get_favorites_link( $user_id );
     2579}
     2580        /**
     2581         * bbp_get_favorites_link ()
     2582         *
     2583         * Return the link to the user's favorites page (author page)
     2584         *
     2585         * @package bbPress
     2586         * @subpackage Template Tags
     2587         * @since bbPress (r2652)
     2588         *
     2589         * @param int $user_id optional
     2590         * @uses apply_filters
     2591         * @uses get_author_posts_url
     2592         * @return string Permanent link to topic
     2593         */
     2594        function bbp_get_favorites_link ( $user_id = 0 ) {
     2595                return apply_filters( 'bbp_get_favorites_link', get_author_posts_url( $user_id ) );
     2596        }
     2597
     2598/**
     2599 * bbp_user_favorites_link ()
     2600 *
     2601 * Output the link to the user's favorites page (author page)
     2602 *
     2603 * @package bbPress
     2604 * @subpackage Template Tags
     2605 * @since bbPress (r2652)
     2606 *
     2607 * @param array $add optional
     2608 * @param array $rem optional
     2609 * @param int $user_id optional
     2610 *
     2611 * @uses bbp_get_favorites_link()
     2612 */
     2613function bbp_user_favorites_link ( $add = array(), $rem = array(), $user_id = 0 ) {
     2614        echo bbp_get_user_favorites_link( $add, $rem, $user_id );
     2615}
     2616        /**
     2617         * bbp_get_user_favorites_link ()
     2618         *
     2619         * Return the link to the user's favorites page (author page)
     2620         *
     2621         * @package bbPress
     2622         * @subpackage Template Tags
     2623         * @since bbPress (r2652)
     2624         *
     2625         * @param array $add optional
     2626         * @param array $rem optional
     2627         * @param int $user_id optional
     2628         *
     2629         * @uses apply_filters
     2630         * @uses get_author_posts_url
     2631         * @return string Permanent link to topic
     2632         */
     2633        function bbp_get_user_favorites_link ( $add = array(), $rem = array(), $user_id = 0 ) {
     2634                global $current_user;
     2635                wp_get_current_user();
     2636
     2637                if ( !$user_id && !$user_id = $current_user->ID )
     2638                        return;
     2639
     2640                $topic = get_post( bbp_get_topic_id() );
     2641
     2642                if ( empty( $add ) || !is_array( $add ) )
     2643                        $add = array( 'mid' => __( 'Add this topic to your favorites', 'bbpress' ), 'post' => __( ' (%?%)', 'bbpress' ) );
     2644
     2645                if ( empty( $rem ) || !is_array( $rem ) )
     2646                        $rem = array( 'pre' => __( 'This topic is one of your %favorites% [', 'bbpress' ), 'mid' => __( '&times;', 'bbpress' ), 'post' => __( ']', 'bbpress' ) );
     2647
     2648                if ( !current_user_can( 'edit_user', (int) $user_id ) )
     2649                        return false;
     2650
     2651                $url = esc_url( bbp_get_favorites_link( $user_id ) );
     2652
     2653                if ( bbp_is_user_favorite( $user_id, $topic->ID ) ) {
     2654                        $rem  = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $rem );
     2655                        $favs = array( 'fav' => '0', 'topic_id' => $topic->ID );
     2656                        $pre  = ( is_array( $rem ) && isset( $rem['pre']  ) ) ? $rem['pre']  : '';
     2657                        $mid  = ( is_array( $rem ) && isset( $rem['mid']  ) ) ? $rem['mid']  : ( is_string( $rem ) ? $rem : '' );
     2658                        $post = ( is_array( $rem ) && isset( $rem['post'] ) ) ? $rem['post'] : '';
     2659                } else {
     2660                        $add  = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $add );
     2661                        $favs = array( 'fav' => '1', 'topic_id' => $topic->ID );
     2662                        $pre  = ( is_array( $add ) && isset( $add['pre']  ) ) ? $add['pre']  : '';
     2663                        $mid  = ( is_array( $add ) && isset( $add['mid']  ) ) ? $add['mid']  : ( is_string( $add ) ? $add : '' );
     2664                        $post = ( is_array( $add ) && isset( $add['post'] ) ) ? $add['post'] : '';
     2665                }
     2666
     2667                $url = esc_url( wp_nonce_url( add_query_arg( $favs, bbp_get_favorites_link( $user_id ) ), 'toggle-favorite_' . $topic->ID ) );
     2668
     2669                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>" );
     2670        }
     2671
     2672/** END Favorites Functions ***************************************************/
    25492673
    25502674/** START User Functions ******************************************************/
    … …  
    26472771 * @uses wp_nonce_field, bbp_forum_id
    26482772 */
    2649 function bbp_new_topic_form_fields () { 
    2650        
     2773function bbp_new_topic_form_fields () {
     2774
    26512775        if ( bbp_is_forum() ) : ?>
    26522776
  • branches/plugin/bbp-themes/bbp-twentyten/loop-bbp_topics.php

    r2628 r2652  
    88?>
    99
    10 <?php if ( bbp_has_topics() ) : ?>
     10<?php
     11if ( !bbp_is_favorites() ) :
     12        $_bbp_query = bbp_has_topics();
     13else :
     14        $_bbp_query = true;
     15endif;
     16?>
     17
     18<?php if ( $_bbp_query ) : ?>
    1119
    1220        <?php get_template_part( 'pagination', 'bbp_topics' ); ?>
  • branches/plugin/bbp-themes/bbp-twentyten/single-bbp_topic.php

    r2635 r2652  
    2020
    2121                                                        <?php bbp_topic_tag_list(); ?>
     22
     23                                                        <?php bbp_user_favorites_link(); ?>
     24
     25                                                        <div id="ajax-response"></div>
    2226
    2327                                                        <table class="bbp-topic" id="bbp-topic-<?php bbp_topic_id(); ?>">
  • branches/plugin/bbpress.php

    r2650 r2652  
    8888
    8989                // Unique identifiers
    90                 $this->forum_id       = apply_filters( 'bbp_forum_post_type', 'bbp_forum' );
    91                 $this->topic_id       = apply_filters( 'bbp_topic_post_type', 'bbp_topic' );
    92                 $this->reply_id       = apply_filters( 'bbp_reply_post_type', 'bbp_reply' );
     90                $this->forum_id       = apply_filters( 'bbp_forum_post_type', 'bbp_forum'     );
     91                $this->topic_id       = apply_filters( 'bbp_topic_post_type', 'bbp_topic'     );
     92                $this->reply_id       = apply_filters( 'bbp_reply_post_type', 'bbp_reply'     );
    9393                $this->topic_tag_id   = apply_filters( 'bbp_topic_tag_id',    'bbp_topic_tag' );
    9494
    … …  
    113113
    114114                // Load the files
    115                 require_once ( $this->plugin_dir . '/bbp-includes/bbp-loader.php' );
    116                 require_once ( $this->plugin_dir . '/bbp-includes/bbp-caps.php' );
    117                 require_once ( $this->plugin_dir . '/bbp-includes/bbp-filters.php' );
    118                 require_once ( $this->plugin_dir . '/bbp-includes/bbp-classes.php' );
     115                require_once ( $this->plugin_dir . '/bbp-includes/bbp-loader.php'    );
     116                require_once ( $this->plugin_dir . '/bbp-includes/bbp-caps.php'      );
     117                require_once ( $this->plugin_dir . '/bbp-includes/bbp-filters.php'   );
     118                require_once ( $this->plugin_dir . '/bbp-includes/bbp-classes.php'   );
    119119                require_once ( $this->plugin_dir . '/bbp-includes/bbp-functions.php' );
    120                 require_once ( $this->plugin_dir . '/bbp-includes/bbp-template.php' );
     120                require_once ( $this->plugin_dir . '/bbp-includes/bbp-users.php'     );
     121                require_once ( $this->plugin_dir . '/bbp-includes/bbp-template.php'  );
    121122
    122123                // Quick admin check and load if needed
Note: See TracChangeset for help on using the changeset viewer.