| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-admin-custom-posts.php |
| 4 |
* |
| 5 |
* Copyright (c) 2012 "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 Antonio Blanco |
| 18 |
* @package groups |
| 19 |
* @since groups 1.4.2 |
| 20 |
*/ |
| 21 |
|
| 22 |
if ( !defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Post column extensions. |
| 28 |
*/ |
| 29 |
class Groups_Admin_Post_Columns { |
| 30 |
|
| 31 |
const CAPABILITIES = 'capabilities'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Adds an admin_init action. |
| 35 |
*/ |
| 36 |
public static function init() { |
| 37 |
add_action( 'admin_init', array( __CLASS__, 'admin_init' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Adds the filters and actions only for users who have the right |
| 42 |
* Groups permissions and for the post types that have access |
| 43 |
* restrictions enabled. |
| 44 |
*/ |
| 45 |
public static function admin_init() { |
| 46 |
if ( current_user_can( GROUPS_ACCESS_GROUPS ) ) { |
| 47 |
$post_types = get_post_types( array( 'public' => true ) ); |
| 48 |
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() ); |
| 49 |
foreach ( $post_types as $post_type ) { |
| 50 |
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) { |
| 51 |
if ( ( $post_type == 'attachment' ) ) { |
| 52 |
// filters to display the media's access restriction capabilities |
| 53 |
add_filter( 'manage_media_columns', array( __CLASS__, 'columns' ) ); |
| 54 |
// args: string $column_name, int $media_id |
| 55 |
add_action( 'manage_media_custom_column', array( __CLASS__, 'custom_column' ), 10, 2 ); |
| 56 |
} else { |
| 57 |
// filters to display the posts' access restriction capabilities |
| 58 |
add_filter( 'manage_' . $post_type . '_posts_columns', array( __CLASS__, 'columns' ) ); |
| 59 |
// args: string $column_name, int $post_id |
| 60 |
add_action( 'manage_' . $post_type . '_posts_custom_column', array( __CLASS__, 'custom_column' ), 10, 2 ); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Adds a new column to the post type's table showing the access |
| 69 |
* restriction capabilities. |
| 70 |
* |
| 71 |
* @param array $column_headers |
| 72 |
* @return array column headers |
| 73 |
*/ |
| 74 |
public static function columns( $column_headers ) { |
| 75 |
$column_headers[self::CAPABILITIES] = sprintf( |
| 76 |
__( '<span title="%s">Access Restrictions</span>', GROUPS_PLUGIN_DOMAIN ), |
| 77 |
esc_attr( __( 'One or more capabilities required to read the entry.', GROUPS_PLUGIN_DOMAIN ) ) |
| 78 |
); |
| 79 |
return $column_headers; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Renders custom column content. |
| 84 |
* |
| 85 |
* @param string $column_name |
| 86 |
* @param int $post_id |
| 87 |
* @return string custom column content |
| 88 |
*/ |
| 89 |
public static function custom_column( $column_name, $post_id ) { |
| 90 |
$output = ''; |
| 91 |
switch ( $column_name ) { |
| 92 |
case self::CAPABILITIES : |
| 93 |
$read_caps = get_post_meta( $post_id, Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY ); |
| 94 |
$valid_read_caps = Groups_Options::get_option( Groups_Post_Access::READ_POST_CAPABILITIES, array( Groups_Post_Access::READ_POST_CAPABILITY ) ); |
| 95 |
if ( count( $valid_read_caps ) > 0 ) { |
| 96 |
sort( $valid_read_caps ); |
| 97 |
$output = '<ul>'; |
| 98 |
foreach( $valid_read_caps as $valid_read_cap ) { |
| 99 |
if ( $capability = Groups_Capability::read_by_capability( $valid_read_cap ) ) { |
| 100 |
if ( in_array( $valid_read_cap, $read_caps ) ) { |
| 101 |
$output .= '<li>'; |
| 102 |
$output .= wp_strip_all_tags( $capability->capability ); |
| 103 |
$output .= '</li>'; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
$output .= '</ul>'; |
| 108 |
} else { |
| 109 |
$output .= ''; |
| 110 |
} |
| 111 |
break; |
| 112 |
} |
| 113 |
echo $output; |
| 114 |
} |
| 115 |
} |
| 116 |
Groups_Admin_Post_Columns::init(); |
| 117 |
|