mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-06-05 15:09:37 +09:00
Merge pull request #861 from ultimatemember/fix/restrict_content_exclude
Restrict content exclude (issue #859)
This commit is contained in:
@@ -52,6 +52,10 @@ if ( ! class_exists( 'um\core\Access' ) ) {
|
||||
$this->allow_access = false;
|
||||
|
||||
add_filter( 'pre_get_posts', array( &$this, 'exclude_posts' ), 99, 1 );
|
||||
add_filter( 'pre_get_comments', array( &$this, 'exclude_posts_comments' ), 99, 1 );
|
||||
add_filter( 'get_next_post_where', array( &$this, 'exclude_navigation_posts' ), 99, 5 );
|
||||
add_filter( 'get_previous_post_where', array( &$this, 'exclude_navigation_posts' ), 99, 5 );
|
||||
add_filter( 'widget_posts_args', array( &$this, 'exclude_resticted_posts_widget' ), 99, 1 );
|
||||
|
||||
//there is posts (Posts/Page/CPT) filtration if site is accessible
|
||||
//there also will be redirects if they need
|
||||
@@ -1027,20 +1031,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
|
||||
*/
|
||||
function exclude_posts( $query ) {
|
||||
if ( $query->is_main_query() ) {
|
||||
global $wpdb;
|
||||
|
||||
$exclude_posts = array();
|
||||
$posts = $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'um_content_restriction'");
|
||||
foreach ( $posts as $post ) {
|
||||
$content_restriction = $this->get_post_privacy_settings( $post );
|
||||
|
||||
if ( ! empty( $content_restriction['_um_access_hide_from_queries'] ) ) {
|
||||
if ( $this->is_restricted( $post ) ) {
|
||||
array_push( $exclude_posts, $post );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$exclude_posts = $this->exclude_posts_array( true );
|
||||
if ( ! empty( $exclude_posts ) ) {
|
||||
$post__not_in = $query->get( 'post__not_in', array() );
|
||||
$query->set( 'post__not_in', array_merge( $post__not_in, $exclude_posts ) );
|
||||
@@ -1049,6 +1040,84 @@ if ( ! class_exists( 'um\core\Access' ) ) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Exclude comments from restricted posts in widgets
|
||||
*
|
||||
* @param \WP_Comment_Query $query
|
||||
*
|
||||
*/
|
||||
function exclude_posts_comments( $query ){
|
||||
$exclude_posts = $this->exclude_posts_array( false );
|
||||
if ( ! empty( $exclude_posts ) ) {
|
||||
$query->query_vars['post__not_in'] = $exclude_posts;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get array with restricted posts
|
||||
*
|
||||
* @param boolean $in_query
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function exclude_posts_array( $in_query = false ) {
|
||||
global $wpdb;
|
||||
|
||||
$exclude_posts = array();
|
||||
$posts = $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'um_content_restriction'");
|
||||
foreach ( $posts as $post ) {
|
||||
$content_restriction = $this->get_post_privacy_settings( $post );
|
||||
if ( ! empty( $content_restriction['_um_access_hide_from_queries'] ) || $in_query === false ) {
|
||||
if ( $this->is_restricted( $post ) ) {
|
||||
array_push( $exclude_posts, $post );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $exclude_posts;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Exclude posts from next, previous navigation
|
||||
*
|
||||
* @param string $where
|
||||
* @param bool $in_same_term
|
||||
* @param array $excluded_terms
|
||||
* @param string $taxonomy.
|
||||
* @param WP_Post $post
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function exclude_navigation_posts( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) {
|
||||
$exclude_posts = $this->exclude_posts_array();
|
||||
if ( ! empty( $exclude_posts ) ) {
|
||||
$exclude_string = implode(',', $exclude_posts);
|
||||
$where .= ' AND ID NOT IN ( ' . $exclude_string . ' )';
|
||||
}
|
||||
|
||||
return $where;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Exclude restricted posts in widgets
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function exclude_resticted_posts_widget( $array ) {
|
||||
$exclude_posts = $this->exclude_posts_array();
|
||||
if ( ! empty( $exclude_posts ) ) {
|
||||
$array['post__not_in'] = $exclude_posts;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $single_template
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user