| @@ -14,8 +14,9 @@ | ||
| 14 | 14 | * |
| 15 | 15 | * This header and all notices must be kept intact. |
| 16 | 16 | * |
| 17 | 17 | * @author Antonio Blanco |
| 18 | + * @author Karim Rahimpur | |
| 18 | 19 | * @package groups |
| 19 | 20 | * @since groups 1.4.2 |
| 20 | 21 | */ |
| 21 | 22 | |
| @@ -27,11 +28,36 @@ | ||
| 27 | 28 | * Post column extensions. |
| 28 | 29 | */ |
| 29 | 30 | class Groups_Admin_Post_Columns { |
| 30 | 31 | |
| 31 | - const CAPABILITIES = 'capabilities'; | |
| 32 | + /** | |
| 33 | + * Groups column header id. | |
| 34 | + * @var string | |
| 35 | + */ | |
| 36 | + const GROUPS = 'groups-read'; | |
| 32 | 37 | |
| 33 | 38 | /** |
| 39 | + * Field name. | |
| 40 | + * | |
| 41 | + * @var string | |
| 42 | + */ | |
| 43 | + const GROUPS_READ = 'groups-read'; | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Cache group key. | |
| 47 | + * | |
| 48 | + * @var string | |
| 49 | + */ | |
| 50 | + const CACHE_GROUP = 'groups'; | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * Cache key prefix. | |
| 54 | + * | |
| 55 | + * @var string | |
| 56 | + */ | |
| 57 | + const EDIT_TERM_LINK = 'edit-term-link'; | |
| 58 | + | |
| 59 | + /** | |
| 34 | 60 | * Adds an admin_init action. |
| 35 | 61 | */ |
| 36 | 62 | public static function init() { |
| 37 | 63 | add_action( 'admin_init', array( __CLASS__, 'admin_init' ) ); |
| @@ -42,23 +68,27 @@ | ||
| 42 | 68 | * Groups permissions and for the post types that have access |
| 43 | 69 | * restrictions enabled. |
| 44 | 70 | */ |
| 45 | 71 | public static function admin_init() { |
| 46 | - if ( current_user_can( GROUPS_ACCESS_GROUPS ) ) { | |
| 72 | + if ( Groups_User::current_user_can( GROUPS_ACCESS_GROUPS ) ) { | |
| 47 | 73 | $post_types = get_post_types( array( 'public' => true ) ); |
| 48 | 74 | $post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() ); |
| 49 | 75 | foreach ( $post_types as $post_type ) { |
| 50 | 76 | if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) { |
| 51 | 77 | if ( ( $post_type == 'attachment' ) ) { |
| 52 | - // filters to display the media's access restriction capabilities | |
| 78 | + // filters to display the media's access restriction groups | |
| 53 | 79 | add_filter( 'manage_media_columns', array( __CLASS__, 'columns' ) ); |
| 54 | 80 | // args: string $column_name, int $media_id |
| 55 | 81 | add_action( 'manage_media_custom_column', array( __CLASS__, 'custom_column' ), 10, 2 ); |
| 82 | + // make the groups column sortable | |
| 83 | + add_filter( 'manage_upload_sortable_columns', array( __CLASS__, 'manage_edit_post_sortable_columns' ) ); | |
| 56 | 84 | } else { |
| 57 | - // filters to display the posts' access restriction capabilities | |
| 85 | + // filters to display the posts' access restriction groups | |
| 58 | 86 | add_filter( 'manage_' . $post_type . '_posts_columns', array( __CLASS__, 'columns' ) ); |
| 59 | 87 | // args: string $column_name, int $post_id |
| 60 | 88 | add_action( 'manage_' . $post_type . '_posts_custom_column', array( __CLASS__, 'custom_column' ), 10, 2 ); |
| 89 | + // make the groups column sortable | |
| 90 | + add_filter( 'manage_edit-' . $post_type . '_sortable_columns', array( __CLASS__, 'manage_edit_post_sortable_columns' ) ); | |
| 61 | 91 | } |
| 62 | 92 | } |
| 63 | 93 | } |
| 64 | 94 | } |
| @@ -65,17 +95,20 @@ | ||
| 65 | 95 | } |
| 66 | 96 | |
| 67 | 97 | /** |
| 68 | 98 | * Adds a new column to the post type's table showing the access |
| 69 | - * restriction capabilities. | |
| 70 | - * | |
| 99 | + * restriction groups. | |
| 100 | + * | |
| 71 | 101 | * @param array $column_headers |
| 102 | + * | |
| 72 | 103 | * @return array column headers |
| 73 | 104 | */ |
| 74 | 105 | 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 ) ) | |
| 106 | + $column_headers[self::GROUPS] = sprintf( | |
| 107 | + '<span title="%s">%s</span>' . | |
| 108 | + ' <span style="float: unset; font-size: inherit; line-height: inherit; font-weight: normal;" class="dashicons dashicons-lock"></span>', | |
| 109 | + esc_attr__( 'One or more groups granting access to entries.', 'groups' ), | |
| 110 | + esc_html_x( 'Groups', 'Column header', 'groups' ) | |
| 78 | 111 | ); |
| 79 | 112 | return $column_headers; |
| 80 | 113 | } |
| 81 | 114 | |
| @@ -80,37 +113,128 @@ | ||
| 80 | 113 | } |
| 81 | 114 | |
| 82 | 115 | /** |
| 83 | 116 | * Renders custom column content. |
| 84 | - * | |
| 117 | + * | |
| 85 | 118 | * @param string $column_name |
| 86 | 119 | * @param int $post_id |
| 87 | - * @return string custom column content | |
| 88 | 120 | */ |
| 89 | 121 | public static function custom_column( $column_name, $post_id ) { |
| 90 | 122 | $output = ''; |
| 91 | 123 | 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>'; | |
| 124 | + case self::GROUPS : | |
| 125 | + $entries = array(); | |
| 126 | + $groups_read = Groups_Post_Access::get_read_group_ids( $post_id ); | |
| 127 | + | |
| 128 | + if ( count( $groups_read ) > 0 ) { | |
| 129 | + $groups = Groups_Group::get_groups( array( 'order_by' => 'name', 'order' => 'ASC', 'include' => $groups_read ) ); | |
| 130 | + if ( ( count( $groups ) > 0 ) ) { | |
| 131 | + foreach ( $groups as $group ) { | |
| 132 | + $entries[] = $group->name ? stripslashes( wp_strip_all_tags( $group->name ) ) : ''; | |
| 133 | + } | |
| 134 | + } | |
| 135 | + } | |
| 136 | + if ( | |
| 137 | + function_exists( 'get_term_meta' ) && // >= WordPress 4.4 | |
| 138 | + class_exists( 'Groups_Restrict_Categories' ) && | |
| 139 | + method_exists( 'Groups_Restrict_Categories', 'get_controlled_taxonomies' ) && | |
| 140 | + method_exists( 'Groups_Restrict_Categories', 'get_term_read_groups' ) // >= Groups Restrict Categories 2.0.0 | |
| 141 | + ) { | |
| 142 | + $taxonomies = Groups_Restrict_Categories::get_controlled_taxonomies(); | |
| 143 | + if ( count( $taxonomies ) > 0 ) { | |
| 144 | + $terms = wp_get_object_terms( $post_id, $taxonomies ); | |
| 145 | + if ( !( $terms instanceof WP_Error ) ) { | |
| 146 | + foreach ( $terms as $term ) { | |
| 147 | + if ( in_array( $term->taxonomy, $taxonomies ) ) { | |
| 148 | + $term_group_ids = Groups_Restrict_Categories::get_term_read_groups( $term->term_id ); | |
| 149 | + if ( count( $term_group_ids ) > 0 ) { | |
| 150 | + | |
| 151 | + if ( !empty( $term_group_ids ) ) { | |
| 152 | + $edit_term_link = self::get_edit_term_link( $term->term_id, $term->taxonomy ); | |
| 153 | + $taxonomy_label = ''; | |
| 154 | + if ( $taxonomy = get_taxonomy( $term->taxonomy ) ) { | |
| 155 | + $taxonomy_label = isset( $taxonomy->label ) ? $taxonomy->label : ''; // $taxonomy->label is already translated | |
| 156 | + $labels = isset( $taxonomy->labels ) ? $taxonomy->labels : null; | |
| 157 | + if ( $labels !== null ) { | |
| 158 | + if ( isset( $labels->singular_name ) ) { | |
| 159 | + $taxonomy_label = $labels->singular_name; // this is already translated | |
| 160 | + } | |
| 161 | + } | |
| 162 | + } | |
| 163 | + $term_taxonomy_title = !empty( $term->name ) ? $term->name : ''; | |
| 164 | + $term_taxonomy_title.= !empty( $taxonomy_label ) ? ' ' . $taxonomy_label : ''; | |
| 165 | + foreach ( $term_group_ids as $group_id ) { | |
| 166 | + if ( $group = Groups_Group::read( $group_id ) ) { | |
| 167 | + $entries[] = sprintf( | |
| 168 | + '%s <a href="%s" title="%s" style="cursor: help">%s</a>', | |
| 169 | + $group->name ? stripslashes( wp_strip_all_tags( $group->name ) ) : '', | |
| 170 | + // @since 4.0.0 guarded as $edit_term_link can be null causing deprecated notice | |
| 171 | + esc_url( $edit_term_link ?? '' ), // @phpstan-ignore nullCoalesce.variable | |
| 172 | + esc_attr( $term_taxonomy_title), | |
| 173 | + esc_html( $term->name ) | |
| 174 | + ); | |
| 175 | + } | |
| 176 | + } | |
| 177 | + } | |
| 178 | + } | |
| 179 | + } | |
| 104 | 180 | } |
| 105 | 181 | } |
| 106 | 182 | } |
| 183 | + } | |
| 184 | + if ( !empty( $entries ) ) { | |
| 185 | + sort( $entries ); | |
| 186 | + $output .= '<ul>'; | |
| 187 | + foreach ( $entries as $entry ) { | |
| 188 | + $output .= '<li>'; | |
| 189 | + $output .= $entry; // entries are already escaped for output | |
| 190 | + $output .= '</li>'; | |
| 191 | + } | |
| 107 | 192 | $output .= '</ul>'; |
| 108 | - } else { | |
| 109 | - $output .= ''; | |
| 110 | 193 | } |
| 111 | 194 | break; |
| 112 | 195 | } |
| 113 | - echo $output; | |
| 196 | + echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 114 | 197 | } |
| 198 | + | |
| 199 | + /** | |
| 200 | + * Helper to reduce query redundancy due to usage of current_user_can() in get_edit_term_link() etc. | |
| 201 | + * | |
| 202 | + * @param int $term_id | |
| 203 | + * @param string $taxonomy | |
| 204 | + * | |
| 205 | + * @return string or null if edit link could not be retrieved | |
| 206 | + */ | |
| 207 | + private static function get_edit_term_link( $term_id, $taxonomy ) { | |
| 208 | + $result = null; | |
| 209 | + $user_id = get_current_user_id(); | |
| 210 | + $cached = Groups_Cache::get( self::EDIT_TERM_LINK . '_' . $term_id . '_' . $user_id, self::CACHE_GROUP ); | |
| 211 | + if ( $cached !== null ) { | |
| 212 | + $result = $cached->get_value(); | |
| 213 | + unset( $cached ); | |
| 214 | + } else { | |
| 215 | + $result = get_edit_term_link( $term_id, $taxonomy ); | |
| 216 | + Groups_Cache::set( self::EDIT_TERM_LINK . '_' . $term_id . '_' . $user_id, $result, self::CACHE_GROUP ); | |
| 217 | + } | |
| 218 | + return $result; | |
| 219 | + } | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * Groups column is sortable. | |
| 223 | + * | |
| 224 | + * Sorting depends on the filters Groups_Admin_Posts::posts_join() and Groups_Admin_Posts::posts_orderby() | |
| 225 | + * which add the relevant group information and sort by group name. | |
| 226 | + * | |
| 227 | + * @see Groups_Admin_Posts::posts_join() | |
| 228 | + * @see Groups_Admin_Posts::posts_orderby() | |
| 229 | + * | |
| 230 | + * @param array $sortable_columns | |
| 231 | + * | |
| 232 | + * @return array | |
| 233 | + */ | |
| 234 | + public static function manage_edit_post_sortable_columns( $sortable_columns ) { | |
| 235 | + $sortable_columns[self::GROUPS] = self::GROUPS; | |
| 236 | + return $sortable_columns; | |
| 237 | + } | |
| 238 | + | |
| 115 | 239 | } |
| 116 | 240 | Groups_Admin_Post_Columns::init(); |