PluginProbe
Groups – Memberships and Access Control / 1.1.5
Groups – Memberships and Access Control v1.1.5
4.7.1 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 All 131 releases
groups / lib / access / class-groups-post-access.php

class-groups-post-access.php in Groups – Memberships and Access Control 1.1.5, at lib/access/class-groups-post-access.php

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