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] - 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 *