| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-post-access.php |
| 4 |
* |
| 5 |
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 6 |
* |
| 7 |
* This code is released under the GNU General Public License. |
| 8 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* This header and all notices must be kept intact. |
| 16 |
* |
| 17 |
* @author Karim Rahimpur |
| 18 |
* @package groups |
| 19 |
* @since groups 1.0.0 |
| 20 |
*/ |
| 21 |
class Groups_Post_Access { |
| 22 |
|
| 23 |
const POSTMETA_PREFIX = 'groups-'; |
| 24 |
|
| 25 |
const READ_POST_CAPABILITY = "groups_read_post"; |
| 26 |
const READ_POST_CAPABILITY_NAME = "Read Post"; |
| 27 |
|
| 28 |
/** |
| 29 |
* Create needed capabilities on plugin activation. |
| 30 |
* Must be called explicitly or hooked into activation. |
| 31 |
*/ |
| 32 |
public static function activate() { |
| 33 |
if ( !Groups_Capability::read_by_capability( self::READ_POST_CAPABILITY ) ) { |
| 34 |
Groups_Capability::create( array( "capability" => self::READ_POST_CAPABILITY ) ); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public static function init() { |
| 39 |
|
| 40 |
// for translation |
| 41 |
// @see self::READ_POST_CAPABILITY_NAME |
| 42 |
__( "Read Post", GROUPS_PLUGIN_DOMAIN ); |
| 43 |
|
| 44 |
// post access |
| 45 |
add_filter( 'get_pages', array( __CLASS__, "get_pages" ), 1 ); |
| 46 |
add_filter( 'the_posts', array( __CLASS__, "the_posts" ), 1, 2 ); |
| 47 |
add_filter( 'wp_get_nav_menu_items', array( __CLASS__, "wp_get_nav_menu_items" ), 1, 3 ); |
| 48 |
|
| 49 |
// content access |
| 50 |
add_filter( "get_the_excerpt", array( __CLASS__, "get_the_excerpt" ), 1 ); |
| 51 |
add_filter( "the_content", array( __CLASS__, "the_content" ), 1 ); |
| 52 |
|
| 53 |
// @todo these could be interesting to add later ... |
| 54 |
// add_filter( "plugin_row_meta", array( __CLASS__, "plugin_row_meta" ), 1 ); |
| 55 |
// add_filter( "posts_join_paged", array( __CLASS__, "posts_join_paged" ), 1 ); |
| 56 |
// add_filter( "posts_where_paged", array( __CLASS__, "posts_where_paged" ), 1 ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Filter pages by access capability. |
| 61 |
* |
| 62 |
* @param array $pages |
| 63 |
*/ |
| 64 |
public static function get_pages( $pages ) { |
| 65 |
|
| 66 |
$result = array(); |
| 67 |
foreach ( $pages as $page ) { |
| 68 |
$read_post_capability = self::read( $page->ID, self::READ_POST_CAPABILITY ); |
| 69 |
if ( $read_post_capability ) { |
| 70 |
$user_id = get_current_user_id(); |
| 71 |
$groups_user = new Groups_User( $user_id ); |
| 72 |
if ( $groups_user->can( self::READ_POST_CAPABILITY ) ) { |
| 73 |
$result[] = $page; |
| 74 |
} |
| 75 |
} else { |
| 76 |
$result[] = $page; |
| 77 |
} |
| 78 |
} |
| 79 |
return $result; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Filter posts by access capability. |
| 85 |
* |
| 86 |
* @param array $posts list of posts |
| 87 |
* @param WP_Query $query |
| 88 |
*/ |
| 89 |
public static function the_posts( $posts, $query ) { |
| 90 |
|
| 91 |
$result = array(); |
| 92 |
foreach ( $posts as $post ) { |
| 93 |
$read_post_capability = self::read( $post->ID, self::READ_POST_CAPABILITY ); |
| 94 |
if ( $read_post_capability ) { |
| 95 |
$user_id = get_current_user_id(); |
| 96 |
$groups_user = new Groups_User( $user_id ); |
| 97 |
if ( $groups_user->can( self::READ_POST_CAPABILITY ) ) { |
| 98 |
$result[] = $post; |
| 99 |
} |
| 100 |
} else { |
| 101 |
$result[] = $post; |
| 102 |
} |
| 103 |
} |
| 104 |
return $result; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Filter menu items by access capability. |
| 109 |
* |
| 110 |
* @param array $items |
| 111 |
* @param mixed $menu |
| 112 |
* @param array $args |
| 113 |
*/ |
| 114 |
public static function wp_get_nav_menu_items( $items = null, $menu = null, $args = null ) { |
| 115 |
$result = array(); |
| 116 |
foreach ( $items as $item ) { |
| 117 |
// @todo might want to check $item->object and $item->type first, |
| 118 |
// for example these are 'page' and 'post_type' for a page |
| 119 |
$read_post_capability = self::read( $item->object_id, self::READ_POST_CAPABILITY ); |
| 120 |
if ( $read_post_capability ) { |
| 121 |
$user_id = get_current_user_id(); |
| 122 |
$groups_user = new Groups_User( $user_id ); |
| 123 |
if ( $groups_user->can( self::READ_POST_CAPABILITY ) ) { |
| 124 |
$result[] = $item; |
| 125 |
} |
| 126 |
} else { |
| 127 |
$result[] = $item; |
| 128 |
} |
| 129 |
} |
| 130 |
return $result; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Filter excerpt by access capability. |
| 135 |
* |
| 136 |
* @param string $output |
| 137 |
* @return $output if access granted, otherwise '' |
| 138 |
*/ |
| 139 |
public static function get_the_excerpt( $output ) { |
| 140 |
global $post; |
| 141 |
$result = ''; |
| 142 |
if ( isset( $post->ID ) ) { |
| 143 |
$read_post_capability = self::read( $post->ID, self::READ_POST_CAPABILITY ); |
| 144 |
if ( $read_post_capability ) { |
| 145 |
$user_id = get_current_user_id(); |
| 146 |
$groups_user = new Groups_User( $user_id ); |
| 147 |
if ( $groups_user->can( self::READ_POST_CAPABILITY ) ) { |
| 148 |
$result = $output; |
| 149 |
} |
| 150 |
} else { |
| 151 |
$result = $output; |
| 152 |
} |
| 153 |
} |
| 154 |
return $result; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Filter content by access capability. |
| 159 |
* |
| 160 |
* @param string $output |
| 161 |
* @return $output if access granted, otherwise '' |
| 162 |
*/ |
| 163 |
public static function the_content( $output ) { |
| 164 |
global $post; |
| 165 |
$result = ''; |
| 166 |
if ( isset( $post->ID ) ) { |
| 167 |
$read_post_capability = self::read( $post->ID, self::READ_POST_CAPABILITY ); |
| 168 |
if ( $read_post_capability ) { |
| 169 |
$user_id = get_current_user_id(); |
| 170 |
$groups_user = new Groups_User( $user_id ); |
| 171 |
if ( $groups_user->can( self::READ_POST_CAPABILITY ) ) { |
| 172 |
$result = $output; |
| 173 |
} |
| 174 |
} else { |
| 175 |
$result = $output; |
| 176 |
} |
| 177 |
} |
| 178 |
return $result; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Adds an access capability requirement. |
| 183 |
* |
| 184 |
* $map must contain 'post_id' |
| 185 |
* |
| 186 |
* For now this only should be used to add the READ_POST_CAPABILITY which |
| 187 |
* it does automatically. Nothing else is checked for granting access. |
| 188 |
* |
| 189 |
* @param array $map |
| 190 |
* @return true if the capability could be added to the post, otherwis false |
| 191 |
*/ |
| 192 |
public static function create( $map ) { |
| 193 |
extract( $map ); |
| 194 |
$result = false; |
| 195 |
|
| 196 |
if ( !isset( $capability ) ) { |
| 197 |
$capability = self::READ_POST_CAPABILITY; |
| 198 |
} |
| 199 |
|
| 200 |
if ( !empty( $post_id ) && !empty( $capability) ) { |
| 201 |
$result = update_post_meta( $post_id, self::POSTMETA_PREFIX . $capability, true ); |
| 202 |
} |
| 203 |
return $result; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns true if the post requires the given capability to grant access. |
| 208 |
* |
| 209 |
* Currently only READ_POST_CAPABILITY should be used, this is also taken |
| 210 |
* as the default. |
| 211 |
* |
| 212 |
* @param int $post_id |
| 213 |
* @param string $capability capability label |
| 214 |
* @return true if the capability is required, otherwise false |
| 215 |
*/ |
| 216 |
public static function read( $post_id, $capability = self::READ_POST_CAPABILITY ) { |
| 217 |
return get_post_meta( $post_id, self::POSTMETA_PREFIX . $capability ); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Currently does nothing, always returns false. |
| 222 |
* |
| 223 |
* @param array $map |
| 224 |
* @return false |
| 225 |
*/ |
| 226 |
public static function update( $map ) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Removes a capability requirement from a post. |
| 232 |
* |
| 233 |
* @param int $post_id |
| 234 |
* @param string $capability |
| 235 |
* @return true on success, otherwise false |
| 236 |
*/ |
| 237 |
public static function delete( $post_id, $capability = self::READ_POST_CAPABILITY ) { |
| 238 |
$result = false; |
| 239 |
if ( !empty( $post_id ) && !empty( $capability) ) { |
| 240 |
$result = delete_post_meta( $post_id, self::POSTMETA_PREFIX . $capability ); |
| 241 |
} |
| 242 |
return $result; |
| 243 |
} |
| 244 |
} |
| 245 |
Groups_Post_Access::init(); |
| 246 |
|