Skip to:
Content
Pages
Categories
Search
Top
Bottom

Favoriting Blog Posts — Code Inside worked with 1.2.x, not so much with 1.5.x

  • @stwc

    Participant

    So I’ve finally decided to take the plunge and try and update my BP community at Gamefilter.net to 1.5.x, figuring the dust has pretty well settled by now. Working on my localhost install, so the live site is still working fine. The upgrade has gone pretty well, for the most part, except for one thing.

    It’s all a little complicated, but I’m hoping it’s something that someone more skilled with wrangling BP code than me can just eyeball it and say ‘There’s your problem’!

    So I’m using both a heavily customized version of the old BP-Like plugin to provide a ‘Thanks’ functionality for people’s activity, including posting to the main blog. This is counted — thanks given and received — on user profiles.

    Separate to that is BP favoriting (which I’ve renamed to ‘Bookmark’), which lets people, yeah, save stuff as favorites, that they can go back to later. I extended BP favoriting to blog posts with the following functions.php code:

    class BP_Blog_Post_Fav {
    
    var $activity_id;
    
    function init() {
    add_action( 'the_post', array( &$this, 'get_blog_post_activity_id' ) );
    }
    
    function get_blog_post_activity_id() {
    global $current_blog, $post;
    
    if( isset($post->ID) ) :
    $activity_id = bp_activity_get_activity_id( array(
    'user_id' => $post->post_author,
    'type' => 'new_blog_post',
    'component' => 'blogs',
    'item_id' => $current_blog->blog_id,
    'secondary_item_id' => $post->ID) );
    
    $this->activity_id = $activity_id;
    
    if( !empty($this->activity_id) ) {
    add_filter( 'bp_get_activity_favorite_link', array( &$this, 'bp_get_activity_favorite_link' ) );
    add_filter( 'bp_get_activity_unfavorite_link', array( &$this, 'bp_get_activity_unfavorite_link' ) );
    add_filter( 'bp_get_activity_is_favorite', array( &$this, 'bp_get_activity_is_favorite' ) );
    }
    
    endif;
    
    }
    
    function bp_get_activity_favorite_link() {
    global $current_blog;
    
    if( $current_blog->blog_id != BP_ROOT_BLOG )
    switch_to_blog( BP_ROOT_BLOG );
    
    $nonce = wp_nonce_url( site_url( BP_ACTIVITY_SLUG . '/favorite/' . $this->activity_id . '/' ), 'mark_favorite' );
    
    if( $current_blog->blog_id != BP_ROOT_BLOG )
    restore_current_blog();
    
    return $nonce;
    }
    
    function bp_get_activity_unfavorite_link() {
    global $current_blog;
    
    if( $current_blog->blog_id != BP_ROOT_BLOG )
    switch_to_blog( BP_ROOT_BLOG );
    
    $nonce = wp_nonce_url( site_url( BP_ACTIVITY_SLUG . '/unfavorite/' . $this->activity_id . '/' ), 'unmark_favorite' );
    
    if( $current_blog->blog_id != BP_ROOT_BLOG )
    restore_current_blog();
    
    return $nonce;
    }
    
    function bp_get_activity_is_favorite() {
    global $bp;
    
    return in_array( $this->activity_id, (array)bp_activity_get_user_favorites($bp->loggedin_user->id) );
    }
    }
    $my_bp_favorite = new BP_Blog_Post_Fav();
    $my_bp_favorite->init();
    

    and I build a favorite link on a blog post (remember, it’s called ‘bookmark’ on the live site, in case you want to look at it (but it’s only visible for logged-in users, so)) like so

    <a class=&quot;button fav bp-secondary-action&quot; href=&quot;" title=""></a>

    That builds a link that looks like this (on localhost)

    http://localhost/gamefilter/activity/favorite/837/?_wpnonce=66c4831cef

    This worked swimmingly on my current 1.2 install on the live site, but now, on my local testbed, clicking the link doesn’t do any ajax, and just takes me to the search results page, with no results found.

    I guess something obvious has changed since 1.2, but I’ve spent all day hammering away and can’t seem to figure it out. If anybody has any ideas, I’d be very grateful. Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • @djpaul

    Keymaster

    Are you running BP-Default 1.2.x’s javascript, or are you using 1.5s?

    @stwc

    Participant

    Er, um. I haven’t done anything deliberate in either direction — just upgraded BP (and the template pack, because I wasn’t sure at all how that fit into things). So, since I don’t have an _inc folder in my theme, I’m assuming it’s coming from BP 1.5.x….?

    Is there a quick reference anywhere for wrangling BP’s js stuff? I must admit after all this time and code, there is still a lot that is very much opaque to me with BP.

    Edit: Back at my dev machine, and I’m definitely loading global.js from the updated BP1.5 default theme.

    @stwc

    Participant

    I *think* I have narrowed it down to the Buddypress template pack, which has kinda confused me since square one, like 2 years ago….

    @stwc

    Participant

    Semi-resolved: I still don’t know why it was happening, but I’ve duplicated and modified the stream event delegation functions (for favoriting) (global.js line 166 or so) and targetted them to the posts container div on the front page instead, and the ajax is firing now.

    Followup question: if I have _inc/global.js in my theme, it’ll override the one in bp-default, like everything else would, yes? And thus be safe from BP upgrade overwriting?

    @stwc

    Participant

    Hmm. I just tried that, and no joy, so I’m not so sure now where to put the js. In my theme’s custom js, for now, I guess.

    @stwc

    Participant

    Ajax fires, actual favoriting code isn’t writing to wp_bp_activity_meta any more. Sigh.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Favoriting Blog Posts — Code Inside worked with 1.2.x, not so much with 1.5.x’ is closed to new replies.
Skip to toolbar