settings = get_option( 'aitm_settings', array() );
$this->init_hooks();
}
/**
* Initialize hooks
*/
private function init_hooks() {
// Get enabled post types with fallback to default
$enabled_post_types = isset( $this->settings['enabled_post_types'] )
? $this->settings['enabled_post_types']
: array( 'post', 'page' );
// Ensure it's an array
if ( ! is_array( $enabled_post_types ) ) {
$enabled_post_types = array( 'post', 'page' );
}
if ( empty( $enabled_post_types ) ) {
// Default to post and page if empty
$enabled_post_types = array( 'post', 'page' );
}
// Add thumbnail column for each enabled post type
foreach ( $enabled_post_types as $post_type ) {
// Skip if post type doesn't exist
if ( ! post_type_exists( $post_type ) ) {
continue;
}
// Register column
add_filter( "manage_{$post_type}_posts_columns", array( $this, 'add_thumbnail_column' ), 5 );
// Render column content
add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'render_thumbnail_column' ), 10, 2 );
}
// Add styles for thumbnail column
add_action( 'admin_head', array( $this, 'add_column_styles' ) );
}
/**
* Add thumbnail column to posts list
*
* @param array $columns Existing columns.
* @return array Modified columns.
*/
public function add_thumbnail_column( $columns ) {
// Create new columns array with thumbnail as first column
$new_columns = array();
// Add thumbnail column first
$new_columns['aitm_thumbnail'] = __( 'Thumbnail', 'ai-thumbnails-maker' );
// Add all other columns
foreach ( $columns as $key => $value ) {
$new_columns[ $key ] = $value;
}
return $new_columns;
}
/**
* Render thumbnail column content
*
* @param string $column_name Column name.
* @param int $post_id Post ID.
*/
public function render_thumbnail_column( $column_name, $post_id ) {
if ( $column_name !== 'aitm_thumbnail' ) {
return;
}
$thumbnail_id = get_post_thumbnail_id( $post_id );
if ( $thumbnail_id ) {
// Get thumbnail URL
$thumbnail_url = wp_get_attachment_image_url( $thumbnail_id, 'thumbnail' );
if ( $thumbnail_url ) {
printf(
'
',
esc_url( $thumbnail_url ),
esc_attr( get_the_title( $post_id ) )
);
} else {
echo '—';
}
} else {
echo '—';
}
}
/**
* Add styles for thumbnail column
*/
public function add_column_styles() {
global $pagenow;
// Only add styles on post list pages
if ( $pagenow !== 'edit.php' && $pagenow !== 'upload.php' ) {
return;
}
?>