*/ class ARC_Public { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ // public function enqueue_styles() { // // wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/arc-public.css', array(), $this->version, 'all' ); // // } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ // public function enqueue_scripts() { // // wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/arc-public.js', array( 'jquery' ), $this->version, false ); // } /** * check the WP_Term object to be restricted * * @since 1.0.0 */ private function is_term_restricted($wp_term) { $is_restricted = ( get_term_meta($wp_term->term_id, 'arc_restricted_category_value', true) && get_term_meta($wp_term->term_id, 'arc_restricted_category_value', true) === 'yes' ) ? true : false; return $is_restricted; } /** * check the WP_Post object to be restricted * * @since 1.0.0 */ private function is_post_restricted($wp_post) { $is_restricted = ( get_post_meta($wp_post->ID, 'arc_restricted_post', true) && (bool) get_post_meta($wp_post->ID, 'arc_restricted_post', true) === true ) ? true : false; return $is_restricted; } /** * redirect to login with option to back to requested page after logging in * * @since 1.0.0 */ private function redirect_login($wp_obj) { wp_redirect(wp_login_url(get_permalink($wp_obj->ID))); } /** * check for content restrictions and redirect to login page * * @since 1.0.0 */ public function redirect_restricted_content_to_login() { global $wp_query; if ( is_user_logged_in() ) return true; // do nothing for logged in users $queried_object = get_queried_object(); $is_content_restricted = false; if ( $queried_object instanceof WP_Term ) { // category requested $is_content_restricted = $this->is_term_restricted($queried_object); } elseif ( $queried_object instanceof WP_Post ) { // post requested $is_content_restricted = $this->is_post_restricted($queried_object); // check also its parent categories if ( !$is_content_restricted ) { $parent_terms = wp_get_post_terms($queried_object->ID, array('category')); if ( is_array($parent_terms) && sizeof($parent_terms) > 0 ) { foreach ($parent_terms as $one_term) { $is_content_restricted = $this->is_term_restricted($one_term); if ( $is_content_restricted ) break; } } } } // redirect to login if content restricted if ( $is_content_restricted && !is_user_logged_in() ) $this->redirect_login($queried_object); return true; } /** * update get posts query to prevent show up restricted content * prevent from displaying restricted posts on hompage and feeds * * @since 1.2.0 */ public function hide_rectricted_posts_in_query($query) { if ( ! is_admin() && $query->is_main_query() && !is_user_logged_in() ) { // Not a query for an admin page. // It's the main query for a front end page of your site. // User NOT logged in if ( !is_single() ) { // do not apply this filter for single posts $meta_query = array( 'relation' => 'OR', array( 'key' => 'arc_restricted_post', 'value' => 1, 'compare' => '!=', 'type' => 'NUMERIC' ), array( 'key' => 'arc_restricted_post', 'compare' => 'NOT EXISTS', ) ); $query->set( 'meta_query', $meta_query ); } } return $query; } /** * prevent from displaying restricted posts in recent posts widget * * @since 1.2.0 */ public function hide_rectricted_posts_in_recent_posts($query_args) { if ( ! is_admin() && !is_user_logged_in() ) { // Not a query for an admin page. // User NOT logged in $query_args['meta_query'] = array( 'relation' => 'OR', array( 'key' => 'arc_restricted_post', 'value' => 1, 'compare' => '!=', 'type' => 'NUMERIC' ), array( 'key' => 'arc_restricted_post', 'compare' => 'NOT EXISTS', ) ); } return $query_args; } /** * return list of post ids which are restricted, for internal use only * * @since 1.2.0 */ private function get_restricted_posts_ids() { $query_args = array( 'posts_per_page' => -1, 'offset' => 0, 'fields' => 'ids', 'meta_query' => array( array( 'key' => 'arc_restricted_post', 'value' => 1, 'compare' => '=', 'type' => 'NUMERIC' ) ) ); $postslist = get_posts( $query_args ); return $postslist; } /** * prevent from displaying restricted posts in recent comments widget * * @since 1.2.0 */ public function hide_rectricted_posts_in_recent_comments($query_args) { $restricted_posts = $this->get_restricted_posts_ids(); if ( ! is_admin() && !is_user_logged_in() && is_array($restricted_posts) && sizeof($restricted_posts) > 0 ) { // Not a query for an admin page. // User NOT logged in $query_args['post__not_in'] = $restricted_posts; } return $query_args; } }