Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eighty20results/561d98088fed03231e37 to your computer and use it in GitHub Desktop.
Save eighty20results/561d98088fed03231e37 to your computer and use it in GitHub Desktop.
Protect all child pages of a page protected by the "Protect Add-on Pages" add-on
<?php
/*
Plugin Name: PMPro Protect Child Pages
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/
Description: Apply protection to all child pages of a Add-on Page that's protected.
Version: .1
Author: Thomas Sjolshagen @ Stranger Studios <thomas@eighty20results.com>
Author URI: https://eighty20results.com/thomas-sjolshagen/
*/
/**
* Apply protection of parent page to child pages.
*
* @param $hasaccess
* @param $thepost
* @param $theuser
* @param $post_membership_levels
* @return bool
*
* @since v.1
*/
function hide_child_posts( $hasaccess, $thepost, $theuser, $post_membership_levels)
{
global $wpdb;
global $pmpro_inaccessible_parent;
global $pmpro_pages;
if (in_array($thepost->ID, $pmpro_pages)) {
return $hasaccess;
}
$pmpro_inaccessible_parent = null;
//if PMPro says false already, return false
if (!$hasaccess)
return false;
//if PMPro Addon Packages is not installed, return
if (!function_exists('pmproap_hasAccess'))
return $hasaccess;
// this post/page doesn't have a parent so nothing to check.
if (empty( $thepost->post_parent ) ) {
return $hasaccess;
}
if ( ! pmproap_hasAccess($theuser->ID, $thepost->post_parent) ) {
$pmpro_inaccessible_parent = $thepost->post_parent;
$hasaccess = false;
}
return $hasaccess;
}
add_filter("pmpro_has_membership_access_filter", "hide_child_posts", 10, 4);
/**
* Show a link to checkout for the addon package for child-posts of a Add-on protected parent page
* @param $text
* @return string
*/
function pmproap_pmpro_text_filter_for_child_posts($text)
{
global $wpdb, $current_user, $post, $pmpro_currency_symbol;
global $pmpro_inaccessible_parent;
global $pmpro_pages;
if (empty($post->post_parent) || ( !empty($post->post_parent) && in_array( $post->post_parent, $pmpro_pages) )) {
error_log("No parent post configured, so exiting");
return $text;
}
if ( ! pmproap_hasAccess($current_user->ID, $post->post_parent) ) {
//which level to use for checkout link?
$text_level_id = pmproap_getLevelIDForCheckoutLink($post->post_parent, $current_user->ID);
//what's the price
$pmproap_price = get_post_meta($post->post_parent, "_pmproap_price", true);
//check for all access levels
$all_access_levels = apply_filters("pmproap_all_access_levels", array(), $current_user->ID, $post->post_parent);
//update text
if (!empty($all_access_levels)) {
$level_names = array();
foreach ($all_access_levels as $level_id) {
$level = pmpro_getLevel($level_id);
$level_names[] = $level->name;
}
error_log("Set additional access text for all access levels");
$text = "<p>This content requires that you purchase additional access. The price is " . $pmpro_currency_symbol . $pmproap_price . " or free for our " . pmpro_implodeToEnglish($level_names) . " members.</p>";
$text .= "<p><a href=\"" . pmpro_url("checkout", "?level=" . $text_level_id . "&ap=" . $post->post_parent) . "\">Purchase this Content (" . pmpro_formatPrice($pmproap_price) . ")</a> <a href=\"" . pmpro_url("levels") . "\">Choose a Membership Level</a></p>";
} elseif( !empty($text_level_id)) {
error_log("Set additional access text for all level: {$text_level_id}");
$text = "<p>This content requires that you purchase additional access. The price is " . pmpro_formatPrice($pmproap_price) . ".</p>";
$text .= "<p><a href=\"" . pmpro_url("checkout", "?level=" . $text_level_id . "&ap=" . $post->post_parent) . "\">Click here to checkout</a></p>";
}
}
return $text;
}
add_filter("pmpro_non_member_text_filter", "pmproap_pmpro_text_filter_for_child_posts", 15);
add_filter("pmpro_not_logged_in_text_filter", "pmproap_pmpro_text_filter_for_child_posts", 15);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment