Files
peptide-repo-core/includes/class-pr-core-activator.php
peptiderepo 850279e756 feat(cpt): consolidate peptide CPT with PSA; drop pr_peptide (v0.2.0)
Resolves the rewrite slug collision that was 404'ing all 89 production
peptide detail pages. PR Core v0.1.1 registered an empty pr_peptide CPT
claiming /peptides/%postname%/; WP's rewrite resolver picked it over PSA's
populated peptide CPT. This release consolidates both registrations onto a
single peptide CPT owned by PR Core. PSA v4.5.0 drops its registration in
favor of this one.

## Contract (per CTO msg 02)
- POST_TYPE: pr_peptide -> peptide
- TAX_CATEGORY: pr_peptide_category -> peptide_category
- TAX_FAMILY const removed (pr_peptide_family was never populated)
- CPT + taxonomy registration guarded with post_type_exists() / taxonomy_exists()
  so deploy order between PR Core and PSA is forgiving
- Args harmonized as superset of PSA + prior PR Core (thumbnail added to
  supports; capability_type + map_meta_cap preserved)
- Activator: flush_rewrite_rules(false) on activation; new
  maybe_flush_on_version_change() handler hooked at init:999 for in-place
  updates that bypass the activation hook (e.g. SCP deploys)
- uninstall.php: blanket DELETE replaced with JOIN on _pr_core_authored so
  PSA-authored peptide posts (the 89 canonical monographs) are never destroyed;
  peptide_category terms preserved (shared ownership)
- Scrub scope: narrow — CPT-slug-derived identifiers only; class names,
  file names, PR_CORE_* constants, pr_core_* options, and pr-core/v1 REST
  namespace remain

## Docs
- ARCHITECTURE.md: new Key Decision #7 (v0.2.0 consolidation) and §2.9
  Uninstall specification
- CONVENTIONS.md: CPT Ownership rule + _pr_core_authored flag contract

## Tests
- Lightweight harness (tests/bootstrap.php) that stubs the handful of WP
  functions exercised by the CPT class
- tests/unit/test-peptide-cpt.php asserts: constants match contract,
  TAX_FAMILY is removed, guard no-ops when post_type_exists/taxonomy_exists
  returns true, args payload shape (thumbnail in supports, rewrite slug,
  REST namespace), hooks wired
- CI: new 'Unit tests (lightweight, no PHPUnit)' step in lint-php job,
  runs across PHP 8.1/8.2/8.3. Full PHPUnit + wp-env harness is a
  follow-up

Refs: convo/prcore/threads/2026-04-peptides-cpt-collision/02-cto-correction.md
2026-04-22 08:32:00 +00:00

98 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
/**
* Plugin activation handler.
*
* What: Runs migrations, adds capabilities, flushes rewrite rules.
* Also detects version changes on subsequent init cycles and re-flushes
* rewrite rules once so in-place plugin updates (without a deactivate +
* reactivate cycle) still pick up CPT/taxonomy slug changes.
* Who calls it: register_activation_hook() in peptide-repo-core.php; and
* PR_Core::init() registers maybe_flush_on_version_change on init:999.
* Dependencies: PR_Core_Migration_Runner, PR_Core_Peptide_CPT.
*
* @see peptide-repo-core.php — Registers this class on activation.
* @see includes/class-pr-core.php — Wires the init:999 version-change handler.
*/
class PR_Core_Activator {
/** @var string Option key storing the last activated/seen plugin version. */
private const VERSION_OPTION = 'pr_core_version';
/**
* Run on plugin activation.
*
* Side effects: database table creation, capability grants, rewrite flush,
* pr_core_version option write.
*
* @return void
*/
public static function activate(): void {
// Run all pending migrations (creates tables).
$runner = new PR_Core_Migration_Runner();
$runner->run_pending();
// Register CPT so rewrite rules exist before flush.
PR_Core_Peptide_CPT::register_peptide_post_type();
PR_Core_Peptide_CPT::register_taxonomies();
// Grant manage_peptide_content to administrators and editors.
self::add_capabilities();
// Flush rewrite rules for the new CPT.
flush_rewrite_rules( false );
// Record the activated version so the init:999 drift handler can
// detect future in-place updates and re-flush without a manual
// deactivate/reactivate cycle.
update_option( self::VERSION_OPTION, PR_CORE_VERSION, false );
}
/**
* Detect in-place plugin version changes and flush rewrite rules once.
*
* Wired to init:999 so all CPTs + taxonomies (ours and anyone else's)
* are registered before the flush. When the option is absent (first
* install that bypassed the activation hook — e.g., manual SCP deploy)
* or is older than the current constant, we flush once and update.
*
* Side effects: may call flush_rewrite_rules( false ); updates option.
*
* @return void
*/
public static function maybe_flush_on_version_change(): void {
if ( ! defined( 'PR_CORE_VERSION' ) ) {
return;
}
$recorded = get_option( self::VERSION_OPTION, '' );
if ( PR_CORE_VERSION === $recorded ) {
return;
}
flush_rewrite_rules( false );
update_option( self::VERSION_OPTION, PR_CORE_VERSION, false );
}
/**
* Grant the manage_peptide_content capability to appropriate roles.
*
* Side effects: modifies role capabilities in database.
*
* @return void
*/
private static function add_capabilities(): void {
$cap = 'manage_peptide_content';
$roles = [ 'administrator', 'editor' ];
foreach ( $roles as $role_name ) {
$role = get_role( $role_name );
if ( $role && ! $role->has_cap( $cap ) ) {
$role->add_cap( $cap );
}
}
}
}