// 从文本域解析图片URL,返回指定数量的不同随机图片URL
private function get_random_img_urls( $count = 1 ) {
$raw = get_option( self::OPTION_IMG_URLS, '' );
if ( empty( $raw ) ) return array();
$lines = explode( "\n", $raw );
$urls = array();
foreach ( $lines as $line ) {
$url = trim( $line );
if ( ! empty( $url ) ) {
$urls[] = $url;
}
}
if ( empty( $urls ) ) return array();
// 打乱数组顺序,保证随机性
shuffle( $urls );
// 如果需要的数量大于可用数量,循环补充
$result = array();
$urlCount = count( $urls );
for ( $i = 0; $i < $count; $i++ ) {
$result[] = $urls[ $i % $urlCount ];
}
return $result;
}
// 单张随机图兼容方法(保留原有调用)
private function get_random_img_url() {
$urls = $this->get_random_img_urls( 1 );
return $urls ? $urls[0] : '';
}
private function process_post( $post_id, $source ) {
$post = get_post( $post_id );
if ( ! $post || 'publish' !== $post->post_status ) return;
$content = (string)$post->post_content;
$images = $this->extract_image_urls( $content );
// 文章无图片 → 中间插入图片(原有逻辑不变)
if ( empty( $images ) ) {
$new_img_url = $this->get_random_img_url();
if ( empty( $new_img_url ) ) {
$this->insert_log( array(
'post_id' => $post_id,
'post_title' => get_the_title( $post_id ),
'status' => 'failed',
'reason' => '未配置可用图片地址,无法新增图片',
'image_count' => 0,
'broken_count' => 0,
'replaced_count' => 0,
'old_urls' => array(),
'new_urls' => array(),
'run_source' => $source
) );
return;
}
$img_tag = '
 . ')
';
$content_len = mb_strlen( $content );
$mid_pos = (int)( $content_len / 2 );
$new_content = mb_substr( $content, 0, $mid_pos ) . $img_tag . mb_substr( $content, $mid_pos );
$updated = wp_update_post( array(
'ID' => $post_id,
'post_content' => $new_content
), true );
if ( is_wp_error( $updated ) ) {
$this->insert_log( array(
'post_id' => $post_id,
'post_title' => get_the_title( $post_id ),
'status' => 'failed',
'reason' => '新增图片失败:' . $updated->get_error_message(),
'image_count' => 0,
'broken_count' => 0,
'replaced_count' => 1,
'old_urls' => array(),
'new_urls' => array( $new_img_url ),
'run_source' => $source
) );
return;
}
clean_post_cache( $post_id );
$this->insert_log( array(
'post_id' => $post_id,
'post_title' => get_the_title( $post_id ),
'status' => 'add_image',
'reason' => '文章无图片,已在内容中间插入图片',
'image_count' => 1,
'broken_count' => 0,
'replaced_count' => 1,
'old_urls' => array(),
'new_urls' => array( $new_img_url ),
'run_source' => $source
) );
return;
}
// 检测失效图片(原有逻辑不变)
$broken_urls = array();
$reasons = array();
foreach ( $images as $url ) {
$check = $this->is_image_url_valid( $url );
if ( ! $check['valid'] ) {
$broken_urls[] = $url;
$reasons[] = $url . ' => ' . $check['reason'];
}
}
$broken_urls = array_values( array_unique( $broken_urls ) );
if ( empty( $broken_urls ) ) {
$this->insert_log( array(
'post_id' => $post_id,
'post_title' => get_the_title( $post_id ),
'status' => 'no_need',
'reason' => '所有图片均可正常访问',
'image_count' => count( $images ),
'broken_count' => 0,
'replaced_count' => 0,
'old_urls' => array(),
'new_urls' => array(),
'run_source' => $source
) );
return;
}
// 替换失效图片(核心修改部分)
$new_content = $content;
$replace_map = array();
$replaced_count = 0;
$failed_reason = '';
// 获取与失效图片数量匹配的随机备用图
$rand_imgs = $this->get_random_img_urls( count( $broken_urls ) );
if ( empty( $rand_imgs ) ) {
$failed_reason = '未配置可用图片地址,无法替换';
} else {
// 为每个失效URL分配不同的备用图
foreach ( $broken_urls as $index => $bad_url ) {
$new_img = $rand_imgs[ $index ];
if ( false !== strpos( $new_content, $bad_url ) ) {
$new_content = str_replace( $bad_url, $new_img, $new_content );
$replace_map[ $bad_url ] = $new_img;
$replaced_count++;
}
}
}
if ( $failed_reason ) {
$this->insert_log( array(
'post_id' => $post_id,
'post_title' => get_the_title( $post_id ),
'status' => 'failed',
'reason' => $failed_reason . ';失效明细:' . implode( ';', $reasons ),
'image_count' => count( $images ),
'broken_count' => count( $broken_urls ),
'replaced_count' => $replaced_count,
'old_urls' => $broken_urls,
'new_urls' => $replace_map,
'run_source' => $source
) );
return;
}
if ( $new_content === $content || $replaced_count <= 0 ) {
$this->insert_log( array(
'post_id' => $post_id,
'post_title' => get_the_title( $post_id ),
'status' => 'failed',
'reason' => '检测到失效图片,但未能完成替换;失效明细:' . implode( ';', $reasons ),
'image_count' => count( $images ),
'broken_count' => count( $broken_urls ),
'replaced_count' => 0,
'old_urls' => $broken_urls,
'new_urls' => $replace_map,
'run_source' => $source
) );
return;
}
$updated = wp_update_post( array(
'ID' => $post_id,
'post_content' => $new_content
), true );
if ( is_wp_error( $updated ) ) {
$this->insert_log( array(
'post_id' => $post_id,
'post_title' => get_the_title( $post_id ),
'status' => 'failed',
'reason' => '文章更新失败:' . $updated->get_error_message(),
'image_count' => count( $images ),
'broken_count' => count( $broken_urls ),
'replaced_count' => 0,
'old_urls' => $broken_urls,
'new_urls' => $replace_map,
'run_source' => $source
) );
return;
}
clean_post_cache( $post_id );
$this->insert_log( array(
'post_id' => $post_id,
'post_title' => get_the_title( $post_id ),
'status' => 'success',
'reason' => '已替换失效图片;失效明细:' . implode( ';', $reasons ),
'image_count' => count( $images ),
'broken_count' => count( $broken_urls ),
'replaced_count' => $replaced_count,
'old_urls' => $broken_urls,
'new_urls' => $replace_map,
'run_source' => $source
) );
}
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/www.dongwubaike.cn/fanhao/wp-content/plugins/img-batch-replace/wp-image-health-repair-bulk-v2.php:1) in /www/wwwroot/www.dongwubaike.cn/fanhao/wp-includes/rest-api/class-wp-rest-server.php on line 1902
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/www.dongwubaike.cn/fanhao/wp-content/plugins/img-batch-replace/wp-image-health-repair-bulk-v2.php:1) in /www/wwwroot/www.dongwubaike.cn/fanhao/wp-includes/rest-api/class-wp-rest-server.php on line 1902
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/www.dongwubaike.cn/fanhao/wp-content/plugins/img-batch-replace/wp-image-health-repair-bulk-v2.php:1) in /www/wwwroot/www.dongwubaike.cn/fanhao/wp-includes/rest-api/class-wp-rest-server.php on line 1902
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/www.dongwubaike.cn/fanhao/wp-content/plugins/img-batch-replace/wp-image-health-repair-bulk-v2.php:1) in /www/wwwroot/www.dongwubaike.cn/fanhao/wp-includes/rest-api/class-wp-rest-server.php on line 1902
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/www.dongwubaike.cn/fanhao/wp-content/plugins/img-batch-replace/wp-image-health-repair-bulk-v2.php:1) in /www/wwwroot/www.dongwubaike.cn/fanhao/wp-includes/rest-api/class-wp-rest-server.php on line 1902
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/www.dongwubaike.cn/fanhao/wp-content/plugins/img-batch-replace/wp-image-health-repair-bulk-v2.php:1) in /www/wwwroot/www.dongwubaike.cn/fanhao/wp-includes/rest-api/class-wp-rest-server.php on line 1902
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/www.dongwubaike.cn/fanhao/wp-content/plugins/img-batch-replace/wp-image-health-repair-bulk-v2.php:1) in /www/wwwroot/www.dongwubaike.cn/fanhao/wp-includes/rest-api/class-wp-rest-server.php on line 1902
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/www.dongwubaike.cn/fanhao/wp-content/plugins/img-batch-replace/wp-image-health-repair-bulk-v2.php:1) in /www/wwwroot/www.dongwubaike.cn/fanhao/wp-includes/rest-api/class-wp-rest-server.php on line 1902
{"id":2769,"date":"2020-11-02T08:32:41","date_gmt":"2020-11-02T00:32:41","guid":{"rendered":"https:\/\/www.meiwenfeng.com\/?p=2769"},"modified":"2026-06-16T11:36:09","modified_gmt":"2026-06-16T03:36:09","slug":"%e6%ac%a1%e5%ad%90%e7%9b%b8%e9%9a%8f%e9%80%81%e5%87%ba%e5%a4%b4%e6%89%93%e4%b8%80%e5%ad%97","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/2769.html","title":{"rendered":"\u6b21\u5b50\u76f8\u968f\u9001\u51fa\u5934(\u6253\u4e00\u5b57)"},"content":{"rendered":"\u6bcf\u65e5\u4e00\u7ec3\uff1a\u4eca\u5929\u6211\u4eec\u6765\u5206\u4eab\u4e00\u4e9b\u6b21\u5b50\u6253\u4e00\u5b57\u76f8\u5173\u8c1c\u8bed\uff0c\u6bcf\u5929\u575a\u6301\u7ec3\u4e60\u731c\u5b57\u8c1c\uff0c\u953b\u70bc\u513f\u7ae5\u7684\u601d\u7ef4\u667a\u5546\u3002<\/p>\n
\u5c0f\u8d34\u58eb\uff1a\u201c\u6b21\u5b50\u201d\u6307\u201c\u4e8c\u513f\u201d\uff0c\u201c\u9001\u201d\u5b57\u53bb\u6389\u201c\u5173\u201d\uff0c\u5269\u4e0b\u201c\u8fb6\u201d\uff0c\u4e0e\u201c\u4e8c\u513f\u201d\u53ef\u7ec4\u5408\u6210\u201c\u8fdc\u201d\u5b57\u3002<\/p>\n
\u8c1c\u9762\uff1a\u6b21\u5b50\u76f8\u968f\u9001\u51fa\u5934 \uff08\u6253<\/p>\n
<\/a><\/p>\n\u4e00\u5b57\uff09<\/h2>\n
\u8c1c\u5e95\uff1a\u8fdc<\/p>\n
\u8c1c\u8bed\uff1a\u6c5f\u5c71\u5982\u6b64\u591a\u5a07(\u731c\u56fd\u540d\u7b80\u79f0\u4e09)<\/h2>\n
\u8c1c\u5e95\u7b54\u6848\uff1a\u3010\u4e2d\u3001\u571f\u3001\u7f8e\u3011<\/p>\n
\u5b57\u8c1c\uff1a\u6c5f\u6c34\u6a2a\u5ea6\u987a\u4e1c\u6d41(\u731c\u5b57\u4e00)\u8c1c\u5e95\uff1a\u3010\u5dde\u3011<\/p>\n\u8c1c\u8bed\uff1a\u6c5f\u5934\u7531\u6765\u6885\u65e9\u5f00(\u731c\u4e00\u5730\u7406\u8bcd\u8bed)<\/h2>\n
\u8c1c\u5e95\u7b54\u6848\uff1a\u3010\u6d77\u69fd\u3011<\/p>\n","protected":false},"excerpt":{"rendered":"
\u6bcf\u65e5\u4e00\u7ec3\uff1a\u4eca\u5929\u6211\u4eec\u6765\u5206\u4eab\u4e00\u4e9b\u6b21\u5b50\u6253\u4e00\u5b57\u76f8\u5173\u8c1c\u8bed\uff0c\u6bcf\u5929\u575a\u6301\u7ec3\u4e60\u731c\u5b57\u8c1c\uff0c\u953b\u70bc\u513f\u7ae5\u7684\u601d\u7ef4\u667a\u5546\u3002 \u5c0f\u8d34\u58eb\uff1a\u201c\u6b21\u5b50\u201d\u6307\u201c\u4e8c\u513f\u201d\uff0c\u201c\u9001\u201d\u5b57\u53bb\u6389\u201c\u5173\u201d\uff0c\u5269\u4e0b\u201c\u8fb6\u201d\uff0c\u4e0e\u201c\u4e8c\u513f\u201d\u53ef\u7ec4\u5408\u6210\u201c\u8fdc\u201d\u5b57\u3002 \u8c1c\u9762\uff1a\u6b21\u5b50\u76f8\u968f\u9001\u51fa\u5934 \uff08\u6253 \u4e00\u5b57\uff09 \u8c1c\u5e95\uff1a\u8fdc \u8c1c\u8bed\uff1a\u6c5f\u5c71\u5982\u6b64 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2769","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/2769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/comments?post=2769"}],"version-history":[{"count":1,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/2769\/revisions"}],"predecessor-version":[{"id":260931,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/2769\/revisions\/260931"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=2769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=2769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=2769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}