From f1754e46797436ea5adffbd516e716e202427ec3 Mon Sep 17 00:00:00 2001 From: Nikita Sinelnikov Date: Thu, 26 Aug 2021 14:53:12 +0300 Subject: [PATCH] - Fixed: Hiding comments feed for pages with active comments - Fixed: Changing the post title to the restricted value while displaying in the loop that uses direct `post_title` variable - Fixed: PHP errors related to not passed function attributes from 3rd-party plugins or themes when using WP native hooks --- includes/core/class-access.php | 99 +++++++++++++++++++++++++--------- readme.txt | 3 ++ 2 files changed, 77 insertions(+), 25 deletions(-) diff --git a/includes/core/class-access.php b/includes/core/class-access.php index 92837eb6..9f117d53 100644 --- a/includes/core/class-access.php +++ b/includes/core/class-access.php @@ -106,7 +106,6 @@ if ( ! class_exists( 'um\core\Access' ) ) { 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' ) ); - add_action( 'plugins_loaded', array( &$this, 'disable_restriction_pre_queries' ), 1 ); } @@ -498,7 +497,7 @@ if ( ! class_exists( 'um\core\Access' ) ) { * * @return object */ - function custom_count_posts_handler( $counts, $type, $perm ) { + function custom_count_posts_handler( $counts, $type = 'post', $perm = '' ) { if ( current_user_can( 'administrator' ) ) { return $counts; } @@ -597,17 +596,19 @@ if ( ! class_exists( 'um\core\Access' ) ) { /** * Exclude restricted posts in widgets * - * @param string $sql_where + * @param string $sql_where * @param array $parsed_args * * @return string */ - function exclude_restricted_posts_archives_widget( $sql_where, $parsed_args ) { + function exclude_restricted_posts_archives_widget( $sql_where, $parsed_args = array() ) { if ( current_user_can( 'administrator' ) ) { return $sql_where; } - $exclude_posts = $this->exclude_posts_array( false, $parsed_args['post_type'] ); + $post_type = ! empty( $parsed_args['post_type'] ) ? $parsed_args['post_type'] : false; + + $exclude_posts = $this->exclude_posts_array( false, $post_type ); if ( ! empty( $exclude_posts ) ) { $exclude_string = implode( ',', $exclude_posts ); $sql_where .= ' AND ID NOT IN ( ' . $exclude_string . ' )'; @@ -620,19 +621,23 @@ 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 + * @param string $where + * @param bool $in_same_term + * @param string|array $excluded_terms + * @param string $taxonomy + * @param null|\WP_Post $post * * @return string */ - function exclude_navigation_posts( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) { + function exclude_navigation_posts( $where, $in_same_term = false, $excluded_terms = '', $taxonomy = 'category', $post = null ) { if ( current_user_can( 'administrator' ) ) { return $where; } + if ( empty( $post ) ) { + return $where; + } + $exclude_posts = $this->exclude_posts_array( false, $post->post_type ); if ( ! empty( $exclude_posts ) ) { $exclude_string = implode( ',', $exclude_posts ); @@ -726,7 +731,7 @@ if ( ! class_exists( 'um\core\Access' ) ) { * * @return string */ - function filter_restricted_post_excerpt( $post_excerpt, $post ) { + function filter_restricted_post_excerpt( $post_excerpt = '', $post = null ) { if ( empty( $post ) ) { return $post_excerpt; } @@ -774,7 +779,11 @@ if ( ! class_exists( 'um\core\Access' ) ) { * * @return bool */ - function filter_post_thumbnail( $has_thumbnail, $post, $thumbnail_id ) { + function filter_post_thumbnail( $has_thumbnail, $post = null, $thumbnail_id = false ) { + if ( empty( $thumbnail_id ) ) { + return $has_thumbnail; + } + if ( current_user_can( 'administrator' ) ) { return $has_thumbnail; } @@ -839,7 +848,7 @@ if ( ! class_exists( 'um\core\Access' ) ) { * @return array */ function get_available_comments_post_types() { - global $wp_taxonomies; + global $wp_taxonomies, $wpdb; $restricted_posts = UM()->options()->get( 'restricted_access_post_metabox' ); if ( empty( $restricted_posts ) ) { @@ -860,7 +869,17 @@ if ( ! class_exists( 'um\core\Access' ) ) { $restricted_posts = array_unique( $restricted_posts ); foreach ( $restricted_posts as $k => $post_type ) { if ( 'closed' === get_default_comment_status( $post_type ) ) { - unset( $restricted_posts[ $k ] ); + $open_comments = $wpdb->get_var( $wpdb->prepare( + "SELECT ID + FROM {$wpdb->posts} + WHERE post_type = %s AND + comment_status != 'closed'", + $post_type + ) ); + + if ( empty( $open_comments ) ) { + unset( $restricted_posts[ $k ] ); + } } } @@ -894,12 +913,12 @@ if ( ! class_exists( 'um\core\Access' ) ) { /** - * @param object $stats + * @param array|object $stats * @param int $post_id Post ID. Can be 0 for the whole website * * @return object */ - function custom_comments_count_handler( $stats, $post_id ) { + function custom_comments_count_handler( $stats = array(), $post_id = 0 ) { if ( ! empty( $stats ) || current_user_can( 'administrator' ) ) { return $stats; } @@ -1044,7 +1063,7 @@ if ( ! class_exists( 'um\core\Access' ) ) { * @param int $post_id * @return boolean */ - function disable_comments_open_number( $count, $post_id ) { + function disable_comments_open_number( $count, $post_id = 0 ) { if ( current_user_can( 'administrator' ) ) { return $count; } @@ -1070,11 +1089,11 @@ if ( ! class_exists( 'um\core\Access' ) ) { /** * Protect Post Types in menu query * Restrict content new logic - * @param $menu_items - * @param $args + * @param array $menu_items + * @param array $args * @return array */ - function filter_menu( $menu_items, $args ) { + function filter_menu( $menu_items, $args = array() ) { //if empty if ( empty( $menu_items ) ) { return $menu_items; @@ -1209,12 +1228,42 @@ if ( ! class_exists( 'um\core\Access' ) ) { } + /** + * @param \WP_Post $post + * + * @return \WP_Post + */ + function maybe_replace_title( $post ) { + if ( ! UM()->options()->get( 'restricted_post_title_replace' ) ) { + return $post; + } + + if ( current_user_can( 'administrator' ) ) { + return $post; + } + + if ( ! is_a( $post, '\WP_Post' ) ) { + return $post; + } + + $ignore = apply_filters( 'um_ignore_restricted_title', false, $post->ID ); + if ( $ignore ) { + return $post; + } + + $restricted_global_title = UM()->options()->get( 'restricted_access_post_title' ); + $post->post_title = stripslashes( $restricted_global_title ); + + return $post; + } + + /** * Protect Post Types in query * Restrict content new logic * - * @param $posts - * @param \WP_Query $query + * @param array $posts + * @param array|\WP_Query $query * @return array */ function filter_protected_posts( $posts, $query ) { @@ -1296,7 +1345,7 @@ if ( ! class_exists( 'um\core\Access' ) ) { */ do_action( 'um_access_fix_external_post_content' ); - $filtered_posts[] = $post; + $filtered_posts[] = $this->maybe_replace_title( $post ); continue; } } elseif ( '1' == $restriction['_um_noaccess_action'] ) { @@ -1319,7 +1368,7 @@ if ( ! class_exists( 'um\core\Access' ) ) { } } else { if ( UM()->options()->get( 'disable_restriction_pre_queries' ) || empty( $restriction['_um_access_hide_from_queries'] ) ) { - $filtered_posts[] = $post; + $filtered_posts[] = $this->maybe_replace_title( $post ); continue; } } diff --git a/readme.txt b/readme.txt index 38106311..b8f990f3 100644 --- a/readme.txt +++ b/readme.txt @@ -159,6 +159,9 @@ The plugin works with popular caching plugins by automatically excluding Ultimat * Bugfixes: + - Fixed: Hiding comments feed for pages with active comments + - Fixed: Changing the post title to the restricted value while displaying in the loop that uses direct `post_title` variable + - Fixed: PHP errors related to not passed function attributes from 3rd-party plugins or themes when using WP native hooks - Fixed: Handling the login form errors via \WP_Error = 2.2.3: August 25, 2021 =