From 6d4014042b3c7fbbdc1b77f1a1d9b71cc1b54543 Mon Sep 17 00:00:00 2001 From: Moon Date: Sun, 21 May 2023 16:07:01 -0700 Subject: [PATCH] Add woocommerce_suggest_jetpack filter to exlude Jetpack suggestion in OBW (#38286) * Add filter for free extensions endpoint * Add changelog * Add filter doc * Support woocommerce_suggest_jetpack filter to exlude Jetpack * Update plugins/woocommerce/src/Admin/API/OnboardingFreeExtensions.php Co-authored-by: Chi-Hsuan Huang --------- Co-authored-by: Chi-Hsuan Huang --- ...dd-filter-to-free-extensions-rest-endpoint | 4 +++ .../Admin/API/OnboardingFreeExtensions.php | 34 +++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 plugins/woocommerce/changelog/add-38285-add-filter-to-free-extensions-rest-endpoint diff --git a/plugins/woocommerce/changelog/add-38285-add-filter-to-free-extensions-rest-endpoint b/plugins/woocommerce/changelog/add-38285-add-filter-to-free-extensions-rest-endpoint new file mode 100644 index 00000000000..6305f77401c --- /dev/null +++ b/plugins/woocommerce/changelog/add-38285-add-filter-to-free-extensions-rest-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add a filter to free extensions REST endpoint diff --git a/plugins/woocommerce/src/Admin/API/OnboardingFreeExtensions.php b/plugins/woocommerce/src/Admin/API/OnboardingFreeExtensions.php index 4719ceff621..71336fee518 100644 --- a/plugins/woocommerce/src/Admin/API/OnboardingFreeExtensions.php +++ b/plugins/woocommerce/src/Admin/API/OnboardingFreeExtensions.php @@ -10,6 +10,11 @@ namespace Automattic\WooCommerce\Admin\API; defined( 'ABSPATH' ) || exit; use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\Init as RemoteFreeExtensions; +use WC_REST_Data_Controller; +use WP_Error; +use WP_REST_Request; +use WP_REST_Response; +use WP_REST_Server; /** * Onboarding Payments Controller. @@ -17,7 +22,7 @@ use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\Init as RemoteFre * @internal * @extends WC_REST_Data_Controller */ -class OnboardingFreeExtensions extends \WC_REST_Data_Controller { +class OnboardingFreeExtensions extends WC_REST_Data_Controller { /** * Endpoint namespace. @@ -42,7 +47,7 @@ class OnboardingFreeExtensions extends \WC_REST_Data_Controller { '/' . $this->rest_base, array( array( - 'methods' => \WP_REST_Server::READABLE, + 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_available_extensions' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), ), @@ -59,7 +64,7 @@ class OnboardingFreeExtensions extends \WC_REST_Data_Controller { */ public function get_items_permissions_check( $request ) { if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { - return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); + return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); } return true; @@ -68,12 +73,29 @@ class OnboardingFreeExtensions extends \WC_REST_Data_Controller { /** * Return available payment methods. * - * @param \WP_REST_Request $request Request data. + * @param WP_REST_Request $request Request data. * - * @return \WP_Error|\WP_REST_Response + * @return WP_Error|WP_REST_Response */ public function get_available_extensions( $request ) { - return RemoteFreeExtensions::get_extensions(); + $extensions = RemoteFreeExtensions::get_extensions(); + /** + * Remove Jetpack when woocommerce_suggest_jetpack is false. + * + * @since 7.8 + */ + if ( false === apply_filters( 'woocommerce_suggest_jetpack', true ) ) { + foreach ( $extensions as &$extension ) { + $extension['plugins'] = array_filter( + $extension['plugins'], + function( $plugin ) { + return 'jetpack' !== $plugin->key; + } + ); + } + } + + return new WP_REST_Response( $extensions ); } }