From 27704012a63feec606ebd5b9d279d2d52fee2b58 Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Wed, 8 Apr 2020 17:26:03 -0400 Subject: [PATCH] Enable the default homepage template to be filtered (https://github.com/woocommerce/woocommerce-admin/pull/4072) This commit adds the "woocommerce_admin_onboarding_homepage_template" filter, enabling themes/plugins to filter the default homepage template created by the WooCommerce onboarding process. --- .../src/API/OnboardingTasks.php | 30 +++++++++++-------- .../tests/api/onboarding-tasks.php | 20 +++++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/plugins/woocommerce-admin/src/API/OnboardingTasks.php b/plugins/woocommerce-admin/src/API/OnboardingTasks.php index 6958fc7e7db..4d3635c1b5a 100644 --- a/plugins/woocommerce-admin/src/API/OnboardingTasks.php +++ b/plugins/woocommerce-admin/src/API/OnboardingTasks.php @@ -252,11 +252,9 @@ class OnboardingTasks extends \WC_REST_Data_Controller { private static function get_homepage_template( $post_id ) { $products = wp_count_posts( 'product' ); if ( $products->publish >= 4 ) { - $images = self::sideload_homepage_images( $post_id, 1 ); - $image_1 = ! empty( $images[0] ) ? $images[0] : ''; - $cover = self::get_homepage_cover_block( $image_1 ); - - return $cover . ' + $images = self::sideload_homepage_images( $post_id, 1 ); + $image_1 = ! empty( $images[0] ) ? $images[0] : ''; + $template = self::get_homepage_cover_block( $image_1 ) . '

' . __( 'Shop by Category', 'woocommerce-admin' ) . '

@@ -288,15 +286,20 @@ class OnboardingTasks extends \WC_REST_Data_Controller {
[products limit="4" columns="4" best_selling="1"]
'; + + /** + * Modify the template/content of the default homepage. + * + * @param string $template The default homepage template. + */ + return apply_filters( 'woocommerce_admin_onboarding_homepage_template', $template ); } - $images = self::sideload_homepage_images( $post_id, 3 ); - $image_1 = ! empty( $images[0] ) ? $images[0] : ''; - $image_2 = ! empty( $images[1] ) ? $images[1] : ''; - $image_3 = ! empty( $images[2] ) ? $images[2] : ''; - $cover = self::get_homepage_cover_block( $image_1 ); - - return $cover . ' + $images = self::sideload_homepage_images( $post_id, 3 ); + $image_1 = ! empty( $images[0] ) ? $images[0] : ''; + $image_2 = ! empty( $images[1] ) ? $images[1] : ''; + $image_3 = ! empty( $images[2] ) ? $images[2] : ''; + $template = self::get_homepage_cover_block( $image_1 ) . '

' . __( 'New Products', 'woocommerce-admin' ) . '

@@ -308,6 +311,9 @@ class OnboardingTasks extends \WC_REST_Data_Controller { self::get_homepage_media_block( $image_3, 'right' ) . ' '; + + /** This filter is documented in src/API/OnboardingTasks.php. */ + return apply_filters( 'woocommerce_admin_onboarding_homepage_template', $template ); } /** diff --git a/plugins/woocommerce-admin/tests/api/onboarding-tasks.php b/plugins/woocommerce-admin/tests/api/onboarding-tasks.php index 5eb6548a090..2e98f5ed4d4 100644 --- a/plugins/woocommerce-admin/tests/api/onboarding-tasks.php +++ b/plugins/woocommerce-admin/tests/api/onboarding-tasks.php @@ -65,4 +65,24 @@ class WC_Tests_API_Onboarding_Tasks extends WC_REST_Unit_Test_Case { $this->assertEquals( get_option( 'woocommerce_onboarding_homepage_post_id' ), $data['post_id'] ); $this->assertEquals( htmlspecialchars_decode( get_edit_post_link( get_option( 'woocommerce_onboarding_homepage_post_id' ) ) ), $data['edit_post_link'] ); } + + /** + * Test that the default homepage template can be filtered. + */ + public function test_homepage_template_can_be_filtered() { + wp_set_current_user( $this->user ); + + add_filter( + 'woocommerce_admin_onboarding_homepage_template', + function ( $template ) { + return 'Custom post content'; + } + ); + + $request = new WP_REST_Request( 'POST', $this->endpoint . '/create_homepage' ); + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + + $this->assertSame( 'Custom post content', get_the_content( null, null, $data['post_id'] ) ); + } }