*/ 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; } }