BuddyDev

Search

Replies

  • Keymaster
    (BuddyDev Team)
    Posts: 25130

    Hi,
    Thank you. The code below is modified code from last step.

    Here, we have broken the auto creation into 2 functions to make it even simpler.

    
    /**
     * Creates a gallery
     *  
     * @param array $args
     *	@type int $creator_id owner of the gallery
     *	@type string $title gallery title
     *	@type string $description gallery description
     *  @type string $status possible values( public, private, loggedin etc)
     *	@type string $component possible values( 'members', 'groups' etc)
     *  @type int $component_id owner id( user_id or group id depending on specified $component)
     *	@type string $type media type( possible values photo, audio, video, doc)
     *  
     * @return int|boolean|WP_Error
     */
    function mpp_custom_create_gallery( $args ) {
    	
    	$user_id = get_current_user_id();
    	
    	$default = array(
    			'creator_id'	 => $user_id,
    			'title'			 => '',
    			'description'	 => '', //add description if any
    			'status'		 => mpp_get_default_status(),//can use any of the status e.g. 'public' , 'private', 'loggedin', 'friends' etc
    			'component'		 => 'members',//for user
    			'component_id'	 => $user_id,//no need to change
    			'type'			 => 'photo', //can change to "audio", "video", "doc"
    	);
    	
    	
    	$args = wp_parse_args( $args, $default );
    	
    	$gallery_id = mpp_create_gallery( $args );
    	
    	return $gallery_id;
    
    }
    
    function mpp_custom_auto_create_gallery() {
    	
    	if ( ! bp_is_my_profile() ) {
    		return ;
    	}
    	
    	//make sure that user gallery functions are available
    	if ( ! function_exists( 'mpp_create_gallery' ) ) {
    		return;
    	}
    	
    	$user_id = get_current_user_id();
    	
    	//does this user has predefined gallery?
    	//we use this check to avoid same type of gallery again and again for the user
    	if ( ! get_user_meta(  $user_id, '_mpp_predefined_gallery_photo_1', true ) ) {
    		
    		$gallery_id = mpp_custom_create_gallery( array(
    			'title'	=> 'Smple Gallery 1',
    			'type'	=> 'photo', //can be 'audio', 'video', 'doc'
    			'status'=> mpp_get_default_status()
    			
    		) );
    		
    	//let us keep the id in usermeta to avoid creating it again, right?
    		if ( $gallery_id && !  is_wp_error( $gallery_id ) ) {
    			update_user_meta( $user_id, '_mpp_predefined_gallery_photo_1', $gallery_id ); //you can change the key to anything
    
    		}
    	
    		
    		
    	}//end of repeatable block
    	
    	//2nd gallery
    	if ( ! get_user_meta(  $user_id, '_mpp_predefined_gallery_photo_2', true ) ) {
    
    		$gallery_id = mpp_custom_create_gallery( array(
    			'title'	=> 'Smple Gallery 2',
    			'type'	=> 'photo', //can be 'audio', 'video', 'doc'
    			'status'=> mpp_get_default_status()
    
    		) );
    
    	//let us keep the id in usermeta to avoid creating it again, right?
    		if ( $gallery_id && !  is_wp_error( $gallery_id ) ) {
    			update_user_meta( $user_id, '_mpp_predefined_gallery_photo_2', $gallery_id ); //you can change the key to anything
    
    		}
    
    		
    		
    	}
    		
    		//3rd gallery
    	if ( ! get_user_meta(  $user_id, '_mpp_predefined_gallery_photo_3', true ) ) {
    
    		$gallery_id = mpp_custom_create_gallery( array(
    			'title'	=> 'Smple Gallery 3',
    			'type'	=> 'photo', //can be 'audio', 'video', 'doc'
    			'status'=> mpp_get_default_status()
    
    		) );
    
    	//let us keep the id in usermeta to avoid creating it again, right?
    		if ( $gallery_id && !  is_wp_error( $gallery_id ) ) {
    			update_user_meta( $user_id, '_mpp_predefined_gallery_photo_3', $gallery_id ); //you can change the key to anything
    
    		}
    
    	}
    	
    	//and so on
    
    }
    
    add_action( 'mpp_actions', 'mpp_custom_auto_create_gallery' );
    
    

    So, please use this code for the last part instead of the previous. I hope, It gives you better idea.

  • Keymaster
    (BuddyDev Team)
    Posts: 25130

    Hi,
    No problem. I can do that in a minute. Can you please give me the Name for galleries and type for these galleries.

    Thank you
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 25130

    Thank you. I am happy it is working. Please do let me know how it went.

  • Keymaster
    (BuddyDev Team)
    Posts: 25130
    Brajesh Singh on in reply to: BuddyPress Confirm Actions + Stripe #2468

    Hi Kenny,
    You can. what is the change in code that you have made? I don’t need the stripe code, just a skeleton idea.

    Thank you
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 25130

    Hi,
    My mistake, I forgot the code block to attach the creation function

    Please add this line too

    
    
    add_action( 'mpp_actions', 'mpp_custom_auto_create_gallery' );
    
    
  • Keymaster
    (BuddyDev Team)
    Posts: 25130

    Thank you. I am looking forward to hear your experience. Please do let me know if you need further assistance.

  • Keymaster
    (BuddyDev Team)
    Posts: 25130

    Thank you.

    As you might see, I am using usermeta to avoid creating the same gallery again and again for the user.
    Since you are going to create multiple galleries(and in future you may want to even add some more), so my idea was to use 1 user meta per gallery to avoid duplicate but keep the options open for user.

    Using a different usermeta key for each gallery will allow you to achieve that goal. Hope that helps.

  • Keymaster
    (BuddyDev Team)
    Posts: 25130

    Thank you. Here we go with the code. Please put these code blocks in your bp-custom.php

    1. Let us stop user from creating gallery

    
    
    // Stop gallery creation, only superadmin can
    function mpp_custom_check_gallery_create_permission( $can_do, $component,  $user_id ) {
    	
    	if ( is_super_admin() ) {
    		$can_do = true; //ok super admin can create it
    	} else {
    		$can_do = false;//no one else can
    	}
    	
    	return $can_do;
    }
    
    add_filter( 'mpp_user_can_create_gallery', 'mpp_custom_check_gallery_create_permission', 10, 3 );
    
    

    That will stop users from creating gallery. Site admins can still create it.

    2. Let us stop users from editing gallery

    
    // Restrict gallery editing
    function mpp_custom_check_gallery_edit_permission( $can, $gallery, $user_id ){
    	// only superadmin can
    	if ( is_super_admin() ) {
    		$can =  true;
    	} else {
    		$can = false;
    	}
    	
    	return $can;//do not allow it at all
    }
    
    add_filter( 'mpp_user_can_edit_gallery', 'mpp_custom_check_gallery_edit_permission', 10, 3 );
    
    

    That will stop users from editing gallery but allow siteadmins to do so.

    Step 3: Let us stop users from deleting gallery

    
    
    // Restrict gallery deletion
    function mpp_custom_check_gallery_delete_permission( $can, $user_id ){
    	// only superadmin can
    	if ( is_super_admin() ) {
    		$can =  true;
    	} else {
    		$can = false;
    	}
    	
    	return $can;//do not allow it at all
    }
    
    add_filter( 'mpp_user_can_delete_gallery', 'mpp_custom_check_gallery_delete_permission', 10, 2 );
    

    and the final step, let us create the predefined galleries automatically for the user.
    In this example, I have created only one gallery, you can modify the repeatable block(make sure to change the user meta key for each) and create as many predefined galleries as you want. When a user visits his/her own profile, the galleries will be created.

    
    function mpp_custom_auto_create_gallery() {
    	
    	if ( ! bp_is_my_profile() ) {
    		return ;
    	}
    	
    	//make sure that user gallery functions are available
    	if ( ! function_exists( 'mpp_create_gallery' ) ) {
    		return;
    	}
    	
    	
    	$user_id = get_current_user_id();
    		
    	//does this user has predefined gallery?
    	//we use this check to avoid same type of gallery again and again for the user
    	if ( ! get_user_meta(  $user_id, '_mpp_predefined_gallery_photo_1', true ) ) {
    		//No, then we need to create it.
    		
    		//repeat it as you want
    		$gallery_id = mpp_create_gallery( array(
    			'creator_id'	 => $user_id,
    			'title'			 => __( 'Predefined Photo Gallery' ),
    			'description'	 => '', //add description if any
    			'status'		 => mpp_get_default_status(),//can use any of the status e.g. 'public' , 'private', 'loggedin', 'friends' etc
    			'component'		 => 'members',//for user
    			'component_id'	 => $user_id,//no need to change
    			'type'			 => 'photo', //can change to "audio", "video", "doc"
    		) );
    	
    		//let us keep the id in usermeta to avoid creating it again, right?
    		if ( !  is_wp_error( $gallery_id ) ) {
    			update_user_meta( $user_id, '_mpp_predefined_gallery_photo_1', $gallery_id ); //you can change the key to anything
    
    		}
    		
    	}//end of repeatable block
    	
    	//go ahead, do it for other types too
    	
    	
    	
    }
    add_action( 'bp_template_redirect', 'mpp_custom_auto_create_gallery');
    
    

    Please do let me know how it goes for you.

    Thank you
    Brajesh

    • This reply was modified 5 years, 1 month ago by Brajesh Singh. Reason: updated to fix the gallery creation. Was missing the template redirect hook attachment
  • Keymaster
    (BuddyDev Team)
    Posts: 25130

    Thank you. That makes it clear. I will have some code in 30 mins to an hour.

    Thank you
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 25130

    Hi,
    Thank you for asking. I have a few questions before I can put the sample code for you.

    1. Do you want users to be able to upload from activity or not?

    2. Do you want to allow users to delete the predefined galleries or not?

    3. Can you please explain what do you mean by

    “Additionally, I need to split the images and videos into separate entities. Currently it’s all in one.”

    MediaPress does not allow uploading video/photo to one gallery. It is the default behaviour to have separate galleries for each of the type.

    Thank you
    Brajesh