From c45ba990d09e2588d9218bb55e76fbbbdb25b6b9 Mon Sep 17 00:00:00 2001 From: ashubawork <43743394+ashubawork@users.noreply.github.com> Date: Tue, 6 Jul 2021 11:26:57 +0300 Subject: [PATCH 1/4] - hide post in navigation to the next/previous post (issue #859) --- includes/core/class-access.php | 62 ++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/includes/core/class-access.php b/includes/core/class-access.php index 37c5e20d..ebaceb87 100644 --- a/includes/core/class-access.php +++ b/includes/core/class-access.php @@ -52,6 +52,8 @@ if ( ! class_exists( 'um\core\Access' ) ) { $this->allow_access = false; add_filter( 'pre_get_posts', array( &$this, 'exclude_posts' ), 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 ); //there is posts (Posts/Page/CPT) filtration if site is accessible //there also will be redirects if they need @@ -1027,20 +1029,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(); 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 +1038,51 @@ if ( ! class_exists( 'um\core\Access' ) ) { } + /** + * 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; + } + + + /** + * get array with restricted posts + * + */ + function exclude_posts_array() { + 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 ); + } + } + } + + return $exclude_posts; + } + + /** * @param string $single_template * From ef0f9ad946bec122f86f76f833031c728273f751 Mon Sep 17 00:00:00 2001 From: ashubawork <43743394+ashubawork@users.noreply.github.com> Date: Tue, 6 Jul 2021 12:00:00 +0300 Subject: [PATCH 2/4] - hide restricted posts in widgets - hide comments from restricted posts in widgets --- includes/core/class-access.php | 38 +++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/includes/core/class-access.php b/includes/core/class-access.php index ebaceb87..c09bef14 100644 --- a/includes/core/class-access.php +++ b/includes/core/class-access.php @@ -54,6 +54,8 @@ if ( ! class_exists( 'um\core\Access' ) ) { add_filter( 'pre_get_posts', array( &$this, 'exclude_posts' ), 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_comments_args', array( &$this, 'exclude_comments_resticted_posts_widget' ), 99, 1 ); + 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 @@ -1049,7 +1051,7 @@ if ( ! class_exists( 'um\core\Access' ) ) { * * @return string */ - function exclude_navigation_posts( $where, $in_same_term, $excluded_terms, $taxonomy, $post ){ + 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); @@ -1060,6 +1062,40 @@ if ( ! class_exists( 'um\core\Access' ) ) { } + /** + * Exclude comments from restricted posts in widgets + * + * @param array $array + * + * @return array + */ + function exclude_comments_resticted_posts_widget( $array ) { + $exclude_posts = $this->exclude_posts_array(); + if ( ! empty( $exclude_posts ) ) { + $array['post__not_in'] = $exclude_posts; + } + + return $array; + } + + + /** + * 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; + } + + /** * get array with restricted posts * From 2712cb760af48a692c45dd5e5a9bee6a111cd6f2 Mon Sep 17 00:00:00 2001 From: ashubawork <43743394+ashubawork@users.noreply.github.com> Date: Tue, 6 Jul 2021 12:55:25 +0300 Subject: [PATCH 3/4] - changed a hook for hide restricted post's comments --- includes/core/class-access.php | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/includes/core/class-access.php b/includes/core/class-access.php index c09bef14..3e2c311f 100644 --- a/includes/core/class-access.php +++ b/includes/core/class-access.php @@ -52,9 +52,9 @@ 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_comments_args', array( &$this, 'exclude_comments_resticted_posts_widget' ), 99, 1 ); add_filter( 'widget_posts_args', array( &$this, 'exclude_resticted_posts_widget' ), 99, 1 ); //there is posts (Posts/Page/CPT) filtration if site is accessible @@ -1040,6 +1040,20 @@ 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(); + if ( ! empty( $exclude_posts ) ) { + $query->query_vars['post__not_in'] = $exclude_posts; + } + } + + /** * Exclude posts from next, previous navigation * @@ -1062,23 +1076,6 @@ if ( ! class_exists( 'um\core\Access' ) ) { } - /** - * Exclude comments from restricted posts in widgets - * - * @param array $array - * - * @return array - */ - function exclude_comments_resticted_posts_widget( $array ) { - $exclude_posts = $this->exclude_posts_array(); - if ( ! empty( $exclude_posts ) ) { - $array['post__not_in'] = $exclude_posts; - } - - return $array; - } - - /** * Exclude restricted posts in widgets * From b021a2e8812bcd1cab67a28deade8a313b17ddbc Mon Sep 17 00:00:00 2001 From: ashubawork <43743394+ashubawork@users.noreply.github.com> Date: Tue, 6 Jul 2021 13:50:51 +0300 Subject: [PATCH 4/4] - fixed restricted posts for comments --- includes/core/class-access.php | 52 ++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/includes/core/class-access.php b/includes/core/class-access.php index 3e2c311f..4349ee7e 100644 --- a/includes/core/class-access.php +++ b/includes/core/class-access.php @@ -1031,7 +1031,7 @@ if ( ! class_exists( 'um\core\Access' ) ) { */ function exclude_posts( $query ) { if ( $query->is_main_query() ) { - $exclude_posts = $this->exclude_posts_array(); + $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 ) ); @@ -1047,13 +1047,38 @@ if ( ! class_exists( 'um\core\Access' ) ) { * */ function exclude_posts_comments( $query ){ - $exclude_posts = $this->exclude_posts_array(); + $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 * @@ -1093,29 +1118,6 @@ if ( ! class_exists( 'um\core\Access' ) ) { } - /** - * get array with restricted posts - * - */ - function exclude_posts_array() { - 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 ); - } - } - } - - return $exclude_posts; - } - - /** * @param string $single_template *