From 1627abae6154b135ade5a931184aa17010bad80d Mon Sep 17 00:00:00 2001
From: Paul Biron <paul@sparrowhawkcomputing.com>
Date: Thu, 3 Dec 2020 15:29:52 -0700
Subject: [PATCH] Send stats to the API when plugin or theme update fails.

---
 src/wp-admin/includes/class-wp-upgrader.php | 32 +++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/src/wp-admin/includes/class-wp-upgrader.php b/src/wp-admin/includes/class-wp-upgrader.php
index 5faac56213..4563afc63d 100644
--- a/src/wp-admin/includes/class-wp-upgrader.php
+++ b/src/wp-admin/includes/class-wp-upgrader.php
@@ -666,6 +666,7 @@ class WP_Upgrader {
 	 *                              or false if unable to connect to the filesystem.
 	 */
 	public function run( $options ) {
+		$start_time = time();
 
 		$defaults = array(
 			'package'                     => '', // Please always pass this.
@@ -798,6 +799,37 @@ class WP_Upgrader {
 			)
 		);
 
+		// If WP_Error send data to WordPress.org.
+		if ( is_wp_error( $result ) ) {
+			global $wp_version, $wp_filesystem;
+			$stats = array(
+				'update_type'      => null,
+				'name'             => null,
+				'update_version'   => null,
+				'success'          => false,
+				'fs_method'        => $wp_filesystem->method,
+				'fs_method_forced' => defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ),
+				'fs_method_direct' => ! empty( $GLOBALS['_wp_filesystem_direct_method'] ) ? $GLOB['_wp_filesystem_direct_method'] : '',
+				'time_taken'       => time() - $start_time,
+				'wp_version'       => $wp_version,
+				'wp_error'         => $result,
+			);
+			if ( $this instanceof Plugin_Upgrader ) {
+				$stats['update_type']    = 'plugin';
+				$stats['name']           = $this->skin->plugin_info['Name'];
+				$stats['update_version'] = $this->skin->plugin_info['Version'];
+
+				wp_update_plugins( $stats );
+			}
+			if ( $this instanceof Theme_Upgrader ) {
+				$stats['update_type']    = 'theme';
+				$stats['name']           = $this->skin->theme_info->get('Name');
+				$stats['update_version'] = $this->skin->theme_info->get('Version');
+
+				wp_update_themes( $stats );
+			}
+		}
+
 		$this->skin->set_result( $result );
 		if ( is_wp_error( $result ) ) {
 			$this->skin->error( $result );
-- 
2.28.0.windows.1

