VOID001
继续学习WP源码 每天都保证2h来学习它

继续学习WP源码 每天都保证2h来学习它 这里先来说一下 注释里面 的那个 @**** 使用 @号是一种标准的注释格式,是对这个程序的功能的一个很清晰的说明和解释 ,在自己写PHP代码的时候也要会用这种注释。

下面继续 version.php中,也没有什么东西,只是一些对版本变量的定义和一些必要的配置 ,语言 以及manifest ver之类的 为了格式一致,这里也放上它的代码

[mw_shl_code=php,true]<?php

/**

* The WordPress version string

*

* @global string $wp_version

*/

$wp_version = '3.4.1';

/**

* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

*

* @global int $wp_db_version

*/

$wp_db_version = 21115;

/**

* Holds the TinyMCE version

*

* @global string $tinymce_version

*/

$tinymce_version = '349-20805';

/**

* Holds the cache manifest version

*

* @global string $manifest_version

*/

$manifest_version = '20111113';

/**

* Holds the required PHP version

*

* @global string $required_php_version

*/

$required_php_version = '5.2.4';

/**

* Holds the required MySQL version

*

* @global string $required_mysql_version

*/

$required_mysql_version = '5.0';

$wp_local_package = 'zh_CN';[/mw_shl_code]

继续回来看 wp-settings.php,代码参见前文 ,下面, 调用了wp_initial_constants()函数,这个函数的作用是将WP的一些常量诸如内存限制 调试模式之类的 设置好。

[mw_shl_code=php,true]/**

* Defines initial WordPress constants

*

* @see wp_debug_mode()

*

* @since 3.0.0

*/

function wp_initial_constants( ) {

global $blog_id;

// set memory limits

if ( !defined('WP_MEMORY_LIMIT') ) {

if( is_multisite() ) {

define('WP_MEMORY_LIMIT', '64M');

} else {

define('WP_MEMORY_LIMIT', '32M');

}

}

if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {

define( 'WP_MAX_MEMORY_LIMIT', '256M' );

}

/**

* The $blog_id global, which you can change in the config allows you to create a simple

* multiple blog installation using just one WordPress and changing $blog_id around.

*

* @global int $blog_id

* @since 2.0.0

*/

if ( ! isset($blog_id) )

$blog_id = 1;

// set memory limits.

if ( function_exists('memory_get_usage') && ( (int) @ini_get('memory_limit') < abs(intval(WP_MEMORY_LIMIT)) ) )

@ini_set('memory_limit', WP_MEMORY_LIMIT);

if ( !defined('WP_CONTENT_DIR') )

define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); // no trailing slash, full paths only - WP_CONTENT_URL is defined further down

// Add define('WP_DEBUG', true); to wp-config.php to enable display of notices during development.

if ( !defined('WP_DEBUG') )

define( 'WP_DEBUG', false );

// Add define('WP_DEBUG_DISPLAY', null); to wp-config.php use the globally configured setting for

// display_errors and not force errors to be displayed. Use false to force display_errors off.

if ( !defined('WP_DEBUG_DISPLAY') )

define( 'WP_DEBUG_DISPLAY', true );

// Add define('WP_DEBUG_LOG', true); to enable error logging to wp-content/debug.log.

if ( !defined('WP_DEBUG_LOG') )

define('WP_DEBUG_LOG', false);

if ( !defined('WP_CACHE') )

define('WP_CACHE', false);

/**

* Private

*/

if ( !defined('MEDIA_TRASH') )

define('MEDIA_TRASH', false);

if ( !defined('SHORTINIT') )

define('SHORTINIT', false);

}[/mw_shl_code]

这个函数主要就是对还没有定义的常量进行定义,并且根据不同的情况赋予不同的值,下首先 ,会根据是否支持多站点而来决定内存限制的大小, WordPress可以支持多个人创建博客 ,通过很方便的 blog id这个变量来标明不同的blog 设置内存大小的时候,还考虑了如果MemroyLimit大于了PHP配置文件里分配的大小,那么就修改配置文件里的memory limit 然后定义wp-content路径的名称常量 等等一系列的定义 ,看代码就能明白,不再过多赘述

回到 wp-settings.php文件 然后又调用了wp_check_php_mysql_versions()函数 ,这个函数也包含在load.php里了,我们来看一下

[mw_shl_code=php,true]/**

* Check for the required PHP version, and the MySQL extension or a database drop-in.

*

* Dies if requirements are not met.

*

* @Access private

* @since 3.0.0

*/

function wp_check_php_mysql_versions() {

global $required_php_version, $wp_version;

$php_version = phpversion();

if ( version_compare( $required_php_version, $php_version, '>' ) ) {

wp_load_translations_early();

wp_die( sprintf( __( 'Your server is running PHP version %1$s but WordPress %2$s requires at least %3$s.' ), $php_version, $wp_version, $required_php_version ) );

}

if ( ! extension_loaded( 'mysql' ) && ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) {

wp_load_translations_early();

wp_die( __( 'Your PHP installation appears to be missing the MySQL extension which is required by WordPress.' ) );

}

}[/mw_shl_code]

phpversion函数就是一个获取系统的PHP版本并返回的函数,这里不做过多介绍 ,version_compare函数倒有些意思 , 标准的说明是这样的

version_compare() compares two "PHP-standardized" version number strings

mixed version_compare ( string $version1 , string $version2 [, string $operator ] )

其实,这个功能实现很简单,只不过这个语法很有趣 ,PHP还有这样的函数 ,可以直接比较两个版本 ,这是我学C语言的时候没遇到过的 ,而且,相比较什么关系只要加上一个比较运符就可以了十分方便呢

version_compare() compares two "PHP-standardized" version number strings.