publishpress/publishpress-instance-protection

此包已被废弃,不再维护。作者建议使用publishpress/instance-protection包。

保护WordPress插件避免重复运行的库。

v1.0.3 2022-10-28 16:14 UTC

This package is auto-updated.

Last update: 2023-04-28 18:51:02 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);
}

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

函数

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

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

添加管理员通知库

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

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

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

免费插件示例

<?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);
}