Skip to content

Instantly share code, notes, and snippets.

@mahfelwp
Last active March 30, 2020 14:56
Show Gist options
  • Save mahfelwp/62f0851249a2d28e7ea4a4011aba64c6 to your computer and use it in GitHub Desktop.
Save mahfelwp/62f0851249a2d28e7ea4a4011aba64c6 to your computer and use it in GitHub Desktop.
display random vip users (by subscription level on Restrict Content Pro)
<?php
function random_user_query( &$query )
{
$query->query_orderby = "ORDER BY RAND()";
}
function display_random_vip_users( $atts ) {
$atts = shortcode_atts(
array( 'status' => 'active',
'subscription' => null,
'offset' => 0,
'number' => 5,
'order' => 'DESC',
'title' => 'کاربران ویژه',
'title_color' => '#514a9d',
'title_fw' => 'bold',
'a_color' => '#399b0e',
), $atts,'random_vip_users' );
// Create a function to override the ORDER BY clause
add_action( 'pre_user_query', 'random_user_query' );
$args = array(
'offset' => $atts['offset'],
'number' => 9999,
'count_total' => false,
'fields' => 'ID',
'orderby' => 'rand',
'order' => $atts['order'],
'meta_query' => array(
array(
'key' => 'rcp_status',
'value' => $atts['status']
)
)
);
if( ! empty( $atts['subscription'] ) ) {
$args['meta_query'][] = array(
'key' => 'rcp_subscription_level',
'value' => (int) $atts['subscription']
);
}
$members = get_users( $args );
$is_revers_array = (boolean) rand(0,1);
if ($is_revers_array) {
$members = array_reverse($members);
}
?>
<style type="text/css">
.rand-user-box {
margin: 20px 0 20px 0;
padding: 0;
display: flex;
flex-wrap: wrap;
justify-content: flex-start;
list-style: none;
align-content: center;
align-items: center;
background: #fff !important;
border: 1px solid #eaeaea !important;
border-radius: 3px !important;
}
.rand-user-box a {
padding: 0.75rem 0.5rem;
font-size: 1.6rem;
font-weight: normal;
text-decoration: none;
display: flex;
align-items: center;
text-decoration: none !important;
color: #399b0e !important;
}
.rand-user-box li {
list-style: none !important;
border-left: 1px solid #d3d8d6 !important;
font-family: sahel !important;
flex-basis: 30%;
}
.rand-user-box li:last-child {
border-left: none !important;
}
#rand-user-title {
color: #595959 !important;
font-weight: bold !important;
padding-right: 20px;
border-top-right-radius: 3px;
padding-top: 21px;
padding-bottom: 21px;
}
.rand-user-box a span {
margin-right: 10px !important;
font-size: 15px !important;
}
@media all and (min-width: 220px) {
.rand-user-box {
flex-wrap: wrap;
}
.rand-user-box li {
flex-basis: 50%;
}
#rand-user-title{
flex-basis: 100%;
}
.rand-user-box li:last-child {
border-left: 1px solid #d3d8d6 !important;
}
}
@media all and (min-width: 780px) {
.rand-user-box {
flex-wrap: wrap;
}
.rand-user-box li {
flex-basis: 50%;
}
#rand-user-title{
flex-basis: 100%;
}
.rand-user-box li:last-child {
border-left: 1px solid #d3d8d6 !important;
}
}
@media all and (min-width: 920px) {
.rand-user-box {
flex-wrap: wrap;
}
.rand-user-box li {
flex-basis: 20%;
}
#rand-user-title {
flex-basis: 20%;
}
}
@media all and (min-width: 1200px) {
.rand-user-box a {
min-width: 220px;
font-size: 2rem;
}
.rand-user-box {
flex-wrap: wrap;
}
.rand-user-box li {
flex-basis: 15%;
}
#rand-user-title {
flex-basis: 15%;
}
}
</style>
<ul class="rand-user-box">
<li id="rand-user-title"><span><?php echo $atts['title']; ?></span></li>
<?php
$rand_max = count( $members ) - $atts['number'];
if ($rand_max < 0) {
$rand_max = 0;
}
foreach ( array_slice( $members, rand( 0, $rand_max ), $atts['number'] ) as $user_id ) {
$last_name = yz_data('last_name', $user_id );
$first_name = yz_data('first_name', $user_id );
$username = $first_name.' '.$last_name;
printf(
'<li class="rand-user-item"><a target="_blank" href="%1$s"> %2$s <span>%3$s</span></a></li>',
bp_core_get_user_domain( $user_id ),
bp_core_fetch_avatar( array( 'item_id' => $user_id ) ),
$username
);
}
?>
</ul>
<?php
// Remove the hook
remove_action( 'pre_user_query', 'random_user_query' );
}
add_shortcode('random_vip_users','display_random_vip_users');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment