Last active
June 4, 2018 13:07
-
-
Save robinbastien/dc1da2b105f96ca33588 to your computer and use it in GitHub Desktop.
Page Memberships Pro Components
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 | |
/** | |
* machine-pmpro.php | |
* A dedicated function file for Paid Memberships Pro functionality | |
* | |
* @package WordPress | |
* @subpackage Machine | |
*/ | |
$options = (object) array( | |
'profile_level_selector' => false, // Let users select their own level in their profile | |
'username_as_email' => true, // Set user username as their email addy | |
'pmpro_metaboxes_CPT' => true // Sets the PMPRO metaboxes for custom post types. CPT's must be defined in the function. | |
); | |
/* ========================================================================== | |
Pmpro Component Include function | |
========================================================================== */ | |
function pmpro_component($file) { | |
if($file) { | |
include(locate_template('paid-memberships-pro/components/component-' . $file . '.php')); | |
} | |
} | |
function set_html_content_type() { | |
return 'text/html'; | |
} | |
/* ========================================================================== | |
Show account status' when the user has made an action of sorts | |
========================================================================== */ | |
function status_messages() { | |
if($_GET['updated']) { | |
$status = 'updated'; | |
$message = __('Your account has been updated','sbmedia'); | |
$icon = 'check'; | |
} | |
elseif($_GET['checkemail'] == 'confirm') { | |
$status = 'alert'; | |
$message = __('A password reset link was sent to your email address','sbmedia'); | |
$icon = 'check'; | |
} | |
elseif($_GET['resetpass'] == 'complete') { | |
$status = 'updated'; | |
$message = __('Your password has been updated. You may login below.','sbmedia'); | |
$icon = 'check'; | |
} | |
elseif($_REQUEST['error'] == 'invalidkey') { | |
$status = 'alert'; | |
$message = __('Your password should be a minimum of 6 characters with numbers, letters, and symbols. ','sbmedia'); | |
$icon = 'times'; | |
} | |
if($status) { | |
$output = '<div class="status-message status-message--' . $status . '">'; | |
$output .= '<span class="status-message__icon icon icon-' . $icon . '"></span>'; | |
$output .= $message; | |
$output .= '</div>'; | |
echo $output; | |
} | |
} | |
/* ========================================================================== | |
Remove the membership dropdown from the edit profile page | |
========================================================================== */ | |
if( !$options->profile_level_selector ) { | |
remove_action( 'show_user_profile', 'pmpro_membership_level_profile_fields' ); | |
remove_action( 'edit_user_profile', 'pmpro_membership_level_profile_fields' ); | |
remove_action( 'profile_update', 'pmpro_membership_level_profile_fields_update' ); | |
} | |
/* ========================================================================== | |
Paid Memberships Pro — Get required membership levels of a post | |
- Returns an array of level ID's | |
========================================================================== */ | |
function get_required_levels($post_ID) { | |
global $wpdb; | |
$page_levels = $wpdb->get_col("SELECT membership_id FROM {$wpdb->pmpro_memberships_pages} WHERE page_id = '{$post_ID}'"); | |
return $page_levels; | |
} | |
/* ========================================================================== | |
Paid Memberships Pro — Set Email As Username | |
========================================================================== */ | |
if($options->username_as_email) { | |
function my_init_email_as_username() | |
{ | |
//check for level as well to make sure we're on checkout page | |
if(empty($_REQUEST['level'])) | |
return; | |
if(!empty($_REQUEST['bemail'])) | |
$_REQUEST['username'] = $_REQUEST['bemail']; | |
if(!empty($_POST['bemail'])) | |
$_POST['username'] = $_POST['bemail']; | |
if(!empty($_GET['bemail'])) | |
$_GET['username'] = $_GET['bemail']; | |
} | |
add_action('init', 'my_init_email_as_username'); | |
} | |
/* ========================================================================== | |
Paid Memberships Pro — Querying users | |
========================================================================== */ | |
/** | |
* Ever want to query users by their membership levels, but couldn't because of how PMPRO stores | |
* data in its own table? Yeah, I have too. Here's a snippet giving you the basic idea of joining | |
* them. | |
**/ | |
// Gets a count of all PMPRO users that are active | |
function memberCount( ) { | |
global $wpdb; | |
$query = $wpdb->get_results("SELECT COUNT(*) count | |
FROM wp_users u JOIN wp_pmpro_memberships_users mu | |
ON u.ID = mu.user_id | |
WHERE mu.membership_id = $membership_id | |
AND (SELECT meta_value FROM wp_usermeta WHERE user_id = u.ID AND meta_key = 'wp_capabilities') LIKE '%subscriber%' | |
AND mu.status = 'active' | |
"); | |
if( isset($query) ) return $query; | |
} | |
// Get a list of users based on their membership level | |
function get_pmpro_members( $levels = '1,2,3' ) { | |
global $wpdb; | |
$query = $wpdb->get_results("SELECT u.ID, | |
(SELECT meta_value FROM $wpdb->usermeta WHERE user_id = u.ID AND meta_key = 'first_name') first, | |
(SELECT meta_value FROM $wpdb->usermeta WHERE user_id = u.ID AND meta_key = 'last_name') last, | |
u.user_email, | |
mu.membership_id | |
FROM $wpdb->users u | |
JOIN $wpdb->pmpro_memberships_users mu ON u.ID = mu.user_id | |
WHERE mu.status = 'active' | |
AND mu.membership_id IN($levels)"); | |
if( isset($query) ) return $query; | |
} | |
/* ========================================================================== | |
Paid Memberships Pro — add metabox to a custom post type | |
========================================================================== */ | |
// if($options->pmpro_metaboxes_CPT) { | |
// function pmpro_metaboxes() { | |
// //duplicate this row for each CPT | |
// add_meta_box('pmpro_page_meta', 'Require Membership', 'pmpro_page_meta', 'secure_document', 'side'); | |
// } | |
// function pmpro_cpt_init(){ | |
// if (is_admin()) { | |
// add_action('admin_menu', 'pmpro_metaboxes'); | |
// } | |
// } | |
// add_action("init", "pmpro_cpt_init", 20); | |
// } | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment