feat: initial peptide-repo-core plugin (v0.1.0) — canonical schema sprint
Implements ADR-0002: canonical peptide schema as shared data layer for the peptiderepo.com ecosystem. All Tier 1 tools (Reconstitution Calculator, Dosage Reference, Legal Status Tracker, PSA enhancements) build on this. Includes: - pr_peptide CPT with REST, taxonomies, 11 meta fields - 3 custom tables: pr_dosing_rows, pr_legal_cells, pr_ai_candidate_queue - Sequential migration runner with schema versioning - 4 typed DTOs and 4 repository classes (full CRUD) - REST API (pr-core/v1): 6 endpoints (read public, write auth-gated) - Admin UI: 3 meta boxes, custom columns, AI candidate queue page - Shared disclaimer component (shortcode + static API) - JSON-LD Drug schema emission on single peptide pages - Seed data fixture (3 peptides, 10 dosing rows, 5 legal cells) - Full teardown in uninstall.php - CI: PHP lint 8.0-8.2, PHPCS WordPress, 300-line check All 22 PHP files under 300 lines. Highest: peptide-cpt at 265. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Migration 0001: Create the pr_dosing_rows table.
|
||||
*
|
||||
* What: Creates the high-cardinality dosing data table (1:many with peptide).
|
||||
* Who calls it: PR_Core_Migration_Runner::run_pending().
|
||||
* Dependencies: WordPress $wpdb, dbDelta().
|
||||
*
|
||||
* @see ARCHITECTURE.md — Table schema specification.
|
||||
*/
|
||||
class PR_Core_Migration_0001_Dosing_Rows {
|
||||
|
||||
/**
|
||||
* Create the pr_dosing_rows table.
|
||||
*
|
||||
* Side effects: database DDL via dbDelta().
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up(): void {
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'pr_dosing_rows';
|
||||
$charset = $wpdb->get_charset_collate();
|
||||
|
||||
$sql = "CREATE TABLE {$table} (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
peptide_id BIGINT UNSIGNED NOT NULL,
|
||||
dose_min DECIMAL(12,4) NULL,
|
||||
dose_max DECIMAL(12,4) NULL,
|
||||
dose_unit VARCHAR(16) NOT NULL,
|
||||
route VARCHAR(32) NOT NULL,
|
||||
frequency VARCHAR(64) NULL,
|
||||
duration_value INT UNSIGNED NULL,
|
||||
duration_unit VARCHAR(16) NULL,
|
||||
population VARCHAR(32) NOT NULL,
|
||||
indication VARCHAR(255) NULL,
|
||||
evidence_strength VARCHAR(32) NOT NULL,
|
||||
study_title VARCHAR(500) NULL,
|
||||
study_year SMALLINT UNSIGNED NULL,
|
||||
citation_pubmed_id VARCHAR(16) NULL,
|
||||
citation_doi VARCHAR(128) NULL,
|
||||
citation_url VARCHAR(500) NULL,
|
||||
notes TEXT NULL,
|
||||
schema_version SMALLINT UNSIGNED NOT NULL DEFAULT 1,
|
||||
source VARCHAR(32) NOT NULL DEFAULT 'manual',
|
||||
ai_candidate_id BIGINT UNSIGNED NULL,
|
||||
added_by BIGINT UNSIGNED NOT NULL,
|
||||
added_at DATETIME NOT NULL,
|
||||
reviewed_by BIGINT UNSIGNED NULL,
|
||||
reviewed_at DATETIME NULL,
|
||||
superseded_by_id BIGINT UNSIGNED NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_peptide (peptide_id),
|
||||
KEY idx_peptide_route (peptide_id, route),
|
||||
KEY idx_peptide_population (peptide_id, population),
|
||||
KEY idx_pubmed (citation_pubmed_id)
|
||||
) {$charset};";
|
||||
|
||||
dbDelta( $sql );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Migration 0002: Create the pr_legal_cells table.
|
||||
*
|
||||
* What: Creates the per-country legal status table (1:many with peptide).
|
||||
* Who calls it: PR_Core_Migration_Runner::run_pending().
|
||||
* Dependencies: WordPress $wpdb, dbDelta().
|
||||
*
|
||||
* Uniqueness: only one active cell per peptide x country (superseded_by_id NULL).
|
||||
*
|
||||
* @see ARCHITECTURE.md — Table schema specification.
|
||||
*/
|
||||
class PR_Core_Migration_0002_Legal_Cells {
|
||||
|
||||
/** @var string[] Valid legal status enum values. */
|
||||
public const STATUSES = [
|
||||
'prescription',
|
||||
'ruo',
|
||||
'otc',
|
||||
'restricted',
|
||||
'banned',
|
||||
'unclear',
|
||||
];
|
||||
|
||||
/**
|
||||
* Create the pr_legal_cells table.
|
||||
*
|
||||
* Side effects: database DDL via dbDelta().
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up(): void {
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'pr_legal_cells';
|
||||
$charset = $wpdb->get_charset_collate();
|
||||
|
||||
$sql = "CREATE TABLE {$table} (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
peptide_id BIGINT UNSIGNED NOT NULL,
|
||||
country_code CHAR(2) NOT NULL,
|
||||
status VARCHAR(32) NOT NULL,
|
||||
regulatory_framework VARCHAR(128) NULL,
|
||||
regulatory_text_url VARCHAR(500) NULL,
|
||||
regulatory_text_quote TEXT NULL,
|
||||
notes TEXT NULL,
|
||||
last_verified_at DATETIME NOT NULL,
|
||||
schema_version SMALLINT UNSIGNED NOT NULL DEFAULT 1,
|
||||
reviewer_id BIGINT UNSIGNED NOT NULL,
|
||||
superseded_by_id BIGINT UNSIGNED NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_peptide (peptide_id),
|
||||
KEY idx_country (country_code)
|
||||
) {$charset};";
|
||||
|
||||
dbDelta( $sql );
|
||||
|
||||
// dbDelta doesn't support UNIQUE with nullable columns well.
|
||||
// Add the unique constraint manually if not present.
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$index_exists = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND INDEX_NAME = %s",
|
||||
DB_NAME,
|
||||
$table,
|
||||
'uq_peptide_country_active'
|
||||
)
|
||||
);
|
||||
|
||||
if ( '0' === $index_exists ) {
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$wpdb->query(
|
||||
"ALTER TABLE {$table} ADD UNIQUE KEY uq_peptide_country_active (peptide_id, country_code, superseded_by_id)"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Migration 0003: Create the pr_ai_candidate_queue table.
|
||||
*
|
||||
* What: Creates the AI-extracted dosing candidate queue table.
|
||||
* Who calls it: PR_Core_Migration_Runner::run_pending().
|
||||
* Dependencies: WordPress $wpdb, dbDelta().
|
||||
*
|
||||
* Approved rows are copied into pr_dosing_rows with source='ai-candidate-approved'
|
||||
* and ai_candidate_id set; queue row marked 'merged'.
|
||||
*
|
||||
* @see ARCHITECTURE.md — AI-assist candidate-extraction pipeline.
|
||||
*/
|
||||
class PR_Core_Migration_0003_Candidate_Queue {
|
||||
|
||||
/** @var string[] Valid queue status enum values. */
|
||||
public const STATUSES = [
|
||||
'pending',
|
||||
'approved',
|
||||
'rejected',
|
||||
'merged',
|
||||
];
|
||||
|
||||
/**
|
||||
* Create the pr_ai_candidate_queue table.
|
||||
*
|
||||
* Side effects: database DDL via dbDelta().
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up(): void {
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'pr_ai_candidate_queue';
|
||||
$charset = $wpdb->get_charset_collate();
|
||||
|
||||
$sql = "CREATE TABLE {$table} (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
peptide_id BIGINT UNSIGNED NOT NULL,
|
||||
dose_min DECIMAL(12,4) NULL,
|
||||
dose_max DECIMAL(12,4) NULL,
|
||||
dose_unit VARCHAR(16) NOT NULL,
|
||||
route VARCHAR(32) NOT NULL,
|
||||
frequency VARCHAR(64) NULL,
|
||||
duration_value INT UNSIGNED NULL,
|
||||
duration_unit VARCHAR(16) NULL,
|
||||
population VARCHAR(32) NOT NULL,
|
||||
indication VARCHAR(255) NULL,
|
||||
evidence_strength VARCHAR(32) NOT NULL,
|
||||
study_title VARCHAR(500) NULL,
|
||||
study_year SMALLINT UNSIGNED NULL,
|
||||
citation_pubmed_id VARCHAR(16) NULL,
|
||||
citation_doi VARCHAR(128) NULL,
|
||||
citation_url VARCHAR(500) NULL,
|
||||
notes TEXT NULL,
|
||||
extraction_confidence FLOAT NOT NULL DEFAULT 0,
|
||||
queue_status VARCHAR(16) NOT NULL DEFAULT 'pending',
|
||||
extracted_at DATETIME NOT NULL,
|
||||
reviewed_by BIGINT UNSIGNED NULL,
|
||||
reviewed_at DATETIME NULL,
|
||||
reviewer_notes TEXT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_peptide (peptide_id),
|
||||
KEY idx_status (queue_status),
|
||||
KEY idx_confidence (extraction_confidence),
|
||||
KEY idx_peptide_status (peptide_id, queue_status)
|
||||
) {$charset};";
|
||||
|
||||
dbDelta( $sql );
|
||||
}
|
||||
}
|
||||
97
includes/migrations/class-pr-core-migration-runner.php
Normal file
97
includes/migrations/class-pr-core-migration-runner.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Schema migration runner.
|
||||
*
|
||||
* What: Compares current schema version against target, runs pending migrations.
|
||||
* Who calls it: PR_Core::init() on every plugins_loaded, PR_Core_Activator on activation.
|
||||
* Dependencies: WordPress $wpdb, dbDelta().
|
||||
*
|
||||
* Migrations are numbered sequentially (0001, 0002, ...). Each migration class
|
||||
* must implement up() and optionally down(). All migrations are idempotent.
|
||||
*
|
||||
* @see peptide-repo-core.php — Defines PR_CORE_TARGET_SCHEMA_VERSION.
|
||||
* @see CONVENTIONS.md — "How To: Add a New Migration".
|
||||
*/
|
||||
class PR_Core_Migration_Runner {
|
||||
|
||||
/** @var string Option key storing current schema version. */
|
||||
private const VERSION_OPTION = 'pr_core_schema_version';
|
||||
|
||||
/**
|
||||
* Ordered list of migration classes.
|
||||
* Index 0 = migration 1 (bumps schema to version 1).
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private const MIGRATIONS = [
|
||||
'PR_Core_Migration_0001_Dosing_Rows',
|
||||
'PR_Core_Migration_0002_Legal_Cells',
|
||||
'PR_Core_Migration_0003_Candidate_Queue',
|
||||
];
|
||||
|
||||
/**
|
||||
* Run all pending migrations up to PR_CORE_TARGET_SCHEMA_VERSION.
|
||||
*
|
||||
* Side effects: creates/alters database tables, updates schema version option.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run_pending(): void {
|
||||
$current = (int) get_option( self::VERSION_OPTION, 0 );
|
||||
$target = PR_CORE_TARGET_SCHEMA_VERSION;
|
||||
|
||||
if ( $current >= $target ) {
|
||||
return;
|
||||
}
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
|
||||
for ( $i = $current; $i < $target; $i++ ) {
|
||||
if ( ! isset( self::MIGRATIONS[ $i ] ) ) {
|
||||
break;
|
||||
}
|
||||
|
||||
$class = self::MIGRATIONS[ $i ];
|
||||
if ( ! class_exists( $class ) ) {
|
||||
// Migration file not autoloaded — load explicitly.
|
||||
$file = $this->class_to_file( $class );
|
||||
if ( file_exists( $file ) ) {
|
||||
require_once $file;
|
||||
}
|
||||
}
|
||||
|
||||
if ( class_exists( $class ) ) {
|
||||
$migration = new $class();
|
||||
$migration->up();
|
||||
}
|
||||
|
||||
update_option( self::VERSION_OPTION, $i + 1 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current schema version.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_current_version(): int {
|
||||
return (int) get_option( self::VERSION_OPTION, 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert migration class name to file path.
|
||||
*
|
||||
* PR_Core_Migration_0001_Dosing_Rows => migrations/class-pr-core-migration-0001-dosing-rows.php
|
||||
*
|
||||
* @param string $class Fully-qualified class name.
|
||||
* @return string Absolute file path.
|
||||
*/
|
||||
private function class_to_file( string $class ): string {
|
||||
$suffix = substr( $class, 8 ); // Strip 'PR_Core_'.
|
||||
$filename = 'class-pr-core-' . str_replace( '_', '-', strtolower( $suffix ) ) . '.php';
|
||||
|
||||
return PR_CORE_PLUGIN_DIR . 'includes/migrations/' . $filename;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user