websharks / wp-php-rv
WordPress 主题/插件 PHP vX.x+ 的占位符
Requires
- php: >=5.2
This package is not auto-updated.
Last update: 2024-09-17 03:47:40 UTC
README
WordPress 主题/插件 PHP vX.x+ 的占位符(即您定义的最小版本)
典型 WordPress 主题/插件文件中的示例用法
<?php /* Plugin Name: My Plugin Plugin URI: http://example.com/my-plugin Description: Example plugin. Author: Example Author. Version: 0.1-alpha Author URI: http://example.com Text Domain: my-plugin */ $GLOBALS['wp_php_rv'] = '5.3'; // Require PHP vX.x+ (you configure this). if(require('wp-php-rv/src/includes/check.php')) // `true` if running PHP vX.x+. require dirname(__FILE__).'/my-plugin-code.php'; // It's OK to load your plugin. else wp_php_rv_notice(); // Creates a nice PHP vX.x+ dashboard notice for the site owner.
替代方法
在您的脚本中使用 include()
或 require()
包含 check.php
文件时,该文件将自动返回 true
;即,如果安装站点正在运行 PHP vX.x+(由 $GLOBALS['wp_php_rv']
配置)。否则它返回 false
。因此,运行检查的最简单方法是使用 if(require('wp-php-rv/src/includes/check.php'))
。 但是,您也可以选择这样做。
<?php $GLOBALS['wp_php_rv'] = '5.3'; // Require PHP vX.x+. require 'wp-php-rv/src/includes/check.php'; // Include. if(wp_php_rv()) // `true` if running PHP vX.x+. require dirname(__FILE__).'/my-plugin-code.php'; // It's OK to load your plugin. else wp_php_rv_notice(); // Creates a nice PHP vX.x+ dashboard notice for the site owner.
仪表板通知,通过名称调用您的软件
<?php $GLOBALS['wp_php_rv'] = '5.3'; // Require PHP vX.x+. if(require('wp-php-rv/src/includes/check.php')) // `true` if running PHP vX.x+. require dirname(__FILE__).'/my-plugin-code.php'; // It's OK to load your plugin. else wp_php_rv_notice('My Plugin'); // Dashboard notice mentions your software specifically.
注意:如果您省略了 $brand_name
参数,将使用默认值。默认值为 ucwords('[calling file basedir]')
;例如,如果 /my-plugin/stub.php
调用 wp-php-rv/src/includes/check.php
,则默认的 $software_name
自动变为 My Plugin
。太棒了!
如果有多个主题/插件使用这个怎么办?
这没问题! :-) wp-php-rv/src/includes/check.php
文件使用 function_exists()
作为包装器;这允许它被任何数量的插件多次包含,并从任何数量的位置包含。 需要注意的是,您必须确保每次都定义 $GLOBALS['wp_php_rv']
;即,每次您 include('wp-php-rv/src/includes/check.php')
或 require('wp-php-rv/src/includes/check.php')
时。
这里的重点是,$GLOBALS['wp_php_rv']
定义了一个与您的插件需求特定的 PHP 版本,因此每个插件开发者都应该在它们 include('wp-php-rv/src/includes/check.php')
或 require('wp-php-rv/src/includes/check.php')
之前显式地定义它。
这可以直接放在我现有的主题/插件文件顶部吗?
不,有两件重要的事情需要记住。
- 别忘了将
websharks/wp-php-rv
仓库的一个副本打包到您的主题/插件中。您实际上只需要/src
目录。 - 不要将现有的代码留在同一个文件中。使用这个文件作为占位符文件,先检查 PHP vX.x+(如上面示例所示),然后再加载依赖 PHP vX.x+ 的代码。为什么?如果您在现有的 PHP 文件顶部放置一个 PHP vX.x+ 检查,并且该特定的 PHP 文件恰好包含仅适用于 PHP v5.2(例如)的代码,则它可能仍然会引发语法错误。因此,您应该将代码移到单独的文件中,并创建一个先检查 PHP vX.x+ 存在的占位符文件。
我是否也可以测试所需的 PHP 扩展?
是的,$GLOBALS['wp_php_rv']
可以是一个包含所需版本的字符串,也可以是一个包含所需版本和嵌套数组(包含所需 PHP 扩展)的数组。最简单的方法是通过示例来展示它的工作原理(如下所示)。注意,您所需的 PHP 扩展数组必须与 PHP 的 extension_loaded()
函数兼容。
<?php $GLOBALS['wp_php_rv']['min'] = '5.3'; $GLOBALS['wp_php_rv']['extensions'] = array('curl', 'mbstring'); if(require('wp-php-rv/src/includes/check.php')) // `true` if running PHP vX.x+ w/ all required extensions. require dirname(__FILE__).'/my-plugin-code.php'; // It's OK to load your plugin. else wp_php_rv_notice('My Plugin'); // Dashboard notice mentions your software specifically.
我还可以使用这个系统测试什么?
兼容的操作系统、兼容的 PHP 版本、所需位、所需 PHP 函数、所需 PHP 扩展和兼容的 WP 版本。
<?php $GLOBALS['wp_php_rv']['os'] = 'nix'; // Requires a Unix-like OS. // This is one of two operating system identifiers (always in lowercase): `nix` or `win` // As far as WP PHP RV is concerned, their OS is either `nix` (Unix-like) or `win` (Windows). // If you only want to support Unix-like systems, set this to: `nix`, making Windows incompatible. // If you only want to support Windows systems, set this to: `win`, making others incompatible. $GLOBALS['wp_php_rv']['min'] = '5.3'; // Minimum PHP version. $GLOBALS['wp_php_rv']['max'] = '7.0.4'; // Max compatible PHP version, if applicable. $GLOBALS['wp_php_rv']['bits'] = 64; // e.g., 32 or 64 bit architecture. $GLOBALS['wp_php_rv']['functions'] = array('eval'); // Functions (or constructs). $GLOBALS['wp_php_rv']['extensions'] = array('curl', 'mbstring'); // See previous FAQ. $GLOBALS['wp_php_rv']['wp']['min'] = '4.2'; // Minimum WP version. $GLOBALS['wp_php_rv']['wp']['max'] = '4.5.2'; // Max compatible WP version, if applicable. if(require('wp-php-rv/src/includes/check.php')) // `true` if no issue. require dirname(__FILE__).'/my-plugin-code.php'; // It's OK to load your plugin. else wp_php_rv_notice('My Plugin'); // Dashboard notice mentions your software specifically.
PHP 兼容性
请注意,WP PHP RV软件本身需要PHP v5.2或更高版本,与WordPress核心版本相同。
版权:© 2015 WebSharks, Inc.(在美国编写)
在GNU通用公共许可证的条款下发布。