From e8adce77e98a85f3403b4400c9f167400f31e3f0 Mon Sep 17 00:00:00 2001 From: peptiderepo Date: Fri, 17 Apr 2026 02:43:13 +0700 Subject: [PATCH] =?UTF-8?q?refactor:=20extract=20PSA=5FPost=5FType=5FMeta?= =?UTF-8?q?=20from=20PSA=5FPost=5FType=20(343=E2=86=92193+140)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Meta box registration, rendering (core + extended), and save handler moved to PSA_Post_Type_Meta. PSA_Post_Type retains CPT/taxonomy registration, default categories, admin columns, and constants. Backward-compatible proxy methods on PSA_Post_Type. Co-Authored-By: Claude Opus 4.6 --- includes/class-psa-post-type-meta.php | 140 ++++++++++++ includes/class-psa-post-type.php | 296 +++++++------------------- peptide-search-ai.php | 1 + 3 files changed, 214 insertions(+), 223 deletions(-) create mode 100644 includes/class-psa-post-type-meta.php diff --git a/includes/class-psa-post-type-meta.php b/includes/class-psa-post-type-meta.php new file mode 100644 index 0000000..7f21480 --- /dev/null +++ b/includes/class-psa-post-type-meta.php @@ -0,0 +1,140 @@ +'; + foreach ( PSA_Post_Type::META_FIELDS as $key => $label ) { + $value = get_post_meta( $post->ID, $key, true ); + echo ''; + echo ''; + echo ''; + + if ( 'psa_source' === $key ) { + $options = array( + '' => '— Select —', 'ai-generated' => 'AI Generated', + 'pubchem' => 'PubChem Verified', 'manual' => 'Manual Entry', 'pending' => 'Pending Generation', + ); + echo ''; + } elseif ( in_array( $key, $textarea_fields, true ) ) { + echo ''; + } else { + echo ''; + } + + echo ''; + } + echo ''; + } + + /** + * Render extended meta box fields (half-life, stability, storage, etc.). + * + * @param WP_Post $post The current post object. + */ + public static function render_extended_meta_box( $post ): void { + echo ''; + foreach ( PSA_Post_Type::EXTENDED_META_FIELDS as $key => $label ) { + $value = get_post_meta( $post->ID, $key, true ); + echo ''; + echo ''; + echo ''; + } + echo '
'; + + if ( 'psa_vial_size_mg' === $key ) { + echo ' mg'; + } elseif ( 'psa_amino_acid_count' === $key ) { + echo ''; + } else { + echo ''; + } + + echo '
'; + } + + /** + * Save meta field values (core + extended) on post save. + * + * @param int $post_id The post ID. + */ + public static function save_meta( int $post_id ): void { + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput + if ( ! isset( $_POST['psa_meta_nonce'] ) || + ! wp_verify_nonce( wp_unslash( $_POST['psa_meta_nonce'] ), 'psa_save_meta' ) ) { + return; + } + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { + return; + } + if ( ! current_user_can( 'edit_post', $post_id ) ) { + return; + } + + $numeric_fields = array( 'psa_vial_size_mg', 'psa_amino_acid_count' ); + $all_fields = array_merge( PSA_Post_Type::META_FIELDS, PSA_Post_Type::EXTENDED_META_FIELDS ); + foreach ( $all_fields as $key => $label ) { + if ( isset( $_POST[ $key ] ) ) { + $raw = wp_unslash( $_POST[ $key ] ); + if ( in_array( $key, $numeric_fields, true ) ) { + $value = ( '' === trim( $raw ) ) ? '' : (string) max( 0, floatval( $raw ) ); + } else { + $value = sanitize_textarea_field( $raw ); + } + update_post_meta( $post_id, $key, $value ); + } + } + } +} diff --git a/includes/class-psa-post-type.php b/includes/class-psa-post-type.php index 3b6c707..37dd354 100755 --- a/includes/class-psa-post-type.php +++ b/includes/class-psa-post-type.php @@ -1,14 +1,16 @@ display name. */ const DEFAULT_CATEGORIES = array( 'tissue-repair' => 'Tissue Repair', 'lipid-metabolism' => 'Lipid Metabolism', @@ -48,10 +47,7 @@ class PSA_Post_Type { 'psa_pubchem_cid' => 'PubChem CID', ); - /** - * Extended meta fields added in v4.3.0 for the directory feature. - * Separated from META_FIELDS so existing code continues to work unchanged. - */ + /** Extended meta fields added in v4.3.0 for the directory feature. */ const EXTENDED_META_FIELDS = array( 'psa_half_life' => 'Half-Life', 'psa_stability' => 'Stability', @@ -64,93 +60,72 @@ class PSA_Post_Type { 'psa_amino_acid_count' => 'Amino Acid Count', ); - /** - * Register admin-only hooks (meta boxes, save, admin columns). - */ + /** Register admin-only hooks (meta boxes, save, admin columns). */ public static function init_admin(): void { - add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_boxes' ) ); - add_action( 'save_post_peptide', array( __CLASS__, 'save_meta' ) ); + add_action( 'add_meta_boxes', array( 'PSA_Post_Type_Meta', 'add_meta_boxes' ) ); + add_action( 'save_post_peptide', array( 'PSA_Post_Type_Meta', 'save_meta' ) ); add_filter( 'manage_peptide_posts_columns', array( __CLASS__, 'add_admin_columns' ) ); add_action( 'manage_peptide_posts_custom_column', array( __CLASS__, 'render_admin_column' ), 10, 2 ); } - /** - * Register the peptide CPT. - */ + /** Register the peptide CPT. */ public static function register_peptide_post_type(): void { if ( post_type_exists( 'peptide' ) ) { return; } - $labels = array( - 'name' => 'Peptides', - 'singular_name' => 'Peptide', - 'menu_name' => 'Peptides', - 'add_new' => 'Add Peptide', - 'add_new_item' => 'Add New Peptide', - 'edit_item' => 'Edit Peptide', - 'new_item' => 'New Peptide', - 'view_item' => 'View Peptide', - 'search_items' => 'Search Peptides', - 'not_found' => 'No peptides found', - 'not_found_in_trash' => 'No peptides found in Trash', - ); - - register_post_type( - 'peptide', - array( - 'labels' => $labels, - 'public' => true, - 'has_archive' => true, - 'rewrite' => array( 'slug' => 'peptides' ), - 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ), - 'menu_icon' => 'dashicons-database', - 'show_in_rest' => true, - 'capability_type' => 'post', - 'hierarchical' => false, - ) - ); + register_post_type( 'peptide', array( + 'labels' => array( + 'name' => 'Peptides', + 'singular_name' => 'Peptide', + 'menu_name' => 'Peptides', + 'add_new' => 'Add Peptide', + 'add_new_item' => 'Add New Peptide', + 'edit_item' => 'Edit Peptide', + 'new_item' => 'New Peptide', + 'view_item' => 'View Peptide', + 'search_items' => 'Search Peptides', + 'not_found' => 'No peptides found', + 'not_found_in_trash' => 'No peptides found in Trash', + ), + 'public' => true, + 'has_archive' => true, + 'rewrite' => array( 'slug' => 'peptides' ), + 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ), + 'menu_icon' => 'dashicons-database', + 'show_in_rest' => true, + 'capability_type' => 'post', + 'hierarchical' => false, + ) ); } - /** - * Register the peptide_category taxonomy on the peptide CPT. - * Called from psa_init() after CPT registration. - */ + /** Register the peptide_category taxonomy on the peptide CPT. */ public static function register_taxonomy(): void { if ( taxonomy_exists( 'peptide_category' ) ) { return; } - $labels = array( - 'name' => __( 'Peptide Categories', 'peptide-search-ai' ), - 'singular_name' => __( 'Peptide Category', 'peptide-search-ai' ), - 'search_items' => __( 'Search Categories', 'peptide-search-ai' ), - 'all_items' => __( 'All Categories', 'peptide-search-ai' ), - 'edit_item' => __( 'Edit Category', 'peptide-search-ai' ), - 'update_item' => __( 'Update Category', 'peptide-search-ai' ), - 'add_new_item' => __( 'Add New Category', 'peptide-search-ai' ), - 'new_item_name' => __( 'New Category Name', 'peptide-search-ai' ), - 'menu_name' => __( 'Categories', 'peptide-search-ai' ), - ); - - register_taxonomy( - 'peptide_category', - 'peptide', - array( - 'labels' => $labels, - 'hierarchical' => true, - 'public' => true, - 'show_in_rest' => true, - 'show_admin_column' => true, - 'rewrite' => array( 'slug' => 'peptide-category' ), - ) - ); + register_taxonomy( 'peptide_category', 'peptide', array( + 'labels' => array( + 'name' => __( 'Peptide Categories', 'peptide-search-ai' ), + 'singular_name' => __( 'Peptide Category', 'peptide-search-ai' ), + 'search_items' => __( 'Search Categories', 'peptide-search-ai' ), + 'all_items' => __( 'All Categories', 'peptide-search-ai' ), + 'edit_item' => __( 'Edit Category', 'peptide-search-ai' ), + 'update_item' => __( 'Update Category', 'peptide-search-ai' ), + 'add_new_item' => __( 'Add New Category', 'peptide-search-ai' ), + 'new_item_name' => __( 'New Category Name', 'peptide-search-ai' ), + 'menu_name' => __( 'Categories', 'peptide-search-ai' ), + ), + 'hierarchical' => true, + 'public' => true, + 'show_in_rest' => true, + 'show_admin_column' => true, + 'rewrite' => array( 'slug' => 'peptide-category' ), + ) ); } - /** - * Pre-populate default category terms on activation. - * Safe to call multiple times — skips existing terms. - */ + /** Pre-populate default category terms on activation. Idempotent. */ public static function populate_default_categories(): void { foreach ( self::DEFAULT_CATEGORIES as $slug => $name ) { if ( ! term_exists( $slug, 'peptide_category' ) ) { @@ -159,138 +134,6 @@ class PSA_Post_Type { } } - /** - * Add meta boxes to the peptide editor. - */ - public static function add_meta_boxes(): void { - add_meta_box( - 'psa_peptide_data', - 'Peptide Scientific Data', - array( __CLASS__, 'render_meta_box' ), - 'peptide', - 'normal', - 'high' - ); - add_meta_box( - 'psa_extended_data', - 'Extended Research Data', - array( __CLASS__, 'render_extended_meta_box' ), - 'peptide', - 'normal', - 'default' - ); - } - - /** - * Render the core meta box fields. - * - * @param WP_Post $post The current post object. - * @return void - */ - public static function render_meta_box( $post ): void { - wp_nonce_field( 'psa_save_meta', 'psa_meta_nonce' ); - - $textarea_fields = array( - 'psa_sequence', - 'psa_mechanism', - 'psa_research_apps', - 'psa_safety_profile', - 'psa_dosage_info', - 'psa_references', - ); - - echo ''; - foreach ( self::META_FIELDS as $key => $label ) { - $value = get_post_meta( $post->ID, $key, true ); - echo ''; - echo ''; - echo ''; - } - echo '
'; - - if ( 'psa_source' === $key ) { - $options = array( - '' => '— Select —', - 'ai-generated' => 'AI Generated', - 'pubchem' => 'PubChem Verified', - 'manual' => 'Manual Entry', - 'pending' => 'Pending Generation', - ); - echo ''; - } elseif ( in_array( $key, $textarea_fields, true ) ) { - echo ''; - } else { - echo ''; - } - - echo '
'; - } - - /** - * Render the extended meta box fields (half-life, stability, storage, etc.). - * - * @param WP_Post $post The current post object. - * @return void - */ - public static function render_extended_meta_box( $post ): void { - echo ''; - foreach ( self::EXTENDED_META_FIELDS as $key => $label ) { - $value = get_post_meta( $post->ID, $key, true ); - echo ''; - echo ''; - echo ''; - } - echo '
'; - - if ( 'psa_vial_size_mg' === $key ) { - echo ' mg'; - } elseif ( 'psa_amino_acid_count' === $key ) { - echo ''; - } else { - echo ''; - } - - echo '
'; - } - - /** - * Save meta field values (core + extended). - * - * @param int $post_id The post ID. - */ - public static function save_meta( int $post_id ): void { - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput - if ( ! isset( $_POST['psa_meta_nonce'] ) || - ! wp_verify_nonce( wp_unslash( $_POST['psa_meta_nonce'] ), 'psa_save_meta' ) ) { - return; - } - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { - return; - } - if ( ! current_user_can( 'edit_post', $post_id ) ) { - return; - } - - $numeric_fields = array( 'psa_vial_size_mg', 'psa_amino_acid_count' ); - $all_fields = array_merge( self::META_FIELDS, self::EXTENDED_META_FIELDS ); - foreach ( $all_fields as $key => $label ) { - if ( isset( $_POST[ $key ] ) ) { - $raw = wp_unslash( $_POST[ $key ] ); - if ( in_array( $key, $numeric_fields, true ) ) { - // Numeric fields: store as non-negative number or empty string. - $value = ( '' === trim( $raw ) ) ? '' : (string) max( 0, floatval( $raw ) ); - } else { - $value = sanitize_textarea_field( $raw ); - } - update_post_meta( $post_id, $key, $value ); - } - } - } - /** * Add custom columns to the peptide admin list. * @@ -301,7 +144,6 @@ class PSA_Post_Type { $new = array(); foreach ( $columns as $key => $label ) { $new[ $key ] = $label; - // Insert source column after title. if ( 'title' === $key ) { $new['psa_source'] = __( 'Source', 'peptide-search-ai' ); } @@ -319,11 +161,8 @@ class PSA_Post_Type { if ( 'psa_source' === $column ) { $source = get_post_meta( $post_id, 'psa_source', true ); $labels = array( - 'ai-generated' => 'AI', - 'pubchem' => 'PubChem', - 'manual' => 'Manual', - 'pending' => 'Pending', - 'failed' => 'Failed', + 'ai-generated' => 'AI', 'pubchem' => 'PubChem', + 'manual' => 'Manual', 'pending' => 'Pending', 'failed' => 'Failed', ); echo esc_html( $labels[ $source ] ?? '—' ); } @@ -335,9 +174,20 @@ class PSA_Post_Type { * @return array Meta keys. */ public static function get_all_meta_keys(): array { - return array_merge( - array_keys( self::META_FIELDS ), - array_keys( self::EXTENDED_META_FIELDS ) - ); + return array_merge( array_keys( self::META_FIELDS ), array_keys( self::EXTENDED_META_FIELDS ) ); } + + // ── Backward-compatible proxies (meta boxes moved to PSA_Post_Type_Meta) ── + + /** @see PSA_Post_Type_Meta::add_meta_boxes() */ + public static function add_meta_boxes(): void { PSA_Post_Type_Meta::add_meta_boxes(); } + + /** @see PSA_Post_Type_Meta::render_meta_box() */ + public static function render_meta_box( $post ): void { PSA_Post_Type_Meta::render_meta_box( $post ); } + + /** @see PSA_Post_Type_Meta::render_extended_meta_box() */ + public static function render_extended_meta_box( $post ): void { PSA_Post_Type_Meta::render_extended_meta_box( $post ); } + + /** @see PSA_Post_Type_Meta::save_meta() */ + public static function save_meta( int $post_id ): void { PSA_Post_Type_Meta::save_meta( $post_id ); } } diff --git a/peptide-search-ai.php b/peptide-search-ai.php index 79bb92e..caee81a 100755 --- a/peptide-search-ai.php +++ b/peptide-search-ai.php @@ -23,6 +23,7 @@ define( 'PSA_PLUGIN_FILE', __FILE__ ); require_once PSA_PLUGIN_DIR . 'includes/class-psa-config.php'; require_once PSA_PLUGIN_DIR . 'includes/class-psa-encryption.php'; require_once PSA_PLUGIN_DIR . 'includes/class-psa-post-type.php'; +require_once PSA_PLUGIN_DIR . 'includes/class-psa-post-type-meta.php'; require_once PSA_PLUGIN_DIR . 'includes/class-psa-search-handler.php'; require_once PSA_PLUGIN_DIR . 'includes/class-psa-search.php'; require_once PSA_PLUGIN_DIR . 'includes/class-psa-cost-tracker.php';