/**
  *
  * @param mixed $security Takes a security array, by default - but can provide an int or string (post_id) if security array isnt already available.
  * @param int $cur_page_id Optional. The current page id. If null, tries to get current page id from $_REQUEST['post'] or $_REQUEST['postid'].
  * @return string
  */
 public static function render_sidebar_attached_groups($security = null, $cur_page_id = null)
 {
     if (is_numeric($security) || is_string($security)) {
         //Get array with security requirements for this page
         $security = CTXPS_Security::get_post_protection($security, false);
     }
     //Default vars
     $return = '';
     $termGroups = array();
     //If $cur_page_id isn't set, try to get the value from the querystring
     if (empty($cur_page_id)) {
         if (!empty($_REQUEST['post_id'])) {
             $cur_page_id = $_REQUEST['post_id'];
         } else {
             if (!empty($_REQUEST['post'])) {
                 $cur_page_id = $_REQUEST['post'];
             } else {
                 if (!empty($_REQUEST['postid'])) {
                     $cur_page_id = $_REQUEST['postid'];
                 }
             }
         }
     }
     //Fetch term groups, if we have a page id
     if (!empty($cur_page_id)) {
         $termGroups = CTXPS_Queries::get_groups_by_post_terms($cur_page_id, true);
     }
     //Count the number of term groups
     $groupcount = count($termGroups);
     //Count the number of groups directly attached to this page (including inherited groups)
     if (!empty($security)) {
         foreach ($security as $securityGroups) {
             $groupcount += count($securityGroups);
         }
     }
     //Show groups that are already added to this page
     if ($groupcount === 0) {
         //Display this if we have no groups (inherited or otherwise)
         $return .= '<div><em>' . __('No groups have been added yet.', 'contexture-page-security') . '</em></div>';
     } else {
         if (!empty($security)) {
             foreach ($security as $sec_array_id => $sec_array) {
                 //If this is the current page (and not an ancestor)
                 if ($sec_array_id == $cur_page_id) {
                     foreach ($sec_array as $currentGroupId => $currentGroup) {
                         $return .= '<div class="ctx-ps-sidebar-group">&bull; <span class="ctx-ps-sidebar-group-title">' . $currentGroup . '</span> <a style="text-decoration:none;" href="' . admin_url('/users.php?page=ps_groups_edit&groupid=' . $currentGroupId) . '">&raquo;</a><span class="removegrp" onclick="CTXPS_Ajax.removeGroupFromPage(' . $currentGroupId . ',jQuery(this))" title="' . __('Click to remove group access.', 'contexture-page-security') . '" >' . __('remove', 'contexture-page-security') . '</span></div>';
                     }
                 } else {
                     foreach ($sec_array as $currentGroupId => $currentGroup) {
                         $return .= '<div class="ctx-ps-sidebar-group inherited">&bull; <span class="ctx-ps-sidebar-group-title">' . $currentGroup . '</span> <a style="text-decoration:none;" href="' . admin_url('/users.php?page=ps_groups_edit&groupid=' . $currentGroupId) . '">&raquo;</a><a class="viewgrp" target="_blank" href="' . admin_url('post.php?post=' . $sec_array_id . '&action=edit') . '" title="' . __('Protection has been inherited from an ancestor. Click to view ancestor.', 'contexture-page-security') . '">' . __('ancestor', 'contexture-page-security') . '</a></div>';
                     }
                     //foreach
                 }
                 //else
             }
             //foreach
         }
         //if
         //Show terms that are already added to this list
         foreach ($termGroups as $tgroup) {
             //Get the term archive URL. If one doesnt exist, dont link
             $term_archive_link = admin_url(sprintf('/edit-tags.php?action=edit&taxonomy=%s&tag_ID=%s', $tgroup['taxonomy'], $tgroup['term_id']));
             //Build the link HTML for terms
             $return .= '<div class="ctx-ps-sidebar-group inherited">&bull; <span class="ctx-ps-sidebar-group-title">' . $tgroup['group_title'] . '</span> <a style="text-decoration:none;" href="' . $term_archive_link . '">&raquo;</a><a class="viewgrp" target="_blank" href="' . $term_archive_link . '" title="' . __('Protection has been inherited from a term. Click to view term.', 'contexture-page-security') . '">' . __('term', 'contexture-page-security') . '</a></div>';
         }
     }
     return $return;
 }
 /**
  * This function will check the security for the specified page and all parent pages.
  * If security exists, a multi-dimensional array will be returned following the format
  * array( pageid=>array(groupid=>groupname) ), with the first item being the current
  * page and additional items being parents. If no security is present for any ancestor
  * then the function will return false.
  *
  * @global wpdb $wpdb
  *
  * @param int $post_id The id of the post to get permissions for.
  * @param bool $include_terms Optional. Define whether or not term protection should be merged into page protection.
  * @return mixed Returns an array with all the required permissions to access this page. If no security is present, returns false.
  */
 public static function get_post_protection($post_id, $include_terms = true)
 {
     //If this branch isn't protected, just stop now and save all that processing power
     if (!CTXPS_Queries::check_section_protection($post_id, true)) {
         return false;
     }
     //If we're still going, then it means something above us is protected, so lets get the list of permissions
     global $wpdb;
     $return = array();
     $group_array = array();
     /**Gets the parent id of the current page/post*/
     $parent_id = get_post($post_id);
     $parent_id = (int) $parent_id->post_parent;
     /** 1. If I am secure, get my groups ***********************************/
     //Get Group relationship info for this page
     $groups = CTXPS_Queries::get_groups_by_object('post', $post_id, true);
     //If 0 results, dont do anything. Otherwise, re-sort into id=>name array
     if (!empty($groups)) {
         foreach ($groups as $group) {
             $group_array[$group->group_id] = $group->group_title;
         }
         unset($group);
     }
     unset($groups);
     //Add an item to the array. 'pageid'=>array('groupid','groupname')
     $return[(string) $post_id] = $group_array;
     unset($group_array);
     /** 1b. Get term protections *******************************************/
     if ($include_terms) {
         //If term protection exists for this post, get the array
         if (CTXPS_Queries::check_post_term_protection($post_id)) {
             //Term branch is protected, get attached groups
             $termreqs = CTXPS_Queries::get_groups_by_post_terms($post_id);
             //Merge those changes back into $pagereqs
             $return[(string) $post_id] += $termreqs;
         }
     }
     //wp_die('|||'.print_r($termreqs,true));
     //wp_die('|||'.print_r($return[(string)$post_id],true));
     /** 2. If I have a parent, recurse  ************************************/
     //Using our earlier results, check post_parent. If it's != 0 then recurse this function, adding the return value to $array
     if ($parent_id != 0) {
         //$recursedArray = CTXPS_Security::get_protection($parentid);
         //$array = array_merge($array,$recursedArray);
         $parent_array = self::get_post_protection($parent_id, $include_terms);
         if (!!$parent_array) {
             $return += $parent_array;
         }
     }
     //3. Return the completed $array
     return $return;
 }