WordPress:WordPress Coding Standards

来自站长百科
Xxf3325讨论 | 贡献2008年5月12日 (一) 18:14的版本
跳转至: 导航、​ 搜索
可打印版不再被支持且可能有渲染错误。请更新您的浏览器书签并改用浏览器默认的打印功能。

一些WordPress代码结构遗留下来的部分对于PHP的开发在类型上是不一致的。 WordPress致力于通过帮助用户维护代码一致性,维持整洁易读的程度来逐渐改变这点。

编写WordPress 代码的时候,要记得以下几点,不论是核心程序代码, 插件,或者是 WordPress 主题。指导方针与 Pear 标准相似,但是在某些关键地方也有不同。

也可以参见wp-hackers 列表中的这个文章


还有一篇关于被提议的内嵌文档标准的页面.

单引号和双引号
适当的时候使用单引号和双引号。如果你不能评测字符串中的东西的时候,使用单引号。字符串无法摆脱HTML 引号,因为你只可以改变你的引用方式,如:
echo "<a href='$link' title='$linktitle'>$linkname</a>";
echo '<a href="/static/link" title="Yeah yeah!">Link name</a>';
唯一的例外是JavaScript,它有时要求双或者单引号。属性中的文本必须通过attribute_escape()处理,这样单或者双引号不需要结束属性的值,也不会导致XHTML 不合法而导致安全问题。
缩进
缩进通常反映出逻辑结构。使用真正的制表符不要使用空格,这通过客户端允许得到最大的适应性。
例外: 如果代码排列整齐了会使你的代码块更易读的话,你可以做一些简单的制表符缩进,然后把不同的地方用空格标记:
[tab]$foo   = 'somevalue';
[tab]$foo2  = 'somevalue2';
[tab]$foo34 = 'somevalue3';
[tab]$foo5  = 'somevalue4';
注意制表符如何用来做简单的缩进,和空格只是用来让每行排列整齐。
Brace 格式
Brace应该用于多行代码块:
if ( condition ) {
    action1();
    action2();
} elseif ( condition2 && condition3 ) {
    action3();
    action4();
} else {
   defaultaction();
}
此外如果你有特别长的代码块,考虑是否可以分成两个或者更多的短些的块或者函数。如果你认为这种长代码不可避免,请在末尾做一个简单的注释,这样人们可以在看过之后知道结束括号结束了什么。 -- 尤其是适合逻辑块,超过35行的代码,当然任何代码都是可以做出注释的。
单行程序块可以省略大括号:
if ( condition )
    action1();
elseif ( condition2 )
    action2();
else
    action3();
include_once vs. require_once
了解 include_oncerequire_once之间的不同,适当的使用它们。引用[1]: "这两种结构除了在如何处理故障方面,其他的都是一样的。当require() 导致一个致命错误的时候,include() 生成一个警告。" 致命错误停止代码执行。
Regular expressions
Perl compatible regular expressions (PCRE, preg_ functions) should be used in preference to their POSIX counterparts.
No Shorthand PHP
Never use shorthand PHP start tags (<? ... ?> or <?=$var?>). Always use full PHP tags (<?php ... ?>). Make sure to remove trailing whitespace after closing PHP tag.
常规表达
Perl 兼容常规表达(PCRE, preg_ 函数)应该被优先用在 它们的POSIX 副本.
不要使用简写PHP
不要使用简写PHP 开始标签 (<? ... ?> 或者 <?=$var?>)。要一直使用完全的PHP 标签 (<?php ... ?>). 确认关闭PHP标签后移除空白空格。
Space Usage
Always put spaces after commas and on both sides of logical and assignment operators like "x == 23", "foo && bar", "array( 1, 2, 3 )", , as well as on both sides of the opening and closing parenthesis of if, elseif, foreach, for and switch blocks (e.g. foreach ( $foo as $bar ) { ...). When defining a function, do it like so: function myfunction( $param1 = 'foo', $param2 = 'bar' ) { and when calling a function, do it like so: myfunction( $param1, funcparam( $param2 ) );
空格用法
在逗号后面要加一个空格,在逻辑和分配算子两边也要加上空格如 "x == 23", "foo && bar", "array( 1, 2, 3 )", , 同样在打开和关闭if, elseif, foreach, forswitch 语句的括号两边也加上空格 (如foreach ( $foo as $bar ) { ...). 定义一个函数的时候,这样做: function myfunction( $param1 = 'foo', $param2 = 'bar' ) { 在调用函数的时候,这样做: myfunction( $param1, funcparam( $param2 ) );
Formatting SQL statements
When formatting SQL statements you may break it into several lines and indent if it is sufficiently complex to warrant it. Most statements work well as one line though. Always capitalize the SQL parts of the statement like UPDATE or WHERE.
格式化 SQL 声明
如果十分复杂而不能辨别的话,当格式化SQL声明的时候你可以把它分成几行,然后缩进。多数声明在一行内运行良好。声明的SQL部分如UPDATE 或者 WHERE一定要大写。
Functions that update the database should expect their parameters to lack SQL slash escaping when passed. Escaping should be done as close to the time of the query as possible, preferably by using $wpdb->prepare()
升级数据库的函数尽量减少参数应该盼望参数在传递过程中减少丢失。溢出应该在尽可能接近请求的时候完成。最适合的时候是正在使用 $wpdb->prepare()时。
$wpdb->prepare() is a method that handles escaping, quoting, and int-casting for SQL queries. It uses a subset of the sprintf() style of formatting. Example :
$var = "dangerous'"; // raw data that may or may not need to be escaped
$id  = some_foo_number(); // data we expect to be an integer, but we're not certain

$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) );
$wpdb->prepare()是一个方法,为SQL请求控制溢出,引用和int-casting。它使用一个格式化的sprintf()格式的的子集。如 :
$var = "dangerous'"; // raw data that may or may not need to be escaped
$id  = some_foo_number(); // data we expect to be an integer, but we're not certain

$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) );
%s is used for string placeholders and %d is used for integer placeholders. Note that they are not 'quoted'! $wpdb->prepare() will take care of escaping and quoting for us. The benefit of this is that we don't have to remember to manually use $wpdb->escape(), and also that it is easy to see at a glance whether something has been escaped or not, because it happens right when the query happens.
%s 是用于字符串占位符的,%d是用于整型占位符。注意它们没有被'引用'! $wpdb->prepare()会照应好溢出和引用的。这样的好处就是我们不需要记住手动使用$wpdb->escape(),这样一眼就能看出是否有什么溢出,因为它在请求发生的那一刻就起作用了。
Database Queries
Avoid touching the database directly. If there is a defined function that can get the data you need, use it. Database abstraction (using functions instead of queries) helps keep your code forward-compatible and, in cases where results are cached in memory, it can be many times faster. If you must touch the database, get in touch with some developers by posting a message to the wp-hackers mailing list. They may want to consider creating a function for the next WordPress version to cover the functionality you wanted.
数据库请求
避免直接改动数据库。如果有一个定义过的函数可以得到你想要的数据,就用它,数据库提取(使用函数代替请求)对保证你的代码的向前兼容性有帮助,这样结果在内存中找到后,它可以用数倍更快的速度。如果你必须改动数据库的话,通过发布信息到wp-hackers 邮件列表联系几个开发人员。他们可能会考虑为下一个WordPress 版本开发一个新的函数来达到你想要的功能。
Variables, functions, and operators
If you don't use a variable more than once, don't create it. This includes queries. Always use the wpdb Class of functions when interacting with the database.
变量, 函数和算子
如果你不使用变量,就不要创建。这包括请求在内。于数据库交换数据时使用函数wpdb类
Ternary operators are fine, but always have them test if the statement is true, not false. Otherwise it just gets confusing.
// GOOD example:
// (if statement is true) ? (do this) : (if false, do this);
$musictype = ('jazz' == $music) ? 'cool' : 'blah';

三重算子是可以的,声明为真时要记得测试,否则它会混淆。
// GOOD example:
// (if statement is true) ? (do this) : (if false, do this);
$musictype = ('jazz' == $music) ? 'cool' : 'blah';

Another important point in the above example, when doing logical comparisons always put the variable on the right side, like above. If you forget an equal sign it'll throw a parse error instead of just evaluating true and executing the statement. It really takes no extra time to do, so if this saves one bug it's worth it.
另外一个重点是在上边的例子中,当做逻辑比较的时候,要把变量放到右边,象上边那样。如果你忘记了等号,它会出现一个解析错误而不是判断为真或者停止声明。它并不占用过多的时间,所以这如果避免了一个错误,是值得的。
single line blocks can omit braces for brevity:
if ( condition )
    action1();
elseif ( condition2 )
    action2();
else
    action3();