@@ -27,6 +27,67 @@ function plvt_polyfill_theme_support(): void {
27
27
add_theme_support ( 'view-transitions ' );
28
28
}
29
29
30
+ /**
31
+ * Sanitizes theme support arguments for the 'view-transitions' feature.
32
+ *
33
+ * If the feature was part of WordPress Core, the logic of this function would become part of the `add_theme_support()`
34
+ * function instead. There is no action or filter that could be used though, hence it is implemented here in a separate
35
+ * function that runs after `after_setup_theme`, but before the 'view-transitions' feature arguments are possibly used.
36
+ *
37
+ * @since 1.0.0
38
+ *
39
+ * @global array<string, mixed> $_wp_theme_features Theme support features added and their arguments.
40
+ */
41
+ function plvt_sanitize_view_transitions_theme_support (): void {
42
+ global $ _wp_theme_features ;
43
+
44
+ if ( ! isset ( $ _wp_theme_features ['view-transitions ' ] ) ) {
45
+ return ;
46
+ }
47
+
48
+ $ args = $ _wp_theme_features ['view-transitions ' ];
49
+
50
+ $ defaults = array (
51
+ 'post-selector ' => '.wp-block-post.post, article.post, body.single main ' ,
52
+ 'global-transition-names ' => array (
53
+ 'header ' => 'header ' ,
54
+ 'main ' => 'main ' ,
55
+ ),
56
+ 'post-transition-names ' => array (
57
+ '.wp-block-post-title, .entry-title ' => 'post-title ' ,
58
+ '.wp-post-image ' => 'post-thumbnail ' ,
59
+ '.wp-block-post-content, .entry-content ' => 'post-content ' ,
60
+ ),
61
+ );
62
+
63
+ // If no specific `$args` were provided, simply use the defaults.
64
+ if ( true === $ args ) {
65
+ $ args = $ defaults ;
66
+ } else {
67
+ /*
68
+ * By default, `add_theme_support()` will take all function parameters as `$args`, but for the
69
+ * 'view-transitions' feature, only a single associative array of arguments is relevant, which is expected as
70
+ * the sole (optional) parameter.
71
+ */
72
+ if ( count ( $ args ) === 1 && isset ( $ args [0 ] ) && is_array ( $ args [0 ] ) ) {
73
+ $ args = $ args [0 ];
74
+ }
75
+
76
+ $ args = wp_parse_args ( $ args , $ defaults );
77
+
78
+ // Enforce correct types.
79
+ if ( ! is_array ( $ args ['global-transition-names ' ] ) ) {
80
+ $ args ['global-transition-names ' ] = array ();
81
+ }
82
+ if ( ! is_array ( $ args ['post-transition-names ' ] ) ) {
83
+ $ args ['post-transition-names ' ] = array ();
84
+ }
85
+ }
86
+
87
+ // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
88
+ $ _wp_theme_features ['view-transitions ' ] = $ args ;
89
+ }
90
+
30
91
/**
31
92
* Loads view transitions based on the current configuration.
32
93
*
@@ -42,4 +103,44 @@ function plvt_load_view_transitions(): void {
42
103
wp_register_style ( 'wp-view-transitions ' , false , array (), null ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
43
104
wp_add_inline_style ( 'wp-view-transitions ' , $ stylesheet );
44
105
wp_enqueue_style ( 'wp-view-transitions ' );
106
+
107
+ $ theme_support = get_theme_support ( 'view-transitions ' );
108
+
109
+ /*
110
+ * No point in loading the script if no specific view transition names are configured.
111
+ */
112
+ if (
113
+ ( ! is_array ( $ theme_support ['global-transition-names ' ] ) || count ( $ theme_support ['global-transition-names ' ] ) === 0 ) &&
114
+ ( ! is_array ( $ theme_support ['post-transition-names ' ] ) || count ( $ theme_support ['post-transition-names ' ] ) === 0 )
115
+ ) {
116
+ return ;
117
+ }
118
+
119
+ $ config = array (
120
+ 'postSelector ' => $ theme_support ['post-selector ' ],
121
+ 'globalTransitionNames ' => $ theme_support ['global-transition-names ' ],
122
+ 'postTransitionNames ' => $ theme_support ['post-transition-names ' ],
123
+ );
124
+
125
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
126
+ $ src_script = file_get_contents ( plvt_get_asset_path ( 'js/view-transitions.js ' ) );
127
+ if ( false === $ src_script || '' === $ src_script ) {
128
+ // This clause should never be entered, but is needed to please PHPStan. Can't hurt to be safe.
129
+ return ;
130
+ }
131
+
132
+ $ init_script = sprintf (
133
+ 'plvtInitViewTransitions( %s ) ' ,
134
+ wp_json_encode ( $ config , JSON_FORCE_OBJECT )
135
+ );
136
+
137
+ /*
138
+ * This must be in the <head>, not in the footer.
139
+ * This is because the pagereveal event listener must be added before the first rAF occurs since that is when the event fires. See <https://issues.chromium.org/issues/40949146#comment10>.
140
+ * An inline script is used to avoid an extra request.
141
+ */
142
+ wp_register_script ( 'wp-view-transitions ' , false , array (), null , array () ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
143
+ wp_add_inline_script ( 'wp-view-transitions ' , $ src_script );
144
+ wp_add_inline_script ( 'wp-view-transitions ' , $ init_script );
145
+ wp_enqueue_script ( 'wp-view-transitions ' );
45
146
}
0 commit comments