thoughtful-web/activation-requirements-wp

用于简化WordPress插件和主题激活要求的PHP库。作为免费开源软件发布,遵循GNU GPL-2.0+许可证

v1.2.0 2022-02-16 13:52 UTC

This package is auto-updated.

Last update: 2024-09-15 17:52:38 UTC


README

遵循GNU GPL-2.0+许可证的免费开源软件。
版权所有 Zachary Kendall Watkins 2022。

此库简化了在您的插件激活之前需要激活其他插件的依赖。在配置文件 (*.php 或 *.json) 中声明插件激活要求,并使用库的Plugin类应用这些要求。

目录

  1. 功能
  2. 要求
  3. 安装
  4. 最简单实现
  5. 实现
  6. 创建配置文件
  7. 路线图
  8. 开发要求、安装和说明

功能

  1. 在配置文件中声明插件要求,并在插件的激活阶段自动执行这些要求。
  2. 使用wp_die()官方文档链接)格式化有用的消息,以指示需要安装和/或激活哪些插件才能成功激活您的插件。

返回顶部

要求

  1. WordPress 5.4及以上。
  2. PHP 7.3.5及以上。
  3. 此库位于您的插件根目录下两级目录中。示例
    a. vendor/thoughtful-web/activation-requirements-wp
    b. lib/thoughtful-web/activation-requirements-wp
  4. 配置文件或PHP数组(参见创建配置文件

返回顶部

安装

要从Composer直接安装此模块,请使用命令行。然后,使用Composer的自动加载器或在您的PHP中直接要求每个3个类文件。

$ composer require thoughtful-web/activation-requirements-wp

要从Github使用Composer安装此模块,请将其添加为composer.json文件中的存储库

{
    "name": "zachwatkins/wordpress-plugin-name",
    "description": "WordPress plugin boilerplate using best practices, tools, and commonly needed modules.",
	"repositories": [
		{
			"type": "vcs",
			"url": "https://github.com/thoughtful-web/activation-requirements-wp"
		}
	],
	"require": {
		"thoughtful-web/activation-requirements-wp": "dev-main"
	}
}

返回顶部

最简单实现

此库的最简单实现是在以下位置添加配置文件(1)./config/thoughtful-web/activation-requirements.php 或(2)./config/thoughtful-web/activation-requirements.json。然后使用Composer的自动加载器和插件根文件中的主类文件。它应该看起来像这样

require __DIR__ . '/vendor/autoload.php;
new \ThoughtfulWeb\ActivationRequirementsWP\Plugin();

返回顶部

实现

面向公众的类的唯一参数是可选的,可以是配置文件名、文件路径或数组。要使用带有(或没有)配置参数的Plugin类,您应该了解可接受的值

@param array $config The configuration parameters. Either a configuration file name, file path, or array of configuration options.

此库将使用include语句加载PHP文件,或使用file_read_contents()加载JSON文件。以下是此参数的可能值的解释

  1. “无参数”方法要求配置文件位于此处:./config/thoughtful-web/activation-requirements.php。示例
    a. new \ThoughtfulWeb\ActivationRequirementsWP\Plugin();

  2. “文件名”方法接受PHP或JSON文件名,并要求文件位于./config/thoughtful-web/<file>。示例
    a. new \ThoughtfulWeb\ActivationRequirementsWP\Plugin( 'filename.php' );
    b. new \ThoughtfulWeb\ActivationRequirementsWP\Plugin( 'filename.json' );

  3. “文件路径”方法允许配置文件位于服务器上的任何位置,只要./src/Config.php类文件有读取权限。示例
    a. new \ThoughtfulWeb\ActivationRequirementsWP\Plugin( __DIR__ . '/config/filename.json' );
    b. new \ThoughtfulWeb\ActivationRequirementsWP\Plugin( '/home/website/filename.php' );

  4. “数组”方法允许您传递一个包含配置值的最终状态的PHP数组。示例

$config = array(
	'plugins' => array(
		'relation' => 'OR',
		'advanced-custom-fields/acf.php',
		'advanced-custom-fields-pro/acf.php',
	),
)
new \ThoughtfulWeb\ActivationRequirementsWP\Plugin( $config );

注意:为了获得最佳性能,请尽早在您的插件代码中调用类。另外,您必须在没有动作钩子的情况下调用类,或者在与执行顺序中尽早调用动作钩子,以免跳过在此库的类文件中使用的WordPress动作、过滤器和函数。尚未确定哪些动作钩子与类的实例化兼容。

返回顶部

创建配置文件

以下显示了示例配置文件。

./config/thoughtful-web/activation-requirements.json

{
    "plugins": {
        "relation": "OR",
        "0": "advanced-custom-fields\/acf.php",
        "1": "advanced-custom-fields-pro\/acf.php"
    }
}

./config/thoughtful-web/activation-requirements.php

<?php
// For security.
if ( ! defined( 'ABSPATH' ) ) {
    http_response_code( 404 );
    ?><html><head><title>HTTP 404 Not Found</title></head><body><p>The requested page does not exist.</p></body></html>
    <?php
    die();
}

return array(
    'plugins' => array(
        'relation' => 'OR',
        'advanced-custom-fields/acf.php',
        'advanced-custom-fields-pro/acf.php',
    ),
);

以下是一个使用“AND”关系参数的示例

return array(
    'plugins' => array(
        'relation' => 'AND',
        'advanced-custom-fields/acf.php',
        'gravityforms/gravityforms.php',
        'post-smtp/postman-smtp.php',
    ),
);

返回顶部

路线图

以下是我正在考虑或将寻求实施的变化。

  1. 添加一个主题顶层类,该类模仿插件类,但使用主题钩子,例如动作钩子after_switch_theme
  2. 将“themes”参数添加到配置数组中,以检查活动主题或父主题的存在。
  3. 提供配置值,以使用户能够接收激活后的通知。
  4. 考虑通过包含更多插件数据参数来改进错误页面内容(如果可用)。
  5. 在配置中同时声明“AND”和“OR”子句。e 返回顶部

开发要求、安装和说明

要求

  1. PHP 7.3.5+
  2. Composer包管理工具。 获取Composer
  3. PHP Codesniffer Composer模块。
    a. $ composer global require squizlabs/php_codesniffer
  4. WordPress编码标准规则集。
    a. $ composer global require wp-coding-standards/wpcs
    b. $ phpcs --config-set installed_paths $HOME/AppData/Roaming/Composer/vendor/wp-coding-standards/wpcs

安装

在您的命令行界面中运行以下命令,无论您计划在哪里安装库。如果您以这种方式安装库,通常会在类似my-wordpress-plugin/inc/thoughtful-web/的目录中进行。当前git钩子仅自动化创建zip文件,在将更改推送到仓库的主要分支后使用最新标签,并且仅针对Windows用户,因为它运行*.ps1文件。

  1. $ git clone https://github.com/thoughtful-web/activation-requirements-wp
  2. $ cd activation-requirements-wp
  3. $ composer install
  4. $ git config core.hooksPath hooks
  5. 如果您使用Visual Studio Code,我建议您采取以下额外步骤
    a. 安装以下扩展:“PHP Intelephense by Ben Mewburn”和“phpcs by Ioannis Kappas”
    b. 在您的Visual Studio Code工作区设置文件中包含以下键值
{
    "phpcs.standard": "WordPress",
	"intelephense.stubs": [
		"wordpress",
		"apache",
		"bcmath",
		"bz2",
		"calendar",
		"com_dotnet",
		"Core",
		"ctype",
		"curl",
		"date",
		"dba",
		"dom",
		"enchant",
		"exif",
		"FFI",
		"fileinfo",
		"filter",
		"fpm",
		"ftp",
		"gd",
		"gettext",
		"gmp",
		"hash",
		"iconv",
		"imap",
		"intl",
		"json",
		"ldap",
		"libxml",
		"mbstring",
		"meta",
		"mysqli",
		"oci8",
		"odbc",
		"openssl",
		"pcntl",
		"pcre",
		"PDO",
		"pdo_ibm",
		"pdo_mysql",
		"pdo_pgsql",
		"pdo_sqlite",
		"pgsql",
		"Phar",
		"posix",
		"pspell",
		"readline",
		"Reflection",
		"session",
		"shmop",
		"SimpleXML",
		"snmp",
		"soap",
		"sockets",
		"sodium",
		"SPL",
		"sqlite3",
		"standard",
		"superglobals",
		"sysvmsg",
		"sysvsem",
		"sysvshm",
		"tidy",
		"tokenizer",
		"xml",
		"xmlreader",
		"xmlrpc",
		"xmlwriter",
		"xsl",
		"Zend OPcache",
		"zip",
		"zlib"
	]
}

注释

  1. 此存储库使用修改后的WordPress编码标准版本,以允许PSR-4兼容的类文件名。
  2. 要添加新的git钩子文件,请运行$ git add --chmod=+x hooks/<hook-file-name> && git commit -m "Add git hook"

返回顶部