Last active
September 26, 2021 06:14
-
-
Save nunks/c8d9ff9dd0eddc3d702f566ec28ab816 to your computer and use it in GitHub Desktop.
Snippet to rewrite the "no access" message
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 | |
/* | |
* Rebuilds the "no access" message before Paid Memberships Pro applies any substitution. | |
* Parses current post for [membership] shortcodes as needed. | |
* Here I'm just getting rid of the "Oxford comma", but it can be used for | |
* rebuilding the message any way you like. | |
*/ | |
//the filters are called by PMPro just before any "!!" variable substitution: | |
// https://github.com/strangerstudios/paid-memberships-pro/blob/2190e731fbb7ccebfa07f246f926859de014e79a/includes/functions.php#L1928 | |
add_filter('pmpro_non_member_text_filter', 'pp_pmpro_custom_content_message', 10, 2); | |
add_filter('pmpro_not_logged_in_text_filter', 'pp_pmpro_custom_content_message', 10, 2); | |
//if you also need to filter rss content... | |
//add_filter('pmpro_rss_text_filter', 'pp_pmpro_custom_content_message', 10, 2); | |
function pp_pmpro_custom_content_message($text) { | |
//get current post | |
$post = get_post(); | |
$levels = array(); | |
//if post has a [membership] shortcode, parse it to find out which levels are needed to acces content | |
if(has_shortcode($post->post_content, 'membership')) { | |
preg_match_all( | |
'/' . get_shortcode_regex(array('membership')) . '/', | |
$post->post_content, | |
$matches, | |
PREG_SET_ORDER | |
); | |
foreach($matches as $match) { | |
$level_ids = array(); | |
//check if shortcode actually shows the "noaccess" message. | |
if(strpos($match[0], 'show_noaccess="true"') >=0) { | |
preg_match('/level="([0-9,-]+)"/', $match[3], $level_ids); | |
$level_ids = explode(',', $level_ids[1]); | |
//get all the level names needed | |
$levels = array_map(function($id) { | |
if(strpos($id, '-') === false) { | |
return pmpro_getLevel($id)->name; | |
} | |
} , $level_ids); | |
//don't check any further (I'm assuming there aren't two shortcodes with "noacces" | |
//for different levels in the same post). | |
break; | |
} | |
} | |
} | |
//if the post doesn't contain a "noaccess" shortcode, get levels set on the post itself | |
if(empty($levels)) { | |
$levels = pmpro_has_membership_access($post->ID, NULL, true); | |
$levels = $levels[2]; | |
} | |
//break the last level to apply a conjunction before it | |
$last = array_pop($levels); | |
//build the string | |
if(count($levels) > 0) { | |
// here's the conjunction ↓ | |
$levels_string = (implode(', ', $levels) . ' and ' . $last); | |
} else { | |
//if there's only one level... | |
$levels_string = $last; | |
} | |
//replace the !! variables for PMPro. If you leave any variables here, PMPro will replace them normally | |
$text = str_replace('!!levels!!', $levels_string, $text); | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment