// 从文本域解析图片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":211259,"date":"2022-08-25T09:48:36","date_gmt":"2022-08-25T01:48:36","guid":{"rendered":"https:\/\/www.keaglegz.com\/211259.html"},"modified":"2022-08-25T09:48:36","modified_gmt":"2022-08-25T01:48:36","slug":"606%e7%94%b5%e5%bd%b1%e7%bd%91%ef%bc%88606%e7%94%b5%e5%bd%b1%e7%bd%91%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/211259.html","title":{"rendered":"606\u7535\u5f71\u7f51\uff08606\u7535\u5f71\u7f51\uff09"},"content":{"rendered":"<\/p>\n\u60a8\u597d,\u73b0\u5728\u6c49\u683c\u6765\u4e3a\u5927\u5bb6\u89e3\u7b54\u4ee5\u4e0a\u7684\u95ee\u9898\u3002606\u7535\u5f71\u7f51\uff0c606\u7535\u5f71\u7f51\u76f8\u4fe1\u5f88\u591a\u5c0f\u4f19\u4f34\u8fd8\u4e0d\u77e5\u9053,\u73b0\u5728\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n
1\u3001\u5982\u679c\u4f60\u80fd\u7528\u5b83\u76844G\u7f51\u7edc\u770b\u7684\u8bdd~~~\u970d\u970d\u901f\u5ea6\u4e0d\u662f\u76d6\u7684~~~\u3002<\/p>\n
\u672c\u6587\u5c31\u4e3a\u5927\u5bb6\u5206\u4eab\u5230\u8fd9\u91cc\uff0c\u5e0c\u671b\u5c0f\u4f19\u4f34\u4eec\u4f1a\u559c\u6b22\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u60a8\u597d,\u73b0\u5728\u6c49\u683c\u6765\u4e3a\u5927\u5bb6\u89e3\u7b54\u4ee5\u4e0a\u7684\u95ee\u9898\u3002606\u7535\u5f71\u7f51\uff0c606\u7535\u5f71\u7f51\u76f8\u4fe1\u5f88\u591a\u5c0f\u4f19\u4f34\u8fd8\u4e0d\u77e5\u9053,\u73b0\u5728\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 1\u3001\u5982\u679c\u4f60\u80fd\u7528\u5b83\u76844G\u7f51\u7edc\u770b\u7684\u8bdd~~~\u970d\u970d\u901f\u5ea6\u4e0d\u662f\u76d6\u7684~~~\u3002 \u672c\u6587\u5c31\u4e3a\u5927\u5bb6\u5206\u4eab\u5230\u8fd9\u91cc\uff0c\u5e0c\u671b\u5c0f\u4f19\u4f34\u4eec\u4f1a\u559c\u6b22\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99072],"tags":[],"class_list":["post-211259","post","type-post","status-publish","format-standard","hentry","category-wd"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/211259","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=211259"}],"version-history":[{"count":0,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/211259\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=211259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=211259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=211259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}