Last active
January 1, 2017 20:15
-
-
Save eighty20results/0ba77d0c2bbc496a9e8de5ad62e5fb2a to your computer and use it in GitHub Desktop.
Create `[pmpro_list_addon_users]` shortcode to list addon package (page/post) users who're registered as having purchased the specified list of addon package IDs.
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 | |
/* | |
Plugin Name: PMPro Add-on Page/Post user list | |
Plugin URI: https://eighty20results.com/paid-memberships-pro/do-it-for-me/ | |
Description: List all user(s) who've bought a specific Add-on Post/Page | |
Version: 1.1 | |
Author: Eighty / 20 Results by Wicked Strong Chicks, LLC <[email protected]> | |
Author URI: https://eighty20results.com/thomas-sjolshagen/ | |
License: | |
* Copyright (C) 2016 Thomas Sjolshagen - Eighty / 20 Results by Wicked Strong Chicks, LLC | |
* | |
* This program is free software; you can redistribute it and/or | |
* modify it under the terms of the GNU General Public License | |
* as published by the Free Software Foundation; either version 2 | |
* of the License, or (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, write to the Free Software | |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
*/ | |
class e20rAddonPageUsers { | |
/** | |
* @var e20rAddonPageUsers | |
*/ | |
static $instance; | |
function __construct() { | |
add_shortcode('pmpro_list_addon_users', array( $this, 'list_addon_users') ); | |
} | |
/** | |
* Shortcode [pmpro_list_addon_users] to list all users who've been assigned/bought an add-on page/post. | |
* | |
* Requirements: `post_id` - the Post ID(s) for the add-on post/page. Can be a list (comma separated) | |
* Optional: `name_option` - Valid options: `display_name` (default), `first_name` or `last_name`, `user_email`, `user_login` | |
* @param array $attrs | |
* | |
* @return null|string | |
*/ | |
public function list_addon_users( $attrs = array() ) { | |
global $wpdb; | |
$post_id = null; | |
$name_option = ''; | |
$attrs = shortcode_atts( | |
array( | |
'post_id' => '', // Can be a comma separated list of Addon Package Page/Post IDs. | |
'name_option' => 'display_name' | |
), | |
$attrs | |
); | |
if ( !empty( $attrs['name_option'])) { | |
$name_option = trim($attrs['name_option']); | |
} | |
$posts = array_map('trim', explode(',', $attrs['post_id'])); | |
$post_list = implode(',', $posts ) ; | |
// Nothing to show/list. | |
if ( empty( $posts ) || empty( $attrs['post_id'] )) { | |
return "No Addon Package ID specified"; | |
} | |
// Find the serialized list of users associated with the specified AP posts/pages | |
$sql = "SELECT post_id, meta_value | |
FROM {$wpdb->postmeta} | |
WHERE meta_key = '_pmproap_users' | |
AND post_id IN ( {$post_list} )"; | |
$serialized = $wpdb->get_results( $sql ); | |
$users = array(); | |
// Process if we found data | |
if (!empty( $serialized ) ) { | |
// Create list of users keyed on post IDs | |
foreach( $serialized as $record ) { | |
$post_id = $record->post_id; | |
$user_list = maybe_unserialize( $record->meta_value ); | |
// Make sure the user list is an array. | |
if ( !is_array( $user_list ) ) { | |
$user_list = array( $user_list ); | |
} | |
$users[$post_id] = $user_list; | |
} | |
} | |
//Prepare to output the list (per page). | |
ob_start(); | |
foreach( $users as $post => $user_list ) { | |
?> | |
<div class="e20r-ap-post-list"> | |
<div class="e20r-ap-post-title"> | |
<h2><?php echo get_the_title( $post ); ?></h2> | |
</div> | |
<div class="e20r-ap-post-userlist"> | |
<ul class="e20r-ap-post-users"> | |
<?php | |
foreach( $user_list as $user_id ) { | |
$user = get_userdata( $user_id ); | |
?><li class="e20r-ap-post-user"><?php echo ( empty($name_option) ? 'Unknown' : esc_attr( $user->{$name_option} ) ); ?></li> | |
<?php | |
} | |
?> | |
</ul> | |
</div> | |
</div> | |
<?php | |
} | |
$html = ob_get_clean(); | |
return $html; | |
} | |
static public function getInstance() { | |
if (is_null( self::$instance ) ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
} | |
add_action('plugins_loaded', 'e20rAddonPageUsers::getInstance' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment