PluginProbe
Groups – Memberships and Access Control / 1.11.2
Groups – Memberships and Access Control v1.11.2
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 / admin / class-groups-admin-posts.php

class-groups-admin-posts.php in Groups – Memberships and Access Control 1.11.2, at lib/admin/class-groups-admin-posts.php

344 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-admin-posts.php
4 *
5 * Copyright (c) 2013 "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.4.2
20 */
21
22 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Additions to post overview admin screens.
28 */
29 class Groups_Admin_Posts {
30
31 const NOT_RESTRICTED = "#not-restricted#";
32
33 /**
34 * Sets up an admin_init hook where our actions and filters are added.
35 */
36 public static function init() {
37 add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
38 }
39
40 /**
41 * Adds actions and filters to handle filtering by access restriction
42 * capability.
43 */
44 public static function admin_init() {
45 if ( current_user_can( GROUPS_ACCESS_GROUPS ) ) {
46 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
47 add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
48 add_action( 'restrict_manage_posts', array( __CLASS__, 'restrict_manage_posts' ) );
49 add_filter( 'parse_query', array( __CLASS__, 'parse_query' ) );
50
51 add_action( 'bulk_edit_custom_box', array( __CLASS__, 'bulk_edit_custom_box' ), 10, 2);
52 add_action( 'save_post', array( __CLASS__, 'save_post' ) );
53 }
54 }
55
56 /**
57 * Enqueues the select script.
58 */
59 public static function admin_enqueue_scripts() {
60
61 global $pagenow;
62
63 if ( $pagenow == 'edit.php' ) {
64 $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
65 $post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
66 if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
67 Groups_UIE::enqueue( 'select' );
68 }
69 }
70 }
71
72 /**
73 * Adds CSS rules to display our access restriction filter coherently.
74 */
75 public static function admin_head() {
76
77 global $pagenow;
78
79 if ( $pagenow == 'edit.php' ) {
80 $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
81 $post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
82 if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
83 echo '<style type="text/css">';
84 echo '.groups-capabilities-container { display: inline-block; line-height: 24px; padding-bottom: 1em; vertical-align: top; margin-left: 4px; margin-right: 4px; }';
85 echo '.groups-capabilities-container .groups-select-container { display: inline-block; vertical-align: top; }';
86 echo '.groups-capabilities-container .groups-select-container select, .groups-bulk-container select.groups-action { float: none; margin-right: 4px; vertical-align: top; }';
87 echo '.groups-capabilities-container .selectize-control { min-width: 128px; }';
88 echo '.groups-capabilities-container .selectize-control, .groups-bulk-container select.groups-action { margin-right: 4px; vertical-align: top; }';
89 echo '.groups-capabilities-container .selectize-input { font-size: inherit; line-height: 18px; padding: 1px 2px 2px 2px; vertical-align: middle; }';
90 echo '.groups-capabilities-container .selectize-input input[type="text"] { font-size: inherit; vertical-align: middle; }';
91 echo '.groups-capabilities-container input.button { margin-top: 1px; vertical-align: top; }';
92 echo '.inline-edit-row fieldset .capabilities-bulk-container label span.title { min-width: 5em; padding: 2px 1em; width: auto; }';
93 echo '.tablenav .actions { overflow: visible; }'; // this is important so that the selectize options aren't hidden
94 echo '.wp-list-table td { overflow: visible; }'; // idem for bulk actions
95 echo '</style>';
96 }
97 }
98 }
99
100 /**
101 * Renders the access restriction field.
102 */
103 public static function restrict_manage_posts() {
104
105 global $pagenow, $wpdb;
106
107 if ( is_admin() ) {
108
109 if ( $pagenow == 'edit.php' ) { // check that we're on the right screen
110
111 $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
112 $post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
113
114 if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
115
116 $output = '';
117
118 // capabilities select
119 $output .= '<div class="groups-capabilities-container">';
120 $applicable_read_caps = Groups_Options::get_option( Groups_Post_Access::READ_POST_CAPABILITIES, array( Groups_Post_Access::READ_POST_CAPABILITY ) );
121 $output .= sprintf(
122 '<select class="select capability" name="%s[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
123 esc_attr( Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY ),
124 esc_attr( __( 'Access restrictions &hellip;', GROUPS_PLUGIN_DOMAIN ) ) ,
125 esc_attr( __( 'Access restrictions &hellip;', GROUPS_PLUGIN_DOMAIN ) )
126 );
127
128 $previous_selected = array();
129 if ( !empty( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY] ) ) {
130 $previous_selected = $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY];
131 if ( !is_array( $previous_selected ) ) {
132 $previous_selected = array();
133 }
134 }
135 $selected = in_array( self::NOT_RESTRICTED, $previous_selected ) ? ' selected="selected" ' : '';
136 $output .= sprintf( '<option value="%s" %s >%s</option>', self::NOT_RESTRICTED, esc_attr( $selected ), esc_attr( __( '(only unrestricted)', GROUPS_PLUGIN_DOMAIN ) ) );
137
138 foreach( $applicable_read_caps as $capability ) {
139 $selected = in_array( $capability, $previous_selected ) ? ' selected="selected" ' : '';
140 $output .= sprintf( '<option value="%s" %s >%s</option>', esc_attr( $capability ), esc_attr( $selected ), wp_filter_nohtml_kses( $capability ) );
141 }
142 $output .= '</select>';
143 $output .= '</div>';
144 $output .= Groups_UIE::render_select( '.select.capability' );
145
146 echo $output;
147 }
148
149 }
150 }
151
152 }
153
154 /**
155 * Bulk-edit access restriction capabilities.
156 *
157 * @param string $column_name
158 * @param string $post_type
159 */
160 public static function bulk_edit_custom_box( $column_name, $post_type ) {
161
162 global $pagenow, $wpdb;
163
164 if ( $column_name == 'capabilities' ) {
165
166 if ( $pagenow == 'edit.php' ) { // check that we're on the right screen
167
168 $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
169 $post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
170
171 if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
172
173 $output = '<fieldset class="inline-edit-col-right">';
174 $output .= '<div class="bulk-edit-groups">';
175
176 // capability/access restriction bulk actions added through extra_tablenav()
177 $output .= '<div id="capability-bulk-actions" class="capabilities-bulk-container" style="display:inline">';
178
179 $output .= '<label style="display:inline;">';
180 $output .= '<span class="title">';
181 $output .= __( 'Access Restrictions', GROUPS_PLUGIN_DOMAIN );
182 $output .= '</span>';
183 $output .= '<select class="capabilities-action" name="capabilities-action">';
184 $output .= '<option selected="selected" value="-1">' . __( '&mdash; No Change &mdash;', GROUPS_PLUGIN_DOMAIN ) . '</option>';
185 $output .= '<option value="add-capability">' . __( 'Add restriction', GROUPS_PLUGIN_DOMAIN ) . '</option>';
186 $output .= '<option value="remove-capability">' . __( 'Remove restriction', GROUPS_PLUGIN_DOMAIN ) . '</option>';
187 $output .= '</select>';
188 $output .= '</label>';
189
190 $output .= '<div class="groups-capabilities-container">';
191 $valid_read_caps = Groups_Access_Meta_Boxes::get_valid_read_caps_for_user();
192 $output .= sprintf(
193 '<select class="select bulk-capability" name="%s[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
194 esc_attr( Groups_Post_Access::POSTMETA_PREFIX . 'bulk-' . Groups_Post_Access::READ_POST_CAPABILITY ),
195 esc_attr( __( 'Choose access restrictions &hellip;', GROUPS_PLUGIN_DOMAIN ) ) ,
196 esc_attr( __( 'Choose access restrictions &hellip;', GROUPS_PLUGIN_DOMAIN ) )
197 );
198
199 foreach( $valid_read_caps as $capability ) {
200 $output .= sprintf( '<option value="%s" >%s</option>', esc_attr( $capability ), wp_filter_nohtml_kses( $capability ) );
201 }
202 $output .= '</select>';
203 $output .= '</div>'; // .groups-capabilities-container
204 $output .= Groups_UIE::render_select( '.select.bulk-capability' );
205
206 $output .= '</div>'; // .capabilities-bulk-container
207
208 $output .= '</div>'; // .bulk-edit-groups
209 $output .= '</fieldset>'; // .inline-edit-col-right
210
211 $output .= wp_nonce_field( 'post-capability', 'bulk-post-capability-nonce', true, false );
212
213 echo $output;
214 }
215 }
216 }
217 }
218
219 /**
220 * Handles access restriction capability modifications from bulk-editing.
221 * This is called once for each post that is included in bulk-editing.
222 * The fields that are handled here are rendered through the
223 * bulk_edit_custom_box() method in this class.
224 *
225 * @param int $post_id
226 */
227 public static function save_post( $post_id ) {
228 if ( isset( $_REQUEST['capabilities-action'] ) ) {
229 if ( wp_verify_nonce( $_REQUEST['bulk-post-capability-nonce'], 'post-capability' ) ) {
230 $field = Groups_Post_Access::POSTMETA_PREFIX . 'bulk-' . Groups_Post_Access::READ_POST_CAPABILITY;
231 if ( !empty( $_REQUEST[$field] ) && is_array( $_REQUEST[$field] ) ) {
232 if ( Groups_Access_Meta_Boxes::user_can_restrict() ) {
233 $valid_read_caps = Groups_Access_Meta_Boxes::get_valid_read_caps_for_user();
234 foreach( $_REQUEST[$field] as $capability_name ) {
235 if ( $capability = Groups_Capability::read_by_capability( $capability_name ) ) {
236 if ( in_array( $capability->capability, $valid_read_caps ) ) {
237 switch( $_REQUEST['capabilities-action'] ) {
238 case 'add-capability' :
239 Groups_Post_Access::create( array(
240 'post_id' => $post_id,
241 'capability' => $capability->capability
242 ) );
243 break;
244 case 'remove-capability' :
245 Groups_Post_Access::delete( $post_id, $capability->capability );
246 break;
247 }
248 }
249 }
250 }
251 }
252 }
253 }
254 }
255 }
256
257 /**
258 * Query modifier to take the selected access restriction capability into
259 * account.
260 *
261 * @param WP_Query $query query object passed by reference
262 */
263 public static function parse_query( &$query ) {
264
265 global $pagenow;
266
267 if ( is_admin() ) {
268
269 if ( $pagenow == 'edit.php' ) { // check that we're on the right screen
270
271 $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
272 $post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
273
274 if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
275
276 if ( !empty( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY] ) &&
277 is_array( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY] )
278 ) {
279
280 $include_unrestricted = false;
281 if ( in_array( self::NOT_RESTRICTED, $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY] ) ) {
282 $include_unrestricted = true;
283 }
284
285 $capabilities = array();
286 foreach ( $_GET[Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY] as $capability ) {
287 if ( Groups_Capability::read_by_capability( $capability ) ) {
288 $capabilities[] = $capability;
289 }
290 }
291
292 if ( !empty( $capabilities ) ) {
293 if ( $include_unrestricted ) {
294 // meta_query does not handle a conjunction
295 // on the same meta field correctly
296 // (at least not up to WordPress 3.7.1)
297 // $query->query_vars['meta_query'] = array (
298 // 'relation' => 'OR',
299 // array (
300 // 'key' => Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY,
301 // 'value' => $capabilities,
302 // 'compare' => 'IN'
303 // ),
304 // array (
305 // 'key' => Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY,
306 // 'compare' => 'NOT EXISTS'
307 // )
308 // );
309 // we'll limit it to show just unrestricted entries
310 // until the above is solved
311 $query->query_vars['meta_query'] = array (
312 array (
313 'key' => Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY,
314 'compare' => 'NOT EXISTS'
315 )
316 );
317 } else {
318 $query->query_vars['meta_query'] = array (
319 array (
320 'key' => Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY,
321 'value' => $capabilities,
322 'compare' => 'IN'
323 )
324 );
325 }
326 } else if ( $include_unrestricted ) {
327 $query->query_vars['meta_query'] = array (
328 array (
329 'key' => Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY,
330 'compare' => 'NOT EXISTS'
331 )
332 );
333 }
334 }
335 }
336 }
337
338 }
339
340 }
341
342 }
343 Groups_Admin_Posts::init();
344