Created
April 30, 2020 16:22
-
-
Save ronalfy/dde0799018a5126bfbd5b11e2438f0e8 to your computer and use it in GitHub Desktop.
Paid Memberships Pro - Show Content List
This file contains hidden or 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 | |
/** | |
* Show pages/posts a level/user has access to. | |
* | |
* @param array $atts Shortcode attributes. | |
* | |
* @return string HTML for the post list. | |
*/ | |
function member_content_list( $atts ) { | |
if ( ! is_user_logged_in() ) { | |
return ''; | |
} | |
$args = shortcode_atts( | |
array( | |
'post_type' => 'page', | |
'level_id' => 0, | |
), | |
$atts | |
); | |
if ( ! function_exists( 'pmpro_getMembershipLevelForUser' ) ) { | |
return ''; | |
} | |
$post_type = $args['post_type']; | |
$level_id = absint( $args['level_id'] ); | |
global $wpdb; | |
// Get protected posts (limit to first 100). | |
if ( 'post' === $post_type && $level_id > 0 ) { | |
$results = $wpdb->get_results( | |
$wpdb->prepare( | |
"SELECT category_id FROM $wpdb->pmpro_memberships_categories WHERE membership_id = %d", | |
$level_id | |
), | |
OBJECT | |
); | |
if ( ! $results ) { | |
return ''; | |
} else { | |
$category_ids = array(); | |
foreach ( $results as $result ) { | |
$category_ids = $result->category_id; | |
} | |
$post_args = array( | |
'post_type' => 'post', | |
'posts_per_page' => 100, | |
'orderby' => 'title', | |
'order' => 'ASC', | |
'category__in' => $category_ids, | |
'post_status' => 'publish', | |
); | |
$posts = get_posts( $post_args ); | |
if ( ! $posts ) { | |
return ''; | |
} | |
ob_start(); | |
?> | |
<ul> | |
<?php | |
foreach ( $posts as $post ) { | |
echo sprintf( | |
'<li><a href="%s">%s</a></li>', | |
esc_url( get_permalink( $post->ID ) ), | |
esc_html( get_the_title( $post ) ) | |
); | |
} | |
?> | |
</ul> | |
<?php | |
return ob_get_clean(); | |
} | |
} | |
// Get protected pages/post-types. | |
$level = pmpro_getMembershipLevelForUser(); | |
$results = $wpdb->get_results( | |
$wpdb->prepare( | |
"SELECT * FROM $wpdb->pmpro_memberships_pages LEFT JOIN $wpdb->posts ON $wpdb->pmpro_memberships_pages.page_id = $wpdb->posts.ID WHERE post_type = %s AND $wpdb->pmpro_memberships_pages.membership_id = %d", | |
$post_type, | |
$level->id | |
), | |
OBJECT | |
); | |
if ( is_wp_error( $results ) || ! $results ) { | |
return ''; | |
} | |
ob_start(); | |
echo '<ul>'; | |
foreach ( $results as $result ) { | |
?> | |
<li> | |
<a href="<?php echo esc_url( get_permalink( $result->page_id ) ); ?>"> | |
<?php echo esc_html( $result->post_title ); ?> | |
</a> | |
</li> | |
<?php | |
} | |
echo '</ul>'; | |
return ob_get_clean(); | |
} | |
add_shortcode( 'member_content_list', 'member_content_list' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment