/**
  * Prepare the list and choose which items to display.
  *
  * This is the core logic of the listtable parent class!
  *
  * @since  1.0.0
  */
 public function prepare_items()
 {
     $args = null;
     $this->_column_headers = array($this->get_columns(), $this->get_hidden_columns(), $this->get_sortable_columns());
     // Some columns have a pre-defined title that cannot be changed.
     if (isset($this->_column_headers[0]['cb'])) {
         $this->_column_headers[0]['cb'] = '<input type="checkbox" />';
     }
     $is_dripped = in_array($this->model->rule_type, MS_Model_Rule::get_dripped_rule_types());
     if ($is_dripped) {
         $this->_column_headers[0]['dripped'] = __('Reveal Content', 'membership2');
     } else {
         unset($this->_column_headers[0]['dripped']);
     }
     if (isset($this->_column_headers[0]['access'])) {
         $this->_column_headers[0]['access'] = __('Who Has Access', 'membership2');
     }
     // Initialize current pagination Page
     $per_page = $this->get_items_per_page("{$this->id}_per_page", self::DEFAULT_PAGE_SIZE);
     /**
      * Custom filter to modify the items on all Protection Rule list tables.
      *
      * @since 1.0.1.0
      * @var   int
      */
     $per_page = apply_filters('rule_items_per_page', $per_page, $this->id);
     $current_page = $this->get_pagenum();
     $args = array('posts_per_page' => $per_page, 'number' => $per_page, 'offset' => ($current_page - 1) * $per_page);
     // Add a status filter
     if (!empty($_GET['status'])) {
         $args['rule_status'] = $_GET['status'];
     }
     // Search string.
     if (!empty($_REQUEST['s'])) {
         $this->search_string = $_REQUEST['s'];
         $args['s'] = $_REQUEST['s'];
         $args['posts_per_page'] = -1;
         $args['number'] = false;
         $args['offset'] = 0;
     }
     // Month filter.
     if (!empty($_REQUEST['m']) && 6 == strlen($_REQUEST['m'])) {
         $args['year'] = substr($_REQUEST['m'], 0, 4);
         $args['monthnum'] = substr($_REQUEST['m'], 5, 2);
     }
     // If a membership is filtered then only show protected items
     if (!empty($_REQUEST['membership_id'])) {
         $args['membership_id'] = $_REQUEST['membership_id'];
     }
     // Allow other helper list tables to customize the args array.
     $args = $this->prepare_items_args($args);
     // Count items
     $total_items = $this->model->get_content_count($args);
     // List available items
     $this->items = apply_filters("ms_rule_{$this->id}_items", $this->model->get_contents($args));
     // Save the args for use in later functions
     $this->prepared_args = $args;
     // Prepare the table pagination
     $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page));
 }
 /**
  * Set initial protection.
  *
  * @since  1.0.0
  */
 public function protect_content()
 {
     parent::protect_content();
     /*
      * Replace the "menu" attribute of the wp_nav_menu() call
      */
     $this->add_filter('wp_nav_menu_args', 'replace_menus');
 }
 /**
  * Set initial protection.
  *
  * @since  1.0.0
  */
 public function protect_content()
 {
     parent::protect_content();
     /*
      * This filter is called by get_theme_mod() in wp-includes/theme.php
      * get_theme_mod( 'nav_menu_locations' ) returns an array of theme
      * menu-areas and assigned custom menus. Our function modifies the
      * assigned menus to reflect the specified matching table.
      */
     $this->add_filter('theme_mod_nav_menu_locations', 'replace_menus');
 }
 /**
  * Set initial protection.
  *
  * @since  1.0.0
  */
 public function protect_admin_content()
 {
     parent::protect_admin_content();
     /*
      * Find out which menu items are allowed.
      */
     $this->add_filter('custom_menu_order', 'prepare_protection', 1);
     /*
      * Remove menu items that are not allowed.
      */
     $this->add_filter('custom_menu_order', 'protect_menus', 10);
 }
 /**
  * Set initial protection.
  *
  * Add [ms-protect-content] shortcode to protect membership content inside post.
  *
  * @since  1.0.0
  */
 public function protect_content()
 {
     parent::protect_content();
     self::$membership_ids[] = $this->membership_id;
     add_shortcode(self::PROTECT_CONTENT_SHORTCODE, array(__CLASS__, 'protect_content_shortcode'));
     if (MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_SHORTCODE)) {
         global $shortcode_tags;
         $exclude = MS_Helper_Shortcode::get_membership_shortcodes();
         foreach ($shortcode_tags as $shortcode => $callback_funciton) {
             if (in_array($shortcode, $exclude)) {
                 continue;
             }
             if (!parent::has_access($shortcode)) {
                 $shortcode_tags[$shortcode] = array(&$this, 'do_protected_shortcode');
             }
         }
     }
 }
 /**
  * Set initial protection.
  *
  * @since  1.0.0
  */
 public function protect_admin_content()
 {
     parent::protect_admin_content();
     $this->add_filter('user_has_cap', 'prepare_caps', 1, 4);
     $this->add_filter('user_has_cap', 'modify_caps', 10, 4);
 }
 /**
  * Get the default query args.
  *
  * @since  1.0.0
  *
  * @param string $args The query post args.
  *     @see @link http://codex.wordpress.org/Class_Reference/WP_Query
  * @return array The parsed args.
  */
 public function get_query_args($args = null)
 {
     $defaults = array('orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'attachment', 'post_status' => 'any');
     $args = wp_parse_args($args, $defaults);
     return parent::prepare_query_args($args, 'get_posts');
 }
 /**
  * Get the default query args.
  *
  * @since  1.0.0
  *
  * @param string $args The query post args.
  * @return array The parsed args.
  */
 public function get_query_args($args = null)
 {
     return parent::prepare_query_args($args, 'get_categories');
 }
 /**
  * Verify access to the current content.
  *
  * @since  1.0.0
  *
  * @param string $id The content id to verify access.
  * @return bool|null True if has access, false otherwise.
  *     Null means: Rule not relevant for current page.
  */
 public function has_access($id, $admin_has_access = true)
 {
     $has_access = null;
     // Only verify permission if NOT ruled by cpt post by post.
     if (MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_CPT_POST_BY_POST)) {
         return $has_access;
     }
     if (!empty($id)) {
         $post = get_post($id);
     } else {
         $post = get_queried_object();
     }
     $post_type = !empty($post->post_type) ? $post->post_type : '';
     if (empty($post_type) && !empty($post->query_var)) {
         $post_type = $post->query_var;
     }
     if (in_array($post_type, self::get_ms_post_types())) {
         // Always allow access to Membership2 pages.
         $has_access = true;
     } elseif (in_array($post_type, self::get_custom_post_types())) {
         // Custom post type
         $has_access = parent::has_access($post_type, $admin_has_access);
     } else {
         // WordPress core pages are ignored by this rule.
         $has_access = null;
     }
     return apply_filters('ms_rule_cptgroup_model_has_access', $has_access, $id, $this);
 }
 /**
  * Merge Membership2 rules.
  *
  * Merge every rule model with Membership2/visitor membership rules.
  * This ensure rules are consistent with Membership2 rules.
  *
  * @since  1.0.0
  * @internal
  */
 public function merge_protection_rules()
 {
     if ($this->is_base()) {
         // This is the visitor membership, no need to merge anything.
         return;
     }
     $base_rules = self::get_base()->_rules;
     foreach ($base_rules as $key => $base_rule) {
         try {
             // Key could be "type" of "site:type" format.
             $rule_type = MS_Rule::rule_type($key);
             $rule = $this->get_rule($rule_type);
             $rule->protect_undefined_items($base_rule, true);
             $this->set_rule($rule_type, $rule);
         } catch (Exception $e) {
             MS_Helper_Debug::log($e);
         }
     }
     $this->_rules = apply_filters('ms_model_membership_merge_protection_rules', $this->_rules, $this);
 }
 /**
  * Get the default query args.
  *
  * @since  1.0.0
  *
  * @param string $args The query post args.
  *     @see @link http://codex.wordpress.org/Class_Reference/WP_Query
  * @return array The parsed args.
  */
 public function get_query_args($args = null)
 {
     return parent::prepare_query_args($args, 'wp_query');
 }
 /**
  * Get WP_Query object arguments.
  *
  * Return default search arguments.
  *
  * @since  1.0.0
  *
  * @param $args The query post args
  *     @see @link http://codex.wordpress.org/Function_Reference/get_pages
  * @return array $args The parsed args.
  */
 public function get_query_args($args = null)
 {
     $cpts = MS_Rule_CptGroup_Model::get_custom_post_types();
     if (!isset($args['post_type'])) {
         $args['post_type'] = $cpts;
     }
     return parent::prepare_query_args($args, 'get_posts');
 }
 /**
  * Set initial protection.
  *
  * @since  1.0.0
  */
 public function protect_content()
 {
     parent::protect_content();
     // ********* COMMENTS **********
     // No comments on special pages (signup, account, ...)
     $this->add_filter('the_content', 'check_special_page');
     /*
      * We find the public comment access once.
      * This is the access ganted to guests or memberships that do not define
      * an explicit comment access rule.
      */
     if (null === self::$comment_public) {
         $base_rule = MS_Model_Membership::get_base()->get_rule($this->rule_type);
         if (null === $base_rule->get_rule_value(self::COMMENT_WRITE)) {
             self::$comment_public = self::COMMENT_WRITE;
         } elseif (null === $base_rule->get_rule_value(self::COMMENT_READ)) {
             self::$comment_public = self::COMMENT_READ;
         } else {
             self::$comment_public = self::COMMENT_NO_ACCESS;
         }
     }
     // Find the most generous comment access rule.
     $has_full = $this->get_rule_value(self::COMMENT_WRITE);
     $has_read = $this->get_rule_value(self::COMMENT_READ);
     $has_none = $this->get_rule_value(self::COMMENT_NO_ACCESS);
     if (true === $has_full) {
         // Membership allows full comment access.
         self::$comment_access = self::COMMENT_WRITE;
     } elseif (true === $has_read) {
         // Membership allows read-only access.
         if (self::$comment_access == self::COMMENT_NO_ACCESS) {
             self::$comment_access = self::COMMENT_READ;
         }
     } elseif (true === $has_none) {
         // Membership does not allow any comment access.
         // (no change, this is the default access level)
     } else {
         // This membership does not define a comment access: Use public access!
         self::$comment_access = self::$comment_public;
     }
     $this->add_action('ms_setup_protection_done', 'protect_comments');
     // ********** READ MORE **********
     $this->protection_message = MS_Plugin::instance()->settings->get_protection_message(MS_Model_Settings::PROTECTION_MSG_MORE_TAG, $this->membership_id);
     if (!parent::has_access(self::MORE_LIMIT)) {
         $this->add_filter('the_content_more_link', 'show_moretag_protection', 99, 2);
         $this->add_filter('the_content', 'replace_more_tag_content', 1);
         $this->add_filter('the_content_feed', 'replace_more_tag_content', 1);
     }
 }
 /**
  * Checks the ability to create groups.
  *
  * Related Action Hooks:
  * - bp_user_can_create_groups
  *
  * @since  1.0.0
  *
  * @param string $can_create The initial access.
  * @return string The initial template if current user can create groups, otherwise blocking message.
  */
 public function protect_create_bp_group($can_create)
 {
     $can_create = false;
     if (parent::has_access(MS_Addon_BuddyPress_Rule::PROTECT_GROUP_CREATION)) {
         $can_create = true;
     }
     return apply_filters('ms_rule_buddypress_protect_create_bp_group', $can_create, $this);
 }
 /**
  * Checks if the specified menu-ID is allowed by this rule.
  *
  * @since  1.0.0
  *
  * @param  object $item The menu item object.
  * @return bool
  */
 protected function can_access_menu($item, $admin_has_access = true)
 {
     $result = false;
     if (parent::has_access($item->ID, $admin_has_access)) {
         $result = true;
     }
     return $result;
 }
 /**
  * Get WP_Query object arguments.
  *
  * Return default search arguments.
  *
  * @since  1.0.0
  *
  * @param $args The query post args
  *     @see @link http://codex.wordpress.org/Class_Reference/WP_Query
  * @return array $args The parsed args.
  */
 public function get_query_args($args = null)
 {
     $defaults = array('posts_per_page' => -1, 'offset' => 0, 'orderby' => 'ID', 'order' => 'DESC', 'post_type' => self::CPT_BB_FORUM, 'post_status' => 'publish');
     $args = wp_parse_args($args, $defaults);
     $args = parent::prepare_query_args($args);
     return apply_filters('ms_addon_bbpress_model_rule_get_query_args', $args);
 }