From b812b3942793adf57ad7ff299d53c0f074995677 Mon Sep 17 00:00:00 2001 From: Christoph Daum Date: Mon, 13 Apr 2026 20:35:32 +0200 Subject: [PATCH 1/4] feat(api): add public plugin_report_get_data() Add RT_Plugin_Report::get_report() static method and a global plugin_report_get_data() wrapper as public API for other plugins to access report data without touching transients directly. Populates cache on demand if not yet available. --- rt-plugin-report.php | 63 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/rt-plugin-report.php b/rt-plugin-report.php index 6b76f78..41a3ba2 100644 --- a/rt-plugin-report.php +++ b/rt-plugin-report.php @@ -144,9 +144,8 @@ public function settings_page() { echo ''; foreach ( $plugins as $key => $plugin ) { - $slug = $this->get_plugin_slug( $key ); - $cache_key = $this->create_cache_key( $slug ); - $cache = get_site_transient( $cache_key ); + $slug = $this->get_plugin_slug( $key ); + $cache = self::get_report( $slug ); if ( $cache ) { // Use the cached report to create a table row. echo $this->render_table_row( $cache ); @@ -773,10 +772,68 @@ public function upgrade_delete_cache_items( $upgrader, $data ) { } } + /** + * Public API: get the report data for a plugin. + * + * Returns cached data when available, otherwise assembles a fresh report + * (which also populates the cache). This is the recommended way for other + * plugins to read Plugin Report data without accessing transients directly. + * + * @param string $slug Plugin slug (e.g. "akismet"). + * + * @return array|null Report data array, or null if the slug is empty or not found. + */ + public static function get_report( $slug ) { + if ( empty( $slug ) ) { + return null; + } + + // Check if get_plugins() function exists. + if ( ! function_exists( 'plugins_api' ) ) { + require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; + } + + $instance = new self(); + $cache_key = $instance->create_cache_key( $slug ); + $cache = get_site_transient( $cache_key ); + + if ( ! empty( $cache ) ) { + return $cache; + } + + return $instance->assemble_plugin_report( $slug ); + } + } // Instantiate the class. $plugin_report_instance = new RT_Plugin_Report(); $plugin_report_instance->init(); + + /** + * Public API: get Plugin Report data for a plugin. + * + * Convenience wrapper around RT_Plugin_Report::get_report(). Returns cached + * data when available, otherwise fetches fresh data from the wordpress.org API + * and caches it. Safe to call from any plugin — if Plugin Report is not active + * or the slug is invalid, returns null. + * + * Example usage: + * + * if ( function_exists( 'plugin_report_get_data' ) ) { + * $report = plugin_report_get_data( 'akismet' ); + * if ( $report && isset( $report['repo_info'] ) ) { + * echo $report['repo_info']->tested; + * } + * } + * + * @param string $slug Plugin slug (e.g. "akismet"). + * + * @return array|null Report data array, or null if not available. + */ + function plugin_report_get_data( $slug ) { + return RT_Plugin_Report::get_report( $slug ); + } + } From c722a78ee9357905a03b429976165d60b49b16cb Mon Sep 17 00:00:00 2001 From: Christoph Daum Date: Mon, 13 Apr 2026 20:39:30 +0200 Subject: [PATCH 2/4] docs(api): simplify example condition isset() on null already returns false, no need for the redundant truthy check. --- rt-plugin-report.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rt-plugin-report.php b/rt-plugin-report.php index 41a3ba2..2ef04d4 100644 --- a/rt-plugin-report.php +++ b/rt-plugin-report.php @@ -823,7 +823,7 @@ public static function get_report( $slug ) { * * if ( function_exists( 'plugin_report_get_data' ) ) { * $report = plugin_report_get_data( 'akismet' ); - * if ( $report && isset( $report['repo_info'] ) ) { + * if ( isset( $report['repo_info'] ) ) { * echo $report['repo_info']->tested; * } * } From 7db1e7d8e341a03a9ab4b2b059abe2b9fa709699 Mon Sep 17 00:00:00 2001 From: Christoph Daum Date: Mon, 13 Apr 2026 20:40:16 +0200 Subject: [PATCH 3/4] style: remove trailing blank line --- rt-plugin-report.php | 1 - 1 file changed, 1 deletion(-) diff --git a/rt-plugin-report.php b/rt-plugin-report.php index 2ef04d4..24d1bf8 100644 --- a/rt-plugin-report.php +++ b/rt-plugin-report.php @@ -835,5 +835,4 @@ public static function get_report( $slug ) { function plugin_report_get_data( $slug ) { return RT_Plugin_Report::get_report( $slug ); } - } From e60cab44d61c58fcd6281518211c380d131ac177 Mon Sep 17 00:00:00 2001 From: Christoph Daum Date: Mon, 13 Apr 2026 20:46:12 +0200 Subject: [PATCH 4/4] refactor(api): make get_report() non-static Use a static local instance in the global wrapper instead of instantiating inside the class method. --- rt-plugin-report.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/rt-plugin-report.php b/rt-plugin-report.php index 24d1bf8..239fbc0 100644 --- a/rt-plugin-report.php +++ b/rt-plugin-report.php @@ -145,7 +145,7 @@ public function settings_page() { foreach ( $plugins as $key => $plugin ) { $slug = $this->get_plugin_slug( $key ); - $cache = self::get_report( $slug ); + $cache = $this->get_report( $slug ); if ( $cache ) { // Use the cached report to create a table row. echo $this->render_table_row( $cache ); @@ -783,7 +783,7 @@ public function upgrade_delete_cache_items( $upgrader, $data ) { * * @return array|null Report data array, or null if the slug is empty or not found. */ - public static function get_report( $slug ) { + public function get_report( $slug ) { if ( empty( $slug ) ) { return null; } @@ -793,15 +793,14 @@ public static function get_report( $slug ) { require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; } - $instance = new self(); - $cache_key = $instance->create_cache_key( $slug ); + $cache_key = $this->create_cache_key( $slug ); $cache = get_site_transient( $cache_key ); if ( ! empty( $cache ) ) { return $cache; } - return $instance->assemble_plugin_report( $slug ); + return $this->assemble_plugin_report( $slug ); } } @@ -814,9 +813,10 @@ public static function get_report( $slug ) { /** * Public API: get Plugin Report data for a plugin. * - * Convenience wrapper around RT_Plugin_Report::get_report(). Returns cached - * data when available, otherwise fetches fresh data from the wordpress.org API - * and caches it. Safe to call from any plugin — if Plugin Report is not active + * Convenience wrapper around RT_Plugin_Report::get_report(). Uses a static + * instance to avoid repeated object creation. Returns cached data when + * available, otherwise fetches fresh data from the wordpress.org API and + * caches it. Safe to call from any plugin — if Plugin Report is not active * or the slug is invalid, returns null. * * Example usage: @@ -833,6 +833,10 @@ public static function get_report( $slug ) { * @return array|null Report data array, or null if not available. */ function plugin_report_get_data( $slug ) { - return RT_Plugin_Report::get_report( $slug ); + static $plugin_report = null; + if ( null === $plugin_report ) { + $plugin_report = new RT_Plugin_Report(); + } + return $plugin_report->get_report( $slug ); } }