Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strangerstudios/62808c617e3a4c54ddce to your computer and use it in GitHub Desktop.
Save strangerstudios/62808c617e3a4c54ddce to your computer and use it in GitHub Desktop.
API method to check if a user has access to a certain post.
<?php
/**
* API method to check if a user has access to a certain post.
* Since v2.0
*/
function pmpro_xmlrpc_hasMembershipAccess($args)
{
// Parse the arguments, assuming they're in the correct order
$username = $args[0];
$password = $args[1];
$post_id = $args[2]; //post id to check
$user_id = $args[3]; //optional user id passed in
$return_membership_levels = $args[4]; //option to also include an array of membership levels with access to the post
global $wp_xmlrpc_server;
// Let's run a check to see if credentials are okay
if ( !$user = $wp_xmlrpc_server->login($username, $password) ) {
return $wp_xmlrpc_server->error;
}
// The user passed should be an admin or have the pmpro_xmlprc capability
if(!user_can($user->ID, "manage_options") && !user_can($user->ID, "pmpro_xmlrpc"))
return "ERROR: User does not have access to the PMPro XMLRPC methods.";
// Default to logged in user if no user_id is given.
if(empty($user_id))
{
$user_id = $user->ID;
}
$has_access = pmpro_has_membership_access($post_id, $user_id, $return_membership_levels);
return $has_access;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment