Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility data when onboarding #7990

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading