- content restriction fixes;

- compatibility with global accessible settings and individual restrict content options for post types;
This commit is contained in:
nikitozzzzzzz
2017-11-23 01:29:10 +02:00
parent d894c67d8e
commit 2c2e76a20c
2 changed files with 328 additions and 854 deletions
+315 -87
View File
@@ -5,102 +5,324 @@ namespace um\core;
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'Access' ) ) {
class Access {
class Access {
function __construct() {
$this->redirect_handler = false;
$this->allow_access = false;
add_action( 'template_redirect', array( &$this, 'template_redirect' ), 1000 );
//protect posts types
add_filter( 'the_posts', array( &$this, 'filter_protected_posts' ), 99, 2 );
//protect pages for wp_list_pages func
add_filter( 'get_pages', array( &$this, 'filter_protected_posts' ), 99, 2 );
//filter menu items
add_filter( 'wp_nav_menu_objects', array( &$this, 'filter_menu' ), 99, 2 );
}
/**
* If true then we use individual restrict content options
* for post
*
* @var bool
*/
private $singular_page;
/**
* Set custom access actions and redirection
*
* Old global restrict content logic
*/
function template_redirect() {
global $post;
do_action('um_access_global_settings');
do_action('um_access_category_settings');
do_action('um_access_tags_settings');
do_action('um_access_user_custom_homepage');
do_action('um_access_frontpage_per_role');
do_action('um_access_homepage_per_role');
if ( $this->redirect_handler && $this->allow_access == false &&
( ! um_is_core_page('login') || um_is_core_page( 'login' ) && is_user_logged_in() ) ) {
// login page add protected page automatically
if ( strstr( $this->redirect_handler, um_get_core_page('login') ) ){
$curr = UM()->permalinks()->get_current_url();
$this->redirect_handler = esc_url( add_query_arg('redirect_to', urlencode_deep( $curr ), $this->redirect_handler) );
}
wp_redirect( $this->redirect_handler );
}
}
/**
* @var bool
*/
private $redirect_handler;
/**
* Get custom access settings meta
* @param integer $post_id
* @return array
*/
function get_meta( $post_id ) {
global $post;
$meta = get_post_custom( $post_id );
if ( isset( $meta ) && is_array( $meta ) ) {
foreach ($meta as $k => $v){
if ( strstr($k, '_um_') ) {
$k = str_replace('_um_', '', $k);
$array[$k] = $v[0];
}
}
}
if ( isset( $array ) )
return (array)$array;
else
return array('');
}
/**
* @var bool
*/
private $allow_access;
/**
* Sets a custom access referer in a redirect URL
*
* @param string $url
* @param string $referer
*
* @return string
*/
function set_referer( $url, $referer ) {
/**
* Access constructor.
*/
function __construct() {
$enable_referer = apply_filters( "um_access_enable_referer", false );
if( ! $enable_referer ) return $url;
$this->singular_page = false;
$url = add_query_arg( 'um_ref', $referer, $url );
return $url;
}
$this->redirect_handler = false;
$this->allow_access = false;
//there is posts (Posts/Page/CPT) filtration if site is accessible
//there also will be redirects if they need
//protect posts types
add_filter( 'the_posts', array( &$this, 'filter_protected_posts' ), 99, 2 );
//protect pages for wp_list_pages func
add_filter( 'get_pages', array( &$this, 'filter_protected_posts' ), 99, 2 );
//filter menu items
add_filter( 'wp_nav_menu_objects', array( &$this, 'filter_menu' ), 99, 2 );
//check the site's accessible more priority have Individual Post/Term Restriction settings
add_action( 'template_redirect', array( &$this, 'template_redirect' ), 1000 );
//add_action( 'um_access_global_settings', array( &$this, 'um_access_global_settings' ) );
//add_action( 'um_access_home_page', array( &$this, 'um_access_home_page' ) );
//add_action( 'um_access_taxonomy_settings', array( &$this, 'um_access_taxonomy_settings' ) );
add_action( 'um_access_check_individual_term_settings', array( &$this, 'um_access_check_individual_term_settings' ) );
add_action( 'um_access_check_global_settings', array( &$this, 'um_access_check_global_settings' ) );
}
/**
* Check individual term Content Restriction settings
*/
function um_access_check_individual_term_settings() {
//check only tax|tags|categories - skip archive, author, and date lists
if ( ! ( is_tax() || is_tag() || is_category() ) ) {
return;
}
if ( is_tag() ) {
$restricted_taxonomies = um_get_option( 'restricted_access_taxonomy_metabox' );
if ( empty( $restricted_taxonomies['post_tag'] ) )
return;
$tag_id = get_query_var( 'tag_id' );
if ( ! empty( $tag_id ) ) {
$restriction = get_term_meta( $tag_id, 'um_content_restriction', true );
}
} elseif ( is_category() ) {
$um_category = get_the_category();
$um_category = current( $um_category );
$restricted_taxonomies = um_get_option( 'restricted_access_taxonomy_metabox' );
if ( empty( $restricted_taxonomies[ $um_category->taxonomy ] ) )
return;
if ( ! empty( $um_category->term_id ) ) {
$restriction = get_term_meta( $um_category->term_id, 'um_content_restriction', true );
}
} elseif ( is_tax() ) {
$tax_name = get_query_var( 'taxonomy' );
$restricted_taxonomies = um_get_option( 'restricted_access_taxonomy_metabox' );
if ( empty( $restricted_taxonomies[ $tax_name ] ) )
return;
$term_name = get_query_var( 'term' );
$term = get_term_by( 'slug', $term_name, $tax_name );
if ( ! empty( $term->term_id ) ) {
$restriction = get_term_meta( $term->term_id, 'um_content_restriction', true );
}
}
if ( ! isset( $restriction ) )
return;
//post is private
if ( '1' == $restriction['_um_accessible'] ) {
//if post for not logged in users and user is not logged in
if ( ! is_user_logged_in() ) {
$this->allow_access = true;
return;
}
} elseif ( '2' == $restriction['_um_accessible'] ) {
//if post for logged in users and user is not logged in
if ( is_user_logged_in() ) {
$custom_restrict = apply_filters( 'um_custom_restriction', true, $restriction );
if ( ! empty( $restriction['_um_access_roles'] ) )
$user_can = $this->user_can( get_current_user_id(), $restriction['_um_access_roles'] );
if ( isset( $user_can ) && $user_can && $custom_restrict ) {
$this->allow_access = true;
return;
}
}
}
if ( '1' == $restriction['_um_noaccess_action'] ) {
$curr = UM()->permalinks()->get_current_url();
if ( ! isset( $restriction['_um_access_redirect'] ) || '0' == $restriction['_um_access_redirect'] ) {
$this->redirect_handler = $this->set_referer( esc_url( add_query_arg( 'redirect_to', urlencode_deep( $curr ), um_get_core_page( 'login' ) ) ), 'individual_term' );
} elseif ( '1' == $restriction['_um_access_redirect'] ) {
if ( ! empty( $restriction['_um_access_redirect_url'] ) ) {
$redirect = $restriction['_um_access_redirect_url'];
} else {
$redirect = esc_url( add_query_arg( 'redirect_to', urlencode_deep( $curr ), um_get_core_page( 'login' ) ) );
}
$this->redirect_handler = $this->set_referer( $redirect, 'individual_term' );
}
}
}
/**
* Check global accessible settings
*/
function um_access_check_global_settings() {
global $post;
if ( is_home() ) {
if ( is_user_logged_in() ) {
$role_meta = UM()->roles()->role_data( um_user( 'role' ) );
if ( ! empty( $role_meta['default_homepage'] ) )
return;
$redirect_to = ! empty( $role_meta['redirect_homepage'] ) ? $role_meta['redirect_homepage'] : um_get_core_page( 'user' );
$this->redirect_handler = $this->set_referer( $redirect_to, "custom_homepage" );
wp_redirect( $this->redirect_handler ); exit;
} else {
$access = um_get_option( 'accessible' );
if ( $access == 2 ) {
//global settings for accessible home page
$home_page_accessible = um_get_option( 'home_page_accessible' );
if ( $home_page_accessible == 0 ) {
//get redirect URL if not set get login page by default
$redirect = um_get_option( 'access_redirect' );
if ( ! $redirect )
$redirect = um_get_core_page( 'login' );
$this->redirect_handler = $this->set_referer( $redirect, 'global' );
wp_redirect( $this->redirect_handler ); exit;
}
}
}
} elseif ( is_category() ) {
if ( ! is_user_logged_in() ) {
$access = um_get_option( 'accessible' );
if ( $access == 2 ) {
//global settings for accessible home page
$category_page_accessible = um_get_option( 'category_page_accessible' );
if ( $category_page_accessible == 0 ) {
//get redirect URL if not set get login page by default
$redirect = um_get_option( 'access_redirect' );
if ( ! $redirect )
$redirect = um_get_core_page( 'login' );
$this->redirect_handler = $this->set_referer( $redirect, 'global' );
wp_redirect( $this->redirect_handler ); exit;
}
}
}
} else {
$access = um_get_option( 'accessible' );
if ( $access == 2 && ! is_user_logged_in() ) {
//build exclude URLs pages
$redirects = array();
$redirects[] = untrailingslashit( um_get_option( 'access_redirect' ) );
$exclude_uris = um_get_option( 'access_exclude_uris' );
if ( ! empty( $exclude_uris ) )
$redirects = array_merge( $redirects, $exclude_uris );
$redirects = array_unique( $redirects );
$current_url = UM()->permalinks()->get_current_url( get_option( 'permalink_structure' ) );
$current_url = untrailingslashit( $current_url );
$current_url_slash = trailingslashit( $current_url );
//get redirect URL if not set get login page by default
$redirect = um_get_option( 'access_redirect' );
if ( ! $redirect )
$redirect = um_get_core_page( 'login' );
if ( ! isset( $post->ID ) || ! ( in_array( $current_url, $redirects ) || in_array( $current_url_slash, $redirects ) ) ) {
//if current page not in exclude URLs
$this->redirect_handler = $this->set_referer( $redirect, 'global' );
wp_redirect( $this->redirect_handler ); exit;
}
}
}
}
/**
* Set custom access actions and redirection
*
* Old global restrict content logic
*/
function template_redirect() {
global $post;
//if we logged by administrator it can access to all content
if ( current_user_can( 'administrator' ) )
return;
//if we use individual restrict content options skip this function
if ( $this->singular_page )
return;
//also skip if we currently at wp-admin or 404 page
if ( is_admin() || is_404() )
return;
//also skip if we currently at UM Register|Login|Reset Password pages
if ( um_is_core_post( $post, 'register' ) ||
um_is_core_post( $post, 'password-reset' ) ||
um_is_core_post( $post, 'login' ) )
return;
//check terms individual restrict options
do_action( 'um_access_check_individual_term_settings' );
//exit from function if term page is accessible
if ( $this->check_access() )
return;
//check global restrict content options
do_action( 'um_access_check_global_settings' );
}
/**
* Check access
*
* @return bool
*/
function check_access() {
if ( $this->allow_access == true )
return true;
if ( $this->redirect_handler ) {
// login page add protected page automatically
/*if ( strstr( $this->redirect_handler, um_get_core_page('login') ) ){
$curr = UM()->permalinks()->get_current_url();
$this->redirect_handler = esc_url( add_query_arg('redirect_to', urlencode_deep( $curr ), $this->redirect_handler) );
}*/
wp_redirect( $this->redirect_handler ); exit;
}
return false;
}
/**
* Sets a custom access referer in a redirect URL
*
* @param string $url
* @param string $referer
*
* @return string
*/
function set_referer( $url, $referer ) {
$enable_referer = apply_filters( "um_access_enable_referer", false );
if( ! $enable_referer ) return $url;
$url = add_query_arg( 'um_ref', $referer, $url );
return $url;
}
/**
@@ -250,6 +472,8 @@ if ( ! class_exists( 'Access' ) ) {
continue;
}
} else {
$this->singular_page = true;
//if single post query
if ( ! isset( $restriction['_um_noaccess_action'] ) || '0' == $restriction['_um_noaccess_action'] ) {
@@ -319,6 +543,8 @@ if ( ! class_exists( 'Access' ) ) {
continue;
}
} else {
$this->singular_page = true;
//if single post query
if ( ! isset( $restriction['_um_noaccess_action'] ) || '0' == $restriction['_um_noaccess_action'] ) {
@@ -378,6 +604,8 @@ if ( ! class_exists( 'Access' ) ) {
continue;
}
} else {
$this->singular_page = true;
//if single post query
if ( ! isset( $restriction['_um_noaccess_action'] ) || '0' == $restriction['_um_noaccess_action'] ) {