jayjay666/wp-requirements-checker

WordPress插件开发者检查需求的小工具。用于验证PHP版本、PHP扩展、插件及其在WordPress插件中的版本的开源库类。

1.2.1 2023-01-12 10:40 UTC

This package is auto-updated.

Last update: 2024-09-23 13:42:41 UTC


README

Not Maintained

用于检查PHP版本、PHP扩展、插件及其在WordPress插件中的版本的开源库类。

用法

将需求传递给此类的新实例,如下所示

use jayjay666\WPRequirementsChecker\Validator;

// ... code before plugin/theme init


// ... start init code with
// Set PHP version, plugin root file path and plugin text domain
$validator = new Validator('7.1', 'my-awesome-plugin/my-awesome-plugin.php', 'my-awesome-plugin');

// Set plugins requirements
$validator->add_required_plugin('woocommerce/woocommerce','1.2.1');
$validator->add_required_plugin('elementor/elementor.php','3.0');

// Set PHP extensions requirements
$validator->add_required_extensions('curl');

if (!$validator->check()) {
    // ... min. requirements not valid. Automatic print error & disable plugin in check() method ad break code
    return;
}

// ... if requirements is valid, run your plugin code
// ... your plugin init code for start plugin

实现和安装

您可以通过两种方式将WP Requirements集成到项目中。

复制此类(不推荐)

您可以从本项目的 /src/Validator.php 复制此类。

重要!如果您选择这样做,请将此类重命名为您的项目使用的名称前缀(例如:从Validator更改为My_Plugin_Validator)。这样,项目之间的命名冲突风险更小。同时,也要更改命名空间!

如果您想手动包含文件,请在类存在条件中包含include或require调用,如下所示

// Namespace & prefix must be yours!!!
if (!class_exists( 'jayjay666\WPRequirementsChecker\Validator') ) {
    // do the file include or require here
} 

使用composer(推荐方式)

使用以下方式包含此库

$ composer require jayjay666/wp-requirements-checker

用法示例

use Elementor\Controls_Manager;use Elementor\Element_Section;
use jayjay666\WPRequirementsChecker\Validator;

class MyAwesomePlugin{
    const DOMAIN = 'my-awesome-plugin';
    
    private static $_instance = null;

    public static function instance()
    {

        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    public function __construct()
    {
        add_action('plugins_loaded', [$this, 'init']);
    }
    
    public function init()
    {
        // Check requirements
        $validator = new Validator('7.1', 'my-awesome-plugin/my-awesome-plugin.php', MyAwesomePlugin::DOMAIN);
        // OR $validator = new Validator('7.1', 'my-awesome-plugin/my-awesome-plugin.php', self::DOMAIN);

        $validator->add_required_plugin('elementor/elementor.php','3.0');
        if (!$validator->check()) {
            return;
        }

        // add controlls to elementor
        add_action('elementor/element/section/section_layout/before_section_end', [__CLASS__, 'add_section_controls']);
    }
  
    public static function add_section_controls(Element_Section $element)
    {
        //
        // Přidám responzivní pro zarovnání slopců
        $element->add_responsive_control(
            'scb_section_horizontal_align',
            [
                'label' => __('Horizontal align', MyAwesomePlugin::DOMAIN),
                'type' => Controls_Manager::SELECT,
                'default' => '',
                'options' => [
                    '' => __('Default', 'elementor'),
                    'flex-start' => __('Start', 'elementor'),
                    'flex-end' => __('End', 'elementor'),
                    'center' => __('Center', 'elementor'),
                    'space-between' => __('Space Between', 'elementor'),
                    'space-around' => __('Space Around', 'elementor'),
                    'space-evenly' => __('Space Evenly', 'elementor'),
                ],
                'selectors' => [ // Zde se předají data do CSS
                    '{{WRAPPER}} .elementor-row' => 'justify-content: {{VALUE}};',
                ],
            ]
        );
    }
}

测试于

最小需求