gpt4 book ai didi

php - PHP 如何用于解析主题和插件的注释/标题?

转载 作者:可可西里 更新时间:2023-11-01 13:47:42 25 4
gpt4 key购买 nike

Wordpress 插件和主题在顶部有这样的评论:

/**
* @package Akismet
*/
/*
Plugin Name: Akismet
Plugin URI: http://akismet.com/?return=true
Description: Used by millions, Akismet is quite possibly the best way in the world to <strong>protect your blog from comment and trackback spam</strong>. It keeps your site protected from spam even while you sleep. To get started: 1) Click the "Activate" link to the left of this description, 2) <a href="http://akismet.com/get/?return=true">Sign up for an Akismet API key</a>, and 3) Go to your <a href="admin.php?page=akismet-key-config">Akismet configuration</a> page, and save your API key.
Version: 2.5.6
Author: Automattic
Author URI: http://automattic.com/wordpress-plugins/
License: GPLv2 or later
*/

当您访问管理界面中的插件页面时,插件列表如下:

screenshot-with-shadow.png http://img854.imageshack.us/img854/4526/screenshotwithshadow.png

这是文档的标准语法吗?我假设它对插件文件执行 file_get_contents 以读取它们,但 Wordpress 如何将其解析为可操作的标准化信息以在 PHP 中使用?

最佳答案

参见 get_plugin_data()在 WordPress 解析器的 wp-admin/includes/plugin.php 中。更具体地说,提取发生在 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 ) && $ma
$all_headers[ $field ] = _cleanup_header_comment( $match[1] );
else
$all_headers[ $field ] = '';
}

WP 之外也有类似的实现。参见 How can plugin systems be designed so they don't waste so many resources?举个例子。

关于php - PHP 如何用于解析主题和插件的注释/标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13391556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com