通过分析WP的代码来学习PHP。1

时间:2022-06-06 05:14:32

下载了WP的代码,并且应用到了网站上面,现在也在正常的运行中,地址是:www.freealgorithm.tk 。具体的申请过程就不赘述了,学习WP的代码。

他的目录结构就不看了,可以下载同名文件我会通过相对目录来区分。

进入网站的第一个默认的页面:/index.php

<?php

/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/ /**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool */ define('WP_USE_THEMES', true); /** Loads the WordPress Environment and Template */
#定义WP_USE_THEMES为true
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
#返回路径中目录部分并且连接字符串得到此php文件的位置,用这个文件代替这个位置
#引入同目录下的wp-blog-header.php文件

知识点:define函数,bool define    ( string $name   , mixed $value   [, bool $case_insensitive = false  ] ) 成功返回true否则false。
name:常量的名字。value:常量的值;仅允许标量和 null。标量的类型是 integer,float,string 或者 boolean。 也能够定义常量值的类型为 resource ,但并不推荐这么做,可能会导致未知状况的发生。

require函数替换参数指向的文件,__FILE__取得当前文件的绝对路径,dirname返回路径中的目录部分。比如这个文件返回的就是根目录/。'.'这个符号是字符串连接运算符,将dirname(__FILE__)返回的字符串与'/wp-blog-header.php'相连接然后require进去。

而/wp-blog-header.php这个文件被包含了,如下:

 <?php

 /**
* Loads the WordPress environment and template.
载入WordPress的环境和模板*
* @package WordPress
*/ if ( !isset($wp_did_header) ) #变量是否定义,定义返回true
{ $wp_did_header = true; require_once( dirname(__FILE__) . '/wp-load.php' ); #用wp-load.php代替此语句
/*如果定义了就不继续执行,否则执行下面的语句 */ wp(); require_once( ABSPATH . WPINC . '/template-loader.php' ); #同样用
}

isset函数用来判断这个变量是否已经被定义了,如果定义了就返回true否则返回false。如果没有定义的话就执行下面的语句,$wp_did_header=true;是定义变量,他是一个布尔变量。然后require_once这个文件 ,require_once函数与require的区别是以后再这个文件里不能再包含这个文件了,即仅仅包含这个文件一次。这些的动作是定义一个布尔变量且包含一个文件。下面的wp();函数指行,现在不知道这个函数的作用,一会就会看到。然后继续包含ABSPATH.WPINC.'/template-loader.php'。这个文件了。ABSPATH是绝对路径的意思,这个是WP自定义的常量,还有WPINC这两个,现在我们也不知道它的值是什么。此文件到此没有结束,我们继续跟踪进入wp-load.php.

 <?php

 /**

  * Bootstrap file for setting the ABSPATH constant
* and loading the wp-config.php file. The wp-config.php
* file will then load the wp-settings.php file, which
* will then set up the WordPress environment.
*
* If the wp-config.php file is not found then an error
* will be displayed asking the visitor to set up the
* wp-config.php file.
*
* Will also search for wp-config.php in WordPress' parent
* directory to allow the WordPress directory to remain
* untouched.
*
* @internal This file must be parsable by PHP4.
*
* @package WordPress
*/ /** Define ABSPATH as this file's directory
*/ define( 'ABSPATH', dirname(__FILE__) . '/' ); error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR ); /*error_reporting() 函数能够在运行时设置 error_reporting 指令。 PHP 有诸多错误级别,使用该函数可以设置在脚本运行时的级别。 如果没有设置可选参数 level, error_reporting() 仅会返回当前的错误报告级别。 */ if ( file_exists( ABSPATH . 'wp-config.php') ) #file_exists — 检查文件或目录是否存在,绝对路径+'wp-config.php'检测是否存在,这个文件是设置文件,后来在网站文件上传完成后进行安装时候来创建,所以在没有安装时候是不存在这个文件的。
{ /** The config file resides in ABSPATH */ require_once( ABSPATH . 'wp-config.php' ); #如果文件存在,require_once把文件拿来
#如果文件不存在,判断绝对路径取 } elseif ( file_exists( dirname(ABSPATH) . '/wp-config.php' ) && ! file_exists( dirname(ABSPATH) . '/wp-settings.php' ) )
{ /** The config file resides one level above ABSPATH but is not part of another install */ require_once( dirname(ABSPATH) . '/wp-config.php' ); } else { // A config file doesn't exist define( 'WPINC', 'wp-includes' ); define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); require_once( ABSPATH . WPINC . '/load.php' ); require_once( ABSPATH . WPINC . '/version.php' ); wp_check_php_mysql_versions(); wp_load_translations_early(); // Standardize $_SERVER variables across setups. wp_fix_server_vars(); require_once( ABSPATH . WPINC . '/functions.php' ); $path = wp_guess_url() . '/wp-admin/setup-config.php'; // Die with an error message $die = __( "There doesn't seem to be a <code>wp-config.php</code> file. I need this before we can get started." ) . '</p>'; $die .= '<p>' . __( "Need more help? <a href='http://codex.wordpress.org/Editing_wp-config.php'>We got it</a>." ) . '</p>'; $die .= '<p>' . __( "You can create a <code>wp-config.php</code> file through a web interface, but this doesn't work for all server setups. The safest way is to manually create the file." ) . '</p>';
$die .= '<p><a href="' . $path . '" class="button button-large">' . __( "Create a Configuration File" ) . '</a>'; wp_die( $die, __( 'WordPress &rsaquo; Error' ) );
}

代码稍长,但是以后的代码会更长。这里看到了他的注释说定义ABSPATH为当前文件的目录,而且看到了这个define语句,知道了ABSPATH是dirname(__FILE__).'/'。其实还是这个根目录。error_reporting函数的说明在代码里面注释着意思是定义错误级别,我在书中也没有看到类似的说明这个函数,现在不予理会。
if当前绝对路径下的  /wp-config.php文件存在的话就require这个文件。不存在的话继续判断(/wp-config.php和/wp-settings.php)文件是否不存在,那么判断wp-config.php这个文件的判断是否多余?wp-setting.php这个文件是存在的,而wp-config.php是后来在安装时后生成的,第一次安装使用时候是没有的。否则就定义WPINC为'wp-includes',define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );require_once( ABSPATH . WPINC . '/load.php' );这句是将 /wp-includes/load.php包含进去,require_once( ABSPATH . WPINC . '/version.php' );这句将/wp-includes/version.php包含进去。先跟踪进去/wp-includes/load.php。

 <?php

 /**
* These functions are needed to load WordPress.
*
* @internal This file must be parsable by PHP4.
*
* @package WordPress
*/ /**
* Turn register globals off.
*
* @access private
* @since 2.1.0
* @return null Will return null if register_globals PHP directive was disabled
*/ function wp_unregister_GLOBALS()
{ if ( !ini_get( 'register_globals' ) ) return; if ( isset( $_REQUEST['GLOBALS'] ) ) die( 'GLOBALS overwrite attempt detected' ); // Variables that shouldn't be unset $no_unset = array( 'GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix' ); $input = array_merge( $_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset( $_SESSION ) && is_array( $_SESSION ) ? $_SESSION : array() );
foreach ( $input as $k => $v ) if ( !in_array( $k, $no_unset ) && isset( $GLOBALS[$k] ) )
{ unset( $GLOBALS[$k] ); } } /**
* Fix $_SERVER variables for various setups.
*
* @access private
* @since 3.0.0
*/ function wp_fix_server_vars()
{ global $PHP_SELF; $default_server_values = array( 'SERVER_SOFTWARE' => '', 'REQUEST_URI' => '', ); $_SERVER = array_merge( $default_server_values, $_SERVER ); // Fix for IIS when running with PHP ISAPI if ( empty( $_SERVER['REQUEST_URI'] ) || ( php_sapi_name() != 'cgi-fcgi' && preg_match( '/^Microsoft-IIS\//', $_SERVER['SERVER_SOFTWARE'] ) ) )
{ // IIS Mod-Rewrite if ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) )
{ $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL']; }
// IIS Isapi_Rewrite else if ( isset( $_SERVER['HTTP_X_REWRITE_URL'] ) ) { $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL']; } else {
// Use ORIG_PATH_INFO if there is no PATH_INFO if ( !isset( $_SERVER['PATH_INFO'] ) && isset( $_SERVER['ORIG_PATH_INFO'] ) ) $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO']; // Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice) if ( isset( $_SERVER['PATH_INFO'] ) ) { if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] ) $_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO']; else $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
} // Append the query string if it exists and isn't null if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; } } } // Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests if ( isset( $_SERVER['SCRIPT_FILENAME'] ) && ( strpos( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) == strlen( $_SERVER['SCRIPT_FILENAME'] ) - 7 ) ) $_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED']; // Fix for Dreamhost and other PHP as CGI hosts if ( strpos( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) !== false ) unset( $_SERVER['PATH_INFO'] ); // Fix empty PHP_SELF $PHP_SELF = $_SERVER['PHP_SELF']; if ( empty( $PHP_SELF ) ) $_SERVER['PHP_SELF'] = $PHP_SELF = preg_replace( '/(\?.*)?$/', '', $_SERVER["REQUEST_URI"] ); } /**
* 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(); header( 'Content-Type: text/html; charset=utf-8' ); 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' ) && ! extension_loaded( 'mysqli' ) && ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) { wp_load_translations_early(); header( 'Content-Type: text/html; charset=utf-8' ); die( __( 'Your PHP installation appears to be missing the MySQL extension which is required by WordPress.' ) ); } } /**
* Don't load all of WordPress when handling a favicon.ico request.
* Instead, send the headers for a zero-length favicon and bail.
*
* @since 3.0.0
*/ function wp_favicon_request() { if ( '/favicon.ico' == $_SERVER['REQUEST_URI'] ) { header('Content-Type: image/vnd.microsoft.icon'); header('Content-Length: 0'); exit; } } /**
* Dies with a maintenance message when conditions are met.
*
* Checks for a file in the WordPress root directory named ".maintenance".
* This file will contain the variable $upgrading, set to the time the file
* was created. If the file was created less than 10 minutes ago, WordPress
* enters maintenance mode and displays a message.
*
* The default message can be replaced by using a drop-in (maintenance.php in
* the wp-content directory).
*
* @access private
* @since 3.0.0
*/ function wp_maintenance() { if ( !file_exists( ABSPATH . '.maintenance' ) || defined( 'WP_INSTALLING' ) ) return; global $upgrading; include( ABSPATH . '.maintenance' ); // If the $upgrading timestamp is older than 10 minutes, don't die. if ( ( time() - $upgrading ) >= 600 ) return; if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) { require_once( WP_CONTENT_DIR . '/maintenance.php' ); die(); } wp_load_translations_early(); $protocol = $_SERVER["SERVER_PROTOCOL"]; if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol ) $protocol = 'HTTP/1.0'; header( "$protocol 503 Service Unavailable", true, 503 ); header( 'Content-Type: text/html; charset=utf-8' ); header( 'Retry-After: 600' ); ?>

这是load.php的php部分,下面就基本是html代码了,html先不予分析。ini_get返回配制选项的值