diff --git a/src/integrations/watchers/woocommerce-beta-editor-watcher.php b/src/integrations/watchers/woocommerce-beta-editor-watcher.php new file mode 100644 index 00000000000..ba10f5c3296 --- /dev/null +++ b/src/integrations/watchers/woocommerce-beta-editor-watcher.php @@ -0,0 +1,137 @@ +notification_center = $notification_center; + $this->notification_helper = $notification_helper; + $this->short_link_helper = $short_link_helper; + $this->presenter = new Woocommerce_Beta_Editor_Presenter( $this->short_link_helper ); + } + + /** + * Returns the conditionals based on which this loadable should be active. + * + * @return string[] The conditionals. + */ + public static function get_conditionals() { + return [ Admin_Conditional::class, Not_Admin_Ajax_Conditional::class ]; + } + + /** + * Initializes the integration. + * + * On admin_init, it is checked whether the notification about Woocommerce product beta editor enabled should be shown. + * + * @return void + */ + public function register_hooks() { + \add_action( 'admin_init', [ $this, 'manage_woocommerce_beta_editor_notification' ] ); + } + + /** + * Manage the Woocommerce product beta editor notification. + * + * Shows the notification if needed and deletes it if needed. + * + * @return void + */ + public function manage_woocommerce_beta_editor_notification() { + if ( \get_option( 'woocommerce_feature_product_block_editor_enabled' ) === 'yes' ) { + $this->maybe_add_woocommerce_beta_editor_notification(); + } + else { + $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID ); + } + } + + /** + * Add the Woocommerce product beta editor enabled notification if it does not exist yet. + * + * @return void + */ + public function maybe_add_woocommerce_beta_editor_notification() { + if ( ! $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID ) ) { + $notification = $this->notification(); + $this->notification_helper->restore_notification( $notification ); + $this->notification_center->add_notification( $notification ); + } + } + + /** + * Returns an instance of the notification. + * + * @return Yoast_Notification The notification to show. + */ + protected function notification() { + return new Yoast_Notification( + $this->presenter->present(), + [ + 'type' => Yoast_Notification::ERROR, + 'id' => self::NOTIFICATION_ID, + 'capabilities' => 'wpseo_manage_options', + 'priority' => 1, + ] + ); + } +} diff --git a/src/presenters/admin/woocommerce-beta-editor-presenter.php b/src/presenters/admin/woocommerce-beta-editor-presenter.php new file mode 100644 index 00000000000..048121b51c6 --- /dev/null +++ b/src/presenters/admin/woocommerce-beta-editor-presenter.php @@ -0,0 +1,60 @@ +short_link_helper = $short_link_helper; + } + + /** + * Returns the notification as an HTML string. + * + * @return string The notification in an HTML string representation. + */ + public function present() { + $notification_text = '
'; + $notification_text .= $this->get_message(); + $notification_text .= '
'; + + return $notification_text; + } + + /** + * Returns the message to show. + * + * @return string The message. + */ + protected function get_message() { + return \sprintf( + '%1$s %2$s', + \esc_html__( 'Compatibility issue: Yoast SEO is incompatible with the beta WooCommerce product editor.', 'wordpress-seo' ), + \sprintf( + /* translators: 1: Yoast SEO, 2: Link start tag to the Learn more link, 3: Link closing tag. */ + \esc_html__( 'The %1$s interface is currently unavailable in the beta WooCommerce product editor. To resolve any issues, please disable the beta editor. %2$sLearn how to disable the beta WooCommerce product editor.%3$s', 'wordpress-seo' ), + 'Yoast SEO', + '', + '' + ) + ); + } +} diff --git a/tests/unit/integrations/watchers/woocommerce-beta-editor-watcher-test.php b/tests/unit/integrations/watchers/woocommerce-beta-editor-watcher-test.php new file mode 100644 index 00000000000..10bf0fca6b1 --- /dev/null +++ b/tests/unit/integrations/watchers/woocommerce-beta-editor-watcher-test.php @@ -0,0 +1,195 @@ +stubTranslationFunctions(); + + $this->notification_center = Mockery::mock( Yoast_Notification_Center::class ); + $this->notification_helper = Mockery::mock( Notification_Helper::class ); + $this->short_link_helper = Mockery::mock( Short_Link_Helper::class ); + + + $this->instance = new Woocommerce_Beta_Editor_Watcher( + $this->notification_center, + $this->notification_helper, + $this->short_link_helper + ); + } + + /** + * Tests the constructor. + * + * @covers ::__construct + */ + public function test_constructor() { + self::assertInstanceOf( + Yoast_Notification_Center::class, + self::getPropertyValue( $this->instance, 'notification_center' ) + ); + self::assertInstanceOf( + Notification_Helper::class, + self::getPropertyValue( $this->instance, 'notification_helper' ) + ); + self::assertInstanceOf( + Short_Link_Helper::class, + self::getPropertyValue( $this->instance, 'short_link_helper' ) + ); + } + + /** + * Tests that the integration is loaded under the right conditions. + * + * @covers ::get_conditionals + */ + public function test_get_conditionals() { + self::assertEquals( [ Admin_Conditional::class, Not_Admin_Ajax_Conditional::class ], Woocommerce_Beta_Editor_Watcher::get_conditionals() ); + } + + /** + * Tests registering the hooks. + * + * @covers ::register_hooks + */ + public function test_register_hooks() { + $this->instance->register_hooks(); + + $this->assertNotFalse( \has_action( 'admin_init', [ $this->instance, 'manage_woocommerce_beta_editor_notification' ] ) ); + } + + /** + * Tests the admin_init callback when woocommerce_beta_editor is diabled. + * + * @covers ::manage_woocommerce_beta_editor_notification + */ + public function test_manage_woocommerce_beta_editor_disable() { + + Monkey\Functions\expect( 'get_option' ) + ->once() + ->with( 'woocommerce_feature_product_block_editor_enabled' ) + ->andReturn( 'no' ); + + $this->notification_center + ->expects( 'remove_notification_by_id' ) + ->once() + ->with( 'wpseo-woocommerce-beta-editor-warning' ); + + $this->instance->manage_woocommerce_beta_editor_notification(); + } + + /** + * Tests the admin_init callback when woocommerce_beta_editor is enabled with active notification. + * + * @covers ::manage_woocommerce_beta_editor_notification + */ + public function test_manage_woocommerce_beta_editor_notification_active_and_enable() { + + Monkey\Functions\expect( 'get_option' ) + ->once() + ->with( 'woocommerce_feature_product_block_editor_enabled' ) + ->andReturn( 'yes' ); + + $this->notification_center + ->expects( 'get_notification_by_id' ) + ->once() + ->with( 'wpseo-woocommerce-beta-editor-warning' ) + ->andReturn( true ); + + $this->instance->manage_woocommerce_beta_editor_notification(); + } + + /** + * Tests the admin_init callback when woocommerce_beta_editor is enabled with no active notification. + * + * @covers ::manage_woocommerce_beta_editor_notification + * @covers ::notification + */ + public function test_manage_woocommerce_beta_editor_notification_not_active_and_enable() { + + Monkey\Functions\expect( 'get_option' ) + ->once() + ->with( 'woocommerce_feature_product_block_editor_enabled' ) + ->andReturn( 'yes' ); + + $this->notification_center + ->expects( 'get_notification_by_id' ) + ->once() + ->with( 'wpseo-woocommerce-beta-editor-warning' ) + ->andReturn( false ); + + Monkey\Functions\expect( 'esc_url' ) + ->once(); + + $this->short_link_helper + ->expects( 'get' ) + ->once() + ->with( 'https://yoa.st/learn-how-disable-beta-woocommerce-product-editor' ) + ->andReturn( 'https://yoa.st/learn-how-disable-beta-woocommerce-product-editor' ); + + Monkey\Functions\expect( 'wp_get_current_user' ) + ->once() + ->andReturn( (object) [ 'ID' => 1 ] ); + + $this->notification_helper + ->expects( 'restore_notification' ) + ->once(); + + $this->notification_center + ->expects( 'add_notification' ) + ->once(); + + $this->instance->manage_woocommerce_beta_editor_notification(); + } +} diff --git a/tests/unit/presenters/admin/woocommerce-beta-editor-presenter-test.php b/tests/unit/presenters/admin/woocommerce-beta-editor-presenter-test.php new file mode 100644 index 00000000000..cccc85d9c4f --- /dev/null +++ b/tests/unit/presenters/admin/woocommerce-beta-editor-presenter-test.php @@ -0,0 +1,71 @@ +stubTranslationFunctions(); + $this->short_link_helper = Mockery::mock( Short_Link_Helper::class ); + $this->instance = new Woocommerce_Beta_Editor_Presenter( $this->short_link_helper ); + } + + /** + * Tests returning the notification as an HTML string. + * + * @covers ::present + * @covers ::get_message + */ + public function test_present() { + + Monkey\Functions\expect( 'esc_url' ) + ->once() + ->with( 'https://yoa.st/learn-how-disable-beta-woocommerce-product-editor' ) + ->andReturn( 'https://yoa.st/learn-how-disable-beta-woocommerce-product-editor' ); + + $this->short_link_helper + ->expects( 'get' ) + ->once() + ->with( 'https://yoa.st/learn-how-disable-beta-woocommerce-product-editor' ) + ->andReturn( 'https://yoa.st/learn-how-disable-beta-woocommerce-product-editor' ); + + + $expected = 'Compatibility issue: Yoast SEO is incompatible with the beta WooCommerce product editor. The Yoast SEO interface is currently unavailable in the beta WooCommerce product editor. To resolve any issues, please disable the beta editor. Learn how to disable the beta WooCommerce product editor.
'; + + $this->assertSame( $expected, $this->instance->present() ); + } +}