Last active
July 19, 2018 22:10
-
-
Save stevenslack/38f4d1f2b675fde557fa to your computer and use it in GitHub Desktop.
Get the term IDs associated with the choosen membership levels assigned to a post or page. Only returns taxonomy terms.
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 | |
/** | |
* Get the page/post IDs or taxonomy term IDs for restricted content | |
* for chosen WooCommerce Memberships levels which have been assigned to a post or page. | |
* | |
* @param string $content_type | accepts either 'taxonomy' or 'post_type' object keys | |
* @param int $post_id The ID of the post or page | |
* @return array post/pageID's or taxonomy term ID's | |
*/ | |
function wc_get_membership_term_ids( $content_type, $post_id ) { | |
// check if woomembership plugin function exists | |
if ( ! function_exists( 'wc_memberships_get_membership_plan' ) ) { | |
return; | |
} | |
// Get the rules set in each page / post | |
$rules = wc_memberships()->rules->get_post_content_restriction_rules( $post_id ); | |
// If no rules are set on the page/post return early | |
if ( empty( $rules ) ) { | |
return; | |
} | |
// set variables as empty arrays | |
$access = $object_ids = array(); | |
// For each rule add the available object id's to the object_ids array | |
foreach ( $rules as $rule ) { | |
// get the membership plan object | |
$membership_plan = wc_memberships_get_membership_plan( $rule->get_membership_plan_id() ); | |
// check whether the current user has access to the membership plan | |
$access = wc_memberships_is_user_active_member( get_current_user_id(), $membership_plan ); | |
// if the user has access assign the ID to the object ID array | |
// or if the user is the admin | |
if ( $access || current_user_can( 'manage_options' ) ) { | |
// if content restriction rules are set get the rules per membership plan | |
$content_restriction_rules = isset( $membership_plan ) ? $membership_plan->get_content_restriction_rules() : array(); | |
foreach ( $content_restriction_rules as $restriction_rule ) { | |
// get the object IDs associated with the restriction rules | |
if ( is_object( $restriction_rule ) && $content_type === $restriction_rule->get_content_type() ) { | |
foreach ( $restriction_rule->get_object_ids() as $id ) { | |
$object_ids[] = $id; | |
} | |
} | |
} | |
} | |
} | |
// returns an array of content type IDs | |
return $object_ids; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment