10 ) ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { return null; } $original_content = wp_remote_retrieve_body( $response ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading local file for diff comparison. $local_content = file_get_contents( $file_path ); if ( false === $local_content ) { return null; } $original_lines = explode( "\n", $original_content ); $local_lines = explode( "\n", $local_content ); return agentic_admin_generate_unified_diff( $original_lines, $local_lines, $old_label, $new_label ); } /** * Generate a unified diff between two arrays of lines. * * Produces output similar to `git diff --unified=3`. * * @param array $old_lines Lines from the original file. * @param array $new_lines Lines from the modified file. * @param string $old_label Label for the original file. * @param string $new_label Label for the modified file. * @return string Unified diff output. */ function agentic_admin_generate_unified_diff( array $old_lines, array $new_lines, string $old_label, string $new_label ): string { return agentic_admin_simple_diff( $old_lines, $new_lines, $old_label, $new_label ); } /** * Simple line-by-line diff. * * @param array $old_lines Lines from the original file. * @param array $new_lines Lines from the modified file. * @param string $old_label Label for the original file. * @param string $new_label Label for the modified file. * @return string Simple diff output. */ function agentic_admin_simple_diff( array $old_lines, array $new_lines, string $old_label, string $new_label ): string { $output = "--- {$old_label}\n+++ {$new_label}\n"; $max_lines = max( count( $old_lines ), count( $new_lines ) ); $has_changes = false; for ( $i = 0; $i < $max_lines; $i++ ) { $old = $old_lines[ $i ] ?? null; $new = $new_lines[ $i ] ?? null; if ( $old === $new ) { continue; } $has_changes = true; if ( null !== $old ) { $output .= "- {$old}\n"; } if ( null !== $new ) { $output .= "+ {$new}\n"; } } return $has_changes ? $output : ''; }