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

General: remove backwards compatibility code now that Jetpack requires WP 5.9 #24086

Merged
merged 14 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
42 changes: 6 additions & 36 deletions docs/examples/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?php
/**
* PHPUnit bootstrap file to apply WordPress core's monkey-patching to versions
* of PHPUnit that don't natively support PHP 8.0 for PHP 8.0.
*
* WARNING: This is outdated as of September 2021. WordPress is in the midst of
* updating their stuff to stop being hacky, but have left WP 5.7 and 5.8 in a
* strange in-between state. Once we only support 5.9+ we should update this
* for whatever it turns out to be.
* PHPUnit bootstrap file.
*
* @package automattic/jetpack
*/
Expand Down Expand Up @@ -52,31 +46,11 @@
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo "Using test root $_tests_dir\n";

// WordPress requires PHPUnit 7.5 or earlier and hacks around a few things to
// make it work with PHP 8. Unfortunately for MockObjects they do it via
// composer.json rather than bootstrap.php, so we have to manually do it here.
if ( version_compare( PHP_VERSION, '8.0', '>=' ) &&
( ! class_exists( PHPUnit\Runner\Version::class ) || version_compare( PHPUnit\Runner\Version::id(), '9.3', '<' ) )
) {
if ( ! class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) &&
file_exists( "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php" )
) {
// phpcs:disable WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath
require "$_tests_dir/includes/phpunit7/MockObject/Builder/NamespaceMatch.php";
require "$_tests_dir/includes/phpunit7/MockObject/Builder/ParametersMatch.php";
require "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php";
require "$_tests_dir/includes/phpunit7/MockObject/MockMethod.php";
// phpcs:enable
} else {
fprintf(
STDOUT,
"Warning: PHPUnit <9.3 is not compatible with PHP 8.0+, and the hack could not be loaded.\n Class %s exists: %s\n File %s exists: %s\n",
PHPUnit\Framework\MockObject\InvocationMocker::class,
class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) ? 'yes (bad)' : 'no (good)',
"$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php",
file_exists( "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php" ) ? 'yes (good)' : 'no (bad)'
);
}
if ( ! is_readable( $_plugin_root . '/vendor/autoload.php' ) ) {
echo 'The plugin is not ready for testing.' . PHP_EOL;
echo PHP_EOL;
echo 'Composer dependencies must be installed.' . PHP_EOL;
exit( 1 );
}

// Give access to tests_add_filter() function.
jeherve marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -96,7 +70,3 @@ function _manually_load_plugin() {

// Load Composer autoloader.
require $_plugin_root . '/vendor/autoload.php';

// Using the Speed Trap Listener provided by WordPress Core testing suite to expose
// slowest running tests. See the configuration in phpunit.xml.dist.
require $_tests_dir . '/includes/listener-loader.php';
3 changes: 1 addition & 2 deletions docs/monorepo.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ WordPress plugins generally want to run within WordPress. All monorepo plugins a

Tests will be run against the latest version of WordPress using the variety of supported PHP versions, and against the previous and master versions of WordPress using the PHP version in `.github/versions.sh`. The environment variable `WP_BRANCH` will be set to 'latest', 'previous', or 'master' accordingly. If you have tests that only need to be run once, run them when `WP_BRANCH` is 'latest'.

<!-- @todo: Update this once we drop support for WP 5.8. -->
Note that the state of WordPress's own PHPUnit integration is currently in flux. For WordPress 5.8 and earlier you need to both use `yoast/phpunit-polyfills` to supply polyfills and need to run with PHPUnit < 8.0 (even on PHP 8, where monkey-patching is required), while for 5.9 you can use `yoast/phpunit-polyfills` normally. Your best bet for the moment is to copy what Jetpack is doing; once the situation has stabilized, we'll update this documentation and [the example bootstrap.php](./examples/bootstrap.php).
When implementing tests within a new plugin, you can follow the example set in [the example bootstrap.php](./examples/bootstrap.php).

### JavaScript tests

Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/update-wp-compat-60-rm-old
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: compat

General: remove backwards compatibility code now that Jetpack requires WordPress 5.9.
8 changes: 1 addition & 7 deletions projects/plugins/jetpack/extensions/blocks/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ export function ButtonEdit( props ) {

usePassthroughAttributes( { attributes, clientId, setAttributes } );

const onChange = value => {
// TODO: Remove `replace` once minimum Gutenberg version is 8.0 (to fully support `disableLineBreaks`)
const newValue = 'input' === element ? value.replace( /<br>/gim, ' ' ) : value;
setAttributes( { text: newValue } );
};

useEffect( () => {
// Reset button width if switching to left or right (floated) alignment for first time.
const alignmentChanged = previousAlign !== align;
Expand Down Expand Up @@ -98,7 +92,7 @@ export function ButtonEdit( props ) {
allowedFormats={ 'input' === element ? [] : undefined }
className={ buttonClasses }
disableLineBreaks={ 'input' === element }
onChange={ onChange }
onChange={ value => setAttributes( { text: value } ) }
placeholder={ placeholder || __( 'Add text…', 'jetpack' ) }
style={ buttonStyles }
value={ text }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
* WordPress dependencies
*/
import { ESCAPE, SPACE } from '@wordpress/keycodes';
import { useFocusReturn, useConstrainedTabbing, useFocusOnMount } from '@wordpress/compose';

// TODO: replace with `import { useMergeRefs } from '@wordpress/compose';` when package is upgraded to ^3.24.4
import useMergeRefs from '../lib/use-merge-refs';
import {
useFocusReturn,
useConstrainedTabbing,
useFocusOnMount,
useMergeRefs,
} from '@wordpress/compose';

export default function ModalFrame( {
overlayClassName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
}

.edit-post-pinned-plugins {
.components-icon-button, // Gutenberg < 7.2.0. @TODO: Remove once we drop support
.components-button.has-icon // Gutenberg >= 7.2.0
{
.components-button.has-icon {
&:not( .is-toggled ),
&:hover,
&.is-toggled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,6 @@ public function callback( $path = '', $blog_id = 0 ) {

if ( $authors_only ) {
$query['capability'] = array( 'edit_posts' );
// To-do: remove this once Jetpack requires WordPress 5.9.
global $wp_version;
if ( version_compare( $wp_version, '5.9-alpha', '<' ) ) {
$query['who'] = 'authors';
unset( $query['capability'] );
}
}

if ( ! empty( $args['search'] ) ) {
Expand Down
41 changes: 11 additions & 30 deletions projects/plugins/jetpack/modules/shortcodes/slideshow.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,39 +216,20 @@ public function slideshow_js( $attr ) {
// Enqueue scripts.
$this->enqueue_scripts();

$output = '';

if ( defined( 'JSON_HEX_AMP' ) ) {
// This is nice to have, but not strictly necessary since we use _wp_specialchars() below.
$gallery = wp_json_encode( $attr['gallery'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); // phpcs:ignore PHPCompatibility
} else {
$gallery = wp_json_encode( $attr['gallery'] );
}

$output .= '<p class="jetpack-slideshow-noscript robots-nocontent">' . esc_html__( 'This slideshow requires JavaScript.', 'jetpack' ) . '</p>';
$output = '<p class="jetpack-slideshow-noscript robots-nocontent">' . esc_html__( 'This slideshow requires JavaScript.', 'jetpack' ) . '</p>';

/*
* The input to json_encode() above can contain '&quot;'.
*
* For calls to json_encode() lacking the JSON_HEX_AMP option,
* that '&quot;' is left unaltered. Running '&quot;' through esc_attr()
* also leaves it unaltered since esc_attr() does not double-encode.
*
* This means we end up with an attribute like
* `data-gallery="{&quot;foo&quot;:&quot;&quot;&quot;}"`,
* which is interpreted by the browser as `{"foo":"""}`,
* which cannot be JSON decoded.
*
* The preferred workaround is to include the JSON_HEX_AMP (and friends)
* options, but these are not available until 5.3.0.
* Alternatively, we can use _wp_specialchars( , , , true ) instead of
* esc_attr(), which will double-encode.
*
* Since we can't rely on JSON_HEX_AMP, we do both.
*
* @todo Update when minimum is PHP 5.3+
* Checking for JSON_HEX_AMP and friends here allows us to get rid of
* '&quot;', that can sometimes be included in the JSON input in some languages like French.
*/
$gallery_attributes = _wp_specialchars( wp_check_invalid_utf8( $gallery ), ENT_QUOTES, false, true );
$gallery_attributes = _wp_specialchars(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect esc_attr() might have been enough now. But this works too.

wp_check_invalid_utf8(
wp_json_encode( $attr['gallery'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT )
),
ENT_QUOTES,
false,
true
);

$output .= sprintf(
'<div id="%s" class="jetpack-slideshow-window jetpack-slideshow jetpack-slideshow-%s" data-trans="%s" data-autostart="%s" data-gallery="%s" itemscope itemtype="https://schema.org/ImageGallery"></div>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,6 @@ public static function widget_admin_setup() {
'fields' => array( 'ID', 'display_name' ),
);

// To-do: remove this once Jetpack requires WordPress 5.9.
global $wp_version;
if ( version_compare( $wp_version, '5.9-alpha', '<' ) ) {
$authors_args['who'] = 'authors';
unset( $authors_args['capability'] );
}

$authors = get_users( $authors_args );

foreach ( $authors as $author ) {
Expand Down
7 changes: 0 additions & 7 deletions projects/plugins/jetpack/modules/widgets/authors.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,6 @@ public function widget( $args, $instance ) {
)
);

// To-do: remove this once Jetpack requires WordPress 5.9.
global $wp_version;
if ( version_compare( $wp_version, '5.9-alpha', '<' ) ) {
$get_author_params['who'] = 'authors';
unset( $get_author_params['capability'] );
}

$authors = get_users( $get_author_params );

echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
Expand Down
1 change: 0 additions & 1 deletion projects/plugins/jetpack/tests/action-phpunit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

set -eo pipefail

. tests/maybe-downgrade-phpunit.sh
exec phpunit "$@"
2 changes: 0 additions & 2 deletions projects/plugins/jetpack/tests/action-test-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

set -eo pipefail

. tests/maybe-downgrade-phpunit.sh

PLUGINDIR="$PWD"

cd "$MONOREPO_BASE/projects/plugins/jetpack"
Expand Down
2 changes: 0 additions & 2 deletions projects/plugins/jetpack/tests/action-test-php.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

set -eo pipefail

. tests/maybe-downgrade-phpunit.sh

echo "::group::Jetpack tests"
phpunit
echo "::endgroup::"
Expand Down
24 changes: 0 additions & 24 deletions projects/plugins/jetpack/tests/maybe-downgrade-phpunit.sh

This file was deleted.

Loading