OOP:如果PHP版本低于5.4,则显示警告并停用插件

时间:2016-08-31 作者:Kamal Ahmed

如果用户的PHP版本低于5.4.

当我使用非OOP创建插件时,代码运行良好。它向用户显示警告并停用插件,并阻止用户激活插件。

工作代码如下:

非OOP

// check for required php version and deactivate the plugin if php version is less.
 if ( version_compare( PHP_VERSION, \'5.4\', \'<\' )) {
add_action( \'admin_notices\', \'show_notice\', 100 );
function show_notice() { ?>
    <div class="error"> <p>
            <?php
            echo \'MyPluginName requires minimum PHP 5.4 to function properly. Please upgrade PHP version. The Plugin has been auto-deactivated.. You have PHP version \'.PHP_VERSION;
            ?>
        </p></div>
    <?php
    if ( isset( $_GET[\'activate\'] ) ) {
        unset( $_GET[\'activate\'] );
    }
}

// deactivate the plugin because required php version is less.
add_action( \'admin_init\', \'MyPluginName_deactivate_self\' );
function MyPluginName_deactivate_self() {
    deactivate_plugins(plugin_basename( __FILE__ ) );
}
return;
}
然而,当我使用Object Oriented Programing. 我尝试了以下方法:

尝试#1

// check for required php version and deactivate the plugin if php version is less.
 if ( version_compare( PHP_VERSION, \'5.4\', \'<\' )) {
add_action( \'admin_notices\', \'show_notice\', 100 );
function show_notice() { ?>
    <div class="error"> <p>
            <?php
            echo \'MyPluginName requires minimum PHP 5.4 to function properly. Please upgrade PHP version. The Plugin has been auto-deactivated.. You have PHP version \'.PHP_VERSION;
            ?>
        </p></div>
    <?php
    if ( isset( $_GET[\'activate\'] ) ) {
        unset( $_GET[\'activate\'] );
    }
}

// deactivate the plugin because required php version is less.
add_action( \'admin_init\', \'MyPluginName_deactivate_self\' );
function MyPluginName_deactivate_self() {
    deactivate_plugins(plugin_basename( __FILE__ ) );
}
return;
}


if ( ! class_exists(\'MyPluginClass\') ) :
class MyPluginClass {
function __construct( ){
        //enqueue scripts/styles only for front-end
        add_action(\'template_redirect\', [$this, \'user_enqueue_scripts\']);
        //enqueue scripts and style only for admin panel
        add_action( \'admin_enqueue_scripts\', array( $this, \'admin_enqueue_scripts\' ) );

        }
}

endif;
$MyPlugin = new MyPluginClass();
尝试#2
if ( ! class_exists(\'MyPluginClass\') ) :
    class MyPluginClass {
        function __construct( ){}

        public function check_php_version (){
            // check for required php version and deactivate the plugin if php version is less.
            if ( version_compare( PHP_VERSION, \'5.4\', \'<\' )) {
                add_action( \'admin_notices\', \'show_notice\', 100 );
                function show_notice() { ?>
                    <div class="error"> <p>
                            <?php
                            echo \'MyPluginName requires minimum PHP 5.4 to function properly. Please upgrade PHP version. The Plugin has been auto-deactivated.. You have PHP version \'.PHP_VERSION;
                            ?>
                        </p></div>
                    <?php
                    if ( isset( $_GET[\'activate\'] ) ) {
                        unset( $_GET[\'activate\'] );
                    }
                }

                // deactivate the plugin because required php version is less.
                add_action( \'admin_init\', \'MyPluginName_deactivate_self\' );
                function MyPluginName_deactivate_self() {
                    deactivate_plugins(plugin_basename( __FILE__ ) );
                }
                return;
            }

        }

        public function init() {
            //enqueue scripts/styles only for front-end
            add_action(\'template_redirect\', [$this, \'user_enqueue_scripts\']);
            //enqueue scripts and style only for admin panel
            add_action( \'admin_enqueue_scripts\', array( $this, \'admin_enqueue_scripts\' ) );
        }

    }

endif;
$MyPlugin = new MyPluginClass();
$MyPlugin->check_php_version(); // show warning if php version is less than 5.4 and deactivate the plugin
$MyPlugin->init();// initialize the plugin.
请让我知道我做错了什么。

1 个回复
最合适的回答,由SO网友:Ethan O\'Sullivan 整理而成

这是我在使用OOP创建插件时使用的基本模板。您可以根据自己的喜好随意修改。

模板

if ( !defined( \'ABSPATH\' ) ) exit; // Exit if accessed directly

if ( !class_exists( \'MyPluginName\' ) ) {

    class MyPluginName {

        public function __construct() { // Call your actions/filters here
            add_action( \'tag\', array( $this, \'plgn_abbr_function\' ), 10, 1 );
        }

        // Begin functions here
        public function plgn_abbr_function() {
            # Code here...
        }
    }
}

if ( class_exists( \'MyPluginName\' ) ) { // Instantiate the plugin class
    global $plgn_abbr;
    $plgn_abbr = new MyPluginName();
}
通过浏览您的尝试,我立即注意到了以下几点:

您想给您的add_actions__construct() 并将您的功能放置在这之外,但您还需要更改它的布局add_action 布局

add_action( \'admin_notices\', \'show_notice\', 100 );

当使用class, 它应该是:

add_action( \'admin_notices\', array( $this, \'show_notice\' ), 100 );

public 在所有这样的功能之前:

public function your_code()

    解决方案

    这里是对您的代码的更新,使用了我在上面提供的模板,它已经过测试,并且在我这方面有效:

    if ( !defined( \'ABSPATH\' ) ) exit; // Exit if accessed directly
    
    if ( !class_exists( \'MyPluginName\' ) ) {
    
        class MyPluginName {
    
            public function __construct() {
                // check for required php version and deactivate the plugin if php version is less.
                 if ( version_compare( PHP_VERSION, \'5.4\', \'<\' ) ) {
                    add_action( \'admin_notices\', array( $this, \'show_notice\' ), 100 );
                    add_action( \'admin_init\', array( $this, \'MyPluginName_deactivate_self\' ) );
                    return;
                }
            }
    
            public function show_notice() {
                ?>
                <div class="error">
                    <p><?php echo \'MyPluginName requires minimum PHP 5.4 to function properly. Please upgrade PHP version. The Plugin has been auto-deactivated.. You have PHP version \'.PHP_VERSION; ?></p>
                </div>
                <?php
                if ( isset( $_GET[\'activate\'] ) ) {
                    unset( $_GET[\'activate\'] );
                }
            }
            public function MyPluginName_deactivate_self() {
                deactivate_plugins( plugin_basename( __FILE__ ) );
            }
        }
    }
    
    if ( class_exists( \'MyPluginName\' ) ) { // Instantiate the plugin class
        global $plgn_abbr;
        $plgn_abbr = new MyPluginName();
    }
    

相关推荐

Can I make plugins required?

我正在开发自己的Wordpress主题,将用于多个客户端。它有一个主题选项页面,这样我每次都可以轻松自定义网站,但我也会在我制作的每个网站上使用一些插件(如SEO插件、安全性等)。有没有办法让它们成为“必需的”,这样我就可以得到这些插件的列表,这样当我在新网站上安装主题时就不必在插件目录中找到它们了?