/** * Generates a "lock" symbol for the "Protected" column, if the current content * is protected. See WP's template.php -> display_page_row() for more. * * @param type $column_name The name of the column to affect ('protected') * @param type $post_id The id of the page to check. */ public static function render_list_protection_column($column_name, $post_id) { //wp_die($column_name.' GOOGLE '.$post_id); //Only do this if we've got the right column if ($column_name === 'protected') { //If page is protected, return lock icon if (CTXPS_Queries::check_protection($post_id)) { CTX_Helper::img(array('alt' => __('Protected', 'contexture-page-security'), 'title' => __('Protected', 'contexture-page-security'), 'src' => CTXPSURL . 'images/protected-inline-2x.png', 'class' => "prot-icon prot-inline")); } else { if (CTXPS_Queries::check_section_protection($post_id)) { CTX_Helper::img(array('alt' => __('Protected (inherited)', 'contexture-page-security'), 'title' => __('Inheriting protection', 'contexture-page-security'), 'src' => CTXPSURL . 'images/protected-inline-descendant-2x.png', 'class' => "prot-icon prot-descendant")); //If theres no direct protection, is this content protected through term inheritance? } else { //If the post belongs to a protected term, show lighter (inherited) icon if (CTXPS_Queries::check_post_term_protection($post_id)) { CTX_Helper::img(array('alt' => __('Protected (inherited from term)', 'contexture-page-security'), 'title' => __('Inheriting protection from term', 'contexture-page-security'), 'src' => CTXPSURL . 'images/protected-inline-inherit-2x.png', 'class' => "prot-icon prot-inline-inherit")); } } } } }
/** * 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; }
/** * Recursively checks security for this page/post and it's ancestors. Returns true * if any of them are protected or false if none of them are protected. * * @param int $post_id Optional. The id of the page or post to check. If left null, will try to check current post id from the loop (if available). * @param bool $dontcheck Optional. Set to true to prevent automatically checking current post id in the loop (if $post_id is null). * @return bool If this page or it's ancestors has the "protected page" flag */ function psc_has_protection($post_id = null, $dontcheck = false) { global $wpdb, $post; //This function is deprecated psc_deprecated('CTXPS_Queries::check_section_protection()'); //If $post_id isnt set, try to get global post id if (empty($post_id) && !$dontcheck && isset($post->ID)) { $post_id = $post->ID; } //Fail if the post id isn't numeric if (!is_numeric($post_id)) { return false; } //Try to get post meta $mymeta = get_post_meta($post_id, 'ctx_ps_security'); //Check permissions for current page if (!empty($mymeta)) { return true; } else { //If this isn't protected, lets see if there's a parent... $parent_id = $wpdb->get_var(sprintf('SELECT post_parent FROM %s WHERE `ID` = %s', $wpdb->posts, $post_id)); //If we have a parent, repeat this check with the parent. if ($parent_id != 0) { return CTXPS_Queries::check_section_protection($parent_id); } else { return false; } } }