深入研究,我认为问题在于 WooCommerce 正在生成有关可下载文件 URL 的 MD5,该 URL 不会从一台服务器传输到另一台服务器。
查看 WordPress wp_postmeta 表中的数据,我明白了
mysql> SELECT post_id,meta_value FROM wordpress.wp_postmeta where meta_key='_downloadable_files';
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| post_id | meta_value |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 33 | a:1:{s:32:"fccc91f867cc071737bea5433d1c3181";a:2:{s:4:"name";s:3:"fox";s:4:"file";s:61:"http://_123456789_.com/wp-content/uploads/2015/03/fox.png";}}
我的猜测是,当数据库转移到新主机时,fccc91f867cc071737bea5433d1c3181 值不知何故未被识别为有效。
为了解决这个问题,我编写了一个 PHP 脚本来读取数据库,然后通过 Gerhard Potgieter 的 WooCommerce REST API PHP client 使用 WooCommerce REST API 重新加载可下载文件。
<?php
error_reporting( E_ALL );
ini_set( 'display_errors', 'On' );
require_once "class-wc-api-client.php";
$consumer_key = 'ck_examplexxx'; // Add your own Consumer Key here
$consumer_secret = 'cs_secretxxx'; // Add your own Consumer Secret here
$store_url = 'http://123456789/'; // Add the home URL to the store you want to connect to here
// Initialize the class
$wc_api = new WC_API_Client( $consumer_key, $consumer_secret, $store_url );
$servername = "_dbhost_";
$username = "wordpress";
$password = "__password__";
$dbname = "wordpress";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT post_id,meta_value FROM wordpress.wp_postmeta where meta_key='_downloadable_files'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$product_id = $row["post_id"];
$meta_value = $row["meta_value"];
preg_match("/.+\:\"(http:\/\/.+\/wp-content\/uploads\/.+\/.+\/(.+)\..+)\".+/", $meta_value, $m);
print_r( $wc_api->update_product( $product_id, '{
"product": {
"downloads": [
{
"name": "' . $m[2] . '",
"file": "' . $m[1] . '"
}
]
}
}'));
}
} else {
echo "0 results";
}
$conn->close();