函数文档

wp_start_scraping_edited_file_errors()

💡 云策文档标注

概述

wp_start_scraping_edited_file_errors() 是一个 WordPress 核心函数,用于启动编辑文件错误的抓取过程。它通过验证请求参数和临时数据来确保安全,并在验证失败时输出错误信息。

关键要点

  • 函数检查 $_REQUEST['wp_scrape_key'] 和 $_REQUEST['wp_scrape_nonce'] 是否存在,缺失则直接返回。
  • 使用 sanitize_key() 和 wp_unslash() 处理 key 和 nonce,验证其非空性。
  • 通过 get_transient() 获取临时数据,验证 nonce 是否匹配,不匹配则输出 JSON 错误信息并终止执行。
  • 验证成功后定义 WP_SANDBOX_SCRAPING 常量,并注册 shutdown 函数 wp_finalize_scraping_edited_file_errors()。

代码示例

if ( ! isset( $_REQUEST['wp_scrape_key'] ) || ! isset( $_REQUEST['wp_scrape_nonce'] ) ) {
    return;
}

$key   = substr( sanitize_key( wp_unslash( $_REQUEST['wp_scrape_key'] ) ), 0, 32 );
$nonce = wp_unslash( $_REQUEST['wp_scrape_nonce'] );
if ( empty( $key ) || empty( $nonce ) ) {
    return;
}

$transient = get_transient( 'scrape_key_' . $key );
if ( false === $transient ) {
    return;
}

if ( $transient !== $nonce ) {
    if ( ! headers_sent() ) {
        header( 'X-Robots-Tag: noindex' );
        nocache_headers();
    }
    echo "###### wp_scraping_result_start:$key ######";
    echo wp_json_encode(
        array(
            'code'    => 'scrape_nonce_failure',
            'message' => __( 'Scrape key check failed. Please try again.' ),
        )
    );
    echo "###### wp_scraping_result_end:$key ######";
    die();
}

if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
    define( 'WP_SANDBOX_SCRAPING', true );
}

register_shutdown_function( 'wp_finalize_scraping_edited_file_errors', $key );

注意事项

  • 此函数主要用于内部错误处理机制,开发者通常无需直接调用。
  • 涉及安全验证,确保 key 和 nonce 正确传递以避免错误中断。
  • 输出错误信息时设置 noindex 和 nocache 头,防止缓存敏感数据。

📄 原文内容

Starts scraping edited file errors.

Source

function wp_start_scraping_edited_file_errors() {
	if ( ! isset( $_REQUEST['wp_scrape_key'] ) || ! isset( $_REQUEST['wp_scrape_nonce'] ) ) {
		return;
	}

	$key   = substr( sanitize_key( wp_unslash( $_REQUEST['wp_scrape_key'] ) ), 0, 32 );
	$nonce = wp_unslash( $_REQUEST['wp_scrape_nonce'] );
	if ( empty( $key ) || empty( $nonce ) ) {
		return;
	}

	$transient = get_transient( 'scrape_key_' . $key );
	if ( false === $transient ) {
		return;
	}

	if ( $transient !== $nonce ) {
		if ( ! headers_sent() ) {
			header( 'X-Robots-Tag: noindex' );
			nocache_headers();
		}
		echo "###### wp_scraping_result_start:$key ######";
		echo wp_json_encode(
			array(
				'code'    => 'scrape_nonce_failure',
				'message' => __( 'Scrape key check failed. Please try again.' ),
			)
		);
		echo "###### wp_scraping_result_end:$key ######";
		die();
	}

	if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
		define( 'WP_SANDBOX_SCRAPING', true );
	}

	register_shutdown_function( 'wp_finalize_scraping_edited_file_errors', $key );
}

Changelog

Version Description
4.9.0 Introduced.