Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions includes/display-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,38 @@
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/

/**
* Filter the content based on the "restrict this content" configuration
*
* @param string $content Unfiltered content.
*
* @since 2.1.4
* @return string Newly modified post content.
*/
function rc_filter_restricted_content( $content ) {

global $rc_options;

if ( ! rc_user_can_access() ) {

// The current user doesn't have access so we need to filter the content.
$required_level = get_post_meta( get_the_ID(), 'rcUserLevel', true );
$restricted_message = isset( $rc_options[ strtolower( $required_level ) . '_message' ] ) ? $rc_options[ strtolower( $required_level ) . '_message' ] : $rc_options['subscriber_message'];
$content = do_shortcode( $restricted_message );

}

return $content;

}

add_filter( 'the_content', 'rc_filter_restricted_content' );

/**
* Display editor message
*
* @deprecated 2.1.4 Handled by rc_user_can_access() and rc_filter_restricted_content() instead.
*
* @param string $content
*
* @return string
Expand All @@ -31,6 +60,8 @@ function rcMetaDisplayEditor( $content ) {
/**
* Display author message
*
* @deprecated 2.1.4 Handled by rc_user_can_access() and rc_filter_restricted_content() instead.
*
* @param string $content
*
* @return string
Expand All @@ -52,6 +83,8 @@ function rcMetaDisplayAuthor( $content ) {
/**
* Display contributor message
*
* @deprecated 2.1.4 Handled by rc_user_can_access() and rc_filter_restricted_content() instead.
*
* @param string $content
*
* @return string
Expand All @@ -73,6 +106,8 @@ function rcMetaDisplayContributor( $content ) {
/**
* Display subscriber message
*
* @deprecated 2.1.4 Handled by rc_user_can_access() and rc_filter_restricted_content() instead.
*
* @param string $content
*
* @return string
Expand All @@ -94,6 +129,8 @@ function rcMetaDisplaySubscriber( $content ) {
/**
* Display error message to non-logged in users
*
* @deprecated 2.1.4 Handled by rc_user_can_access() and rc_filter_restricted_content() instead.
*
* @param $content
*
* @return string
Expand Down
33 changes: 32 additions & 1 deletion includes/user-checks.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* Add a different filter to the post content based on
* the current user's capabilities.
*
* @deprecated 2.1.4 Handled by rc_user_can_access() and rc_filter_restricted_content() instead.
*
* @return void
*/
function rcCheckUser() {
Expand All @@ -38,4 +40,33 @@ function rcCheckUser() {
}
}

add_action( 'loop_start', 'rcCheckUser' );
//add_action( 'loop_start', 'rcCheckUser' );

/**
* Checks whether a user can access a post
*
* @param int $user_id ID of the user to check, or 0 for the current user.
* @param int $post_id ID of the post to check, or 0 for the current post.
*
* @since 2.1.4
* @return bool Whether or not the user has access to view the post.
*/
function rc_user_can_access( $user_id = 0, $post_id = 0 ) {

if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}

if ( empty( $post_id ) ) {
$post_id = get_the_ID();
}

$required_level = get_post_meta( $post_id, 'rcUserLevel', true );

if ( empty( $required_level ) || 'None' == $required_level || current_user_can( 'manage_options' ) ) {
return true;
}

return user_can( $user_id, strtolower( $required_level ) );

}