|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Conversation history hook handler. |
| 4 | + * |
| 5 | + * @package hamelp |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Hametuha\Hamelp\Hooks; |
| 9 | + |
| 10 | +use Hametuha\Hamelp\Pattern\Singleton; |
| 11 | +use Hametuha\Hamelp\Services\ConversationStore; |
| 12 | + |
| 13 | +/** |
| 14 | + * Registers the private conversation post type used for question mining. |
| 15 | + * |
| 16 | + * The post type is intentionally non-public: it is not queryable on the front |
| 17 | + * end and is excluded from search and the REST listing. It is only surfaced in |
| 18 | + * wp-admin so site owners can review what visitors actually asked. |
| 19 | + */ |
| 20 | +class ConversationHistory extends Singleton { |
| 21 | + |
| 22 | + /** |
| 23 | + * Initialize hooks. |
| 24 | + */ |
| 25 | + protected function init() { |
| 26 | + add_action( 'init', [ $this, 'register_post_type' ] ); |
| 27 | + add_filter( 'manage_' . ConversationStore::POST_TYPE . '_posts_columns', [ $this, 'columns' ] ); |
| 28 | + add_action( 'manage_' . ConversationStore::POST_TYPE . '_posts_custom_column', [ $this, 'render_column' ], 10, 2 ); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Register the conversation post type. |
| 33 | + */ |
| 34 | + public function register_post_type() { |
| 35 | + register_post_type( |
| 36 | + ConversationStore::POST_TYPE, |
| 37 | + [ |
| 38 | + 'label' => __( 'Conversations', 'hamelp' ), |
| 39 | + 'labels' => [ |
| 40 | + 'name' => __( 'Conversations', 'hamelp' ), |
| 41 | + 'singular_name' => __( 'Conversation', 'hamelp' ), |
| 42 | + 'menu_name' => __( 'AI Conversations', 'hamelp' ), |
| 43 | + ], |
| 44 | + 'public' => false, |
| 45 | + 'publicly_queryable' => false, |
| 46 | + 'exclude_from_search' => true, |
| 47 | + 'show_ui' => true, |
| 48 | + 'show_in_menu' => true, |
| 49 | + 'show_in_rest' => false, |
| 50 | + 'has_archive' => false, |
| 51 | + 'rewrite' => false, |
| 52 | + 'menu_icon' => 'dashicons-format-chat', |
| 53 | + 'menu_position' => 21, |
| 54 | + 'supports' => [ 'title', 'editor', 'author' ], |
| 55 | + 'map_meta_cap' => true, |
| 56 | + // Read-only data: prevent creating new conversations by hand. |
| 57 | + 'capabilities' => [ |
| 58 | + 'create_posts' => 'do_not_allow', |
| 59 | + ], |
| 60 | + ] |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Customize admin list columns. |
| 66 | + * |
| 67 | + * @param array $columns Existing columns. |
| 68 | + * @return array |
| 69 | + */ |
| 70 | + public function columns( $columns ) { |
| 71 | + $new = []; |
| 72 | + foreach ( $columns as $key => $label ) { |
| 73 | + if ( 'title' === $key ) { |
| 74 | + $new[ $key ] = __( 'First Question', 'hamelp' ); |
| 75 | + $new['turns'] = __( 'Turns', 'hamelp' ); |
| 76 | + } else { |
| 77 | + $new[ $key ] = $label; |
| 78 | + } |
| 79 | + } |
| 80 | + return $new; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Render custom column values. |
| 85 | + * |
| 86 | + * @param string $column Column key. |
| 87 | + * @param int $post_id Post ID. |
| 88 | + */ |
| 89 | + public function render_column( $column, $post_id ) { |
| 90 | + if ( 'turns' !== $column ) { |
| 91 | + return; |
| 92 | + } |
| 93 | + $raw = get_post_meta( $post_id, ConversationStore::META_TURNS, true ); |
| 94 | + $turns = $raw ? json_decode( $raw, true ) : []; |
| 95 | + echo esc_html( is_array( $turns ) ? (string) count( $turns ) : '0' ); |
| 96 | + } |
| 97 | +} |
0 commit comments