diff --git a/includes/display-functions.php b/includes/display-functions.php index ecc74c7..68c4c23 100644 --- a/includes/display-functions.php +++ b/includes/display-functions.php @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/includes/user-checks.php b/includes/user-checks.php index 4681cf4..f816569 100644 --- a/includes/user-checks.php +++ b/includes/user-checks.php @@ -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() { @@ -38,4 +40,33 @@ function rcCheckUser() { } } -add_action( 'loop_start', 'rcCheckUser' ); \ No newline at end of file +//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 ) ); + +} \ No newline at end of file