diff --git a/rt-plugin-report.php b/rt-plugin-report.php index 6b76f78..239fbc0 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 = $this->get_report( $slug ); if ( $cache ) { // Use the cached report to create a table row. echo $this->render_table_row( $cache ); @@ -773,10 +772,71 @@ 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 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'; + } + + $cache_key = $this->create_cache_key( $slug ); + $cache = get_site_transient( $cache_key ); + + if ( ! empty( $cache ) ) { + return $cache; + } + + return $this->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(). 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: + * + * if ( function_exists( 'plugin_report_get_data' ) ) { + * $report = plugin_report_get_data( 'akismet' ); + * if ( 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 ) { + static $plugin_report = null; + if ( null === $plugin_report ) { + $plugin_report = new RT_Plugin_Report(); + } + return $plugin_report->get_report( $slug ); + } }