From bf87c82f69c0cced64bfaecb7e2ce19e6f63c867 Mon Sep 17 00:00:00 2001 From: peptiderepo Date: Wed, 17 Jun 2026 05:50:50 +0000 Subject: [PATCH] fix: PHP 8.3 TypeError in articles-list total count (int cast) number_format() in PHP 8.3 strictly requires int|float; $wpdb->get_var() returns string|null. Cast $total to (int) at point of assignment in admin/partials/articles-list.php to fix HTTP 500 on Articles admin page. Also add PHPUnit test covering the string->int cast contract. Fixes #31. Version bump 2.5.0 -> 2.5.1. Agent-Session: fix-articles-500-20260617 --- CHANGELOG.md | 5 ++++ admin/partials/articles-list.php | 2 +- peptide-news-plugin.php | 4 +-- tests/unit/ArticlesListTotalCastTest.php | 32 ++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/unit/ArticlesListTotalCastTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ba2b9af..809d29f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to Peptide News Plugin are documented here. Format: [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). Versioning: [Semantic Versioning](https://semver.org/). +## [2.5.1] - 2026-06-17 + +### Fixed +- PHP 8.3 `TypeError` on Articles admin page: `number_format()` rejects the numeric string returned by `$wpdb->get_var()`. Cast `$total` to `(int)` at point of assignment in `admin/partials/articles-list.php` (#31). + ## [2.5.0] — 2026-06-16 ### Changed diff --git a/admin/partials/articles-list.php b/admin/partials/articles-list.php index c13b872..9b3ed5c 100755 --- a/admin/partials/articles-list.php +++ b/admin/partials/articles-list.php @@ -19,7 +19,7 @@ $per_page = 25; $offset = ( $page - 1 ) * $per_page; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -$total = $wpdb->get_var( "SELECT COUNT(*) FROM {$table}" ); +$total = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $articles = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} ORDER BY published_at DESC LIMIT %d OFFSET %d", diff --git a/peptide-news-plugin.php b/peptide-news-plugin.php index dfe77bd..8eb2490 100755 --- a/peptide-news-plugin.php +++ b/peptide-news-plugin.php @@ -3,7 +3,7 @@ * Plugin Name: Peptide News Aggregator * Plugin URI: https://github.com/peptiderepo/peptide-news-plugin * Description: Aggregates and displays the latest peptide research news from multiple sources with click analytics and trend reporting. - * Version: 2.5.0 + * Version: 2.5.1 * Author: peptiderepo * Author URI: https://github.com/peptiderepo * License: GPL-2.0+ @@ -24,7 +24,7 @@ if ( ! defined( 'WPINC' ) ) { /** * Current plugin version — follows SemVer. */ -define( 'PEPTIDE_NEWS_VERSION', '2.5.0' ); +define( 'PEPTIDE_NEWS_VERSION', '2.5.1' ); define( 'PEPTIDE_NEWS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); define( 'PEPTIDE_NEWS_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); define( 'PEPTIDE_NEWS_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); diff --git a/tests/unit/ArticlesListTotalCastTest.php b/tests/unit/ArticlesListTotalCastTest.php new file mode 100644 index 0000000..5b8d787 --- /dev/null +++ b/tests/unit/ArticlesListTotalCastTest.php @@ -0,0 +1,32 @@ +get_var returning a numeric string does not + * cause a TypeError in number_format() (PHP 8.3 strict typing). + */ + public function test_total_cast_prevents_type_error() { + $total_as_string = '451'; // what $wpdb->get_var() returns + $total = (int) $total_as_string; + $this->assertIsInt( $total ); + $this->assertSame( 451, $total ); + // This should not throw TypeError on PHP 8.3 + $formatted = number_format( $total ); + $this->assertSame( '451', $formatted ); + } + + /** + * Verify null return (missing table) results in 0, not a TypeError. + */ + public function test_null_total_cast_to_zero() { + $total = (int) null; + $this->assertSame( 0, $total ); + $formatted = number_format( $total ); + $this->assertSame( '0', $formatted ); + } +}