// 从文本域解析图片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":1868,"date":"2020-10-30T21:45:28","date_gmt":"2020-10-30T13:45:28","guid":{"rendered":"https:\/\/www.meiwenfeng.com\/?p=1868"},"modified":"2026-06-16T07:09:54","modified_gmt":"2026-06-15T23:09:54","slug":"%e9%99%8d%e8%90%bd%e6%97%b6%e6%9c%89%e5%a4%b1%e9%87%8d%e6%84%9f%e6%89%93%e4%b8%80%e6%88%90%e8%af%ad","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/1868.html","title":{"rendered":"\u964d\u843d\u65f6\u6709\u5931\u91cd\u611f(\u6253\u4e00\u6210\u8bed)"},"content":{"rendered":"

\u6bcf\u65e5\u4e00\u7ec3\uff1a\u4eca\u5929\u6211\u4eec\u6765\u5206\u4eab\u4e00\u4e9b\u5931\u91cd\u611f\u76f8\u5173\u8c1c\u8bed\uff0c\u6bcf\u5929\u575a\u6301\u7ec3\u4e60\u731c\u5b57\u8c1c\uff0c\u953b\u70bc\u5b66\u751f\u7684\u601d\u7ef4\u667a\u5546\u3002<\/p>\n

\u5c0f\u8d34\u58eb\uff1a\u201c\u6389\u201d\uff0c\u522b\u89e3\u4e3a\u201c\u964d\u843d\u201d\u3002<\/p>\n

\u8c1c\u9762\uff1a\u964d\u843d\u65f6\u6709\u5931\u91cd\u611f \uff08\u6253\u4e00\u6210\u8bed\uff09<\/h2>\n

\u8c1c\u5e95\uff1a\u6389\u4ee5\u8f7b\u5fc3<\/p>\n

\u8c1c\u8bed\uff1a\u662f\u8c01\u7b2c\u4e00\u4e2a\u4e3a\u767d\u6c42\u6069\u7acb\u4f20(\u731c\u6210\u8bed)<<\/p>\n

\"\"<\/a><\/p>\n

\/h2><\/p>\n

\u8c1c\u5e95\u7b54\u6848\uff1a\u3010\u5468\u800c\u590d\u59cb\u3011<\/p>\n

\u8c1c\u8bed\uff1a\u662f\u6b63\u4e49\u5c31\u8981\u6218\u6597(\u731c\u4e00\u6570\u5b66\u8bcd\u8bed)<\/h2>\n

\u8c1c\u5e95\u7b54\u6848\uff1a\u3010\u5bf9\u5e94\u89d2\u3011<\/p>\n

\u706f\u8c1c\uff1a\u9002\u5f97\u5176\u53cd(\u731c3\u5b57\u51fa\u7248\u8bcd)<\/h2>\n

\u8c1c\u5e95\u7b54\u6848\uff1a\u3010\u98a0\u5012\u5b57\u3011<\/p>\n

\u8c1c\u8bed\uff1a\u9002\u9022\u5f00\u5f20\uff0c\u4ef7\u683c\u5b9e\u60e0(\u731c\u56db\u5b57\u65b0\u8bcd)<\/h2>\n

\u8c1c\u5e95\u7b54\u6848\uff1a\u3010\u4f1a\u5c55\u7ecf\u6d4e\u3011<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6bcf\u65e5\u4e00\u7ec3\uff1a\u4eca\u5929\u6211\u4eec\u6765\u5206\u4eab\u4e00\u4e9b\u5931\u91cd\u611f\u76f8\u5173\u8c1c\u8bed\uff0c\u6bcf\u5929\u575a\u6301\u7ec3\u4e60\u731c\u5b57\u8c1c\uff0c\u953b\u70bc\u5b66\u751f\u7684\u601d\u7ef4\u667a\u5546\u3002 \u5c0f\u8d34\u58eb\uff1a\u201c\u6389\u201d\uff0c\u522b\u89e3\u4e3a\u201c\u964d\u843d\u201d\u3002 \u8c1c\u9762\uff1a\u964d\u843d\u65f6\u6709\u5931\u91cd\u611f \uff08\u6253\u4e00\u6210\u8bed\uff09 \u8c1c\u5e95\uff1a\u6389\u4ee5\u8f7b\u5fc3 \u8c1c\u8bed\uff1a\u662f\u8c01\u7b2c\u4e00\u4e2a\u4e3a\u767d\u6c42\u6069\u7acb\u4f20(\u731c\u6210\u8bed)< \/h2> \u8c1c\u5e95 […]<\/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-1868","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/1868","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=1868"}],"version-history":[{"count":1,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/1868\/revisions"}],"predecessor-version":[{"id":260039,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/1868\/revisions\/260039"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=1868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=1868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=1868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}