| 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 |
* @author Karim Rahimpur |
| 19 |
* @package groups |
| 20 |
* @since groups 1.4.2 |
| 21 |
*/ |
| 22 |
|
| 23 |
if ( !defined( 'ABSPATH' ) ) { |
| 24 |
exit; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Post column extensions. |
| 29 |
*/ |
| 30 |
class Groups_Admin_Post_Columns { |
| 31 |
|
| 32 |
/** |
| 33 |
* Groups column header id. |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
const GROUPS = 'groups-read'; |
| 37 |
|
| 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 |
/** |
| 60 |
* Adds an admin_init action. |
| 61 |
*/ |
| 62 |
public static function init() { |
| 63 |
add_action( 'admin_init', array( __CLASS__, 'admin_init' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Adds the filters and actions only for users who have the right |
| 68 |
* Groups permissions and for the post types that have access |
| 69 |
* restrictions enabled. |
| 70 |
*/ |
| 71 |
public static function admin_init() { |
| 72 |
if ( Groups_User::current_user_can( GROUPS_ACCESS_GROUPS ) ) { |
| 73 |
$post_types = get_post_types( array( 'public' => true ) ); |
| 74 |
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() ); |
| 75 |
foreach ( $post_types as $post_type ) { |
| 76 |
if ( !isset( $post_types_option[$post_type]['add_meta_box'] ) || $post_types_option[$post_type]['add_meta_box'] ) { |
| 77 |
if ( ( $post_type == 'attachment' ) ) { |
| 78 |
// filters to display the media's access restriction groups |
| 79 |
add_filter( 'manage_media_columns', array( __CLASS__, 'columns' ) ); |
| 80 |
// args: string $column_name, int $media_id |
| 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' ) ); |
| 84 |
} else { |
| 85 |
// filters to display the posts' access restriction groups |
| 86 |
add_filter( 'manage_' . $post_type . '_posts_columns', array( __CLASS__, 'columns' ) ); |
| 87 |
// args: string $column_name, int $post_id |
| 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' ) ); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Adds a new column to the post type's table showing the access |
| 99 |
* restriction groups. |
| 100 |
* |
| 101 |
* @param array $column_headers |
| 102 |
* |
| 103 |
* @return array column headers |
| 104 |
*/ |
| 105 |
public static function columns( $column_headers ) { |
| 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' ) |
| 111 |
); |
| 112 |
return $column_headers; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Renders custom column content. |
| 117 |
* |
| 118 |
* @param string $column_name |
| 119 |
* @param int $post_id |
| 120 |
*/ |
| 121 |
public static function custom_column( $column_name, $post_id ) { |
| 122 |
$output = ''; |
| 123 |
switch ( $column_name ) { |
| 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 |
} |
| 180 |
} |
| 181 |
} |
| 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 |
} |
| 192 |
$output .= '</ul>'; |
| 193 |
} |
| 194 |
break; |
| 195 |
} |
| 196 |
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 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 |
|
| 239 |
} |
| 240 |
Groups_Admin_Post_Columns::init(); |
| 241 |
|