Support » Plugin: Private groups » Bug with freshness for a given forum_id

  • Resolved Oelita

    (@oelita)


    Hello,

    I’m having a main forum index where I want to add freshness information for every subforum of every forum.
    So I’m calling
    bbp_get_forum_freshness_link($sub_forum->ID ) in my subforum loop of my template file (llop-single-forum).
    And there is a bug : every subforum freshness is equal to its parent forum freshness. Though I’m asking for $sub_forum->ID.

    The bug in your code is that you are putting a filter like this :
    add_filter('bbp_get_forum_freshness_link', 'pg_get_forum_freshness_link' );
    and your function is without any parameter.

    You should have 2 parameters and send the second one, the forum_id, to your subfunction :

    function pg_get_forum_freshness_link ($anchor, $forum_id) {
    	$anchor = pg_get_forum_freshness_link_anchor($forum_id) ;

    I made the correction in my files, and it works.

    I hope you can correct it in your next version, thanks !

    Sylvie

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Robin W

    (@robin-w)

    Thanks for that – always happy to improve my code and 3.3.3 just released should fix that

    Please confirm !

    Thread Starter Oelita

    (@oelita)

    Thanks for reacting.
    But it is not ok, sorry.

    – You miss to send the new $forum_id parameter to your function pg_get_forum_freshness_link_anchor, so I corrected this before my subsequent testing

    – I don’t get the ‘no topics’ message when there are no topics in the forum (your code seems weird to me as you overwrite your $active_id without testing it first ?)

    – There is a wrong freshness on my parent forum, which is one of my subforums freshness, though I have another subforum with a fresher reply. As a hint, I’m in group1 and have access to this forum 1 and all subforums. The subforum which gets promoted as the freshest is a read-private-forum for group 2, while all others subforums are invisible to group2. (but I don’t care, cause I’m in group 1 so I can see them all)

    Does it help ?

    Thread Starter Oelita

    (@oelita)

    and the replies count is weird, too (here is the forum and all its 3 subforums):
    forum and 3 subforums

    • This reply was modified 4 years, 8 months ago by .
    Thread Starter Oelita

    (@oelita)

    The wrong replies count is my code fault (call to bbp_forum_post_count instead of bbp_forum_reply_count).

    The wrong freshness in my parent forum is due to the fact that the freshest message is a subforum topic and not a subforum reply, there is correction to make in your code.
    I have made some correction in rpg_get_forum_last_active_id, and it is now ok for me, would you want to have my code to look at it ?

    Plugin Author Robin W

    (@robin-w)

    I have made some correction in rpg_get_forum_last_active_id, and it is now ok for me, would you want to have my code to look at it ?

    yes please -please post any code corrections you think are needed so that I can take a look.

    I have just been working in this area, as another user has a category and sub-forums, but not all users see all sub-forums, so latest topic/reply in the category is not always the correct one to show, hence some new code that is obviously not working correctly.

    can you also post code for

    – You miss to send the new $forum_id parameter to your function pg_get_forum_freshness_link_anchor, so I corrected this before my subsequent testing

    as I don’t understand exactly what you mean.

    I don’t get the ‘no topics’ message when there are no topics in the forum (your code seems weird to me as you overwrite your $active_id without testing it first ?)

    can you post any new code, or the code area you mean to save me trying to find.

    Really appreciate you help here – it is not easy being a one man coding and testing machine !

    Thread Starter Oelita

    (@oelita)

    function pg_get_forum_freshness_link ($anchor, $forum_id) {
    	$anchor = pg_get_forum_freshness_link_anchor($forum_id) ;
    
    function rpg_get_forum_last_active_id ($active_id, $forum_id) {
    	$forum_id = bbp_get_forum_id( $forum_id );
    	$sub_forums = private_groups_get_permitted_subforums($forum_id) ;
    	$active_id = 0;
    	if ( !empty( $sub_forums ) ) {
    		$show = array();
    		//find the latest permissible 
    		foreach ( $sub_forums as $sub_forum ) {
    			$sub_forum_id =  $sub_forum->ID ;
    			$active_id = get_post_meta( $sub_forum_id , '_bbp_last_active_id', true );
    			$last_active = get_post_meta( $sub_forum_id, '_bbp_last_active_time', true );
    			if ( empty( $active_id ) ) { // not replies, maybe topics ?
    				$active_id = bbp_get_forum_last_topic_id( $sub_forum_id );
    				if ( !empty( $active_id ) ) {
    					$last_active = bbp_get_topic_last_active_time( $active_id );
    				}
    			}
    			if ( !empty( $active_id ) ) {
    				$curdate = strtotime($last_active);
    				$show[$curdate] = $active_id ;
    			}
    		}
    		$mostRecent= 0;
    		foreach($show as $date=>$value){
    			if ($date > $mostRecent) {
    				 $mostRecent = $date;
    			}
    		}
    		if ($mostRecent != 0) {
    			$active_id = $show[$mostRecent] ;
    		} else {
    			$active_id = 0;
    		}
    	}
    	return apply_filters( 'pg_get_forum_last_active_id', $active_id, $forum_id );
    }
    function pg_get_forum_freshness_link_anchor( $forum_id = 0 ) {
    	global $rpg_settingsf ;
    		$forum_id  = bbp_get_forum_id( $forum_id );
    		$active_id = bbp_get_forum_last_active_id( $forum_id ); // last repply
    		if ( empty( $active_id ) )
    			$active_id = bbp_get_forum_last_reply_id( $forum_id );
    		if ( empty( $active_id ) )
    			$active_id = bbp_get_forum_last_topic_id( $forum_id );
    		if ( !empty( $active_id ) ) {
    			//then reset forum_id to the forum of the active topic in case it is a sub forum
    			$forum_id = private_groups_get_forum_id_from_post_id($active_id) ;
    			$link_url  = $title = '';
    			if ( bbp_is_topic( $active_id ) ) {
    				$link_url = bbp_get_forum_last_topic_permalink( $forum_id );
    				$title    = bbp_get_forum_last_topic_title( $forum_id );
    			} elseif ( bbp_is_reply( $active_id ) ) {
    				$link_url = bbp_get_forum_last_reply_url( $forum_id );
    				$title    = bbp_get_forum_last_reply_title( $forum_id );
    			}
    			$time_since = bbp_get_forum_last_active_time( $forum_id );
    			if ( !empty( $time_since ) && !empty( $link_url ) ) {
    				//test if user can see this post, and post link if they can
    				$user_id = wp_get_current_user()->ID;
    				//now we can check if the user can view this, and if it's not private
    				if (private_groups_can_user_view_post($user_id,$active_id) &&  !bbp_is_forum_private($active_id)) {
    					$anchor ['link_url'] = $link_url ;
    					$anchor ['title'] = $title;
    					$anchor ['display'] = $time_since;
    				}
    				//if it is private, then check user can view
    				elseif (private_groups_can_user_view_post($user_id,$active_id) && 	bbp_is_forum_private($active_id) && current_user_can( 'read_private_forums' ) ) {
    					$anchor ['link_url'] = $link_url ;
    					$anchor ['title'] = $title;
    					$anchor ['display'] = $time_since;
    				}	
    				//else we need to work out what to show so 
    				//else if visibility is set...
    				elseif (!empty ($rpg_settingsf['set_forum_visibility'])) {
    					//so we need to set up a $link that will determine where they go, and change the date to a freshness message if set
    					//so set up for noite logged in and logged in
    					if (!is_user_logged_in()) {
    						if($rpg_settingsf['redirect_page2']) {
    							$link_url=$rpg_settingsf['redirect_page2'] ;
    						}	
    						else {		
    							$link_url="/wp-login";
    						}
    					}
    					//so if logged in, 
    					else {
    						if($rpg_settingsf['redirect_page1']) {
    							$link_url=$rpg_settingsf['redirect_page1'] ;
    						}	else {
    							$link_url='/404';
    						}
    					}
    					//now see if there is a freshness message
    					if (!empty ($rpg_settingsf['set_freshness_message']) ) {
    						$title=$rpg_settingsf['freshness_message'] ;
    					}
    					else {
    						$title = esc_html( $time_since ) ;
    					}
    					//so we have a link and a title, so create the anchor
    					$anchor ['link_url'] = $link_url ;
    					$anchor ['title'] = $title;
    					$anchor ['display'] = $title;
    				}
    				else {
    					//visibility is not set, and we have no topics to show the user so
    					$anchor ['title'] = 'no_topics';
    				}
    			}	else {
    				// we have no topics, or something else has gone wrong !
    				$anchor ['title'] = 'no_topics';
    			}
    		}	else {
    			// we have no topics, or something else has gone wrong !
    			$anchor ['title'] = 'no_topics';
    		}
    	return $anchor;
    }
    Plugin Author Robin W

    (@robin-w)

    @oelita – thanks for that code, and for helping me to improve my plugin

    version 3.3.4 should have all this in.

    Plugin Author Robin W

    (@robin-w)

    @oelita The latest code is producing an issue.

    The standard bbp_get_forum_last_active_id calculates the correct $active_id, and passes this to my rpg_get_forum_last_active_id. Unless there are sub forums, in which case we need to check which of the sub forums the user has access to, this $active_id is correct.

    so setting $active_id = 0 in the last line of the fragment below

    function rpg_get_forum_last_active_id ($active_id, $forum_id) {
    	$forum_id = bbp_get_forum_id( $forum_id );
    	$sub_forums = private_groups_get_permitted_subforums($forum_id) ;
    	$active_id = 0;

    means it is always retuned as 0 unless there are sub forums which is incorrect.

    I plan to alter this code to only do this line only if there are sub-forums, so it will now look like

    function rpg_get_forum_last_active_id ($active_id, $forum_id) {
    	//next line not needed as we have already done that in the bbp_get_forum_last_active_id function we are now filtering
    	//$forum_id = bbp_get_forum_id( $forum_id );
    	$sub_forums = private_groups_get_permitted_subforums($forum_id) ;
    	if ( !empty( $sub_forums ) ) {
    		$active_id = 0;

    etc.

    Just checking that this also works for you before I release ?

    • This reply was modified 4 years, 8 months ago by Robin W.
    • This reply was modified 4 years, 8 months ago by Robin W.
    Plugin Author Robin W

    (@robin-w)

    version 3.3.5 just released – let me know if it causes issues

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Bug with freshness for a given forum_id’ is closed to new replies.