role_names as $role => $name ) { // If the WP role is one of the current roles but not a new role, remove it. if ( ! in_array( $role, $roles ) && in_array( $role, $current_roles ) ) members_remove_post_role( $post_id, $role ); } } /** * Deletes all of a post's access roles. * * @since 1.0.0 * @access public * @param int $post_id * @return bool */ function members_delete_post_roles( $post_id ) { return delete_post_meta( $post_id, '_members_access_role' ); } /** * Adds required filters for the content permissions feature if it is active. * * @since 0.2.0 * @access public * @global object $wp_embed * @return void */ function members_enable_content_permissions() { global $wp_embed; // Only add filters if the content permissions feature is enabled and we're not in the admin. if ( members_content_permissions_enabled() && !is_admin() ) { // Filter the content and exerpts. add_filter( 'the_content', 'members_content_permissions_protect', 95 ); add_filter( 'get_the_excerpt', 'members_content_permissions_protect', 95 ); add_filter( 'the_excerpt', 'members_content_permissions_protect', 95 ); add_filter( 'the_content_feed', 'members_content_permissions_protect', 95 ); add_filter( 'get_comment_text', 'members_content_permissions_protect', 95 ); // Filter the comments template to make sure comments aren't shown to users without access. add_filter( 'comments_template', 'members_content_permissions_comments', 95 ); // Use WP formatting filters on the post error message. add_filter( 'members_post_error_message', array( $wp_embed, 'run_shortcode' ), 5 ); add_filter( 'members_post_error_message', array( $wp_embed, 'autoembed' ), 5 ); add_filter( 'members_post_error_message', 'wptexturize', 10 ); add_filter( 'members_post_error_message', 'convert_smilies', 15 ); add_filter( 'members_post_error_message', 'convert_chars', 20 ); add_filter( 'members_post_error_message', 'wpautop', 25 ); add_filter( 'members_post_error_message', 'do_shortcode', 30 ); add_filter( 'members_post_error_message', 'shortcode_unautop', 35 ); } } /** * Denies/Allows access to view post content depending on whether a user has permission to * view the content. * * @since 0.1.0 * @access public * @param string $content * @return string */ function members_content_permissions_protect( $content ) { $post_id = get_the_ID(); return members_can_current_user_view_post( $post_id ) ? $content : members_get_post_error_message( $post_id ); } /** * Disables the comments template if a user doesn't have permission to view the post the * comments are associated with. * * @since 0.1.0 * @param string $template * @return string */ function members_content_permissions_comments( $template ) { // Check if the current user has permission to view the comments' post. if ( ! members_can_current_user_view_post( get_the_ID() ) ) { // Look for a 'comments-no-access.php' template in the parent and child theme. $has_template = locate_template( array( 'comments-no-access.php' ) ); // If the template was found, use it. Otherwise, fall back to the Members comments.php template. $template = $has_template ? $has_template : members_plugin()->dir . 'templates/comments.php'; // Allow devs to overwrite the comments template. $template = apply_filters( 'members_comments_template', $template ); } // Return the comments template filename. return $template; } /** * Gets the error message to display for users who do not have access to view the given post. * The function first checks to see if a custom error message has been written for the * specific post. If not, it loads the error message set on the plugins settings page. * * @since 0.2.0 * @access public * @param int $post_id * @return string */ function members_get_post_error_message( $post_id ) { // Get the error message for the specific post. $message = members_get_post_access_message( $post_id ); // Use default error message if we don't have one for the post. if ( ! $message ) $message = members_get_setting( 'content_permissions_error' ); // Return the error message. return apply_filters( 'members_post_error_message', sprintf( '