我有一个可以在最新版本的 WordPress(撰写本文时为 6.2)上运行的网站。最近我的网站开始发出警告:
<br />
<b>Deprecated</b>: The PSR-0 `Requests_...` class names in the Request library are deprecated. Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience. in <b>/home/username/public_html/wp-includes/Requests/src/Autoload.php</b> on line <b>171</b><br />
问题是:我的 ajax 请求返回此警告以及破坏某些页面的实际输出。现在,我已经打开了建议的
Autoload.php
文件,只是注释掉了该函数中的 trigger_error
函数调用:
public static function load($class_name) {
// Check that the class starts with "Requests" (PSR-0) or "WpOrg\Requests" (PSR-4).
$psr_4_prefix_pos = strpos($class_name, 'WpOrg\\Requests\\');
if (stripos($class_name, 'Requests') !== 0 && $psr_4_prefix_pos !== 0) {
return false;
}
$class_lower = strtolower($class_name);
if ($class_lower === 'requests') {
// Reference to the original PSR-0 Requests class.
$file = dirname(__DIR__) . '/library/Requests.php';
} elseif ($psr_4_prefix_pos === 0) {
// PSR-4 classname.
$file = __DIR__ . '/' . strtr(substr($class_name, 15), '\\', '/') . '.php';
}
if (isset($file) && file_exists($file)) {
include $file;
return true;
}
/*
* Okay, so the class starts with "Requests", but we couldn't find the file.
* If this is one of the deprecated/renamed PSR-0 classes being requested,
* let's alias it to the new name and throw a deprecation notice.
*/
if (isset(self::$deprecated_classes[$class_lower])) {
/*
* Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations
* by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`.
* The constant needs to be defined before the first deprecated class is requested
* via this autoloader.
*/
if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') || REQUESTS_SILENCE_PSR0_DEPRECATIONS !== true) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
trigger_error(
'The PSR-0 `Requests_...` class names in the Request library are deprecated.'
. ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.',
E_USER_DEPRECATED
);
// Prevent the deprecation notice from being thrown twice.
if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) {
define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true);
}
}
// Create an alias and let the autoloader recursively kick in to load the PSR-4 class.
return class_alias(self::$deprecated_classes[$class_lower], $class_name, true);
}
return false;
}
但是我当然知道这个解决方案只是暂时的,也许有人知道如何解决问题而不只是隐藏它?
我还打印出了
$class_name
变量,结果是 Requests_IDNAEncoder
。我没有在代码中使用这个类名,所以我假设它被其他东西使用,这就是为什么我不知道我需要更改什么。
我通过将之前的类名
Requests
更改为新添加的命名空间 WpOrg\Requests\Requests
解决了这个问题
具有新命名空间的示例
//get countries
$response = \WpOrg\Requests\Requests::get(self::$enpoint . 'countries', [
'Authorization' => 'Bearer ' . self::$skkey,
]);
拝启:小さな个人的なウェブサイトは、あなたが楽しむことを愿って 私达のためのあなたの热狂的なサポートをありがとう f88tw|wakoru|非常感谢您的帮助。 https://mypaper.pchome.com.tw/f88tw
听起来您从 WooCommerce 使用的请求库收到了弃用警告。这表明 PSR-0 样式类名(如 Requests_Utility_FilteredIterator)已被弃用,取而代之的是 PSR-4 命名空间版本(如 WpOrg\Requests\Utility\FilteredIterator)。
您可以尝试以下一些方法来解决此问题:
升级到最新版本的 WooCommerce。他们可能已更新请求库以使用新的 PSR-4 类。
手动更新 WordPress 安装中的 Requests 库文件以使用命名空间版本。您需要将所有 Requests_ 类名称替换为 WpOrg\Requests。
手动更新 Requests 库涉及查找包含已弃用的 Requests_ 类名的所有文件,并将其替换为命名空间版本。这仅适用于专业人士,这不会是一个永久的解决方案,随着模块的更新,文件中的更改将会丢失。
所以:我们的想法是找到导致错误的模块。
我做了什么:我禁用了我怀疑可能导致问题的一一模块。就我而言,它是 Woocommerce Stripe Gateway。
后续步骤:我将就该错误与他们联系。
临时修复:我禁用了 Stripe Gateway 模块。