/** * Restricting content to registered users and or user roles * * @since * @access public * * @param $atts * @param $content * @return mixed|void */ function rcp_restrict_shortcode($atts, $content = null) { $atts = shortcode_atts(array('userlevel' => 'none', 'message' => '', 'paid' => false, 'level' => 0, 'subscription' => ''), $atts, 'restrict'); global $rcp_options, $user_ID; if (strlen(trim($atts['message'])) > 0) { $teaser = $atts['message']; } elseif ($atts['paid']) { $teaser = $rcp_options['paid_message']; } else { $teaser = $rcp_options['free_message']; } $subscription = array_map('trim', explode(',', $atts['subscription'])); $has_access = false; if ($atts['paid']) { if (rcp_is_active($user_ID) && rcp_user_has_access($user_ID, $atts['level'])) { $has_access = true; } $classes = 'rcp_restricted rcp_paid_only'; } else { if (rcp_user_has_access($user_ID, $atts['level'])) { $has_access = true; } $classes = 'rcp_restricted'; } if (!empty($subscription) && !empty($subscription[0])) { if (!in_array(rcp_get_subscription_id($user_ID), $subscription) || in_array(rcp_get_subscription_id($user_ID), $subscription) && rcp_is_expired($user_ID)) { $has_access = false; } } if ($atts['userlevel'] === 'admin' && !current_user_can('switch_themes')) { $has_access = false; } if ($atts['userlevel'] === 'editor' && !current_user_can('moderate_comments')) { $has_access = false; } if ($atts['userlevel'] === 'author' && !current_user_can('upload_files')) { $has_access = false; } if ($atts['userlevel'] === 'contributor' && !current_user_can('edit_posts')) { $has_access = false; } if ($atts['userlevel'] === 'subscriber' && !current_user_can('read')) { $has_access = false; } if ($atts['userlevel'] === 'none' && !is_user_logged_in()) { $has_access = false; } if (current_user_can('manage_options')) { $has_access = true; } $has_access = (bool) apply_filters('rcp_restrict_shortcode_has_access', $has_access, $user_ID, $atts); if ($has_access) { return apply_filters('rcp_restrict_shortcode_return', $content); } else { return '<div class="' . $classes . '">' . rcp_format_teaser($teaser) . '</div>'; } }
/** * Check if current post is locked and if user has access to current content. * * @return bool */ function stag_rcp_user_has_no_access() { if (!stag_is_rcp_active()) { return false; } global $post, $user_ID, $rcp_options; $access_level = get_post_meta($post->ID, 'rcp_access_level', true); if (rcp_is_paid_content($post->ID)) { if (!rcp_is_paid_user($user_ID) || !rcp_user_has_access($user_ID, $access_level) && $access_level > 0) { return true; } } return false; }
/** * Determines if the member can access current content * * @access public * @since 2.1 */ public function can_access($post_id = 0) { $subscription_levels = rcp_get_content_subscription_levels($post_id); $access_level = get_post_meta($post_id, 'rcp_access_level', true); $sub_id = $this->get_subscription_id(); // Assume the user can until proven false $ret = true; if (rcp_is_paid_content($post_id) && $this->is_expired()) { $ret = false; } if (!empty($subscription_levels)) { if (is_string($subscription_levels)) { switch ($subscription_levels) { case 'any': $ret = !empty($sub_id) && !$this->is_expired(); break; case 'any-paid': $ret = $this->is_active(); break; } } else { if (in_array($sub_id, $subscription_levels)) { $needs_paid = false; foreach ($subscription_levels as $level) { $price = rcp_get_subscription_price($level); if (!empty($price) && $price > 0) { $needs_paid = true; } } if ($needs_paid) { $ret = $this->is_active(); } else { $ret = true; } } else { $ret = false; } } } if (!rcp_user_has_access($this->ID, $access_level) && $access_level > 0) { $ret = false; } if (user_can($this->ID, 'manage_options')) { $ret = true; } return apply_filters('rcp_member_can_access', $ret, $this->ID, $post_id, $this); }
function rcp_restrict_shortcode( $atts, $content = null ) { extract( shortcode_atts( array( 'userlevel' => 'none', 'message' => '', 'paid' => false, 'level' => 0, 'subscription' => '' ), $atts ) ); global $rcp_options, $user_ID; if( strlen( trim( $message ) ) > 0 ) { $teaser = $message; } elseif( $paid ) { $teaser = $rcp_options['paid_message']; } else { $teaser = $rcp_options['free_message']; } $subscription = explode( ',', $subscription ); if( $paid ) { $has_access = false; if( rcp_is_active( $user_ID ) && rcp_user_has_access( $user_ID, $level ) ) { $has_access = true; if( ! empty( $subscription ) && ! empty( $subscription[0] ) ) { if( ! in_array( rcp_get_subscription_id( $user_ID ), $subscription ) ) { $has_access = false; } } } if ( $userlevel == 'admin' && current_user_can( 'switch_themes' ) && $has_access ) { return do_shortcode( wpautop( $content ) ); } if ( $userlevel == 'editor' && current_user_can( 'moderate_comments' ) && $has_access ) { return do_shortcode( wpautop( $content ) ); } if ( $userlevel == 'author' && current_user_can( 'upload_files' ) && $has_access ) { return do_shortcode( wpautop( $content ) ); } if ( $userlevel == 'contributor' && current_user_can( 'edit_posts' ) && $has_access ) { return do_shortcode( wpautop( $content ) ); } if ( $userlevel == 'subscriber' && current_user_can( 'read' ) && $has_access ) { return do_shortcode( wpautop( $content ) ); } if ( $userlevel == 'none' && is_user_logged_in() && $has_access ) { return do_shortcode( wpautop( $content ) ); } else { return '<div class="rcp_restricted rcp_paid_only">' . rcp_format_teaser($teaser) . '</div>'; } } else { $has_access = false; if(rcp_user_has_access($user_ID, $level)) { $has_access = true; if( ! empty( $subscription ) && ! empty( $subscription[0] ) ) { if( in_array( rcp_get_subscription_id( $user_ID ), $subscription ) ) { $has_access = false; } } } if ( $userlevel == 'admin' && current_user_can( 'switch_themes' ) && $has_access ) { return do_shortcode( wpautop( $content ) ); } elseif ( $userlevel == 'editor' && current_user_can( 'moderate_comments' ) && $has_access ) { return do_shortcode( wpautop( $content ) ); } elseif ( $userlevel == 'author' && current_user_can( 'upload_files' ) && $has_access ) { return do_shortcode( wpautop( $content ) ); } elseif ( $userlevel == 'contributor' && current_user_can( 'edit_posts' ) && $has_access ) { return do_shortcode( wpautop( $content ) ); } elseif ( $userlevel == 'subscriber' && current_user_can( 'read' ) && $has_access ) { return do_shortcode( wpautop( $content ) ); } elseif ( $userlevel == 'none' && is_user_logged_in() && $has_access ) { return do_shortcode( wpautop( $content ) ); } else { return '<div class="rcp_restricted">' . do_shortcode( wpautop( $teaser ) ) . '</div>'; } } }
function rcp_display_message_to_non_loggged_in_users($content) { global $rcp_options, $post, $user_ID; $message = isset($rcp_options['free_message']) ? $rcp_options['free_message'] : ''; $paid_message = isset($rcp_options['paid_message']) ? $rcp_options['paid_message'] : ''; if (rcp_is_paid_content($post->ID)) { $message = $paid_message; } $user_level = get_post_meta($post->ID, 'rcp_user_level', true); $access_level = get_post_meta($post->ID, 'rcp_access_level', true); $has_access = false; if (rcp_user_has_access($user_ID, $access_level)) { $has_access = true; } if (!is_user_logged_in() && ($user_level == 'Administrator' || $user_level == 'Editor' || $user_level == 'Author' || $user_level == 'Contributor' || $user_level == 'Subscriber') && $has_access) { return rcp_format_teaser($message); } // return the content unfilitered return $content; }
/** * Determines if the member can access current content * * @access public * @since 2.1 */ public function can_access($post_id = 0) { $subscription_levels = rcp_get_content_subscription_levels($post_id); $access_level = get_post_meta($post_id, 'rcp_access_level', true); // Assume the user can until proven false $ret = true; if (rcp_is_paid_content($post_id) && !$this->is_active()) { $ret = false; } if (!rcp_user_has_access($this->ID, $access_level) && $access_level > 0) { $ret = false; } if (!empty($subscription_levels)) { if (!in_array($this->get_subscription_id(), $subscription_levels) && !user_can($this->ID, 'manage_options')) { $ret = false; } } return apply_filters('rcp_member_can_access', $ret, $this->ID, $post_id, $this); }
/** * Loads the restricted content template if required. * * @access public * @since 2.5 */ public function hide_template($template, $slug, $name) { $product_id = get_the_ID(); if (!is_singular('product')) { return $template; } if ('content-single-product' !== $slug . '-' . $name) { return $template; } if (current_user_can('edit_post', $product_id)) { return $template; } $active_only = get_post_meta($product_id, '_rcp_woo_active_to_view', true); $levels = (array) get_post_meta($product_id, '_rcp_woo_subscription_levels_to_view', true); $access_level = get_post_meta($product_id, '_rcp_woo_access_level_to_view', true); $product_cat = rcp_is_post_taxonomy_restricted($product_id, 'product_cat'); $product_tag = rcp_is_post_taxonomy_restricted($product_id, 'product_tag'); /** * rcp_is_post_taxonomy_restricted() returns: * - true when restrictions are found for the current user * - false when restrictions are not found for the current user * - -1 when no terms are assigned, for which we don't care. * We're normalizing the value here. If the value is false, * the user has already passed the restriction checks. */ $cat_restricted = true === $product_cat ? true : false; $tag_restricted = true === $product_tag ? true : false; // Return early if no restrictions if (!$active_only && empty($levels[0]) && !$access_level && !$cat_restricted && !$tag_restricted) { return $template; } $visible = true; // Active subscription setting if ($active_only && !rcp_is_active()) { $visible = false; } // Subscription level setting if (!in_array(rcp_get_subscription_id(), $levels)) { $visible = false; } // User level setting if ($access_level && rcp_user_has_access(get_current_user_id(), $access_level)) { $visible = false; } if ($visible) { return $template; } return rcp_get_template_part('woocommerce', 'single-no-access', false); }
/** * Restrict the visibility of a product * * @access public * @since 2.2 */ public function is_visible($ret, $product_id) { if ($ret) { $has_access = true; $active_only = get_post_meta($product_id, '_rcp_woo_active_to_view', true); $levels = (array) get_post_meta($product_id, '_rcp_woo_subscription_levels_to_view', true); $access_level = get_post_meta($product_id, '_rcp_woo_access_level_to_view', true); if ($active_only) { if (!rcp_is_active()) { $has_access = false; } } if (is_array($levels) && !empty($array[0])) { if (!in_array(rcp_get_subscription_id(), $levels)) { $has_access = false; } } if ($access_level) { if (!rcp_user_has_access(get_current_user_id(), $access_level)) { $has_access = false; } } $ret = $has_access; } return $ret; }