/** * Initialize the Shortcodes after we have determined the current user. * * @since 1.0.0 */ public function init() { // By default assume no content for the protected-content code add_shortcode(MS_Helper_Shortcode::SCODE_PROTECTED, array($this, '__return_null')); if (MS_Plugin::is_enabled()) { add_shortcode(MS_Helper_Shortcode::SCODE_REGISTER_USER, array($this, 'membership_register_user')); add_shortcode(MS_Helper_Shortcode::SCODE_SIGNUP, array($this, 'membership_signup')); add_shortcode(MS_Helper_Shortcode::SCODE_MS_TITLE, array($this, 'membership_title')); add_shortcode(MS_Helper_Shortcode::SCODE_MS_PRICE, array($this, 'membership_price')); add_shortcode(MS_Helper_Shortcode::SCODE_MS_BUY, array($this, 'membership_buy')); add_shortcode(MS_Helper_Shortcode::SCODE_MS_DETAILS, array($this, 'membership_details')); add_shortcode(MS_Helper_Shortcode::SCODE_LOGIN, array($this, 'membership_login')); add_shortcode(MS_Helper_Shortcode::SCODE_LOGOUT, array($this, 'membership_logout')); add_shortcode(MS_Helper_Shortcode::SCODE_MS_ACCOUNT, array($this, 'membership_account')); add_shortcode(MS_Helper_Shortcode::SCODE_MS_ACCOUNT_LINK, array($this, 'membership_account_link')); add_shortcode(MS_Helper_Shortcode::SCODE_MS_INVOICE, array($this, 'membership_invoice')); add_shortcode(MS_Helper_Shortcode::SCODE_NOTE, array($this, 'ms_note')); add_shortcode(MS_Helper_Shortcode::SCODE_GREEN_NOTE, array($this, 'ms_green_note')); add_shortcode(MS_Helper_Shortcode::SCODE_RED_NOTE, array($this, 'ms_red_note')); add_shortcode(MS_Helper_Shortcode::SCODE_USER, array($this, 'show_to_user')); add_shortcode(MS_Helper_Shortcode::SCODE_MEMBER_INFO, array($this, 'ms_member_info')); if (MS_Model_Member::is_normal_admin()) { add_shortcode(MS_Rule_Shortcode_Model::PROTECT_CONTENT_SHORTCODE, array('MS_Rule_Shortcode_Model', 'debug_protect_content_shortcode')); } } else { $shortcodes = array(MS_Helper_Shortcode::SCODE_REGISTER_USER, MS_Helper_Shortcode::SCODE_SIGNUP, MS_Helper_Shortcode::SCODE_MS_TITLE, MS_Helper_Shortcode::SCODE_MS_PRICE, MS_Helper_Shortcode::SCODE_MS_DETAILS, MS_Helper_Shortcode::SCODE_LOGIN, MS_Helper_Shortcode::SCODE_LOGOUT, MS_Helper_Shortcode::SCODE_MS_ACCOUNT, MS_Helper_Shortcode::SCODE_MS_ACCOUNT_LINK, MS_Helper_Shortcode::SCODE_MS_INVOICE, MS_Helper_Shortcode::SCODE_NOTE, MS_Helper_Shortcode::SCODE_GREEN_NOTE, MS_Helper_Shortcode::SCODE_RED_NOTE); foreach ($shortcodes as $shortcode) { add_shortcode($shortcode, array($this, 'ms_no_value')); } add_shortcode(MS_Rule_Shortcode_Model::PROTECT_CONTENT_SHORTCODE, array($this, 'hide_shortcode')); add_shortcode(MS_Helper_Shortcode::SCODE_USER, array($this, 'hide_shortcode')); } }
/** * Verify access to the current content. * * @since 1.0.0 * * @param string $id The content id to verify access. * @param bool $admin_has_access Default true: Admin will always have access, * no matter how protection is set up. False will ignore the * admin status and check protection rules normaly. * * @return boolean TRUE if has access, FALSE otherwise. */ public function has_access($id, $admin_has_access = true) { if ($admin_has_access && MS_Model_Member::is_normal_admin()) { return true; } /* * $access will be one of these: * - TRUE .. Access explicitly granted * - FALSE .. Access explicitly denied * - NULL .. Access implicitly allowed (i.e. "not-denied") */ $access = $this->get_rule_value($id); if ($this->is_base_rule) { /* * Base rule .. * - The meaning of TRUE/FALSE is inverted * - NULL is always "allowed" */ $access = !$access; } else { // Apply dripped-content rules if neccessary. if ($access && $this->has_dripped_rules($id)) { if (!empty($this->_subscription_id)) { $subscription = MS_Factory::load('MS_Model_Relationship', $this->_subscription_id); $start_date = $subscription->start_date; } else { $start_date = null; } $avail_date = $this->get_dripped_avail_date($id, $start_date); $now = MS_Helper_Period::current_date(); $access = strtotime($now) >= strtotime($avail_date); } if (MS_Model_Rule::RULE_VALUE_UNDEFINED === $access) { // NULL .. "not-denied" is translated to "allowed" $access = true; } } // At this point $access can either be TRUE or FALSE, not NULL! $access = (bool) $access; return apply_filters('ms_rule_has_access', $access, $id, $this->rule_type, $this); }
/** * Checks if the current user can access the specified attachment. * * @since 1.0.0 * @param int $attachment_id * @return bool */ public function can_access_file($attachment_id) { $access = false; if (MS_Model_Member::is_normal_admin()) { return true; } /* * Default protection mode: * Protect Attachments based on the parent post. */ $parent_id = get_post_field('post_parent', $attachment_id); if (!$parent_id) { $access = true; } else { $member = MS_Model_Member::get_current_member(); foreach ($member->subscriptions as $subscription) { $membership = $subscription->get_membership(); $access = $membership->has_access_to_post($parent_id); if ($access) { break; } } } return apply_filters('ms_rule_media_can_access_file', $access, $attachment_id); }
/** * Verify access to post. * * Verify membership rules hierarchy for specific post or CPT. * * @since 1.0.0 * @api * * @param int $post_id ID of specific post * @return boolean True if has access to current page. Default is false. */ public function has_access_to_post($post_id) { $has_access = null; if (MS_Model_Member::is_normal_admin()) { return true; } if (!empty($post_id)) { $post = get_post($post_id); if ('attachment' === $post->post_type) { $post_id = get_post_field('post_parent', $post_id); } } // If 'has access' is found in the hierarchy, it does have access. $rules = $this->get_rules_hierarchy(); foreach ($rules as $rule) { $rule->prepare_rule($subscription); // url groups have final decision if (MS_Rule_Url::RULE_ID == $rule->rule_type && $rule->has_rule_for_post($post_id)) { $has_access = $rule->has_access($post_id); break; } else { $rule_access = $rule->has_access($post_id); if (null !== $rule_access) { $has_access = $rule_access; } } if ($has_access) { break; } } if (null === $has_access) { // The post is not denied by any rule, so allow access. $has_access = true; } return apply_filters('ms_model_membership_has_access_to_post', $has_access, $this); }
/** * Checks if the current user can access the specified attachment. * * @since 1.0.0 * @param int $attachment_id * @return bool */ public function can_access_file($attachment_id) { $access = false; if (MS_Model_Member::is_normal_admin()) { return true; } if (!MS_Model_Addon::is_enabled(MS_Addon_Mediafiles::ID)) { /* * Default protection mode: * Protect Attachments based on the parent post. */ $parent_id = get_post_field('post_parent', $attachment_id); if (!$parent_id) { $access = true; } else { $member = MS_Model_Member::get_current_member(); foreach ($member->subscriptions as $subscription) { $membership = $subscription->get_membership(); $access = $membership->has_access_to_post($parent_id); if ($access) { break; } } } } else { /* * Advanced protection mode (via Add-on): * Each Attachment can be protected individually. */ $member = MS_Model_Member::get_current_member(); foreach ($member->subscriptions as $subscription) { $rule = $subscription->get_membership()->get_rule(MS_Rule_Media::RULE_ID); $access = $rule->has_access($attachment_id); if ($access) { break; } } } return apply_filters('ms_rule_media_can_access_file', $access, $attachment_id); }