Skip to content

Commit

Permalink
Merge pull request #20816 from Yoast/154-add-notification-when-new-wo…
Browse files Browse the repository at this point in the history
…o-product-editor-is-switched-on

154 add notification when new woo product editor is switched on
  • Loading branch information
enricobattocchi authored Nov 3, 2023
2 parents 14b98de + 033414b commit 21ad804
Show file tree
Hide file tree
Showing 4 changed files with 463 additions and 0 deletions.
137 changes: 137 additions & 0 deletions src/integrations/watchers/woocommerce-beta-editor-watcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace Yoast\WP\SEO\Integrations\Watchers;

use Yoast\WP\SEO\Conditionals\Admin_Conditional;
use Yoast\WP\SEO\Conditionals\Not_Admin_Ajax_Conditional;
use Yoast\WP\SEO\Helpers\Notification_Helper;
use Yoast\WP\SEO\Helpers\Short_Link_Helper;
use Yoast\WP\SEO\Integrations\Integration_Interface;
use Yoast\WP\SEO\Presenters\Admin\Woocommerce_Beta_Editor_Presenter;
use Yoast_Notification;
use Yoast_Notification_Center;

/**
* Shows a notification for users who have Woocommerce product beta editor enabled.
*
* @class Woocommerce_Beta_Editor_Watcher
*/
class Woocommerce_Beta_Editor_Watcher implements Integration_Interface {

/**
* The notification ID.
*/
const NOTIFICATION_ID = 'wpseo-woocommerce-beta-editor-warning';

/**
* The short link helper.
*
* @var Short_Link_Helper
*/
protected $short_link_helper;

/**
* The Yoast notification center.
*
* @var Yoast_Notification_Center
*/
protected $notification_center;

/**
* The notification helper.
*
* @var Notification_Helper
*/
protected $notification_helper;

/**
* The Woocommerce beta editor presenter.
*
* @var Woocommerce_Beta_Editor_Presenter
*/
protected $presenter;

/**
* Woocommerce_Beta_Editor_Watcher constructor.
*
* @param Yoast_Notification_Center $notification_center The notification center.
* @param Notification_Helper $notification_helper The notification helper.
* @param Short_Link_Helper $short_link_helper The short link helper.
*/
public function __construct(
Yoast_Notification_Center $notification_center,
Notification_Helper $notification_helper,
Short_Link_Helper $short_link_helper
) {
$this->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,
]
);
}
}
60 changes: 60 additions & 0 deletions src/presenters/admin/woocommerce-beta-editor-presenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Yoast\WP\SEO\Presenters\Admin;

use Yoast\WP\SEO\Helpers\Short_Link_Helper;
use Yoast\WP\SEO\Presenters\Abstract_Presenter;

/**
* Class Woocommerce_Beta_Editor_Presenter.
*/
class Woocommerce_Beta_Editor_Presenter extends Abstract_Presenter {

/**
* The short link helper.
*
* @var Short_Link_Helper
*/
protected $short_link_helper;

/**
* Woocommerce_Beta_Editor_Presenter constructor.
*
* @param Short_Link_Helper $short_link_helper The short link helper.
*/
public function __construct( Short_Link_Helper $short_link_helper ) {
$this->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 = '<p>';
$notification_text .= $this->get_message();
$notification_text .= '</p>';

return $notification_text;
}

/**
* Returns the message to show.
*
* @return string The message.
*/
protected function get_message() {
return \sprintf(
'<strong>%1$s</strong> %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',
'<a href="' . \esc_url( $this->short_link_helper->get( 'https://yoa.st/learn-how-disable-beta-woocommerce-product-editor' ) ) . '" target="_blank">',
'</a>'
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace Yoast\WP\SEO\Tests\Unit\Integrations\Watchers;

use Brain\Monkey;
use Mockery;
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
use Yoast\WP\SEO\Conditionals\Not_Admin_Ajax_Conditional;
use Yoast\WP\SEO\Helpers\Short_Link_Helper;
use Yoast\WP\SEO\Helpers\Notification_Helper;
use Yoast\WP\SEO\Integrations\Watchers\Woocommerce_Beta_Editor_Watcher;
use Yoast\WP\SEO\Tests\Unit\TestCase;
use Yoast_Notification_Center;

/**
* Class Woocommerce_Beta_Editor_Watcher_Test.
*
* @group integrations
* @group watchers
*
* @coversDefaultClass \Yoast\WP\SEO\Integrations\Watchers\Woocommerce_Beta_Editor_Watcher
*/
class Woocommerce_Beta_Editor_Watcher_Test extends TestCase {

/**
* Yoast_Notification_Center mock.
*
* @var Mockery\MockInterface|Yoast_Notification_Center
*/
protected $notification_center;

/**
* Notification_Helper mock.
*
* @var Mockery\MockInterface|Notification_Helper
*/
protected $notification_helper;

/**
* The short link helper.
*
* @var Mockery\MockInterface|Short_Link_Helper
*/
protected $short_link_helper;

/**
* The instance under test.
*
* @var Woocommerce_Beta_Editor_Watcher
*/
protected $instance;

/**
* Sets up the class under test and mock objects.
*/
public function set_up() {
parent::set_up();
$this->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();
}
}
Loading

0 comments on commit 21ad804

Please sign in to comment.