Skip to content

Instantly share code, notes, and snippets.

@peterwilsoncc
Created October 27, 2023 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterwilsoncc/7d766525c94b664c5c6f11bfaae2dc30 to your computer and use it in GitHub Desktop.
Save peterwilsoncc/7d766525c94b664c5c6f11bfaae2dc30 to your computer and use it in GitHub Desktop.
Demonstration of how plugin land deals with deprecations while supporting multiple versions of WordPress.
<?php
/**
* Shim to account for WordPress 5.8 changes to widgets screen.
*
* This code was written to avoid the block widgets screen from throwing
* notices in WordPress 5.8 while allowing custom block plugins to support
* WordPress 5.1 and earlier.
*
* The original gist was a private gist with identifying information of the
* the plugin I was working on for my employer at the time. I've republished
* it as a public gist with the identifying information removed.
*
* At the time it was republished it's not really needed any more but it's
* purpose is to demonstrate the lengths needed to go to in plugin land to
* deal with changes to the block editor.
*
* This discussion is taking place in the gutenberg repo:
* https://github.com/WordPress/gutenberg/issues/55401
*
* If your plugin still supports WordPress 5.1 you may need it but only
* one of the ten most popular plugins do that so you can probably drop
* support for yours too :)
*/
namespace PWCC\AvoidPhpNoticeOnBlockWidgetsScreen;
function editor_shim() {
// Get a clean copy of $wp_version in to the local scope.
require_once ABSPATH . WPINC . '/version.php';
/*
* Shim to allow plugin to support WP versions prior to 5.2.
*
* In WordPress 5.2 a number of components were relocated from
* @wordpress/editor to @wordpress/block-editor with @wordpress/editor
* providing backward-compatibility support.
*
* In WordPress 5.8 the widgets screen started throwing a notice
* if @wordpress/editor was used to enable support of WordPress 5.1
* and earlier.
*
* @link https://github.com/WordPress/gutenberg/issues/33576
* @link https://core.trac.wordpress.org/ticket/53437
* @link https://make.wordpress.org/core/?p=41500
*/
wp_register_script(
'pwcc-block-editor-package-shim',
false,
version_compare( $wp_version, '5.2.0', 'lt' ) ? [ 'wp-editor' ] : [ 'wp-block-editor' ],
'1.0.0',
true
);
wp_add_inline_script(
'pwcc-block-editor-package-shim',
'window.pwccBlockEditorShim = wp.blockEditor || wp.editor;'
);
wp_enqueue_script(
'pwcc-plugin-editor-script',
plugins_url( 'usage.js', __FILE__ ),
[ 'pwcc-block-editor-package-shim' /* other deps */ ],
'1.0.0',
true
)
}
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\editor_shim' );
import {
FontSizePicker,
ToggleControl,
PanelBody,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
/*
* @TODO: Import from block-editor instead.
*
* This will require bumping the minimum supported version of WP to
* WordPress 5.3 (released late 2019) once done.
*/
const {
InspectorControls,
PanelColorSettings,
RichText,
} = window.pwccBlockEditorShim;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment