Skip to content

Commit 26db270

Browse files
Merge pull request #2038 from WordPress/add/admin-view-transitions
Add view transitions for WP Admin
2 parents ff5cc68 + 78d4e13 commit 26db270

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
lines changed

plugins/view-transitions/hooks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function plvt_render_generator(): void {
3131
add_action( 'after_setup_theme', 'plvt_polyfill_theme_support', PHP_INT_MAX );
3232
add_action( 'init', 'plvt_sanitize_view_transitions_theme_support', 1 );
3333
add_action( 'wp_enqueue_scripts', 'plvt_load_view_transitions' );
34+
add_action( 'admin_head', 'plvt_print_view_transitions_admin_style' );
3435

3536
/**
3637
* Hooks related to the View Transitions settings.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Admin related functions for View Transitions.
4+
*
5+
* @package view-transitions
6+
* @since n.e.x.t
7+
*/
8+
9+
// @codeCoverageIgnoreStart
10+
if ( ! defined( 'ABSPATH' ) ) {
11+
exit; // Exit if accessed directly.
12+
}
13+
// @codeCoverageIgnoreEnd
14+
15+
/**
16+
* Outputs the necessary CSS styles for view transitions.
17+
*
18+
* This function is responsible for printing the required inline styles
19+
* to enable or enhance view transitions within the theme or plugin.
20+
* It should be hooked to an appropriate action to ensure the styles
21+
* are included in the page output.
22+
*
23+
* @since n.e.x.t
24+
*/
25+
function plvt_print_view_transitions_admin_style(): void {
26+
$options = plvt_get_stored_setting_value();
27+
if ( ! isset( $options['enable_admin_transitions'] ) || true !== $options['enable_admin_transitions'] ) {
28+
return;
29+
}
30+
?>
31+
<style>
32+
@view-transition { navigation: auto; }
33+
#adminmenu > .menu-top { view-transition-name: attr(id type(<custom-ident>), none); }
34+
</style>
35+
<?php
36+
}

plugins/view-transitions/includes/settings.php

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function plvt_get_view_transition_animation_labels(): array {
4545
* @since 1.0.0
4646
* @see plvt_sanitize_view_transitions_theme_support()
4747
*
48-
* @return array{ default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string } {
48+
* @return array{ default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string, enable_admin_transitions: bool } {
4949
* Default setting value.
5050
*
5151
* @type string $default_transition_animation Default view transition animation.
@@ -54,6 +54,7 @@ function plvt_get_view_transition_animation_labels(): array {
5454
* @type string $post_title_selector CSS selector for the post title element.
5555
* @type string $post_thumbnail_selector CSS selector for the post thumbnail element.
5656
* @type string $post_content_selector CSS selector for the post content element.
57+
* @type bool $enable_admin_transitions Whether to use view transitions in the admin area.
5758
* }
5859
*/
5960
function plvt_get_setting_default(): array {
@@ -64,6 +65,7 @@ function plvt_get_setting_default(): array {
6465
'post_title_selector' => '.wp-block-post-title, .entry-title',
6566
'post_thumbnail_selector' => '.wp-post-image',
6667
'post_content_selector' => '.wp-block-post-content, .entry-content',
68+
'enable_admin_transitions' => false,
6769
);
6870
}
6971

@@ -72,7 +74,7 @@ function plvt_get_setting_default(): array {
7274
*
7375
* @since 1.0.0
7476
*
75-
* @return array{ default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string } {
77+
* @return array{ default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string, enable_admin_transitions: bool } {
7678
* Stored setting value.
7779
*
7880
* @type string $default_transition_animation Default view transition animation.
@@ -81,6 +83,7 @@ function plvt_get_setting_default(): array {
8183
* @type string $post_title_selector CSS selector for the post title element.
8284
* @type string $post_thumbnail_selector CSS selector for the post thumbnail element.
8385
* @type string $post_content_selector CSS selector for the post content element.
86+
* @type bool $enable_admin_transitions Whether to use view transitions in the admin area.
8487
* }
8588
*/
8689
function plvt_get_stored_setting_value(): array {
@@ -93,7 +96,7 @@ function plvt_get_stored_setting_value(): array {
9396
* @since 1.0.0
9497
*
9598
* @param mixed $input Setting to sanitize.
96-
* @return array{ default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string } {
99+
* @return array{ default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string, enable_admin_transitions: bool } {
97100
* Sanitized setting.
98101
*
99102
* @type string $default_transition_animation Default view transition animation.
@@ -102,6 +105,7 @@ function plvt_get_stored_setting_value(): array {
102105
* @type string $post_title_selector CSS selector for the post title element.
103106
* @type string $post_thumbnail_selector CSS selector for the post thumbnail element.
104107
* @type string $post_content_selector CSS selector for the post content element.
108+
* @type bool $enable_admin_transitions Whether to use view transitions in the admin area.
105109
* }
106110
*/
107111
function plvt_sanitize_setting( $input ): array {
@@ -136,6 +140,11 @@ function plvt_sanitize_setting( $input ): array {
136140
}
137141
}
138142

143+
// Sanitize "enable_admin_transitions" as a boolean.
144+
if ( isset( $input['enable_admin_transitions'] ) ) {
145+
$value['enable_admin_transitions'] = (bool) $input['enable_admin_transitions'];
146+
}
147+
139148
return $value;
140149
}
141150

@@ -270,19 +279,30 @@ static function (): void {
270279
'title' => __( 'Post Content Selector', 'view-transitions' ),
271280
'description' => __( 'Provide the CSS selector to detect the post content element.', 'view-transitions' ),
272281
),
282+
'enable_admin_transitions' => array(
283+
'title' => __( 'WP Admin', 'view-transitions' ),
284+
'description' => __( 'Enable view transitions in the WordPress admin area.', 'view-transitions' ),
285+
),
273286
);
274287
foreach ( $fields as $slug => $args ) {
288+
$additional_args = array(
289+
'field' => $slug,
290+
'label_for' => "plvt-view-transitions-field-{$slug}",
291+
);
292+
293+
// Remove 'label_for' for checkbox field to avoid duplicate label association.
294+
if ( 'enable_admin_transitions' === $slug ) {
295+
unset( $additional_args['label_for'] );
296+
}
297+
275298
add_settings_field(
276299
"plvt_view_transitions_{$slug}",
277300
$args['title'],
278301
'plvt_render_settings_field',
279302
'reading',
280303
'plvt_view_transitions',
281304
array_merge(
282-
array(
283-
'field' => $slug,
284-
'label_for' => "plvt-view-transitions-field-{$slug}",
285-
),
305+
$additional_args,
286306
$args
287307
)
288308
);
@@ -312,6 +332,10 @@ function plvt_render_settings_field( array $args ): void {
312332
$type = 'select';
313333
$choices = plvt_get_view_transition_animation_labels();
314334
break;
335+
case 'enable_admin_transitions':
336+
$type = 'checkbox';
337+
$choices = array(); // Defined just for consistency.
338+
break;
315339
default:
316340
$type = 'text';
317341
$choices = array(); // Defined just for consistency.
@@ -346,12 +370,26 @@ function plvt_render_settings_field( array $args ): void {
346370
?>
347371
</select>
348372
<?php
373+
} elseif ( 'checkbox' === $type ) {
374+
?>
375+
<label for="<?php echo esc_attr( "plvt-view-transitions-field-{$args['field']}" ); ?>">
376+
<input
377+
id="<?php echo esc_attr( "plvt-view-transitions-field-{$args['field']}" ); ?>"
378+
name="<?php echo esc_attr( "plvt_view_transitions[{$args['field']}]" ); ?>"
379+
type="checkbox"
380+
value="1"
381+
<?php checked( $value, 1 ); ?>
382+
class="regular-text code"
383+
>
384+
<?php echo esc_html( $args['description'] ); ?>
385+
</label>
386+
<?php
349387
} else {
350388
?>
351389
<input
352390
id="<?php echo esc_attr( $args['label_for'] ); ?>"
353391
name="<?php echo esc_attr( "plvt_view_transitions[{$args['field']}]" ); ?>"
354-
value="<?php echo esc_attr( $value ); ?>"
392+
value="<?php echo esc_attr( (string) $value ); ?>"
355393
class="regular-text code"
356394
<?php
357395
if ( '' !== $args['description'] ) {
@@ -364,7 +402,7 @@ class="regular-text code"
364402
<?php
365403
}
366404

367-
if ( '' !== $args['description'] ) {
405+
if ( '' !== $args['description'] && 'checkbox' !== $type ) {
368406
?>
369407
<p
370408
id="<?php echo esc_attr( $args['label_for'] . '-description' ); ?>"

plugins/view-transitions/view-transitions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
define( 'VIEW_TRANSITIONS_VERSION', '1.0.1' );
2929
define( 'VIEW_TRANSITIONS_MAIN_FILE', __FILE__ );
3030

31+
require_once __DIR__ . '/includes/admin.php';
3132
require_once __DIR__ . '/includes/class-plvt-view-transition-animation.php';
3233
require_once __DIR__ . '/includes/class-plvt-view-transition-animation-registry.php';
3334
require_once __DIR__ . '/includes/functions.php';

0 commit comments

Comments
 (0)