publishpress/instance-protection

用于保护 WordPress 插件同时运行两次的库。

2.0.1 2023-04-18 17:50 UTC

This package is auto-updated.

Last update: 2024-09-18 20:49:05 UTC


README

这是一个用于保护 WordPress 插件同时运行两次实例的库。

安装

应通过运行以下命令将此库添加为 composer 必需项:

composer require publishpress/instance-protection

如何使用

确保插件在更多实例运行时不会损坏

第一步,在要求此库之前,要确保您的插件在网站上有两个或更多实例同时运行时不会损坏。

可以通过在插件加载后定义的常量来实现这一点,并在加载插件前进行检查。如果已定义常量,则不再加载插件(甚至不要求其库)。

此验证必须在任何钩子之外进行。

请确保根据您的插件重命名变量和常量。以下示例复制了 PublishPress Authors 的代码

免费插件示例

if (! defined('PP_AUTHORS_LOADED')) {
    // include libraries, and dependencies
    // instantiate the plugin, hooks, etc

    define('PP_AUTHORS_LOADED', true);
}

专业插件示例

if (! defined('PP_AUTHORS_PRO_LOADED') && ! defined('PP_AUTHORS_LOADED')) {
    // include libraries, and dependencies
    // instantiate the plugin, hooks, etc
    // initialize the free plugin

    define('PP_AUTHORS_PRO_LOADED', true);
}

请注意,专业插件检查两个常量:其自身的常量和免费插件中定义的常量。这样,如果免费插件已作为独立插件运行,则专业插件不会运行。

函数

在全局 escope 上定义函数之前,始终使用 function_exists 在其中添加一个条件。如果它们定义在一个特定的 includes.php 文件中,例如,您可以在包含之前仅使用一个条件,而不是将条件添加到每个函数中。

在声明类之前,请遵循前面主题中的相同方法,但使用 class_exists

添加管理通知库

此库不使用 composer 的自动加载器,因为它需要在插件中的其他所有内容之前加载。但它内部有自己的自动加载器,遵循 PSR-4 模式。

在尝试包含其内容之前,您应始终检查供应商文件夹是否在预期的路径上。如果不在标准文件夹中,请确保为用户提供一个选项来定义一个常量,该常量提供供应商目录的定制路径。

请查看以下示例,了解如何包含和实例化库,但请确保将此代码作为将在全局 escope 中执行的第一件事添加到您的插件中,在任何钩子之外。

免费插件示例

<?php
$includeFilebRelativePath = '/publishpress/instance-protection/include.php';
if (file_exists(__DIR__ . '/vendor' . $includeFilebRelativePath)) {
    require_once __DIR__ . '/vendor' . $includeFilebRelativePath;
} else if (defined('PP_AUTHORS_VENDOR_PATH') && file_exists(PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath)) {
    require_once PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath;
}

if (class_exists('PublishPressInstanceProtection\\Config')) {
    $pluginCheckerConfig = new PublishPressInstanceProtection\Config();
    $pluginCheckerConfig->pluginSlug = 'publishpress-authors';
    $pluginCheckerConfig->pluginName = 'PublishPress Authors';
    $pluginCheckerConfig->pluginFolder = 'publishpress-authors'; // Only required if the folder is different from the slug.

    $pluginChecker = new PublishPressInstanceProtection\InstanceChecker($pluginCheckerConfig);
}

专业插件示例

<?php
$includeFilebRelativePath = '/publishpress/instance-protection/include.php';
if (file_exists(__DIR__ . '/vendor' . $includeFilebRelativePath)) {
    require_once __DIR__ . '/vendor' . $includeFilebRelativePath;
} else if (defined('PP_AUTHORS_VENDOR_PATH') && file_exists(PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath)) {
    require_once PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath;
}

if (class_exists('PublishPressInstanceProtection\\Config')) {
    $pluginCheckerConfig = new PublishPressInstanceProtection\Config();
    $pluginCheckerConfig->pluginSlug = 'publishpress-authors-pro';
    $pluginCheckerConfig->pluginName = 'PublishPress Authors Pro';
    $pluginCheckerConfig->pluginFolder = 'publishpress-authors-pro'; // Only required if the folder is different from the slug.
    $pluginCheckerConfig->isProPlugin = true;
    $pluginCheckerConfig->freePluginName = 'PublishPress Authors';

    $pluginChecker = new PublishPressInstanceProtection\InstanceChecker($pluginCheckerConfig);
}

唯一需要更改的是,您需要包含两个额外的配置:isProPluginfreePluginName

最终示例

最终的代码应类似于以下内容

$includeFilebRelativePath = '/publishpress/instance-protection/include.php';
if (file_exists(__DIR__ . '/vendor' . $includeFilebRelativePath)) {
    require_once __DIR__ . '/vendor' . $includeFilebRelativePath;
} else if (defined('PP_AUTHORS_VENDOR_PATH') && file_exists(PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath)) {
    require_once PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath;
}

if (class_exists('PublishPressInstanceProtection\\Config')) {
    // ....
}

if (! defined('PP_AUTHORS_LOADED')) {
    // include libraries, and dependencies
    // instantiate the plugin, hooks, etc

    define('PP_AUTHORS_LOADED', true);
}