Pods CPT Admin Fields
<?php
/**
* Name: CPT Admin Fields
*
* Description: Allow adding fields to the CPT editor table.
*
* Version: 2.3
*
* Category: Advanced
*
* Tableless Mode: No
*
* @package Pods\Components
* @subpackage CPT Admin Fields
*/
if ( class_exists( 'Pods_CPT_Admin_Fields' ) ) {
return;
}
class Pods_CPT_Admin_Fields extends PodsComponent {
/**
* Do things like register/enqueue scripts and stylesheets
*
* @since 2.3
*/
public function __construct() {
if ( ! pods_tableless() ) {
add_filter( 'pods_admin_setup_edit_options_post_type', array( $this, 'setup_edit_options' ), 10, 2 );
add_filter( 'pods_register_post_type', array( $this, 'setup_post_type' ), 10, 2 );
}
}
function setup_post_type( $options, $post_type ) {
add_filter( "manage_edit-{$post_type}_sortable_columns", array( $this, "admin_columns" ), 10, 1 );
add_filter( "manage_edit-{$post_type}_columns", array( $this, "admin_columns" ), 10, 1 );
add_action( "manage_{$post_type}_posts_custom_column", array( $this, "custom_column" ), 10, 2 );
return $options;
}
function setup_edit_options( $options, $pod ) {
$options['admin-ui']['ui_fields_manage'] = array(
'label' => __( 'Admin Table Columns', 'pods' ),
'help' => __( 'help', 'pods' ),
'type' => 'pick',
'default' => array(),
'data' => array(),
'pick_format_type' => 'multi'
);
if ( ! empty( $pod['fields'] ) ) {
$options['admin-ui']['ui_fields_manage']['data']['title'] = __( 'Title', 'pods' );
// These defaults match WordPress default settings
$options['admin-ui']['ui_fields_manage']['default'][] = 'title';
$options['admin-ui']['ui_fields_manage']['default'][] = 'date';
foreach ( $pod['fields'] as $field ) {
$options['admin-ui']['ui_fields_manage']['data'][ $field['name'] ] = $field['label'];
}
$options['admin-ui']['ui_fields_manage']['data']['author'] = __( 'Author', 'pods' );
$options['admin-ui']['ui_fields_manage']['data']['date'] = __( 'Date', 'pods' );
} else {
unset( $options['admin-ui']['ui_fields_manage'] );
}
return $options;
}
public function custom_column( $column_name, $post_id = null ) {
preg_match( '/^manage_(.+)_posts_custom_column$/', current_action(), $matches );
if ( $matches[1] ) {
$pod = Pods( $matches[1], $post_id );
echo $pod->display( $column_name, true );
}
}
public function admin_columns( $columns ) {
$post_types = PodsMeta::$post_types;
preg_match( '/^manage_edit-(.*)(_sortable){0,1}_columns$/U', current_action(), $matches );
$pod = $matches[1];
$sortable = isset( $matches[2] ) ? true : false;
if ( ! matches ) {
return $columns;
}
foreach ( $post_types as $type ) {
if ( $pod === $type['name'] ) {
if ( empty( $type['options']['ui_fields_manage'] ) ) {
return $columns;
}
if ( $sortable ) {
$columns = array();
} else {
$columns = array( 'cb' => '<input type="checkbox" />' );
}
foreach ( $type['options']['ui_fields_manage'] as $field ) {
if ( isset( $type['fields'][ $field ] ) ) {
$columns[ $field ] = $type['fields'][ $field ]['label'];
} elseif ( isset( $type['object_fields'][ $field ] ) ) {
$columns[ $field ] = $type['object_fields'][ $field ]['label'];
} elseif ( isset( $type['object_fields'][ 'post_' . $field ] ) ) {
// Also check post_ prefix
$columns[ $field ] = $type['object_fields'][ 'post_' . $field ]['label'];
}
}
// Stop after first matching Pod
break;
}
}
return $columns;
}
}