Skip to content

Commit 27592d1

Browse files
authored
Merge pull request #1998 from WordPress/add/scaffold-view-transitions-plugin-basics
Scaffold View Transitions feature plugin
2 parents ff8b62b + f75b6b1 commit 27592d1

File tree

13 files changed

+260
-0
lines changed

13 files changed

+260
-0
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@
3232

3333
# Plugin: Speculative Loading
3434
/plugins/speculation-rules @felixarntz
35+
36+
# Plugin: View Transitions
37+
/plugins/view-transitions @felixarntz

.github/workflows/php-test-plugins.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ jobs:
9595
npm run test-php:image-prioritizer -- -- -- --coverage-clover=./single-site-reports/coverage-image-prioritizer.xml
9696
npm run test-php:optimization-detective -- -- -- --coverage-clover=./single-site-reports/coverage-optimization-detective.xml
9797
npm run test-php:speculation-rules -- -- -- --coverage-clover=./single-site-reports/coverage-speculation-rules.xml
98+
npm run test-php:view-transitions -- -- -- --coverage-clover=./single-site-reports/coverage-view-transitions.xml
9899
npm run test-php:web-worker-offloading -- -- -- --coverage-clover=./single-site-reports/coverage-web-worker-offloading.xml
99100
npm run test-php:webp-uploads -- -- -- --coverage-clover=./single-site-reports/coverage-webp-uploads.xml
100101
else
@@ -110,6 +111,7 @@ jobs:
110111
npm run test-php-multisite:image-prioritizer -- -- -- --coverage-clover=./multisite-reports/coverage-multisite-image-prioritizer.xml
111112
npm run test-php-multisite:optimization-detective -- -- -- --coverage-clover=./multisite-reports/coverage-multisite-optimization-detective.xml
112113
npm run test-php-multisite:speculation-rules -- -- -- --coverage-clover=./multisite-reports/coverage-multisite-speculation-rules.xml
114+
npm run test-php-multisite:view-transitions -- -- -- --coverage-clover=./multisite-reports/coverage-multisite-view-transitions.xml
113115
npm run test-php-multisite:web-worker-offloading -- -- -- --coverage-clover=./multisite-reports/coverage-multisite-web-worker-offloading.xml
114116
npm run test-php-multisite:webp-uploads -- -- -- --coverage-clover=./multisite-reports/coverage-multisite-webp-uploads.xml
115117
else

.wp-env.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"./plugins/image-prioritizer",
99
"./plugins/performance-lab",
1010
"./plugins/speculation-rules",
11+
"./plugins/view-transitions",
1112
"./plugins/web-worker-offloading",
1213
"./plugins/webp-uploads"
1314
],

composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@format:optimization-detective",
5656
"@format:performance-lab",
5757
"@format:speculation-rules",
58+
"@format:view-transitions",
5859
"@format:web-worker-offloading",
5960
"@format:webp-uploads"
6061
],
@@ -65,6 +66,7 @@
6566
"format:optimization-detective": "@format -- ./plugins/optimization-detective --standard=./plugins/optimization-detective/phpcs.xml.dist",
6667
"format:performance-lab": "@format -- ./plugins/performance-lab --standard=./plugins/performance-lab/phpcs.xml.dist",
6768
"format:speculation-rules": "@format -- ./plugins/speculation-rules --standard=./plugins/speculation-rules/phpcs.xml.dist",
69+
"format:view-transitions": "@format -- ./plugins/view-transitions --standard=./plugins/view-transitions/phpcs.xml.dist",
6870
"format:web-worker-offloading": "@format -- ./plugins/web-worker-offloading --standard=./plugins/web-worker-offloading/phpcs.xml.dist",
6971
"format:webp-uploads": "@format -- ./plugins/webp-uploads --standard=./plugins/webp-uploads/phpcs.xml.dist",
7072
"lint": "phpcs",
@@ -77,6 +79,7 @@
7779
"@lint:optimization-detective",
7880
"@lint:performance-lab",
7981
"@lint:speculation-rules",
82+
"@lint:view-transitions",
8083
"@lint:web-worker-offloading",
8184
"@lint:webp-uploads"
8285
],
@@ -87,6 +90,7 @@
8790
"lint:optimization-detective": "@lint -- ./plugins/optimization-detective --standard=./plugins/optimization-detective/phpcs.xml.dist",
8891
"lint:performance-lab": "@lint -- ./plugins/performance-lab --standard=./plugins/performance-lab/phpcs.xml.dist",
8992
"lint:speculation-rules": "@lint -- ./plugins/speculation-rules --standard=./plugins/speculation-rules/phpcs.xml.dist",
93+
"lint:view-transitions": "@lint -- ./plugins/view-transitions --standard=./plugins/view-transitions/phpcs.xml.dist",
9094
"lint:web-worker-offloading": "@lint -- ./plugins/web-worker-offloading --standard=./plugins/web-worker-offloading/phpcs.xml.dist",
9195
"lint:webp-uploads": "@lint -- ./plugins/webp-uploads --standard=./plugins/webp-uploads/phpcs.xml.dist",
9296
"phpstan": "phpstan analyse --memory-limit=2048M",
@@ -102,6 +106,7 @@
102106
"@test-multisite:optimization-detective",
103107
"@test-multisite:performance-lab",
104108
"@test-multisite:speculation-rules",
109+
"@test-multisite:view-transitions",
105110
"@test-multisite:web-worker-offloading",
106111
"@test-multisite:webp-uploads"
107112
],
@@ -112,6 +117,7 @@
112117
"test-multisite:optimization-detective": "@test-multisite --verbose --testsuite optimization-detective",
113118
"test-multisite:performance-lab": "@test-multisite --verbose --testsuite performance-lab",
114119
"test-multisite:speculation-rules": "@test-multisite --verbose --testsuite speculation-rules",
120+
"test-multisite:view-transitions": "@test-multisite --verbose --testsuite view-transitions",
115121
"test-multisite:web-worker-offloading": "@test-multisite --verbose --testsuite web-worker-offloading",
116122
"test-multisite:webp-uploads": "@test-multisite --verbose --testsuite webp-uploads",
117123
"test:plugins": [
@@ -122,6 +128,7 @@
122128
"@test:optimization-detective",
123129
"@test:performance-lab",
124130
"@test:speculation-rules",
131+
"@test:view-transitions",
125132
"@test:web-worker-offloading",
126133
"@test:webp-uploads"
127134
],
@@ -132,6 +139,7 @@
132139
"test:optimization-detective": "@test --verbose --testsuite optimization-detective",
133140
"test:performance-lab": "@test --verbose --testsuite performance-lab",
134141
"test:speculation-rules": "@test --verbose --testsuite speculation-rules",
142+
"test:view-transitions": "@test --verbose --testsuite view-transitions",
135143
"test:web-worker-offloading": "@test --verbose --testsuite web-worker-offloading",
136144
"test:webp-uploads": "@test --verbose --testsuite webp-uploads"
137145
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"build:plugin:image-prioritizer": "webpack --mode production --env plugin=image-prioritizer",
4444
"build:plugin:optimization-detective": "webpack --mode production --env plugin=optimization-detective",
4545
"build:plugin:speculation-rules": "webpack --mode production --env plugin=speculation-rules",
46+
"build:plugin:view-transitions": "webpack --mode production --env plugin=view-transitions",
4647
"build:plugin:web-worker-offloading": "webpack --mode production --env plugin=web-worker-offloading",
4748
"build:plugin:webp-uploads": "webpack --mode production --env plugin=webp-uploads",
4849
"generate-pending-release-diffs": "bin/generate-pending-release-diffs.sh",
@@ -62,6 +63,7 @@
6263
"test-php:image-prioritizer": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test:image-prioritizer",
6364
"test-php:optimization-detective": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test:optimization-detective",
6465
"test-php:speculation-rules": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test:speculation-rules",
66+
"test-php:view-transitions": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test:view-transitions",
6567
"test-php:web-worker-offloading": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test:web-worker-offloading",
6668
"test-php:webp-uploads": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test:webp-uploads",
6769
"test-php-multisite:performance-lab": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test-multisite:performance-lab",
@@ -71,6 +73,7 @@
7173
"test-php-multisite:image-prioritizer": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test-multisite:image-prioritizer",
7274
"test-php-multisite:optimization-detective": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test-multisite:optimization-detective",
7375
"test-php-multisite:speculation-rules": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test-multisite:speculation-rules",
76+
"test-php-multisite:view-transitions": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test-multisite:view-transitions",
7477
"test-php-multisite:web-worker-offloading": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test-multisite:web-worker-offloading",
7578
"test-php-multisite:webp-uploads": "wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test-multisite:webp-uploads",
7679
"wp-env": "wp-env",

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
<testsuite name="speculation-rules">
3030
<directory suffix=".php">plugins/speculation-rules/tests</directory>
3131
</testsuite>
32+
<testsuite name="view-transitions">
33+
<directory suffix=".php">plugins/view-transitions/tests</directory>
34+
</testsuite>
3235
<testsuite name="web-worker-offloading">
3336
<directory suffix=".php">plugins/web-worker-offloading/tests</directory>
3437
</testsuite>

plugins/view-transitions/hooks.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Hook callbacks used for View Transitions.
4+
*
5+
* @package view-transitions
6+
* @since 1.0.0
7+
*/
8+
9+
// @codeCoverageIgnoreStart
10+
if ( ! defined( 'ABSPATH' ) ) {
11+
exit; // Exit if accessed directly.
12+
}
13+
// @codeCoverageIgnoreEnd
14+
15+
/**
16+
* Displays the HTML generator tag for the plugin.
17+
*
18+
* See {@see 'wp_head'}.
19+
*
20+
* @since 1.0.0
21+
*/
22+
function plvt_render_generator(): void {
23+
// Use the plugin slug as it is immutable.
24+
echo '<meta name="generator" content="view-transitions ' . esc_attr( VIEW_TRANSITIONS_VERSION ) . '">' . "\n";
25+
}
26+
add_action( 'wp_head', 'plvt_render_generator' );
27+
28+
/**
29+
* Filters related to the View Transitions functionality.
30+
*/
31+
add_action( 'after_setup_theme', 'plvt_polyfill_theme_support', PHP_INT_MAX );
32+
add_action( 'wp_enqueue_scripts', 'plvt_load_view_transitions' );
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Theme related functions for View Transitions.
4+
*
5+
* @package view-transitions
6+
* @since 1.0.0
7+
*/
8+
9+
/**
10+
* Polyfills theme support for 'view-transitions', regardless of the theme.
11+
*
12+
* In WordPress Core, the 'view-transitions' feature may end up as an optional feature, or it may be added by default.
13+
* In any case, in the scope of the plugin it does not make sense to have the feature as opt-in, since it is the entire
14+
* purpose of the plugin.
15+
*
16+
* Therefore, this function will unconditionally add support with the default configuration, unless the theme itself
17+
* actually added support for it already.
18+
*
19+
* This function must run at the latest possible priority for `after_setup_theme`.
20+
*
21+
* @since 1.0.0
22+
*/
23+
function plvt_polyfill_theme_support(): void {
24+
if ( current_theme_supports( 'view-transitions' ) ) {
25+
return;
26+
}
27+
add_theme_support( 'view-transitions' );
28+
}
29+
30+
/**
31+
* Loads view transitions based on the current configuration.
32+
*
33+
* @since 1.0.0
34+
*/
35+
function plvt_load_view_transitions(): void {
36+
if ( ! current_theme_supports( 'view-transitions' ) ) {
37+
return;
38+
}
39+
40+
// Use an inline style to avoid an extra request.
41+
$stylesheet = '@view-transition { navigation: auto; }';
42+
wp_register_style( 'wp-view-transitions', false, array(), null ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
43+
wp_add_inline_style( 'wp-view-transitions', $stylesheet );
44+
wp_enqueue_style( 'wp-view-transitions' );
45+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WPP-ViewTransitions">
3+
<description>WordPress Coding Standards for View Transitions Plugin</description>
4+
5+
<rule ref="../../tools/phpcs/phpcs.ruleset.xml"/>
6+
7+
<config name="text_domain" value="view-transitions"/>
8+
9+
<file>.</file>
10+
</ruleset>

plugins/view-transitions/readme.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
=== View Transitions ===
2+
3+
Contributors: wordpressdotorg
4+
Tested up to: 6.8
5+
Stable tag: 1.4.0
6+
License: GPLv2 or later
7+
License URI: https://www.gnu.org/licenses/gpl-2.0.html
8+
Tags: performance, view transitions, smooth transitions, animations
9+
10+
Adds smooth transitions between navigations to your WordPress site.
11+
12+
== Description ==
13+
14+
This plugin implements support for [cross-document view transitions](https://developer.chrome.com/docs/web-platform/view-transitions/cross-document) in WordPress. This effectively replaces the hard transitions when navigating from one URL to the other with a smooth animation, by default using a fade effect.
15+
16+
= Browser support =
17+
18+
Cross-document view transitions are supported in a variety of browsers, including Chrome, Edge, and Safari. Users with browsers that currently do not support it should not see any adverse effects when the plugin is active. They will simply not benefit from the feature and continue to experience the traditional hard transitions between URLs.
19+
20+
[Please refer to "Can I use..." for a comprehensive overview of browser support for the feature.](https://caniuse.com/mdn-css_at-rules_view-transition)
21+
22+
== Installation ==
23+
24+
= Installation from within WordPress =
25+
26+
1. Visit **Plugins > Add New**.
27+
2. Search for **View Transitions**.
28+
3. Install and activate the **View Transitions** plugin.
29+
30+
= Manual installation =
31+
32+
1. Upload the entire plugin folder to the `/wp-content/plugins/` directory.
33+
2. Visit **Plugins**.
34+
3. Activate the **View Transitions** plugin.
35+
36+
== Frequently Asked Questions ==
37+
38+
= Where can I submit my plugin feedback? =
39+
40+
Feedback is encouraged and much appreciated, especially since this plugin may contain future WordPress core features. If you have suggestions or requests for new features, you can [submit them as an issue in the WordPress Performance Team's GitHub repository](https://github.com/WordPress/performance/issues/new/choose). If you need help with troubleshooting or have a question about the plugin, please [create a new topic on our support forum](https://wordpress.org/support/plugin/view-transitions/#new-topic-0).
41+
42+
= Where can I report security bugs? =
43+
44+
The Performance team and WordPress community take security bugs seriously. We appreciate your efforts to responsibly disclose your findings, and will make every effort to acknowledge your contributions.
45+
46+
To report a security issue, please visit the [WordPress HackerOne](https://hackerone.com/wordpress) program.
47+
48+
= How can I contribute to the plugin? =
49+
50+
Contributions are always welcome! Learn more about how to get involved in the [Core Performance Team Handbook](https://make.wordpress.org/performance/handbook/get-involved/).
51+
52+
== Changelog ==
53+
54+
= 1.0.0 =
55+
56+
* Initial release.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Tests for the View Transitions plugin hooks.php file.
4+
*
5+
* @package view-transitions
6+
* @group view-transitions
7+
*/
8+
9+
class Test_ViewTransitions_Hooks extends WP_UnitTestCase {
10+
11+
public function test_hooks(): void {
12+
$this->assertSame( 10, has_action( 'wp_head', 'plvt_render_generator' ) );
13+
$this->assertSame( PHP_INT_MAX, has_action( 'after_setup_theme', 'plvt_polyfill_theme_support' ) );
14+
$this->assertSame( 10, has_action( 'wp_enqueue_scripts', 'plvt_load_view_transitions' ) );
15+
}
16+
17+
public function test_plvt_render_generator(): void {
18+
$expected = '<meta name="generator" content="view-transitions ' . VIEW_TRANSITIONS_VERSION . '">' . "\n";
19+
$output = get_echo( 'plvt_render_generator' );
20+
$this->assertSame( $expected, $output );
21+
}
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Tests for the View Transitions plugin includes/theme.php file.
4+
*
5+
* @package view-transitions
6+
* @group view-transitions
7+
*/
8+
9+
class Test_ViewTransitions_Theme extends WP_UnitTestCase {
10+
11+
public function test_plvt_polyfill_theme_support(): void {
12+
// Test polyfill without support registered.
13+
remove_theme_support( 'view-transitions' );
14+
plvt_polyfill_theme_support();
15+
$this->assertTrue( current_theme_supports( 'view-transitions' ) );
16+
$this->assertTrue( get_theme_support( 'view-transitions' ) );
17+
18+
// Test polyfill does not override theme support arguments if already provided by the actual theme.
19+
add_theme_support( 'view-transitions', array( 'custom_key' => 'custom_value' ) );
20+
plvt_polyfill_theme_support();
21+
$this->assertTrue( current_theme_supports( 'view-transitions' ) );
22+
$this->assertSame( array( array( 'custom_key' => 'custom_value' ) ), get_theme_support( 'view-transitions' ) );
23+
}
24+
25+
public function test_plvt_load_view_transitions(): void {
26+
// Clear up style if it is already registered.
27+
if ( wp_style_is( 'wp-view-transitions', 'registered' ) ) {
28+
unset( wp_styles()->registered['wp-view-transitions'] );
29+
}
30+
31+
// Test that without theme support this does nothing.
32+
remove_theme_support( 'view-transitions' );
33+
plvt_load_view_transitions();
34+
$this->assertFalse( wp_style_is( 'wp-view-transitions', 'registered' ) );
35+
$this->assertFalse( wp_style_is( 'wp-view-transitions', 'enqueued' ) );
36+
37+
// Test that with theme support it registers and enqueues the style.
38+
add_theme_support( 'view-transitions' );
39+
plvt_load_view_transitions();
40+
$this->assertTrue( wp_style_is( 'wp-view-transitions', 'registered' ) );
41+
$this->assertTrue( wp_style_is( 'wp-view-transitions', 'enqueued' ) );
42+
}
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Plugin Name: View Transitions
4+
* Plugin URI: https://github.com/WordPress/performance/tree/trunk/plugins/view-transitions
5+
* Description: Adds smooth transitions between navigations to your WordPress site.
6+
* Requires at least: 6.6
7+
* Requires PHP: 7.2
8+
* Version: 1.0.0
9+
* Author: WordPress Performance Team
10+
* Author URI: https://make.wordpress.org/performance/
11+
* License: GPLv2 or later
12+
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
13+
* Text Domain: view-transitions
14+
*
15+
* @package view-transitions
16+
*/
17+
18+
// @codeCoverageIgnoreStart
19+
if ( ! defined( 'ABSPATH' ) ) {
20+
exit; // Exit if accessed directly.
21+
}
22+
23+
// Define the constant.
24+
if ( defined( 'VIEW_TRANSITIONS_VERSION' ) ) {
25+
return;
26+
}
27+
28+
define( 'VIEW_TRANSITIONS_VERSION', '1.0.0' );
29+
30+
require_once __DIR__ . '/includes/theme.php';
31+
require_once __DIR__ . '/hooks.php';
32+
// @codeCoverageIgnoreEnd

0 commit comments

Comments
 (0)