Skip to content

Commit

Permalink
Merge pull request #21647 from Yoast/275-preprare-backend-to-hook-pro…
Browse files Browse the repository at this point in the history
…blems-and-notifications-into-alert-center-front-end

275 preprare backend to hook problems and notifications into alert center front end
  • Loading branch information
vraja-pro committed Sep 20, 2024
2 parents 6e5689e + 7da2e10 commit 59c08c8
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 12 deletions.
7 changes: 4 additions & 3 deletions admin/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

use Yoast\WP\SEO\Actions\Alert_Dismissal_Action;
use Yoast\WP\SEO\Dashboard\User_Interface\New_Dashboard_Page_Integration;
use Yoast\WP\SEO\Conditionals\New_Dashboard_Ui_Conditional;
use Yoast\WP\SEO\Integrations\Academy_Integration;
use Yoast\WP\SEO\Integrations\Settings_Integration;
use Yoast\WP\SEO\Integrations\Support_Integration;
Expand Down Expand Up @@ -48,9 +48,10 @@ public function __construct() {
* @return void
*/
public function init() {
$conditional = new New_Dashboard_Ui_Conditional();
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
$page = isset( $_GET['page'] ) && is_string( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
if ( in_array( $page, [ Settings_Integration::PAGE, Academy_Integration::PAGE, Support_Integration::PAGE ], true ) ) {
if ( in_array( $page, [ Settings_Integration::PAGE, Academy_Integration::PAGE, Support_Integration::PAGE ], true ) || $conditional->is_met() ) {
// Bail, this is managed in the applicable integration.
return;
}
Expand Down Expand Up @@ -104,7 +105,7 @@ public function config_page_scripts() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
$page = isset( $_GET['page'] ) && is_string( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';

if ( in_array( $page, [ WPSEO_Admin::PAGE_IDENTIFIER, New_Dashboard_Page_Integration::PAGE, 'wpseo_workouts' ], true ) ) {
if ( in_array( $page, [ WPSEO_Admin::PAGE_IDENTIFIER, 'wpseo_workouts' ], true ) ) {
wp_enqueue_media();

$script_data['userEditUrl'] = add_query_arg( 'user_id', '{user_id}', admin_url( 'user-edit.php' ) );
Expand Down
22 changes: 18 additions & 4 deletions src/dashboard/user-interface/new-dashboard-page-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
use Yoast\WP\SEO\Conditionals\New_Dashboard_Ui_Conditional;
use Yoast\WP\SEO\Helpers\Current_Page_Helper;
use Yoast\WP\SEO\Helpers\Notification_Helper;
use Yoast\WP\SEO\Helpers\Product_Helper;
use Yoast\WP\SEO\Helpers\Short_Link_Helper;
use Yoast\WP\SEO\Integrations\Integration_Interface;
Expand All @@ -20,6 +21,13 @@ class New_Dashboard_Page_Integration implements Integration_Interface {
*/
public const PAGE = 'wpseo_dashboard';

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

/**
* Holds the WPSEO_Admin_Asset_Manager.
*
Expand Down Expand Up @@ -55,17 +63,20 @@ class New_Dashboard_Page_Integration implements Integration_Interface {
* @param Current_Page_Helper $current_page_helper The Current_Page_Helper.
* @param Product_Helper $product_helper The Product_Helper.
* @param Short_Link_Helper $shortlink_helper The Short_Link_Helper.
* @param Notification_Helper $notification_helper The Notification_Helper.
*/
public function __construct(
WPSEO_Admin_Asset_Manager $asset_manager,
Current_Page_Helper $current_page_helper,
Product_Helper $product_helper,
Short_Link_Helper $shortlink_helper
Short_Link_Helper $shortlink_helper,
Notification_Helper $notification_helper
) {
$this->asset_manager = $asset_manager;
$this->current_page_helper = $current_page_helper;
$this->product_helper = $product_helper;
$this->shortlink_helper = $shortlink_helper;
$this->notification_helper = $notification_helper;
}

/**
Expand Down Expand Up @@ -141,7 +152,7 @@ public function enqueue_assets() {
\wp_enqueue_media();
$this->asset_manager->enqueue_script( 'new-dashboard' );
$this->asset_manager->enqueue_style( 'new-dashboard' );
$this->asset_manager->localize_script( 'dashboard', 'wpseoScriptData', $this->get_script_data() );
$this->asset_manager->localize_script( 'new-dashboard', 'wpseoScriptData', $this->get_script_data() );
}

/**
Expand All @@ -151,7 +162,7 @@ public function enqueue_assets() {
*/
private function get_script_data() {
return [
'preferences' => [
'preferences' => [
'isPremium' => $this->product_helper->is_premium(),
'isRtl' => \is_rtl(),
'pluginUrl' => \plugins_url( '', \WPSEO_FILE ),
Expand All @@ -160,7 +171,10 @@ private function get_script_data() {
'premiumCtbId' => 'f6a84663-465f-4cb5-8ba5-f7a6d72224b2',
],
],
'linkParams' => $this->shortlink_helper->get_query_params(),
'linkParams' => $this->shortlink_helper->get_query_params(),
'userEditUrl' => \add_query_arg( 'user_id', '{user_id}', \admin_url( 'user-edit.php' ) ),
'problems' => $this->notification_helper->get_problems(),
'notifications' => $this->notification_helper->get_notifications(),
];
}
}
103 changes: 103 additions & 0 deletions src/helpers/notification-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,107 @@ class Notification_Helper {
public function restore_notification( Yoast_Notification $notification ) {
return Yoast_Notification_Center::restore_notification( $notification );
}

/**
* Return the notifications sorted on type and priority. (wrapper function)
*
* @codeCoverageIgnore
*
* @return array|Yoast_Notification[] Sorted Notifications
*/
public function get_sorted_notifications() {
$notification_center = Yoast_Notification_Center::get();

return $notification_center->get_sorted_notifications();
}

/**
* Check if the user has dismissed a notification. (wrapper function)
*
* @param Yoast_Notification $notification The notification to check for dismissal.
* @param int|null $user_id User ID to check on.
*
* @codeCoverageIgnore
*
* @return bool
*/
private function is_notification_dismissed( Yoast_Notification $notification, $user_id = null ) {
return Yoast_Notification_Center::is_notification_dismissed( $notification, $user_id );
}

/**
* Parses all the notifications to an array with just warnings notifications, and splitting them between dismissed
* and active.
*
* @return array<Yoast_Notification>
*/
public function get_notifications(): array {
$all_notifications = $this->get_sorted_notifications();
$notifications = \array_filter(
$all_notifications,
static function ( $notification ) {
return $notification->get_type() !== 'error';
}
);
$dismissed_notifications = \array_filter(
$notifications,
function ( $notification ) {
return $this->is_notification_dismissed( $notification );
}
);
$active_notifications = \array_diff( $notifications, $dismissed_notifications );

return [
'dismissed' => \array_map(
static function ( $notification ) {
return $notification->to_array();
},
$dismissed_notifications
),
'active' => \array_map(
static function ( $notification ) {
return $notification->to_array();
},
$active_notifications
),
];
}

/**
* Parses all the notifications to an array with just error notifications, and splitting them between dismissed and
* active.
*
* @return array<Yoast_Notification>
*/
public function get_problems(): array {
$all_notifications = $this->get_sorted_notifications();
$problems = \array_filter(
$all_notifications,
static function ( $notification ) {
return $notification->get_type() === 'error';
}
);
$dismissed_problems = \array_filter(
$problems,
function ( $notification ) {
return $this->is_notification_dismissed( $notification );
}
);
$active_problems = \array_diff( $problems, $dismissed_problems );

return [
'dismissed' => \array_map(
static function ( $notification ) {
return $notification->to_array();
},
$dismissed_problems
),
'active' => \array_map(
static function ( $notification ) {
return $notification->to_array();
},
$active_problems
),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Yoast\WP\SEO\Conditionals\New_Dashboard_Ui_Conditional;
use Yoast\WP\SEO\Dashboard\User_Interface\New_Dashboard_Page_Integration;
use Yoast\WP\SEO\Helpers\Current_Page_Helper;
use Yoast\WP\SEO\Helpers\Notification_Helper;
use Yoast\WP\SEO\Helpers\Product_Helper;
use Yoast\WP\SEO\Helpers\Short_Link_Helper;
use Yoast\WP\SEO\Integrations\Academy_Integration;
Expand Down Expand Up @@ -51,6 +52,13 @@ final class New_Dashboard_Page_Integration_Test extends TestCase {
*/
private $shortlink_helper;

/**
* Holds the Notification_Helper.
*
* @var Notification_Helper
*/
private $notifications_helper;

/**
* The class under test.
*
Expand All @@ -66,16 +74,18 @@ final class New_Dashboard_Page_Integration_Test extends TestCase {
public function set_up() {
$this->stubTranslationFunctions();

$this->asset_manager = Mockery::mock( WPSEO_Admin_Asset_Manager::class );
$this->current_page_helper = Mockery::mock( Current_Page_Helper::class );
$this->product_helper = Mockery::mock( Product_Helper::class );
$this->shortlink_helper = Mockery::mock( Short_Link_Helper::class );
$this->asset_manager = Mockery::mock( WPSEO_Admin_Asset_Manager::class );
$this->current_page_helper = Mockery::mock( Current_Page_Helper::class );
$this->product_helper = Mockery::mock( Product_Helper::class );
$this->shortlink_helper = Mockery::mock( Short_Link_Helper::class );
$this->notifications_helper = Mockery::mock( Notification_Helper::class );

$this->instance = new New_Dashboard_Page_Integration(
$this->asset_manager,
$this->current_page_helper,
$this->product_helper,
$this->shortlink_helper
$this->shortlink_helper,
$this->notifications_helper
);
}

Expand Down Expand Up @@ -220,6 +230,8 @@ public function test_enqueue_assets() {
->once();

Monkey\Functions\expect( 'wp_enqueue_media' )->once();
Monkey\Functions\expect( 'add_query_arg' )->once();
Monkey\Functions\expect( 'admin_url' )->once();

$this->asset_manager
->expects( 'enqueue_script' )
Expand Down Expand Up @@ -272,6 +284,16 @@ public function expect_get_script_data() {
->once()
->andReturn( $link_params );

$this->notifications_helper
->expects( 'get_problems' )
->once()
->andReturn( [] );

$this->notifications_helper
->expects( 'get_notifications' )
->once()
->andReturn( [] );

return $link_params;
}
}

0 comments on commit 59c08c8

Please sign in to comment.