BuddyPress - Featured Video in Profile Tab


#1

Buddypress has the ability to have different sections of the profile.

I am currently constructing a dating site where the user can upload a video of themselves instead of answering profile questions. The video will appear in the tabs as the first tab IF they have set a video to be “Featured”.

I have set up so that the content doesn’t show up unless the user has a featured item to display.

if (!class_exists('BpMembersTab_rt_media') && function_exists('rtmedia_featured')):

My question is how to alter the tab php so that this specific tab doesn’t show without a featured media being active.

if (!class_exists('BpMembersTabs')): 
class BpMembersTabs {
    
    private static $instance;
    public $tabs;
    public $active_tab = false;
    public $album_exists = false;
    public $has_data = array();
    public $fields_data = array();
    
    public function __construct($tabs) {
        
        self::$instance =& $this;
        
        $this->tabs = $tabs;
        $this->active_tab = apply_filters( 'kleo_bp_profile_default_top_tab',FALSE );

        $this->render();
    }

	public static function &get_instance()
	{
		return self::$instance;
	}
  
    public function render()
    {
        if (!is_array($this->tabs)) {
            return;
        }
        
        echo '<dl class="tabs pill custom">';
        
        foreach ($this->tabs as $key => $tab)
        {
            if (empty($tab)) {
                continue;
            }
            if (!isset($tab['group'])) {
                $tab['group'] = $tab['name'];
                $this->tabs[$key]['group'] = $tab['name'];
            }
            
            $name = 'BpMembersTab_'.$tab['type'];
            if (class_exists($name)) {
                $tabcls = new $name($tab);
            }
            else {
                $tabcls = new BpMembersTab($tab);
            }
            
            if ($tabcls->has_profile_data($tab['group'])) {
                echo $tabcls->title();
            }
            
        }
        echo '</dl>';
        
        echo '<ul class="tabs-content custom">';
        foreach ($this->tabs as $tab)
        {
            
            if (isset($this->fields_data[$tab['group']]))
            {
                $active = '';
                if($this->active_tab == esc_attr(str_replace("%", "", sanitize_title_with_dashes($tab['name']))) ) {
                    $active = 'active';  
                }
                
                echo '<li '.$active.' id="'.esc_attr(str_replace("%", "", sanitize_title_with_dashes($tab['name']))).'Tab" class="'.$active." ".$tab['class'].'">';
                echo $this->fields_data[$tab['group']];
                echo '</li>';
            }
        }
        echo '</ul>';
    }
    
}
endif;

#2

Hello @JuliahS,

Try using below function :

 rtmedia_get_featured() 

It will return false when there is no featured media. Hope this will help you. Let us know if you have any doubt.

Thanks.


#3

The main issue was found to be that the tab does not show up in the front end for a different profile, but if the user is logged in and looking at their own profile, then the tab is there no matter what.

I need it to conditionally show up on the back end as well.

Here is the rtmedia tab php. I’m still learning, so excuse the code.

if (!class_exists('BpMembersTab_rt_media') && function_exists('rtmedia_get_featured')):


	class BpMembersTab_rt_media extends BpMembersTab 
	{
	
		private $friendship;
		private $number = 0;

		public function __construct($args) {
			parent::__construct($args);
			$this->friendship = new RTMediaFriends();
		}
		

		public function title() 
		{
			$active = '';
			if($this->tabs_instance->active_tab === FALSE || $this->tabs_instance->active_tab == $this->args["name"] ) 
			{ 
				$active = 'active';  
				$this->tabs_instance->active_tab = esc_attr(str_replace("%", "",sanitize_title_with_dashes($this->args["name"])));
			}

			return '<dd class="featured-video '.$active.'"><a href="#'.esc_attr(str_replace("%", "",sanitize_title_with_dashes($this->args["name"]))).'">'.$this->args["name"].'</a></dd>';
		}

		public function has_profile_data ( $name ) {
			if (!class_exists('RTMedia') && !function_exists('rtmedia_get_featured')) {
				return false;
			
			}

			global $wpdb;
			$user = $this->get_user();
			$displayed_user = bp_displayed_user_id();
			$table_name = $wpdb->base_prefix."rt_rtm_media";

			$where = "SELECT * FROM {$table_name}";
			$where .= " WHERE {$table_name}.media_author = $displayed_user";
			$where .= " AND ({$table_name}.privacy is NULL OR {$table_name}.privacy<=0";
            $where .= " AND {$table_name}.context = 'profile'";
			if ( $user ) {
				$where .= " OR {$table_name}.privacy=20";
				//if my profile
				if ($user == $displayed_user || is_super_admin())
				{
					$where .= " OR {$table_name}.privacy >= 40" ;
				}
				else
				{
					if ( class_exists ( 'BuddyPress' ) && bp_is_active ( 'friends' ) ) {
						//my friends
						$friends = $this->friendship->get_friends_cache ( $user );
						//if displayed user is my friend -> view its pictures
						if (in_array($displayed_user, $friends))
						{
							$where .= " OR {$table_name}.privacy=40";
						}
					}
				}
			}
			$where .= ") ORDER BY media_id DESC LIMIT ".apply_filters('kleo_rtmedia_photo_limit',1);
			$myrows = $wpdb->get_results($where);
			


			if($myrows && count($myrows) > 0)
			{
				$this->tabs_instance->has_data[$name] = true;
				$user_id = bp_displayed_user_id();

				foreach($myrows as $row)
				{


					$this->tabs_instance->fields_data[$name] .= rtmedia_get_featured($user_id);

				}
				
				}

				if (isset($this->tabs_instance->has_data[$name]) && $this->tabs_instance->has_data[$name] ==  true)
					return true;
		}

		function get_user () {
			if ( is_user_logged_in () ) {
				$user = get_current_user_id();
			} else {
				$user = 0;
			}

			return $user;
		}
		

	}
endif;

#4

Hello @JuliahS,

You can also pass user_id as parameter to this function ‘rtmedia_get_featured(user_id)’. User id can be get from the function ‘bp_displayed_user_id()’ which will help you to add condition for the displayed user_id.

Hope this will help you. Let us know if you have any other doubts.

Thanks.


#5

As you can see by my code I’ve already used this. It feels like you didn’t even look at my code at all.

Please let me know how I can run a check on the featured media function so that if it returns false the tab does not display.


#6

Hello @JuliahS,

Are you getting expected result from the query generated by this code? Can you explain exactly why you have used this specific query so that we can understand the code?

Thanks


#7