Instantly share code, notes, and snippets.

Embed
What would you like to do?
Protect PowerPress Podcast Feeds, but let members access them with their custom RSS key. Requires both BluBrry PowerPress plugin, and the PMPro Member RSS add-on.
<?php
/*
Plugin Name: E20R - PowerPress Podcast Feed Protection for Paid Memberships Pro
Plugin URI: https://eighty20results.com/paid-memberships-pro/do-it-for-me/
Description: Verify PMPro RSS Member Key when accessing PowerPress feed
Version: 1.2
Author: Eighty / 20 Results by Wicked Strong Chicks, LLC <thomas@eighty20results.com>
Author URI: https://eighty20results.com/thomas-sjolshagen/
License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
License: GPLv2 or later
* Copyright (C) 2017-2018 Thomas Sjolshagen - Eighty / 20 Results by Wicked Strong Chicks, LLC
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class pmproPowerPress {
/**
* @var null|pmproPowerPress
*/
private static $instance = null;
/**
* @var array $error_msg Error Messages
*/
private $error_msg = array();
/**
* @var array $error_class - CSS class (notice- prefixed) for the error message type
*/
private $error_class = array();
/**
* pmproPowerPress constructor.
*/
private function __construct() {
}
/**
* Load action and filter hooks for the plugin
*/
public function loadHooks() {
add_action( 'admin_init', array( $this, 'checkDependencies' ), 10 );
add_action( 'admin_notices', array( $this, 'loadAdminNotices' ), 10 );
add_action( 'init', array( $this, 'addFeedHandlers' ), - 999 );
add_filter( 'pmpromrss_feeds', array( $this, 'addFeedsToRSSLinks' ), 10, 1 );
add_filter( 'rss_enclosure', array( $this, 'rssEnclosure' ), 20, 1 );
add_filter( 'pmpro_rss_text_filter', array( $this, 'setAccessDeniedMessage' ), 5, 1 );
add_filter( 'plugin_row_meta', array( $this, 'pluginRowMeta' ), 10, 2 );
}
/**
* Fetch list of Podcast Feeds from PowerPress's settings
*
* @return array
*
* @access private
*/
private function getPowerPressFeeds() {
$settings = get_option( 'powerpress_general' );
$feeds = array_merge( $settings['custom_feeds'],
array_merge(
$settings['custom_feeds'],
array(
'podcast' => apply_filters(
'pmpro_powerpress_default_feed_label',
__( 'Default PowerPress Podcast Feed', 'pmpro-powerpress-feed-protection' )
),
)
)
);
return $feeds;
}
/**
* Include PowerPress feeds in listing on PMPro Account page
*
* @param array $feeds
*
* @return array
*/
public function addFeedsToRSSLinks( $feeds ) {
$default_url = get_bloginfo_rss( 'url' );
foreach ( $this->getPowerPressFeeds() as $slug => $name ) {
$feeds[ $name ] = "{$default_url}/{$slug}/";
}
return $feeds;
}
/**
* Using the "Message for RSS Feed" message as a template, create the appropriate (expanded) error message when
* a user should NOT have access to a feed.
*
* @param string $text
*
* @return string
*/
public function setAccessDeniedMessage( $text ) {
global $powerpress_feed;
global $current_user;
global $pmpromrss_user_id;
global $post;
$post_id = null;
if ( ! empty( $post->ID ) ) {
$post_id = $post->ID;
}
if ( is_user_logged_in() ) {
$user_id = $current_user->ID;
} else {
$user_id = $pmpromrss_user_id;
}
// Process PowerPress feeds
$feeds = $this->getPowerPressFeeds();
// Check access to the podcast post/page
$access = pmpro_has_membership_access( $post_id, $user_id, true );
// Grab the default text for the RSS Text PMPro Advanced Settings page
$denied_text = stripslashes( pmpro_getOption( "rsstext" ) );
$pmpro_content_message_pre = '<div class="pmpro_content_message">';
$pmpro_content_message_post = '</div>';
$sr_search = array( "!!login_url!!", "!!levels!!", "!!referrer!!", "!!podcast_feed!!" );
$sr_replace = array(
wp_login_url(),
!empty($access[2]) ? pmpro_implodeToEnglish( $access[2] ) : '',
urlencode( site_url( $_SERVER['REQUEST_URI'] ) ),
$feeds[ $powerpress_feed['feed-slug'] ],
);
$access_denied_message = $pmpro_content_message_pre . str_replace( $sr_search, $sr_replace, $denied_text ) . $pmpro_content_message_post;
if ( empty( $user_id ) || false == $access[0] ) {
$text = $access_denied_message;
}
return $text;
}
/**
* Process the PowerPress podcast feed for access
*
* @param mixed $var1
* @param string $feed_slug
*/
public function processFeeds( $var1, $feed_slug ) {
global $current_user;
global $pmpromrss_user_id;
global $post;
$post_id = null;
if ( isset( $post->ID ) ) {
$post_id = $post->ID;
}
if ( is_user_logged_in() ) {
$user_id = $current_user->ID;
} else {
$user_id = $pmpromrss_user_id;
}
$access = pmpro_has_membership_access( $post_id, $user_id, true );
if ( ! is_array( $access ) && true == $access ) {
return;
}
if ( empty( $user_id ) || ( is_array( $access ) && false == $access[0] ) ) {
// Remove the default PMPro Membership RSS access denied message handler
remove_filter( 'pmpro_rss_text_filter', 'pmprorss_pmpro_rss_text_filter' );
// Apply any/all other filters
$denied_text = apply_filters( "pmpro_rss_text_filter", stripslashes( pmpro_getOption( "rsstext" ) ) );
wp_die( $denied_text );
}
}
/**
* Add handlers for each of the PowerPress feeds configured
*/
public function addFeedHandlers() {
// Only process if PowerPress is active
if ( ! function_exists( 'powerpress_is_podcast_feed' ) ) {
return;
}
foreach ( $this->getPowerPressFeeds() as $feed_slug => $feed_name ) {
add_action( "do_feed_{$feed_slug}", array( $this, 'processFeeds' ), 10, 2 );
}
}
/**
* Remove the enclosure setting if not able to access post/page/feed
* (Should never get there)
*
* @param string $enclosure
*
* @return string
*/
public function rssEnclosure( $enclosure ) {
global $current_user;
global $pmpromrss_user_id;
global $post;
$post_id = null;
if ( isset( $post->ID ) ) {
$post_id = $post->ID;
}
if ( is_user_logged_in() ) {
$user_id = $current_user->ID;
} else {
$user_id = $pmpromrss_user_id;
}
$has_access = pmpro_has_membership_access( $post_id, $user_id );
if ( false == $has_access ) {
$enclosure = "";
}
return $enclosure;
}
/**
* Load any admin notices/warnings/errors
*/
public function loadAdminNotices() {
if ( ! empty( $this->error_msg ) && ! empty( $this->error_class ) ) {
foreach ( $this->error_msg as $key => $msg ) {
?>
<div class="notice notice-<?php esc_attr_e( $this->error_class[ $key ] ); ?> is-dismissible">
<p><?php echo $msg; ?></p>
</div>
<?php
}
}
$this->error_class = array();
$this->error_msg = array();
}
/**
* Check dependencies for this site
*/
public function checkDependencies() {
$has_PowerPress = function_exists( 'powerpress_premium_content_authorized_filter' );
$has_PMProRSS = function_exists( 'pmpromrss_pmpro_has_membership_access_filter' );
$has_PMPro = function_exists( 'pmpro_has_membership_access' );
if ( is_admin() ) {
if ( false === $has_PowerPress ) {
$this->error_msg[] = sprintf(
__( "Error: The Blubrry '<a href=\"%s\" target=\"_blank\">PowerPress</a>' plug-in must be installed and active!", "pmpro-powerpress-feed-protection" ),
'https://wordpress.org/plugins/powerpress/'
);
$this->error_class[] = 'error';
}
if ( false === $has_PMPro ) {
$this->error_msg[] = sprintf(
__( "Error: The '<a href=\"%s\" target=\"_blank\">Paid Memberships Pro</a>' plugin must be installed and active!", "pmpro-powerpress-feed-protection" ),
'https://wordpress.org/plugins/paid-memberships-pro/'
);
$this->error_class[] = 'error';
}
if ( false === $has_PMProRSS ) {
$this->error_msg[] = sprintf(
__( "Error: The Paid Memberships Pro '<a href=\"%s\" target=\"_blank\">Member RSS</a>' Add-on must be installed and active!", "pmpro-powerpress-feed-protection" ),
'https://www.paidmembershipspro.com/add-ons/pmpro-member-rss/'
);
$this->error_class[] = 'error';
}
}
}
/**
* Add Link to PMPro Support Forum on Plugins page
*
* @param array $links
* @param string $file
*
* @return array
*/
public function pluginRowMeta( $links, $file ) {
if ( false !== strpos( $file, 'pmpro-powerpress-feed-protection.php' ) ) {
$links[] = sprintf( '<a href="%s" title="%s" target="_blank">%s</a>',
esc_url( 'https://paidmembershipsorp.com/support/' ),
__( 'Visit the Customer Support Forum', 'paid-memberships-pro' ),
__( 'Support', 'paid-memberships-pro' )
);
}
return $links;
}
/**
* Get or create instance of this class (pmproPowerPress)
*
* @return pmproPowerPress
*/
public static function getInstance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
}
add_action( 'plugins_loaded', array( pmproPowerPress::getInstance(), ''loadHooks ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment