Skip to content

Commit

Permalink
Add compatibility data when onboarding (#7990)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepearson authored Jan 31, 2024
1 parent edf69a5 commit c4f63db
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 83 deletions.
4 changes: 4 additions & 0 deletions changelog/update-4423-add-compatibility-data-when-onboarding
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add compatibility data to onboarding init payload.
48 changes: 34 additions & 14 deletions includes/class-compatibility-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function __construct( WC_Payments_API_Client $payments_api_client ) {
public function init_hooks() {
add_action( 'woocommerce_payments_account_refreshed', [ $this, 'update_compatibility_data' ] );
add_action( 'after_switch_theme', [ $this, 'update_compatibility_data' ] );
add_filter( 'wc_payments_get_onboarding_data_args', [ $this, 'add_compatibility_onboarding_data' ] );
}

/**
Expand All @@ -48,27 +49,47 @@ public function init_hooks() {
* @return void
*/
public function update_compatibility_data() {
$active_plugins = get_option( 'active_plugins', [] );
$post_types_count = $this->get_post_types_count();
try {
$this->payments_api_client->update_compatibility_data(
[
'woopayments_version' => WCPAY_VERSION_NUMBER,
'woocommerce_version' => WC_VERSION,
'blog_theme' => get_stylesheet(),
'active_plugins' => $active_plugins,
'post_types_count' => $post_types_count,
]
);
$this->payments_api_client->update_compatibility_data( $this->get_compatibility_data() );
} catch ( API_Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
// The exception is already logged if logging is on, nothing else needed.
}
}

/**
* Adds the compatibility data to the onboarding args.
*
* @param array $args The args being sent when onboarding.
*
* @return array
*/
public function add_compatibility_onboarding_data( $args ): array {
$args['compatibility_data'] = $this->get_compatibility_data();
return $args;
}

/**
* Gets the compatibility data.
*
* @return array
*/
private function get_compatibility_data(): array {
$active_plugins = get_option( 'active_plugins', [] );
$post_types_count = $this->get_post_types_count();

return [
'woopayments_version' => WCPAY_VERSION_NUMBER,
'woocommerce_version' => WC_VERSION,
'blog_theme' => get_stylesheet(),
'active_plugins' => $active_plugins,
'post_types_count' => $post_types_count,
];
}

/**
* Gets the count of public posts for each post type.
*
* @return array<\WP_Post_Type|string, string>
* @return array<\WP_Post_Type|string, int>
*/
private function get_post_types_count(): array {
$post_types = get_post_types(
Expand All @@ -80,10 +101,9 @@ private function get_post_types_count(): array {
$post_types_count = [];

foreach ( $post_types as $post_type ) {
$post_types_count[ $post_type ] = wp_count_posts( $post_type )->publish;
$post_types_count[ $post_type ] = (int) wp_count_posts( $post_type )->publish;
}

return $post_types_count;

}
}
Loading

0 comments on commit c4f63db

Please sign in to comment.