';
echo '.groups-capabilities-container { display: inline-block; line-height: 24px; padding-bottom: 1em; vertical-align: top; margin-left: 4px; margin-right: 4px; }';
echo '.groups-capabilities-container .groups-select-container { display: inline-block; vertical-align: top; }';
echo '.groups-capabilities-container .groups-select-container select, .groups-bulk-container select.groups-action { float: none; margin-right: 4px; vertical-align: top; }';
echo '.groups-capabilities-container .selectize-control { min-width: 128px; }';
echo '.groups-capabilities-container .selectize-control, .groups-bulk-container select.groups-action { margin-right: 4px; vertical-align: top; }';
echo '.groups-capabilities-container .selectize-input { font-size: inherit; line-height: 18px; padding: 1px 2px 2px 2px; vertical-align: middle; }';
echo '.groups-capabilities-container .selectize-input input[type="text"] { font-size: inherit; vertical-align: middle; }';
echo '.groups-capabilities-container input.button { margin-top: 1px; vertical-align: top; }';
echo '.inline-edit-row fieldset .capabilities-bulk-container label span.title { min-width: 5em; padding: 2px 1em; width: auto; }';
echo '.tablenav .actions { overflow: visible; }'; // this is important so that the selectize options aren't hidden
echo '.wp-list-table td { overflow: visible; }'; // idem for bulk actions
echo '';
}
}
}
/**
* Renders the access restriction field.
*/
public static function restrict_manage_posts() {
global $pagenow, $wpdb;
if ( is_admin() ) {
if ( $pagenow == 'edit.php' ) { // check that we're on the right screen
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
$post_types_option = Groups_Options::get_option( Groups_Post_Access_Legacy::POST_TYPES, array() );
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
$output = '';
// capabilities select
$output .= '
';
$applicable_read_caps = Groups_Options::get_option( Groups_Post_Access_Legacy::READ_POST_CAPABILITIES, array( Groups_Post_Access_Legacy::READ_POST_CAPABILITY ) );
$output .= sprintf(
'';
$output .= '
';
$output .= Groups_UIE::render_select( '.select.capability' );
echo $output;
}
}
}
}
/**
* Bulk-edit access restriction capabilities.
*
* @param string $column_name
* @param string $post_type
*/
public static function bulk_edit_custom_box( $column_name, $post_type ) {
global $pagenow, $wpdb;
if ( $column_name == 'capabilities' ) {
if ( $pagenow == 'edit.php' ) { // check that we're on the right screen
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
$post_types_option = Groups_Options::get_option( Groups_Post_Access_Legacy::POST_TYPES, array() );
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
$output = ''; // .inline-edit-col-right
$output .= wp_nonce_field( 'post-capability', 'bulk-post-capability-nonce', true, false );
echo $output;
}
}
}
}
/**
* Handles access restriction capability modifications from bulk-editing.
* This is called once for each post that is included in bulk-editing.
* The fields that are handled here are rendered through the
* bulk_edit_custom_box() method in this class.
*
* @param int $post_id
*/
public static function save_post( $post_id ) {
if ( isset( $_REQUEST['capabilities-action'] ) ) {
if ( wp_verify_nonce( $_REQUEST['bulk-post-capability-nonce'], 'post-capability' ) ) {
$field = Groups_Post_Access_Legacy::POSTMETA_PREFIX . 'bulk-' . Groups_Post_Access_Legacy::READ_POST_CAPABILITY;
if ( !empty( $_REQUEST[$field] ) && is_array( $_REQUEST[$field] ) ) {
if ( Groups_Access_Meta_Boxes_Legacy::user_can_restrict() ) {
$valid_read_caps = Groups_Access_Meta_Boxes_Legacy::get_valid_read_caps_for_user();
foreach( $_REQUEST[$field] as $capability_name ) {
if ( $capability = Groups_Capability::read_by_capability( $capability_name ) ) {
if ( in_array( $capability->capability, $valid_read_caps ) ) {
switch( $_REQUEST['capabilities-action'] ) {
case 'add-capability' :
Groups_Post_Access_Legacy::create( array(
'post_id' => $post_id,
'capability' => $capability->capability
) );
break;
case 'remove-capability' :
Groups_Post_Access_Legacy::delete( $post_id, $capability->capability );
break;
}
}
}
}
}
}
}
}
}
/**
* Query modifier to take the selected access restriction capability into
* account.
*
* @param WP_Query $query query object passed by reference
*/
public static function parse_query( &$query ) {
global $pagenow;
if ( is_admin() ) {
if ( $pagenow == 'edit.php' ) { // check that we're on the right screen
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
$post_types_option = Groups_Options::get_option( Groups_Post_Access_Legacy::POST_TYPES, array() );
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) {
if ( !empty( $_GET[Groups_Post_Access_Legacy::POSTMETA_PREFIX . Groups_Post_Access_Legacy::READ_POST_CAPABILITY] ) &&
is_array( $_GET[Groups_Post_Access_Legacy::POSTMETA_PREFIX . Groups_Post_Access_Legacy::READ_POST_CAPABILITY] )
) {
$include_unrestricted = false;
if ( in_array( self::NOT_RESTRICTED, $_GET[Groups_Post_Access_Legacy::POSTMETA_PREFIX . Groups_Post_Access_Legacy::READ_POST_CAPABILITY] ) ) {
$include_unrestricted = true;
}
$capabilities = array();
foreach ( $_GET[Groups_Post_Access_Legacy::POSTMETA_PREFIX . Groups_Post_Access_Legacy::READ_POST_CAPABILITY] as $capability ) {
if ( Groups_Capability::read_by_capability( $capability ) ) {
$capabilities[] = $capability;
}
}
if ( !empty( $capabilities ) ) {
if ( $include_unrestricted ) {
// meta_query does not handle a conjunction
// on the same meta field correctly
// (at least not up to WordPress 3.7.1)
// $query->query_vars['meta_query'] = array (
// 'relation' => 'OR',
// array (
// 'key' => Groups_Post_Access_Legacy::POSTMETA_PREFIX . Groups_Post_Access_Legacy::READ_POST_CAPABILITY,
// 'value' => $capabilities,
// 'compare' => 'IN'
// ),
// array (
// 'key' => Groups_Post_Access_Legacy::POSTMETA_PREFIX . Groups_Post_Access_Legacy::READ_POST_CAPABILITY,
// 'compare' => 'NOT EXISTS'
// )
// );
// we'll limit it to show just unrestricted entries
// until the above is solved
$query->query_vars['meta_query'] = array (
array (
'key' => Groups_Post_Access_Legacy::POSTMETA_PREFIX . Groups_Post_Access_Legacy::READ_POST_CAPABILITY,
'compare' => 'NOT EXISTS'
)
);
} else {
$query->query_vars['meta_query'] = array (
array (
'key' => Groups_Post_Access_Legacy::POSTMETA_PREFIX . Groups_Post_Access_Legacy::READ_POST_CAPABILITY,
'value' => $capabilities,
'compare' => 'IN'
)
);
}
} else if ( $include_unrestricted ) {
$query->query_vars['meta_query'] = array (
array (
'key' => Groups_Post_Access_Legacy::POSTMETA_PREFIX . Groups_Post_Access_Legacy::READ_POST_CAPABILITY,
'compare' => 'NOT EXISTS'
)
);
}
}
}
}
}
}
}
Groups_Admin_Posts_Legacy::init();