` and its
* `
` cells carry `column-aipt_ai`). WooCommerce and Yoast SEO give
* every one of their own product-list columns an explicit width; under
* the list table's `table-layout:fixed`, a column with none can be
* starved to 0px once anything longer than the short "AI" badge is
* printed into it (see the `aipt_product_list_column_css` filter below).
*/
private const COLUMN_WIDTH_CSS = '.column-aipt_ai{width:160px;min-width:160px}';
public static function add_column( array $columns ): array {
$columns['aipt_ai'] = __( 'AI', 'ai-product-tools' );
return $columns;
}
/**
* Reserves the aipt_ai column's width on the product list screen.
* `aipt_product_list_column_css` lets a registrant that prints its own
* content into the same column (Catalog watch) add CSS of its own,
* sharing this one stylesheet instead of enqueueing a second one.
*
* @param string $hook Current admin page hook suffix.
* @return void
*/
public static function enqueue_styles( string $hook ): void {
if ( 'edit.php' !== $hook ) {
return;
}
$screen = get_current_screen();
if ( ! $screen || 'product' !== $screen->post_type ) {
return;
}
/**
* Additional CSS for the product list's aipt_ai column, appended
* after the base width reservation.
*
* @param string $css CSS so far.
*/
$css = self::COLUMN_WIDTH_CSS . (string) apply_filters( 'aipt_product_list_column_css', '' );
if ( ! wp_style_is( self::STYLE_HANDLE, 'registered' ) ) {
wp_register_style( self::STYLE_HANDLE, false, array(), AIPT_VERSION );
}
wp_enqueue_style( self::STYLE_HANDLE );
wp_add_inline_style( self::STYLE_HANDLE, $css );
}
public static function render_column( string $column, int $post_id ): void {
if ( 'aipt_ai' !== $column ) {
return;
}
if ( ! get_post_meta( $post_id, MetaKeys::ROLLUP, true ) ) {
return;
}
$title = self::build_title( $post_id );
printf(
'%s',
esc_attr( 'aipt-ai-badge' ),
esc_attr( $title ),
esc_html( __( 'AI', 'ai-product-tools' ) )
);
}
private static function build_title( int $post_id ): string {
$types = array();
foreach ( MetaKeys::CONTENT_TYPE_TO_KEY as $type => $key ) {
if ( get_post_meta( $post_id, $key, true ) ) {
$types[] = str_replace( '_', ' ', $type );
}
}
if ( array() === $types ) {
return __( 'AI processed', 'ai-product-tools' );
}
return __( 'AI processed:', 'ai-product-tools' ) . ' ' . implode( ', ', $types );
}
}
|