Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: minor
Type: added

Link backups to that backup in the Backup dashboard when the new dashboard is on.
9 changes: 9 additions & 0 deletions projects/packages/activity-log/src/class-initial-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ private function get_data() {
* @param string $template Restore URL template, or an empty string.
*/
'restoreUrlTemplate' => (string) apply_filters( 'jetpack_activity_log_restore_url_template', '' ),
/**
* Filters the in-admin address a link to a backup opens, with `{rewindId}` as the placeholder.
* Empty leaves such links as plain text.
*
* @since $$next-version$$
*
* @param string $template Backup URL template, or an empty string.
*/
'backupUrlTemplate' => (string) apply_filters( 'jetpack_activity_log_backup_url_template', '' ),
),
'config' => array(
// Simple sites have no Jetpack REST API to proxy through, so the app calls WordPress.com directly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ const AnchorBlock: BlockRenderer = ( { content, children, onClick, meta } ) => {
);
};

interface InitialStateWithBackupUrl {
jetpackStatus?: { backupUrlTemplate?: string };
}

declare const JPACTIVITYLOG_INITIAL_STATE: InitialStateWithBackupUrl | undefined;

// The Backup dashboard's view of one backup, with `{rewindId}` to fill in; empty when it can't show one.
const backupUrlTemplate: string =
( typeof JPACTIVITYLOG_INITIAL_STATE !== 'undefined'
? JPACTIVITYLOG_INITIAL_STATE?.jetpackStatus?.backupUrlTemplate
: undefined ) ?? '';

const REWIND_ID = /^\d+(\.\d+)?$/;

// A backup token opens that backup in the Backup dashboard, when there is one to open it in.
const BackupLink: BlockRenderer = ( { content, children } ) => {
if ( ! backupUrlTemplate || ! REWIND_ID.test( content.rewindId ?? '' ) ) {
return <Fragment>{ children }</Fragment>;
}
// Encoded twice: the template puts the id inside the already-encoded `p` query arg.
const rewindId = encodeURIComponent( encodeURIComponent( content.rewindId as string ) );
return <a href={ backupUrlTemplate.replace( '{rewindId}', rewindId ) }>{ children }</a>;
};

// Resolve a token's wp-admin destination (if any). Entities without a
// target (site, backup, or a malformed payload missing an id/slug) fall
// through to plain text.
Expand All @@ -100,10 +124,9 @@ const blockTypeMapping: Record< string, BlockRenderer > = {
person: EntityLink,
plugin: EntityLink,
theme: EntityLink,
// site (we're already on it) and backup (needs the Backup plugin's own
// route) have no generic wp-admin target — render as plain text.
// site has no wp-admin target (we're already on it) — render as plain text.
site: ( { children } ) => <Fragment>{ children }</Fragment>,
backup: ( { children } ) => <Fragment>{ children }</Fragment>,
backup: BackupLink,
};

export const createFormattedBlock = ( mapping: Record< string, BlockRenderer > ) => {
Expand Down
51 changes: 51 additions & 0 deletions projects/packages/activity-log/tests/js/formatted-block.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { render, screen } from '@testing-library/react';

const FORMATTED_BLOCK = '../../src/js/components/ActivityLog/formatted-block';
const TEMPLATE = '/wp-admin/admin.php?page=jetpack-backup&p=%2F%3Fselected%3D%2522{rewindId}%2522';

/**
* Render a backup range with the Backup dashboard's link template in place.
*
* @param rewindId - The range's rewind id.
*/
function renderBackupRange( rewindId: string ) {
( globalThis as Record< string, unknown > ).JPACTIVITYLOG_INITIAL_STATE = {
jetpackStatus: { backupUrlTemplate: TEMPLATE },
};
jest.isolateModules( () => {
// The template is read when the module loads.
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { renderFormattedContent } = require( FORMATTED_BLOCK );
render(
<>
{ renderFormattedContent( {
items: [ { type: 'backup', rewindId, children: [ 'Oct 17, 2026' ] } ],
} ) }
</>
);
} );
}

describe( 'a backup range', () => {
it.each( [ '1792247404', '1792247404.817' ] )(
'links rewind id %s inside the encoded path',
rewindId => {
renderBackupRange( rewindId );

const href =
screen.getByRole( 'link', { name: 'Oct 17, 2026' } ).getAttribute( 'href' ) ?? '';
const path = new URL( href, 'https://example.com' ).searchParams.get( 'p' );
expect( path ).toBe( `/?selected=%22${ rewindId }%22` );
}
);

it.each( [ '123%22&foo=bar', '1792247404.', 'abc', '' ] )(
'stays plain text for rewind id %p',
rewindId => {
renderBackupRange( rewindId );

expect( screen.queryByRole( 'link' ) ).not.toBeInTheDocument();
expect( screen.getByText( 'Oct 17, 2026' ) ).toBeInTheDocument();
}
);
} );
17 changes: 17 additions & 0 deletions projects/packages/activity-log/tests/php/Initial_State_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected function tearDown(): void {
Status_Cache::clear();
wp_set_current_user( 0 );
remove_all_filters( 'jetpack_activity_log_restore_url_template' );
remove_all_filters( 'jetpack_activity_log_backup_url_template' );
parent::tearDown();
}

Expand Down Expand Up @@ -86,4 +87,20 @@ function () use ( $template ) {

$this->assertSame( $template, $this->get_rendered_state()['jetpackStatus']['restoreUrlTemplate'] );
}

public function test_backup_url_template_defaults_to_empty() {
$this->assertSame( '', $this->get_rendered_state()['jetpackStatus']['backupUrlTemplate'] );
}

public function test_backup_url_template_comes_from_the_filter() {
$template = 'https://example.org/wp-admin/admin.php?page=jetpack-backup&p=%2F%3Fselected%3D%2522{rewindId}%2522';
add_filter(
'jetpack_activity_log_backup_url_template',
function () use ( $template ) {
return $template;
}
);

$this->assertSame( $template, $this->get_rendered_state()['jetpackStatus']['backupUrlTemplate'] );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Overview: Open a backup from a link even when it is not on the activity page currently shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Overview: Treat backups that finished with warnings as backups, so they can be opened and restored and their links work.
17 changes: 17 additions & 0 deletions projects/packages/backup/src/class-jetpack-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public static function initialize( array $options = array() ) {
add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
add_action( 'rest_api_init', array( \Automattic\Jetpack\Backup\V0005\REST\Rest_Controller::class, 'register_routes' ) );
add_filter( 'jetpack_activity_log_restore_url_template', array( __CLASS__, 'activity_log_restore_url_template' ) );
add_filter( 'jetpack_activity_log_backup_url_template', array( __CLASS__, 'activity_log_backup_url_template' ) );

add_action( 'admin_menu', array( __CLASS__, 'maybe_load_wp_build' ), 1 );
add_action( 'admin_menu', array( __CLASS__, 'add_wp_admin_submenu' ), 1 ); // Akismet uses 4, so we need to use 1 to ensure both menus are added when only they exist.
Expand Down Expand Up @@ -1326,6 +1327,22 @@ public static function activity_log_restore_url_template( $template ) {
return admin_url( 'admin.php?page=' . self::JETPACK_BACKUP_SLUG . '&p=%2Frestore%2F{rewindId}' );
}

/**
* Sends the Activity Log's links to a backup to this dashboard's Overview, with that backup selected, when it is on.
*
* The router reads search values as JSON, so the rewind id is quoted to stay a string.
*
* @param string $template Backup URL template, with `{rewindId}` as the placeholder.
* @return string
*/
public static function activity_log_backup_url_template( $template ) {
if ( ! self::is_modernized() ) {
return $template;
}

return admin_url( 'admin.php?page=' . self::JETPACK_BACKUP_SLUG . '&p=%2F%3Fselected%3D%2522{rewindId}%2522' );
}

/**
* Returns the modernization filter's value, which defaults to true unless the site has the legacy dashboard sticker.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type FetchArgs = {
number?: number;
page?: number;
sort_order?: ActivitySortOrder;
/** ISO 8601 date-time bounds, so a single backup can be fetched without paging to it. */
after?: string;
before?: string;
};

/**
Expand All @@ -46,6 +49,8 @@ type FetchArgs = {
* @param args.page - 1-indexed page number.
* @param args.number - Items per page.
* @param args.sort_order - Sort direction.
* @param args.after - Only activity after this date-time.
* @param args.before - Only activity before this date-time.
* @return The raw WPCOM-shaped response.
*/
export async function fetchActivityLog(
Expand All @@ -56,6 +61,8 @@ export async function fetchActivityLog(
number: args.number,
page: args.page,
sort_order: args.sort_order,
after: args.after,
before: args.before,
} ),
} );
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ const GRIDICON_TO_KIND: Record< string, ActivityKind > = {
themes: 'theme-update',
};

/**
* A completed backup's activity names. WPCOM swaps a backup's gridicon from
* `cloud` to `notice` when it finished with warnings, so the gridicon alone
* misses those backups (and their "N Warnings" links). Storage-limit failures
* share these names but can't be restored, so they need `is_rewindable` too.
*/
const BACKUP_NAME = /^rewind__backup(_only)?_complete/;

/**
* Whether an entry is a completed backup.
*
* @param entry - WPCOM entry.
* @return True for a backup row.
*/
function isBackupEntry( entry: WpcomActivityEntry ): boolean {
return (
entry.gridicon === 'cloud' ||
( BACKUP_NAME.test( entry.name ?? '' ) && entry.is_rewindable !== false )
);
}

/**
* Convert a single WPCOM rewindable-activity entry into the UI's
* `ActivityItem` shape.
Expand All @@ -30,7 +51,9 @@ const GRIDICON_TO_KIND: Record< string, ActivityKind > = {
* @return The mapped item.
*/
export function normalizeEntry( entry: WpcomActivityEntry ): ActivityItem {
const kind = GRIDICON_TO_KIND[ entry.gridicon ] ?? 'other';
const kind = isBackupEntry( entry )
? 'backup'
: ( GRIDICON_TO_KIND[ entry.gridicon ] ?? 'other' );

const actor = entry.actor ?? { type: 'Application', name: 'Jetpack' };
const base = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ const unknownEntry: WpcomActivityEntry = {
};

describe( 'normalizeEntry', () => {
test( 'maps a backup with warnings to a backup ActivityItem', () => {
expect( normalizeEntry( { ...backupEntry, gridicon: 'notice' } ) ).toMatchObject( {
kind: 'backup',
rewindId: '1777035492',
} );
} );

test( 'keeps a backup-named entry that cannot be restored as other', () => {
expect(
normalizeEntry( { ...backupEntry, gridicon: 'notice', is_rewindable: false } )
).toMatchObject( { kind: 'other' } );
} );

test( 'maps a backup entry to a backup ActivityItem', () => {
const item = normalizeEntry( backupEntry );
expect( item ).toMatchObject( {
Expand Down
3 changes: 3 additions & 0 deletions projects/packages/backup/src/dashboard/data/query-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ export const keys = {
// descending are different rows.
activityLogPage: ( page: number, pageSize: number, sortOrder: ActivitySortOrder ) =>
[ 'backup', 'activity-log', { page, pageSize, sortOrder } ] as const,
// The few seconds of the log around one backup, for a selection no loaded page holds.
activityLogAround: ( rewindId: string ) =>
[ 'backup', 'activity-log', 'around', rewindId ] as const,
// Every loaded page of backup sizes, as one infinite query.
backupSizes: () => [ 'backup', 'sizes' ] as const,
fileTree: ( rewindId: string, folderPath: string | null ) =>
Expand Down
Loading
Loading