Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate views: Enhance notices #40996

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Remove duplicate views: Enhance notices
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Guide } from '@wordpress/components';
import { createRoot, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { __, hasTranslation as _hasTranslation, sprintf } from '@wordpress/i18n';
import {
Icon,
archive,
Expand All @@ -18,8 +18,135 @@ import bgPattern from './removed-calypso-screen-bg-pattern.png';

import './removed-calypso-screen-notice.scss';

const hasTranslation = text => {
const currentLanguage = document.querySelector( 'html' )?.getAttribute( 'lang' );

if ( currentLanguage?.startsWith( 'en' ) ) {
return true;
}

return _hasTranslation( text, undefined, 'jetpack-mu-wpcom' );
};

const Notice = () => {
const [ isOpen, setIsOpen ] = useState( true );

const titleFallback = sprintf(
// translators: %s: page name
__( 'The %s view just got better', 'jetpack-mu-wpcom' ),
removedCalypsoScreenNoticeConfig.title
);

const descriptionFallback = sprintf(
// translators: %s: page name
__(
"We've adopted WordPress's main %s view to bring improvements to you and millions of WordPress users worldwide.",
'jetpack-mu-wpcom'
),
removedCalypsoScreenNoticeConfig.title
);

const config = {
'edit.php': {
icon: verse,
title: hasTranslation( 'The Posts view just got better' )
? __( 'The Posts view just got better', 'jetpack-mu-wpcom' )
: titleFallback,
description: hasTranslation(
"We've adopted WordPress' main Posts view to bring improvements to you and millions of WordPress users worldwide."
)
? __(
"We've adopted WordPress' main Posts view to bring improvements to you and millions of WordPress users worldwide.",
'jetpack-mu-wpcom'
)
: descriptionFallback,
},
'edit.php?post_type=page': {
icon: pages,
title: hasTranslation( 'The Pages view just got better' )
? __( 'The Pages view just got better', 'jetpack-mu-wpcom' )
: titleFallback,
description: hasTranslation(
"We've adopted WordPress' main Pages view to bring improvements to you and millions of WordPress users worldwide."
)
? __(
"We've adopted WordPress' main Pages view to bring improvements to you and millions of WordPress users worldwide.",
'jetpack-mu-wpcom'
)
: descriptionFallback,
},
'edit.php?post_type=jetpack-portfolio': {
icon: archive,
title: hasTranslation( 'The Portfolio Projects view just got better' )
? __( 'The Portfolio Projects view just got better', 'jetpack-mu-wpcom' )
: titleFallback,
description: hasTranslation(
"We've adopted WordPress' main Portfolio Projects view to bring improvements to you and millions of WordPress users worldwide."
)
? __(
"We've adopted WordPress' main Portfolio Projects view to bring improvements to you and millions of WordPress users worldwide.",
'jetpack-mu-wpcom'
)
: descriptionFallback,
},
'edit.php?post_type=jetpack-testimonial': {
icon: commentContent,
title: hasTranslation( 'The Testimonials view just got better' )
? __( 'The Testimonials view just got better', 'jetpack-mu-wpcom' )
: titleFallback,
description: hasTranslation(
"We've adopted WordPress' main Testimonials view to bring improvements to you and millions of WordPress users worldwide."
)
? __(
"We've adopted WordPress' main Testimonials view to bring improvements to you and millions of WordPress users worldwide.",
'jetpack-mu-wpcom'
)
: descriptionFallback,
},
'edit-comments.php': {
icon: postComments,
title: hasTranslation( 'The Comments view just got better' )
? __( 'The Comments view just got better', 'jetpack-mu-wpcom' )
: titleFallback,
description: hasTranslation(
"We've adopted WordPress' main Comments view to bring improvements to you and millions of WordPress users worldwide."
)
? __(
"We've adopted WordPress' main Comments view to bring improvements to you and millions of WordPress users worldwide.",
'jetpack-mu-wpcom'
)
: descriptionFallback,
},
'edit-tags.php?taxonomy=category': {
icon: category,
title: hasTranslation( 'The Categories view just got better' )
? __( 'The Categories view just got better', 'jetpack-mu-wpcom' )
: titleFallback,
description: hasTranslation(
"We've adopted WordPress' main Categories view to bring improvements to you and millions of WordPress users worldwide."
)
? __(
"We've adopted WordPress' main Categories view to bring improvements to you and millions of WordPress users worldwide.",
'jetpack-mu-wpcom'
)
: descriptionFallback,
},
'edit-tags.php?taxonomy=post_tag': {
icon: tag,
title: hasTranslation( 'The Tags view just got better' )
? __( 'The Tags view just got better', 'jetpack-mu-wpcom' )
: titleFallback,
description: hasTranslation(
"We've adopted WordPress' main Tags view to bring improvements to you and millions of WordPress users worldwide."
)
? __(
"We've adopted WordPress' main Tags view to bring improvements to you and millions of WordPress users worldwide.",
'jetpack-mu-wpcom'
)
: descriptionFallback,
},
};

const icons = {
'edit.php': verse,
'edit.php?post_type=page': pages,
Expand Down Expand Up @@ -58,38 +185,27 @@ const Notice = () => {
return (
<Guide
className="removed-calypso-screen-notice"
contentLabel={ title }
contentLabel={ config[ removedCalypsoScreenNoticeConfig.screen ].title }
finishButtonText={ __( 'Got it', 'jetpack-mu-wpcom' ) }
onFinish={ dismiss }
pages={ [
{
image: (
<>
<div
className="removed-calypso-screen-notice__image"
style={ { backgroundImage: `url(${ bgPattern })` } }
>
<Icon
icon={ icons[ removedCalypsoScreenNoticeConfig.screen ] }
size={ 72 }
className="removed-calypso-screen-notice__icon"
></Icon>
</div>
</>
<div
className="removed-calypso-screen-notice__image"
style={ { backgroundImage: `url(${ bgPattern })` } }
>
<Icon
icon={ config[ removedCalypsoScreenNoticeConfig.screen ].icon }
size={ 72 }
className="removed-calypso-screen-notice__icon"
></Icon>
</div>
),
content: (
<>
<h1>{ title }</h1>
<p>
{ sprintf(
// translators: %s: page name
__(
"We've adopted WordPress's main %s view to bring improvements to you and millions of WordPress users worldwide.",
'jetpack-mu-wpcom'
),
removedCalypsoScreenNoticeConfig.title
) }
</p>
<p>{ config[ removedCalypsoScreenNoticeConfig.screen ].description }</p>
</>
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,23 @@ function wpcom_is_duplicate_views_experiment_enabled() {
* the first time.
*/
function wpcom_show_removed_calypso_screen_notice() {
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
$blog_id = get_current_blog_id();
} else {
$jetpack_options = get_option( 'jetpack_options' );
if ( is_array( $jetpack_options ) && isset( $jetpack_options['id'] ) ) {
$blog_id = (int) $jetpack_options['id'];
} else {
$blog_id = get_current_blog_id();
}
}

// Do not show notice on sites created the experiment started.
// 240673796 is the ID of a site created on 2025-01-13.
if ( $blog_id > 240673796 ) {
return;
}

$admin_menu_class = wpcom_get_custom_admin_menu_class();
if ( ! $admin_menu_class ) {
return;
Expand All @@ -466,9 +483,43 @@ function wpcom_show_removed_calypso_screen_notice() {
return;
}

$dismissed_notices = get_user_option( 'wpcom_removed_calypso_screen_dismissed_notices' );
if ( is_array( $dismissed_notices ) && in_array( $current_screen, $dismissed_notices, true ) ) {
return;
if ( ( new Host() )->is_wpcom_simple() ) {
$preferences = get_user_attribute( get_current_user_id(), 'calypso_preferences' );
$is_dismissed = $preferences[ 'removed-calypso-screen-dismissed-notice-' . $current_screen ] ?? false;
if ( $is_dismissed ) {
return;
}
} else {
$notices_dismissed_locally = get_user_option( 'wpcom_removed_calypso_screen_dismissed_notices' );
if ( ! is_array( $notices_dismissed_locally ) ) {
$notices_dismissed_locally = array();
}

if ( in_array( $current_screen, $notices_dismissed_locally, true ) ) {
return;
}

if ( ! ( new Jetpack_Connection() )->is_user_connected() ) {
return;
}

$response = Client::wpcom_json_api_request_as_user( '/me/preferences', 'v2' );
if ( is_wp_error( $response ) ) {
return;
}

$response_code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $response_code ) {
return;
}

$preferences = json_decode( wp_remote_retrieve_body( $response ), true );
$is_dismissed = $preferences[ 'removed-calypso-screen-dismissed-notice-' . $current_screen ] ?? false;
mmtr marked this conversation as resolved.
Show resolved Hide resolved
if ( $is_dismissed ) {
$notices_dismissed_locally[] = $current_screen;
update_user_option( get_current_user_id(), 'wpcom_removed_calypso_screen_dismissed_notices', $notices_dismissed_locally, true );
return;
}
}

if ( ! wpcom_is_duplicate_views_experiment_enabled() ) {
Expand Down Expand Up @@ -531,18 +582,32 @@ function wpcom_get_custom_admin_menu_class() {
}

/**
* Handles the AJAX request to dismiss a notice of a removed Calypsos screen.
* Handles the AJAX request to dismiss a notice of a removed Calypso screen.
*/
function wpcom_dismiss_removed_calypso_screen_notice() {
check_ajax_referer( 'wpcom_dismiss_removed_calypso_screen_notice' );
if ( isset( $_REQUEST['screen'] ) ) {
$screen = sanitize_text_field( wp_unslash( $_REQUEST['screen'] ) );
$dismissed_notices = get_user_option( 'wpcom_removed_calypso_screen_dismissed_notices' );
if ( ! is_array( $dismissed_notices ) ) {
$dismissed_notices = array();
$screen = sanitize_text_field( wp_unslash( $_REQUEST['screen'] ) );
if ( ( new Host() )->is_wpcom_simple() ) {
$preferences = get_user_attribute( get_current_user_id(), 'calypso_preferences' );
$preferences[ 'removed-calypso-screen-dismissed-notice-' . $screen ] = true;
update_user_attribute( get_current_user_id(), 'calypso_preferences', $preferences );
} else {
Client::wpcom_json_api_request_as_user(
'/me/preferences',
'2',
array(
'method' => 'POST',
),
array( 'calypso_preferences' => (object) array( 'removed-calypso-screen-dismissed-notice-' . $screen => true ) )
);
$notices_dismissed_locally = get_user_option( 'wpcom_removed_calypso_screen_dismissed_notices' );
if ( ! is_array( $notices_dismissed_locally ) ) {
$notices_dismissed_locally = array();
}
$notices_dismissed_locally[] = $screen;
update_user_option( get_current_user_id(), 'wpcom_removed_calypso_screen_dismissed_notices', $notices_dismissed_locally, true );
}
$dismissed_notices[] = $screen;
update_user_option( get_current_user_id(), 'wpcom_removed_calypso_screen_dismissed_notices', $dismissed_notices, true );
}
wp_die();
}
Expand Down
Loading