technote/gutenberg-packages

获取 Gutenberg 包信息的库

v1.0.6 2021-06-25 09:57 UTC

README

CI Status codecov CodeFactor License: GPL v2+ PHP: >=5.6 WordPress: >=5.5

此存储库(Gutenberg Packages)管理 Gutenberg 的版本。

Gutenberg Packages此库的包装

Gutenberg Packages 从以下来源获取版本数据:

  1. API(每日更新)
  2. Gutenberg 存储库

并缓存一天。
(当状态(WP 核心版本或 Gutenberg 插件状态)改变时,缓存将被清除)。

目录

详细信息

要求

  • >= PHP 5.6
  • >= WordPress v5.5

安装

composer require technote/gutenberg-packages

使用

<?php
use Technote\GutenbergPackages;

$packages = new GutenbergPackages();

$packages->is_block_editor(); // true or false
$packages->get_gutenberg_version(); // e.g. 6.0.0 (empty string if Gutenberg plugin is not activated)

$packages->get_editor_package_versions(); // array of (package => version), false if block editor is invalid
/** e.g.
[
  "wp-a11y"        => "2.0.2",
  "wp-annotations" => "1.0.8",
  "wp-api-fetch"   => "2.2.8",

  ...

  "wp-url"         => "2.3.3",
  "wp-viewport"    => "2.1.1",
  "wp-wordcount"   => "2.0.3"
]
*/

$packages->get_editor_package_version( 'wp-editor' ); // e.g. 9.0.11
$packages->get_editor_package_version( 'editor' ); // same as above

$packages->is_support_editor_package( 'wp-editor' ); // true or false
$packages->is_support_editor_package( 'editor' ); // same as above

$packages->filter_packages( [
	'editor',
	'wp-editor',
	'test-package',
	'components',
	'wp-data',
	'wp-data',
] );
/** e.g.
[
	'wp-editor',
	'wp-components',
	'wp-data',
]
*/

$packages->fill_package_versions( [
	'editor',
	'wp-editor',
	'test-package',
	'components',
	'wp-data',
	'wp-data',
] );
/** e.g.
[
	'wp-editor'     => '9.0.11',
	'wp-components' => '7.0.8',
	'wp-data'       => '4.2.1',
]
*/

动机

WP 核心没有获取 Block 编辑器包版本的功能。
因此,很难考虑兼容性。

例如
Gutenberg v5.9 输出以下消息。

wp.editor.BlockFormatControls is deprecated and will be removed. Please use wp.blockEditor.BlockFormatControls instead.

如果您的插件使用如下所示的 wp-block-editor 包,则在 WP v5.2 下会出错。

const { BlockFormatControls } = wp.blockEditor;
Uncaught TypeError: Cannot destructure property `BlockFormatControls` of 'undefined' or 'null'.

从 JavaScript 中,检查属性的存在可以轻松解决这个问题。

const { BlockFormatControls } = wp.blockEditor && wp.blockEditor.BlockEdit ? wp.blockEditor : wp.editor;

但您还需要从 PHP 中了解包的有效性,因为 wp_enqueue_script 需要依赖项。
如果将 wp-block-editor 传递给 WP v5.2 下的 wp_enqueue_script,则脚本不会排队。

<?php
wp_enqueue_script( 'test-script', 'path/to/javascript/index.js', [
	'wp-block-editor',
	'wp-components',
	'wp-compose',
	'wp-element',
	'wp-editor',
	'lodash',
] );

此库可以帮助解决这个问题。

<?php
use Technote\GutenbergPackages;

$packages = new GutenbergPackages();
wp_enqueue_script( 'test-script', 'path/to/javascript/index.js', $packages->filter_packages( [
	'wp-block-editor',
	'wp-components',
	'wp-compose',
	'wp-element',
	'wp-editor',
], [ 'lodash' ] ) );

如果使用 WP v5.1,则 wp-block-editor 会过滤。
如果使用超过 WP v5.2,则 wp-block-editor 不会过滤。

您还可以通过 wp_localize_script 将包版本传递给 JavaScript。

<?php
use Technote\GutenbergPackages;

$packages = new GutenbergPackages();

$depends = [
	'wp-block-editor',
	'wp-components',
	'wp-compose',
	'wp-data',
	'wp-element',
	'wp-editor',
];
wp_enqueue_script( 'test-script', 'path/to/javascript/index.js', $packages->filter_packages( $depends, [ 'lodash' ] ) );
wp_localize_script( 'test-script', 'PackageVersions', $packages->fill_package_versions( $depends) );
// JavaScript
console.log( PackageVersions );

依赖关系

作者

GitHub(Technote)
博客