WordPressがテーマを認識する仕組みについて急に不思議に思ったので
「自習 テーマに関するコアファイルを調べてみた。」に書いたようにテーマに関すると思われるコアファイルを調べてはみたものの、わかったようなわからないような。。
引き続き調べてみよう、と思っていました。
そしたら。。。
補習その1 file_existsとis_readableで存在チェック
ヒントをいただきました!
@Webourgeon_com style.cssの読み込みは、WP_Themeクラスのコンストラクタでやってます。file_existsとis_readableで存在チェックしてますね。index.phpがあるかどうかもこのへんで。
— Takuro Hishikawa (@HissyNC) September 24, 2012
class-wp-theme.phpの
if ( is_array( $cache ) ) {
foreach ( array( 'errors', 'headers', 'template' ) as $key ) {
if ( isset( $cache[ $key ] ) )
$this->$key = $cache[ $key ];
}
if ( $this->errors )
return;
if ( isset( $cache['theme_root_template'] ) )
$theme_root_template = $cache['theme_root_template'];
} elseif ( ! file_exists( $this->theme_root . '/' . $theme_file ) ) {
$this->headers['Name'] = $this->stylesheet;
if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet ) )
$this->errors = new WP_Error( 'theme_not_found', __( 'The theme directory does not exist.' ) );
else
$this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
$this->template = $this->stylesheet;
$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
if ( ! file_exists( $this->theme_root ) ) // Don't cache this one.
$this->errors->add( 'theme_root_missing', __( 'ERROR: The themes directory is either empty or doesn’t exist. Please check your installation.' ) );
return;
} elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) {
$this->headers['Name'] = $this->stylesheet;
$this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) );
$this->template = $this->stylesheet;
$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
return;
} else {
$this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' );
// Default themes always trump their pretenders.
// Properly identify default themes that are inside a directory within wp-content/themes.
if ( $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes ) ) {
if ( basename( $this->stylesheet ) != $default_theme_slug )
$this->headers['Name'] .= '/' . $this->stylesheet;
}
}
とかあるあたりっぽい。
file_existsとis_readableのとこで存在チェックして、状況に応じてテーマ・ディレクトリが、存在しないとか、style.cssがないとかそういうエラーをだすようになってる。
ふむふむ。ちょっとずつだけどわかってきたよ。
ちなみに
file_exists→ファイルまたはディレクトリが存在するかどうか調べる
is_readable→ファイルが存在し、読み込み可能であるかどうかを調べる
です。
そしてその夜。。
補習その2 get_file_data
@Webourgeon_com ありぶるじょん。get_file_dataを検索してみる吉
— まがじ (@jim0912) September 24, 2012
検索しました、ありましたっ!
get_file_data:WordPress私的マニュアル
「get_file_data- WordPress規定のヘッダー情報を取得する」とあります。
これです!これ!
これでstyle.cssのヘッダー情報持ってきてるんですね
file_existsとis_readableでstyle.cssがあるかをチェック。
なければそれに応じたエラーを出して、きちんとあれば
WP_Theme Class(class-wp-theme.php)の中で定義されたファイルのヘッダー情報をget_file_dataで取得。
ってな流れでいいのかな。間違ってたら教えてください。。
つながってなかった知識
そしてさらに検索するとこの記事。
プラグインでバージョン情報を使いたいときは、get_file_data を用いるととってもエコなのよ
これ、読んでたんです。でも全くつながってなかった。。><
仕事でやってたり、ブログ読んだり、
断片的に色々入って来てるはずなんだけど、
自分の中でうまく消化されてないというか、
つながってないんだなぁーと、しみじみ思いました。
今回は本当に興味だけで調べ始めて、
いろんなことが少しずつつながっていくのが、
急に目の前がひらけたみたいになって面白かったです。
とはいっても、まだまだ「ココにかいてある〜♪」レベルなので
もっとソースコードしっかり読める様になりたいです。
おまけ
ちなみにget_file_dataは/wp-includes/functions.php にあるというのでちょっと見てみた。
function get_file_data( $file, $default_headers, $context = '' ) {
// We don't need to write to the file, so just open for reading.
$fp = fopen( $file, 'r' );
// Pull only the first 8kiB of the file in.
$file_data = fread( $fp, 8192 );
// PHP will close file handle, but we are good citizens.
fclose( $fp );
// Make sure we catch CR-only line endings.
$file_data = str_replace( "\r", "\n", $file_data );
if ( $context && $extra_headers = apply_filters( "extra_{$context}_headers", array() ) ) {
$extra_headers = array_combine( $extra_headers, $extra_headers ); // keys equal values
$all_headers = array_merge( $extra_headers, (array) $default_headers );
} else {
$all_headers = $default_headers;
}
foreach ( $all_headers as $field => $regex ) {
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] )
$all_headers[ $field ] = _cleanup_header_comment( $match[1] );
else
$all_headers[ $field ] = '';
}
return $all_headers;
}
うむむ。。またしてもクラクラするのでこれは次回以降への宿題。。です。
class-wp-theme.phpも、もうちょっと見てみたいな。