1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
<?php
final class Papi_Admin_Columns {
private $post_type;
private $taxonomy;
public function __construct() {
$this->setup_globals();
$this->setup_actions();
$this->setup_filters();
}
private function get_meta_type_value() {
return empty( $this->taxonomy ) ? $this->post_type : $this->taxonomy;
}
public function manage_page_type_posts_columns( array $defaults = [] ) {
if ( ! in_array( $this->post_type, papi_get_post_types() ) && ! in_array( $this->taxonomy, papi_get_taxonomies() ) ) {
return $defaults;
}
if ( apply_filters( 'papi/settings/column_hide_' . $this->get_meta_type_value(), false ) ) {
return $defaults;
}
$defaults['entry_type'] = apply_filters(
'papi/settings/column_title_' . $this->get_meta_type_value(),
__( 'Type', 'papi' )
);
return $defaults;
}
public function manage_page_type_posts_custom_column( $column_name, $post_id, $term_id = null ) {
if ( ! in_array( $this->post_type, papi_get_post_types() ) && ! in_array( $this->taxonomy, papi_get_taxonomies() ) ) {
return;
}
if ( apply_filters( 'papi/settings/column_hide_' . $this->get_meta_type_value(), false ) ) {
return;
}
if ( $column_name !== 'entry_type' && $post_id !== 'entry_type' ) {
return;
}
$entry_type = papi_get_entry_type_by_meta_id(
is_numeric( $post_id ) ? $post_id : $term_id,
papi_get_meta_type()
);
if ( ! is_null( $entry_type ) ) {
echo esc_html( $entry_type->name );
} else {
$post = ! empty( $this->post_type ) && empty( $this->taxonomy );
$arg = $post ? papi_get_post_type() : papi_get_taxonomy();
$type = $post ? 'page' : 'taxonomy';
echo esc_html( call_user_func_array( "papi_filter_settings_standard_{$type}_type_name", [$arg] ) );
}
}
public function pre_get_posts( WP_Query $query ) {
global $pagenow;
if ( $pagenow === 'edit.php' && ! is_null( papi_get_qs( 'page_type' ) ) ) {
if ( papi_get_qs( 'page_type' ) === 'papi-standard-page' ) {
$query->set( 'meta_query', [
[
'key' => papi_get_page_type_key(),
'compare' => 'NOT EXISTS'
]
] );
} else {
$query->set( 'meta_key', papi_get_page_type_key() );
$query->set( 'meta_value', papi_get_qs( 'page_type' ) );
}
}
return $query;
}
public function restrict_page_types() {
$post_types = papi_get_post_types();
if ( in_array( $this->post_type, $post_types ) ) {
$page_types = papi_get_all_page_types( $this->post_type );
$page_types = array_map( function ( $page_type ) {
return [
'name' => $page_type->name,
'value' => $page_type->get_id()
];
}, $page_types );
if ( papi_filter_settings_show_standard_page_type_in_filter( $this->post_type ) ) {
$page_types[] = [
'name' => papi_filter_settings_standard_page_type_name( $this->post_type ),
'value' => 'papi-standard-page'
];
}
usort( $page_types, function ( $a, $b ) {
return strcmp(
strtolower( $a['name'] ),
strtolower( $b['name'] )
);
} );
?>
<select name="page_type" class="postform">
<option value="0" selected><?php _e( 'All types', 'papi' ); ?></option>
<?php
foreach ( $page_types as $page_type ) {
printf(
'<option value="%s" %s>%s</option>',
$page_type['value'],
papi_get_qs( 'page_type' ) === $page_type['value'] ? ' selected' : '',
$page_type['name']
);
}
?>
</select>
<?php
}
}
private function setup_actions() {
if ( ! empty( $this->post_type ) && empty( $this->taxonomy ) ) {
add_filter( 'pre_get_posts', [$this, 'pre_get_posts'] );
add_action( 'restrict_manage_posts', [ $this, 'restrict_page_types'] );
add_action( sprintf( 'manage_%s_posts_custom_column', $this->post_type ), [
$this,
'manage_page_type_posts_custom_column'
], 10, 2 );
}
if ( ! empty( $this->taxonomy ) ) {
add_action( sprintf( 'manage_%s_custom_column', $this->taxonomy ), [$this, 'manage_page_type_posts_custom_column'], 10, 3 );
}
}
private function setup_filters() {
if ( ! empty( $this->post_type ) && empty( $this->taxonomy ) ) {
add_filter( sprintf( 'manage_%s_posts_columns', $this->post_type ), [$this, 'manage_page_type_posts_columns'] );
}
if ( ! empty( $this->taxonomy ) ) {
add_filter( sprintf( 'manage_edit-%s_columns', $this->taxonomy ), [$this, 'manage_page_type_posts_columns'] );
}
}
private function setup_globals() {
$this->post_type = papi_get_post_type();
$this->taxonomy = papi_get_taxonomy();
}
}
if ( is_admin() ) {
new Papi_Admin_Columns;
}