Last active
September 11, 2020 07:39
-
-
Save raviousprime/9eb24ad9ca30a3639a453f2a3e8593b9 to your computer and use it in GitHub Desktop.
MediaPress auto gallery creation and restrict to one gallery per user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Is user first login | |
* | |
* @param int $user_id User id. | |
* | |
* @return bool | |
*/ | |
function buddydev_is_user_first_login( $user_id ) { | |
// check for user's last activity. | |
$last_activity = bp_get_user_last_activity( $user_id ); | |
if ( empty( $last_activity ) ) { | |
// it is the first login | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Check if user gallery limit has reached or not. | |
* | |
* @param int $user_id User id. | |
* | |
* @return bool | |
*/ | |
function buddydev_has_user_gallery_limit_reached( $user_id ) { | |
if ( mpp_get_gallery_count( array( 'user_id' => $user_id ) ) >= 1 ) { | |
return true; | |
} | |
return false; | |
} | |
add_action( 'bp_template_redirect', function () { | |
if ( ! is_user_logged_in() ) { | |
return; | |
} | |
if ( ! function_exists( 'buddypress' ) || ! function_exists( 'mediapress' ) ) { | |
return; | |
} | |
$user = wp_get_current_user(); | |
if ( ! buddydev_is_user_first_login( $user->ID ) || buddydev_has_user_gallery_limit_reached( $user->ID ) ) { | |
return; | |
} | |
$args = array( | |
'title' => bp_core_get_username( $user->ID ), | |
'component' => 'members', | |
'component_id' => $user->ID, | |
'creator_id' => $user->ID, | |
'status' => 'public', | |
'type' => 'photo', | |
); | |
mpp_create_gallery( $args ); | |
}, 99 ); | |
add_filter( 'mpp_user_can_create_gallery', function ( $can_do ) { | |
if ( ! function_exists( 'buddypress' ) ) { | |
return $can_do; | |
} | |
if ( $can_do && buddydev_has_user_gallery_limit_reached( bp_loggedin_user_id() ) ) { | |
$can_do = false; | |
} | |
return $can_do; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment