From babc2ae72a0a2042e016001ea6bf8236c9ee0c29 Mon Sep 17 00:00:00 2001 From: peptiderepo Date: Fri, 17 Apr 2026 14:49:59 +0700 Subject: [PATCH] fix: address QA P1 + P2 findings from 9aa5309 review P1 fix: Add class_exists() guard in PRC_Preset_Provider before instantiating PR Core repository classes. If PR Core constant is defined but classes fail to autoload, fall back to defaults instead of crashing. P2 fixes: - Remove unused nonce from wp_localize_script (all endpoints public) - Add cross-reference comments pinning SYRINGE_UNITS_PER_ML across PRC_Math, calculator.js, and prcConfig.syringeUnits QA verdict: qa-reviews/peptide-reconstitution-calculator/2026-04-17-9aa5309.md Co-Authored-By: Claude Opus 4.6 --- assets/js/calculator.js | 2 ++ includes/class-prc-math.php | 4 ++++ includes/class-prc-preset-provider.php | 7 +++++++ includes/frontend/class-prc-shortcode.php | 6 +++--- 4 files changed, 16 insertions(+), 3 deletions(-) mode change 100644 => 100755 assets/js/calculator.js mode change 100644 => 100755 includes/class-prc-math.php mode change 100644 => 100755 includes/class-prc-preset-provider.php mode change 100644 => 100755 includes/frontend/class-prc-shortcode.php diff --git a/assets/js/calculator.js b/assets/js/calculator.js old mode 100644 new mode 100755 index 7bc1147..52cdb6f --- a/assets/js/calculator.js +++ b/assets/js/calculator.js @@ -15,6 +15,8 @@ /* ── Constants ────────────────────────────────────────────────────── */ + // Must match PRC_Math::SYRINGE_UNITS_PER_ML in class-prc-math.php + // and prcConfig.syringeUnits in class-prc-shortcode.php. var SYRINGE_UNITS_PER_ML = 100; /* ── State ────────────────────────────────────────────────────────── */ diff --git a/includes/class-prc-math.php b/includes/class-prc-math.php old mode 100644 new mode 100755 index 25fb02e..1d5e8bd --- a/includes/class-prc-math.php +++ b/includes/class-prc-math.php @@ -25,6 +25,10 @@ class PRC_Math { /** * Standard insulin syringe size in units (100 IU = 1 mL). + * + * Mirrored in: assets/js/calculator.js (SYRINGE_UNITS_PER_ML) + * and prcConfig.syringeUnits (class-prc-shortcode.php). + * All three MUST stay in sync. */ private const SYRINGE_UNITS_PER_ML = 100; diff --git a/includes/class-prc-preset-provider.php b/includes/class-prc-preset-provider.php old mode 100644 new mode 100755 index 9d477d7..a1b08cb --- a/includes/class-prc-preset-provider.php +++ b/includes/class-prc-preset-provider.php @@ -85,6 +85,13 @@ class PRC_Preset_Provider { * @return array> */ private function build_presets_from_pr_core(): array { + // Why class_exists: PR_CORE_VERSION constant may be defined but classes + // could fail to autoload (e.g. fatal error in PR Core bootstrap). + // Fall back to empty array so the caller merges in defaults gracefully. + if ( ! class_exists( 'PR_Core_Peptide_Repository' ) || ! class_exists( 'PR_Core_Dosing_Repository' ) ) { + return []; + } + $peptide_repo = new PR_Core_Peptide_Repository(); $dosing_repo = new PR_Core_Dosing_Repository(); diff --git a/includes/frontend/class-prc-shortcode.php b/includes/frontend/class-prc-shortcode.php old mode 100644 new mode 100755 index 41e13e8..acaa522 --- a/includes/frontend/class-prc-shortcode.php +++ b/includes/frontend/class-prc-shortcode.php @@ -93,13 +93,13 @@ class PRC_Shortcode { true ); - // Pass presets and config to JS. + // Pass presets and config to JS — all REST endpoints are public-read, + // so no nonce is needed. The restUrl is provided for future use. $provider = new PRC_Preset_Provider(); wp_localize_script( 'prc-calculator', 'prcConfig', [ 'presets' => $provider->get_all_presets(), 'restUrl' => esc_url_raw( rest_url( 'prc/v1' ) ), - 'nonce' => wp_create_nonce( 'wp_rest' ), - 'syringeUnits' => 100, + 'syringeUnits' => 100, // Must match PRC_Math::SYRINGE_UNITS_PER_ML and calculator.js SYRINGE_UNITS_PER_ML. 'i18n' => [ 'selectPeptide' => __( 'Select a peptide…', 'peptide-reconstitution-calculator' ), 'customEntry' => __( 'Custom / Manual Entry', 'peptide-reconstitution-calculator' ),