- added hook um_access_restricted_post_instance;

- some wpcs ehnancements;
This commit is contained in:
Mykyta Synelnikov
2024-01-05 15:55:02 +02:00
parent b90eb8ab81
commit ae179677a0
+201 -132
View File
@@ -1,19 +1,23 @@
<?php <?php
namespace um\core; namespace um\core;
// Exit if accessed directly use WP_Comment_Query;
if ( ! defined( 'ABSPATH' ) ) exit; use WP_Post;
use WP_Query;
use WP_Term_Query;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\core\Access' ) ) { if ( ! class_exists( 'um\core\Access' ) ) {
/** /**
* Class Access * Class Access
* @package um\core * @package um\core
*/ */
class Access { class Access {
/** /**
* If true then we use individual restrict content options * If true then we use individual restrict content options
* for post * for post
@@ -22,30 +26,26 @@ if ( ! class_exists( 'um\core\Access' ) ) {
*/ */
private $singular_page; private $singular_page;
/** /**
* @var bool * @var bool
*/ */
private $redirect_handler; private $redirect_handler;
/** /**
* @var bool * @var bool
*/ */
private $allow_access; private $allow_access;
private $ignore_exclude = false; private $ignore_exclude = false;
/** /**
* Access constructor. * Access constructor.
*/ */
function __construct() { public function __construct() {
$this->singular_page = false; $this->singular_page = false;
$this->redirect_handler = false; $this->redirect_handler = false;
$this->allow_access = false; $this->allow_access = false;
// NEW HOOKS // NEW HOOKS
// Navigation line below the post content, change query to exclude restricted // Navigation line below the post content, change query to exclude restricted
@@ -140,7 +140,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
* *
* @return array * @return array
*/ */
function exclude_posts_array( $force = false, $post_types = false ) { public function exclude_posts_array( $force = false, $post_types = false ) {
if ( $this->ignore_exclude ) { if ( $this->ignore_exclude ) {
return array(); return array();
} }
@@ -335,16 +335,14 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $exclude_posts; return $exclude_posts;
} }
/** /**
* Get array with restricted terms * Get array with restricted terms
* *
* @param \WP_Term_Query $query * @param WP_Term_Query $query
* *
* @return array * @return array
*/ */
function exclude_terms_array( $query ) { public function exclude_terms_array( $query ) {
$exclude = array(); $exclude = array();
$restricted_taxonomies = UM()->options()->get( 'restricted_access_taxonomy_metabox' ); $restricted_taxonomies = UM()->options()->get( 'restricted_access_taxonomy_metabox' );
@@ -415,11 +413,10 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $exclude; return $exclude;
} }
/** /**
* @param \WP_Term_Query $query * @param WP_Term_Query $query
*/ */
function exclude_hidden_terms_query( $query ) { public function exclude_hidden_terms_query( $query ) {
if ( current_user_can( 'administrator' ) || ! empty( $query->query_vars['um_ignore_exclude'] ) ) { if ( current_user_can( 'administrator' ) || ! empty( $query->query_vars['um_ignore_exclude'] ) ) {
return; return;
} }
@@ -430,11 +427,10 @@ if ( ! class_exists( 'um\core\Access' ) ) {
} }
} }
/** /**
* @param \WP_Query $query * @param WP_Query $query
*/ */
function exclude_posts( $query ) { public function exclude_posts( $query ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return; return;
} }
@@ -472,16 +468,15 @@ if ( ! class_exists( 'um\core\Access' ) ) {
} }
} }
/** /**
* Exclude restricted post from query if there is a single query that exclude post_not_in by default in \WP_Query * Exclude restricted post from query if there is a single query that exclude post_not_in by default in WP_Query
* *
* @param string $where * @param string $where
* @param \WP_Query $query * @param WP_Query $query
* *
* @return mixed * @return mixed
*/ */
function exclude_posts_where( $where, $query ) { public function exclude_posts_where( $where, $query ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $where; return $where;
} }
@@ -502,18 +497,17 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $where; return $where;
} }
/** /**
* Change the posts count based on restriction settings * Change the posts count based on restriction settings
* *
* @param object $counts Post counts * @param object $counts Post counts
* @param string $type Post type * @param string $type Post type
* @param string $perm The permission to determine if the posts are 'readable' * @param string $perm The permission to determine if the posts are 'readable'
* by the current user. * by the current user.
* *
* @return object * @return object
*/ */
function custom_count_posts_handler( $counts, $type = 'post', $perm = '' ) { public function custom_count_posts_handler( $counts, $type = 'post', $perm = '' ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $counts; return $counts;
} }
@@ -565,7 +559,6 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $counts; return $counts;
} }
/** /**
* Exclude restricted posts in Recent Posts widget * Exclude restricted posts in Recent Posts widget
* *
@@ -573,7 +566,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
* *
* @return array * @return array
*/ */
function exclude_restricted_posts_widget( $array ) { public function exclude_restricted_posts_widget( $array ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $array; return $array;
} }
@@ -587,7 +580,6 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $array; return $array;
} }
/** /**
* Exclude restricted posts in Recent Posts widget * Exclude restricted posts in Recent Posts widget
* *
@@ -595,7 +587,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
* *
* @return array * @return array
*/ */
function exclude_restricted_pages( $array ) { public function exclude_restricted_pages( $array ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $array; return $array;
} }
@@ -608,16 +600,15 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $array; return $array;
} }
/** /**
* Exclude restricted posts in widgets * Exclude restricted posts in widgets
* *
* @param string $sql_where * @param string $sql_where
* @param array $parsed_args * @param array $parsed_args
* *
* @return string * @return string
*/ */
function exclude_restricted_posts_archives_widget( $sql_where, $parsed_args = array() ) { public function exclude_restricted_posts_archives_widget( $sql_where, $parsed_args = array() ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $sql_where; return $sql_where;
} }
@@ -633,7 +624,6 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $sql_where; return $sql_where;
} }
/** /**
* Exclude posts from next, previous navigation * Exclude posts from next, previous navigation
* *
@@ -641,11 +631,11 @@ if ( ! class_exists( 'um\core\Access' ) ) {
* @param bool $in_same_term * @param bool $in_same_term
* @param string|array $excluded_terms * @param string|array $excluded_terms
* @param string $taxonomy * @param string $taxonomy
* @param null|\WP_Post $post * @param null|WP_Post $post
* *
* @return string * @return string
*/ */
function exclude_navigation_posts( $where, $in_same_term = false, $excluded_terms = '', $taxonomy = 'category', $post = null ) { public function exclude_navigation_posts( $where, $in_same_term = false, $excluded_terms = '', $taxonomy = 'category', $post = null ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $where; return $where;
} }
@@ -663,16 +653,15 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $where; return $where;
} }
/** /**
* Replace titles of restricted posts * Replace titles of restricted posts.
* *
* @param string $title * @param string $title Post title.
* @param int|null $id * @param int|null $id Post ID.
* *
* @return string * @return string
*/ */
function filter_restricted_post_title( $title, $id = null ) { public function filter_restricted_post_title( $title, $id = null ) {
if ( ! UM()->options()->get( 'restricted_post_title_replace' ) ) { if ( ! UM()->options()->get( 'restricted_post_title_replace' ) ) {
return $title; return $title;
} }
@@ -689,6 +678,29 @@ if ( ! class_exists( 'um\core\Access' ) ) {
$id = absint( $id ); $id = absint( $id );
} }
/**
* Filters a marker for ignoring restricted post title changes.
*
* @param {bool} $ignore Marker for ignoring restricted post title changes. Default `false`. Set to `true` if you want to ignore and show real title.
* @param {int} $post_id Post ID.
*
* @return {bool} Marker for ignoring restricted post title changes.
*
* @since 2.2.3
* @hook um_ignore_restricted_title
*
* @example <caption>Leave real post titles even they are restricted. By post ID (for ID = 400).</caption>
* function change_restricted_title( $ignore, $id ) {
* // your code here
* if ( 400 === $id ) {
* $ignore = true;
* }
* return $ignore;
* }
* add_filter( 'um_ignore_restricted_title', 'change_restricted_title', 10, 2 );
* @example <caption>Leave real post titles even they are restricted. For all posts: site-wide logic.</caption>
* add_filter( 'um_ignore_restricted_title', '__return_true' );
*/
$ignore = apply_filters( 'um_ignore_restricted_title', false, $id ); $ignore = apply_filters( 'um_ignore_restricted_title', false, $id );
if ( $ignore ) { if ( $ignore ) {
return $title; return $title;
@@ -696,21 +708,20 @@ if ( ! class_exists( 'um\core\Access' ) ) {
if ( $this->is_restricted( $id ) ) { if ( $this->is_restricted( $id ) ) {
$restricted_global_title = UM()->options()->get( 'restricted_access_post_title' ); $restricted_global_title = UM()->options()->get( 'restricted_access_post_title' );
$title = stripslashes( $restricted_global_title ); $title = stripslashes( $restricted_global_title );
} }
return $title; return $title;
} }
/** /**
* Replace content of restricted posts * Replace content of restricted posts.
* *
* @param string $content * @param string $content Post Content.
* *
* @return string * @return string
*/ */
function filter_restricted_post_content( $content ) { public function filter_restricted_post_content( $content ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $content; return $content;
} }
@@ -720,6 +731,31 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $content; return $content;
} }
/**
* Filters a marker for ignoring restricted post content changes.
*
* Note: Use with caution! You can make your posts not restricted by using this filter hook.
*
* @param {bool} $ignore Marker for ignoring restricted post content changes. Default `false`. Set to `true` if you want to ignore and show real content.
* @param {int} $post_id Post ID.
*
* @return {bool} Marker for ignoring restricted post content changes.
*
* @since 2.2.3
* @hook um_ignore_restricted_content
*
* @example <caption>Leave real post content even they are restricted. By post ID (for ID = 400).</caption>
* function change_restricted_content( $ignore, $id ) {
* // your code here
* if ( 400 === $id ) {
* $ignore = true;
* }
* return $ignore;
* }
* add_filter( 'um_ignore_restricted_content', 'change_restricted_content', 10, 2 );
* @example <caption>Leave real post content even they are restricted. For all posts: site-wide logic.</caption>
* add_filter( 'um_ignore_restricted_content', '__return_true' );
*/
$ignore = apply_filters( 'um_ignore_restricted_content', false, $id ); $ignore = apply_filters( 'um_ignore_restricted_content', false, $id );
if ( $ignore ) { if ( $ignore ) {
return $content; return $content;
@@ -741,16 +777,15 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $content; return $content;
} }
/** /**
* Replace excerpt of restricted posts * Replace excerpt of restricted posts.
* *
* @param string $post_excerpt * @param string $post_excerpt Post Excerpt.
* @param \WP_Post $post * @param WP_Post $post WP_Post instance.
* *
* @return string * @return string
*/ */
function filter_restricted_post_excerpt( $post_excerpt = '', $post = null ) { public function filter_restricted_post_excerpt( $post_excerpt = '', $post = null ) {
if ( empty( $post ) ) { if ( empty( $post ) ) {
return $post_excerpt; return $post_excerpt;
} }
@@ -759,6 +794,31 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $post_excerpt; return $post_excerpt;
} }
/**
* Filters a marker for ignoring restricted post excerpt changes.
*
* Note: Use with caution! You can make your posts not restricted by using this filter hook.
*
* @param {bool} $ignore Marker for ignoring restricted post excerpt changes. Default `false`. Set to `true` if you want to ignore and show real content.
* @param {int} $post_id Post ID.
*
* @return {bool} Marker for ignoring restricted post excerpt changes.
*
* @since 2.2.3
* @hook um_ignore_restricted_excerpt
*
* @example <caption>Leave real post excerpt even they are restricted. By post ID (for ID = 400).</caption>
* function change_restricted_excerpt( $ignore, $id ) {
* // your code here
* if ( 400 === $id ) {
* $ignore = true;
* }
* return $ignore;
* }
* add_filter( 'um_ignore_restricted_excerpt', 'change_restricted_excerpt', 10, 2 );
* @example <caption>Leave real post excerpt even they are restricted. For all posts: site-wide logic.</caption>
* add_filter( 'um_ignore_restricted_excerpt', '__return_true' );
*/
$ignore = apply_filters( 'um_ignore_restricted_excerpt', false, $post->ID ); $ignore = apply_filters( 'um_ignore_restricted_excerpt', false, $post->ID );
if ( $ignore ) { if ( $ignore ) {
return $post_excerpt; return $post_excerpt;
@@ -771,16 +831,15 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $post_excerpt; return $post_excerpt;
} }
/** /**
* Hide attachment if the post is restricted * Hide attachment if the post is restricted
* *
* @param string $url * @param string $url
* @param int $attachment_id * @param int $attachment_id
* *
* @return boolean|string * @return boolean|string
*/ */
function filter_attachment( $url, $attachment_id ) { public function filter_attachment( $url, $attachment_id ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $url; return $url;
} }
@@ -788,7 +847,6 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return ( $attachment_id && $this->is_restricted( $attachment_id ) ) ? false : $url; return ( $attachment_id && $this->is_restricted( $attachment_id ) ) ? false : $url;
} }
/** /**
* Hide attachment if the post is restricted * Hide attachment if the post is restricted
* *
@@ -798,7 +856,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
* *
* @return bool * @return bool
*/ */
function filter_post_thumbnail( $has_thumbnail, $post = null, $thumbnail_id = false ) { public function filter_post_thumbnail( $has_thumbnail, $post = null, $thumbnail_id = false ) {
if ( empty( $thumbnail_id ) ) { if ( empty( $thumbnail_id ) ) {
return $has_thumbnail; return $has_thumbnail;
} }
@@ -825,14 +883,12 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $has_thumbnail; return $has_thumbnail;
} }
/** /**
* Exclude comments from restricted posts in widgets * Exclude comments from restricted posts in widgets
* *
* @param \WP_Comment_Query $query * @param WP_Comment_Query $query
*/ */
function exclude_posts_comments( $query ) { public function exclude_posts_comments( $query ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return; return;
} }
@@ -862,11 +918,10 @@ if ( ! class_exists( 'um\core\Access' ) ) {
} }
} }
/** /**
* @return array * @return array
*/ */
function get_available_comments_post_types() { public function get_available_comments_post_types() {
global $wp_taxonomies, $wpdb; global $wp_taxonomies, $wpdb;
$restricted_posts = UM()->options()->get( 'restricted_access_post_metabox' ); $restricted_posts = UM()->options()->get( 'restricted_access_post_metabox' );
@@ -907,16 +962,15 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $restricted_posts; return $restricted_posts;
} }
/** /**
* Exclude comments from comments feed * Exclude comments from comments feed
* *
* @param string $where * @param string $where
* @param \WP_Query $query * @param WP_Query $query
* *
* @return string * @return string
*/ */
function exclude_posts_comments_feed( $where, $query ) { public function exclude_posts_comments_feed( $where, $query ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $where; return $where;
} }
@@ -924,20 +978,19 @@ if ( ! class_exists( 'um\core\Access' ) ) {
$exclude_posts = $this->exclude_posts_array( true, $this->get_available_comments_post_types() ); $exclude_posts = $this->exclude_posts_array( true, $this->get_available_comments_post_types() );
if ( ! empty( $exclude_posts ) ) { if ( ! empty( $exclude_posts ) ) {
$exclude_string = implode( ',', $exclude_posts ); $exclude_string = implode( ',', $exclude_posts );
$where .= ' AND comment_post_ID NOT IN ( ' . $exclude_string . ' )'; $where .= ' AND comment_post_ID NOT IN ( ' . $exclude_string . ' )';
} }
return $where; return $where;
} }
/** /**
* @param array|object $stats * @param array|object $stats
* @param int $post_id Post ID. Can be 0 for the whole website * @param int $post_id Post ID. Can be 0 for the whole website
* *
* @return object * @return object
*/ */
function custom_comments_count_handler( $stats = array(), $post_id = 0 ) { public function custom_comments_count_handler( $stats = array(), $post_id = 0 ) {
if ( ! empty( $stats ) || current_user_can( 'administrator' ) ) { if ( ! empty( $stats ) || current_user_can( 'administrator' ) ) {
return $stats; return $stats;
} }
@@ -963,14 +1016,13 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $stats_object; return $stats_object;
} }
/** /**
* @param int $post_id * @param int $post_id
* @param array $exclude_posts * @param array $exclude_posts
* *
* @return array * @return array
*/ */
function get_comment_count( $post_id = 0, $exclude_posts = array() ) { public function get_comment_count( $post_id = 0, $exclude_posts = array() ) {
static $cache = array(); static $cache = array();
if ( isset( $cache[ $post_id ] ) ) { if ( isset( $cache[ $post_id ] ) ) {
@@ -1044,15 +1096,15 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $comment_count; return $comment_count;
} }
/** /**
* Disable comments if user has not permission to access this post * Disable comments if user has not permission to access this post
* *
* @param mixed $open * @param mixed $open
* @param int $post_id * @param int $post_id
* @return boolean *
* @return bool
*/ */
function disable_comments_open( $open, $post_id ) { public function disable_comments_open( $open, $post_id ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $open; return $open;
} }
@@ -1074,15 +1126,15 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $open; return $open;
} }
/** /**
* Disable comments if user has not permission to access this post * Disable comments if user has not permission to access this post
* *
* @param int $count * @param int $count
* @param int $post_id * @param int $post_id
* @return boolean *
* @return bool
*/ */
function disable_comments_open_number( $count, $post_id = 0 ) { public function disable_comments_open_number( $count, $post_id = 0 ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $count; return $count;
} }
@@ -1104,15 +1156,15 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $count; return $count;
} }
/** /**
* Protect Post Types in menu query * Protect Post Types in menu query
* Restrict content new logic * Restrict content new logic
* @param array $menu_items * @param array $menu_items
* @param array $args * @param array $args
*
* @return array * @return array
*/ */
function filter_menu( $menu_items, $args = array() ) { public function filter_menu( $menu_items, $args = array() ) {
//if empty //if empty
if ( empty( $menu_items ) ) { if ( empty( $menu_items ) ) {
return $menu_items; return $menu_items;
@@ -1159,14 +1211,13 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $filtered_items; return $filtered_items;
} }
/** /**
* @param $block_content * @param $block_content
* @param $block * @param $block
* *
* @return string * @return string
*/ */
function restrict_blocks( $block_content, $block ) { public function restrict_blocks( $block_content, $block ) {
if ( is_admin() ) { if ( is_admin() ) {
return $block_content; return $block_content;
} }
@@ -1246,13 +1297,12 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $block_content; return $block_content;
} }
/** /**
* @param \WP_Post $post * @param WP_Post $post
* *
* @return \WP_Post * @return WP_Post
*/ */
function maybe_replace_title( $post ) { public function maybe_replace_title( $post ) {
if ( ! UM()->options()->get( 'restricted_post_title_replace' ) ) { if ( ! UM()->options()->get( 'restricted_post_title_replace' ) ) {
return $post; return $post;
} }
@@ -1261,28 +1311,28 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $post; return $post;
} }
if ( ! is_a( $post, '\WP_Post' ) ) { if ( ! is_a( $post, WP_Post::class ) ) {
return $post; return $post;
} }
/** This filter is documented in includes/core/class-access.php */
$ignore = apply_filters( 'um_ignore_restricted_title', false, $post->ID ); $ignore = apply_filters( 'um_ignore_restricted_title', false, $post->ID );
if ( $ignore ) { if ( $ignore ) {
return $post; return $post;
} }
$restricted_global_title = UM()->options()->get( 'restricted_access_post_title' ); $restricted_global_title = UM()->options()->get( 'restricted_access_post_title' );
$post->post_title = stripslashes( $restricted_global_title ); $post->post_title = stripslashes( $restricted_global_title );
return $post; return $post;
} }
/** /**
* @param \WP_Post $nav_item * @param WP_Post $nav_item
* *
* @return \WP_Post * @return WP_Post
*/ */
function maybe_replace_nav_menu_title( $nav_item ) { public function maybe_replace_nav_menu_title( $nav_item ) {
if ( ! UM()->options()->get( 'restricted_post_title_replace' ) ) { if ( ! UM()->options()->get( 'restricted_post_title_replace' ) ) {
return $nav_item; return $nav_item;
} }
@@ -1291,7 +1341,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $nav_item; return $nav_item;
} }
if ( ! is_a( $nav_item, '\WP_Post' ) ) { if ( ! is_a( $nav_item, WP_Post::class ) ) {
return $nav_item; return $nav_item;
} }
@@ -1306,21 +1356,21 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $nav_item; return $nav_item;
} }
/** /**
* Protect Post Types in query * Protect Post Types in query
* Restrict content new logic * Restrict content new logic
* *
* @param array $posts * @param array $posts Posts query result.
* @param array|\WP_Query $query * @param array|WP_Query $query WP_Query instance.
*
* @return array * @return array
*/ */
function filter_protected_posts( $posts, $query ) { public function filter_protected_posts( $posts, $query ) {
if ( current_user_can( 'administrator' ) ) { if ( current_user_can( 'administrator' ) ) {
return $posts; return $posts;
} }
//Woocommerce AJAX fixes....remove filtration on wc-ajax which goes to Front Page // Woocommerce AJAX fixes....remove filtration on wc-ajax which goes to Front Page.
if ( ! empty( $_GET['wc-ajax'] ) && defined( 'WC_DOING_AJAX' ) && WC_DOING_AJAX ) { if ( ! empty( $_GET['wc-ajax'] ) && defined( 'WC_DOING_AJAX' ) && WC_DOING_AJAX ) {
return $posts; return $posts;
} }
@@ -1336,8 +1386,8 @@ if ( ! class_exists( 'um\core\Access' ) ) {
$is_singular = ! empty( $query->is_singular ) ? true : false; $is_singular = ! empty( $query->is_singular ) ? true : false;
} }
if ( is_object( $query ) && is_a( $query, '\WP_Query' ) && if ( is_object( $query ) && is_a( $query, WP_Query::class ) &&
( $query->is_main_query() || ! empty( $query->query_vars['um_main_query'] ) ) ) { ( $query->is_main_query() || ! empty( $query->query_vars['um_main_query'] ) ) ) {
if ( $is_singular ) { if ( $is_singular ) {
if ( ! UM()->options()->get( 'disable_restriction_pre_queries' ) && $this->is_restricted( $posts[0]->ID ) ) { if ( ! UM()->options()->get( 'disable_restriction_pre_queries' ) && $this->is_restricted( $posts[0]->ID ) ) {
$content_restriction = $this->get_post_privacy_settings( $posts[0]->ID ); $content_restriction = $this->get_post_privacy_settings( $posts[0]->ID );
@@ -1394,7 +1444,30 @@ if ( ! class_exists( 'um\core\Access' ) ) {
*/ */
do_action( 'um_access_fix_external_post_content' ); do_action( 'um_access_fix_external_post_content' );
$filtered_posts[] = $this->maybe_replace_title( $post ); $filtered_post = $this->maybe_replace_title( $post );
/**
* Filters restricted via Ultimate Member settings post instance.
*
* @param {object} $filtered_post Restricted `WP_Post` instance.
* @param {object} $post Base `WP_Post` instance before restriction settings apply.
* @param {object} $query `WP_Query` for getting posts.
*
* @return {object} Restricted `WP_Post` instance.
*
* @since 2.8.2
* @hook um_access_restricted_post_instance
*
* @example <caption>Change restricted post title to custom one.</caption>
* function restricted_post_instance( $filtered_post, $original_post, $query ) {
* // your code here
* $filtered_post->post_title = 'This post is restricted';
* return $filtered_post;
* }
* add_filter( 'um_access_restricted_post_instance', 'restricted_post_instance', 10, 3 );
*/
$filtered_post = apply_filters( 'um_access_restricted_post_instance', $filtered_post, $post, $query );
$filtered_posts[] = $filtered_post;
continue; continue;
} }
} elseif ( '1' == $restriction['_um_noaccess_action'] ) { } elseif ( '1' == $restriction['_um_noaccess_action'] ) {
@@ -1417,7 +1490,11 @@ if ( ! class_exists( 'um\core\Access' ) ) {
} }
} else { } else {
if ( empty( $restriction['_um_access_hide_from_queries'] ) || UM()->options()->get( 'disable_restriction_pre_queries' ) ) { if ( empty( $restriction['_um_access_hide_from_queries'] ) || UM()->options()->get( 'disable_restriction_pre_queries' ) ) {
$filtered_posts[] = $this->maybe_replace_title( $post ); $filtered_post = $this->maybe_replace_title( $post );
/** This filter is documented in includes/core/class-access.php */
$filtered_post = apply_filters( 'um_access_restricted_post_instance', $filtered_post, $post, $query );
$filtered_posts[] = $filtered_post;
continue; continue;
} }
} }
@@ -1427,13 +1504,12 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $filtered_posts; return $filtered_posts;
} }
/** /**
* Set custom access actions and redirection * Set custom access actions and redirection
* *
* Old global restrict content logic * Old global restrict content logic
*/ */
function template_redirect() { public function template_redirect() {
global $post, $wp_query; global $post, $wp_query;
//if we logged by administrator it can access to all content //if we logged by administrator it can access to all content
@@ -1532,11 +1608,10 @@ if ( ! class_exists( 'um\core\Access' ) ) {
$this->check_access(); $this->check_access();
} }
/** /**
* Check Blog page Content Restriction settings * Check Blog page Content Restriction settings
*/ */
function um_access_check_blog_page_settings() { public function um_access_check_blog_page_settings() {
global $wp_query; global $wp_query;
if ( ! empty( $wp_query->is_home ) && ! empty( $wp_query->is_posts_page ) ) { if ( ! empty( $wp_query->is_home ) && ! empty( $wp_query->is_posts_page ) ) {
@@ -1574,7 +1649,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
/** /**
* Check individual term Content Restriction settings * Check individual term Content Restriction settings
*/ */
function um_access_check_individual_term_settings() { public function um_access_check_individual_term_settings() {
//check only tax|tags|categories - skip archive, author, and date lists //check only tax|tags|categories - skip archive, author, and date lists
if ( ! ( is_tax() || is_tag() || is_category() ) ) { if ( ! ( is_tax() || is_tag() || is_category() ) ) {
return; return;
@@ -1627,7 +1702,6 @@ if ( ! class_exists( 'um\core\Access' ) ) {
} }
} }
/** /**
* @param $template * @param $template
* @param $type * @param $type
@@ -1635,7 +1709,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
* *
* @return string * @return string
*/ */
function taxonomy_message( $template, $type, $templates ) { public function taxonomy_message( $template, $type, $templates ) {
return UM()->locate_template( 'restricted-taxonomy.php' ); return UM()->locate_template( 'restricted-taxonomy.php' );
} }
@@ -1646,15 +1720,14 @@ if ( ! class_exists( 'um\core\Access' ) ) {
* *
* @return string * @return string
*/ */
function blog_message( $template, $type, $templates ) { public function blog_message( $template, $type, $templates ) {
return UM()->locate_template( 'restricted-blog.php' ); return UM()->locate_template( 'restricted-blog.php' );
} }
/** /**
* Check global accessible settings * Check global accessible settings
*/ */
function um_access_check_global_settings() { public function um_access_check_global_settings() {
global $post; global $post;
$curr = UM()->permalinks()->get_current_url(); $curr = UM()->permalinks()->get_current_url();
@@ -1776,13 +1849,12 @@ if ( ! class_exists( 'um\core\Access' ) ) {
} }
} }
/** /**
* Check access * Check access
* *
* @return bool * @return bool
*/ */
function check_access() { public function check_access() {
if ( $this->allow_access === true ) { if ( $this->allow_access === true ) {
return true; return true;
} }
@@ -1795,7 +1867,6 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return false; return false;
} }
/** /**
* Sends a HTTP header to limit rendering of pages to same origin iframes when loading sensitive pages. * Sends a HTTP header to limit rendering of pages to same origin iframes when loading sensitive pages.
* *
@@ -1812,7 +1883,6 @@ if ( ! class_exists( 'um\core\Access' ) ) {
} }
} }
/** /**
* Sets a custom access referer in a redirect URL * Sets a custom access referer in a redirect URL
* *
@@ -1821,7 +1891,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
* *
* @return string * @return string
*/ */
function set_referer( $url, $referer ) { public function set_referer( $url, $referer ) {
/** /**
* UM hook * UM hook
@@ -1852,13 +1922,13 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $url; return $url;
} }
/** /**
* Get privacy settings for post * Get privacy settings for post
* return false if post is not private * return false if post is not private
* Restrict content new logic * Restrict content new logic
* *
* @param \WP_Post|int $post Post ID or object * @param WP_Post|int $post Post ID or object
*
* @return bool|array * @return bool|array
*/ */
public function get_post_privacy_settings( $post ) { public function get_post_privacy_settings( $post ) {
@@ -1867,7 +1937,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return false; return false;
} }
if ( ! is_numeric( $post ) && ! is_a( $post, \WP_Post::class ) ) { if ( ! is_numeric( $post ) && ! is_a( $post, WP_Post::class ) ) {
return false; return false;
} }
@@ -2014,15 +2084,15 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return false; return false;
} }
/** /**
* Helper for checking if the user can some of the roles array * Helper for checking if the user can some of the roles array
* *
* @param $user_id * @param $user_id
* @param $roles * @param $roles
*
* @return bool * @return bool
*/ */
function user_can( $user_id, $roles ) { public function user_can( $user_id, $roles ) {
$user_can = false; $user_can = false;
if ( ! empty( $roles ) ) { if ( ! empty( $roles ) ) {
@@ -2037,7 +2107,6 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $user_can; return $user_can;
} }
/** /**
* Helper for 3rd-party integrations with content restriction settings * Helper for 3rd-party integrations with content restriction settings
* *
@@ -2045,7 +2114,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
* *
* @return bool * @return bool
*/ */
function um_custom_restriction( $restriction ) { public function um_custom_restriction( $restriction ) {
/** /**
* UM hook * UM hook
* *
@@ -2070,16 +2139,16 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return apply_filters( 'um_custom_restriction', true, $restriction ); return apply_filters( 'um_custom_restriction', true, $restriction );
} }
/** /**
* Is post restricted? * Is post restricted?
* *
* @param int $post_id * @param int $post_id
* @param bool $on_single_page * @param bool $on_single_page
* @param bool $ignore_cache * @param bool $ignore_cache
*
* @return bool * @return bool
*/ */
function is_restricted( $post_id, $on_single_page = false, $ignore_cache = false ) { public function is_restricted( $post_id, $on_single_page = false, $ignore_cache = false ) {
// break for incorrect post // break for incorrect post
if ( empty( $post_id ) ) { if ( empty( $post_id ) ) {
return false; return false;
@@ -2169,16 +2238,16 @@ if ( ! class_exists( 'um\core\Access' ) ) {
return $restricted; return $restricted;
} }
/** /**
* Is term restricted? * Is term restricted?
* *
* @param int $term_id * @param int $term_id
* @param bool $on_term_page * @param bool $on_term_page
* @param bool $ignore_cache * @param bool $ignore_cache
*
* @return bool * @return bool
*/ */
function is_restricted_term( $term_id, $on_term_page = false, $ignore_cache = false ) { public function is_restricted_term( $term_id, $on_term_page = false, $ignore_cache = false ) {
static $cache = array(); static $cache = array();
if ( isset( $cache[ $term_id ] ) && ! $ignore_cache ) { if ( isset( $cache[ $term_id ] ) && ! $ignore_cache ) {