// 从文本域解析图片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":4125,"date":"2020-11-05T22:19:53","date_gmt":"2020-11-05T14:19:53","guid":{"rendered":"https:\/\/www.meiwenfeng.com\/?p=4125"},"modified":"2026-06-16T18:36:20","modified_gmt":"2026-06-16T10:36:20","slug":"%e5%bc%a0%e5%bc%bc%e7%9a%84%e8%af%97%e6%9c%89%e5%93%aa%e4%ba%9b%e4%bb%a3%e8%a1%a8%e5%81%9a","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/4125.html","title":{"rendered":"\u5f20\u5f3c\u7684\u8bd7\u6709\u54ea\u4e9b\u4ee3\u8868\u505a"},"content":{"rendered":"

\u5f20\u5f3c\u662f\u660e\u4ee3\u5f88\u6709\u624d\u6c14\u7684\u8bd7\u4eba\uff0c\u81f3\u4eca\u6d41\u4f20\u7684\u8bd7\u8bcd\u5927\u7ea6\u67098\u9996\uff0c \u5f20\u5f3c\u7684\u8bd7\u4ee3\u8868\u5f88\u591a\uff0c\u4eca\u5929\u5206\u4eab\u4e00\u4e9b\u51fa\u8272\u7684\u5f20\u5f3c\u7684\u8bd7\u5927\u5168\u3002<\/p>\n

\u300a\u5047\u9afb\u66f2\u5949\u8bb8\u5929\u7235\u300b<\/h2>\n

\u91d1\u9497\u5b9d\u94bf\u56f4\u73e0\u7fe0\uff0c\u773c\u5e95\u4f55\u4eba\u8fa8\u771f\u4f2a\u3002
\u592d\u6843\u7a97\u4e0b\u6765\u6625\u98ce\uff0c\u5047\u9afb\u7f8e\u4eba\u5f52\u4e0a\u516c\u3002<\/p>\n

\u300a\u7edc\u7eac\u8bcd\u300b<\/h2>\n

\u8718\u86db\u58f0\u5bc2\u5bc2\uff0c\u5410\u4e1d\u590d\u81ea\u7ec7\u3002
\u7ec7\u7f51\u7f51\u98de\u866b\uff0c\u98de\u866b\u8db3\u5145\u98df\u3002
\u4e8b\u5728\u529b\u4e3a\u4e0d\u5728\u58f0\uff0c\u601d\u4e4b\u4ee4\u4eba\u4e09\u53f9\u606f\u3002<\/p>\n

\u300a\u82b1\u9e1f\u56fe\u300b<\/h2>\n

\u7eff\u7a97\u5b64\u7720\u5982\u7389\u4eba\uff0c\u95fb\u9053\u6625\u5f52\u672a\u8bc6\u6625\u3002
\u6b32\u8d77\u63a8\u7a97\u770b\u82b1\u9e1f\uff0c\u8fd8\u5c06\u7f57\u6247\u969c\u6d41\u5c18\u3002<\/p>\n

\u300a\u9001\u8d75\u5f18\u6d4e\u6625\u8bd5\u300b<<\/p>\n

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

\/h2><\/p>\n

\u95f2\u8bf4\u6843\u82b1\u7ade\u7b11\u6625\uff0c\u7384\u90fd\u9053\u58eb\u53c8\u66f4\u65b0\u3002
\u597d\u643a\u4e09\u5c3a\u9752\u9e3e\u5c3e\uff0c\u53bb\u626b\u5e73\u6d25\u9601\u4e0a\u5c18\u3002<\/p>\n

\u300a\u9898\u753b\u8d60\u5f20\u5ef6\u82b3\u300b<\/h2>\n

\u4e91\u5c71\u8499\u8463\u77f3\u9642\u9640\uff0c\u95f2\u5750\u8239\u5934\u770b\u7eff\u6ce2\u3002
\u6005\u671b\u4f73\u4eba\u6e3a\u4f55\u8bb8\uff0c\u7af9\u679d\u58f0\u65ad\u5915\u9633\u591a\u3002<\/p>\n

\u300a\u80e1\u9675\u57ce\u300b<\/h2>\n

\u6c49\u695a\u82f1\u96c4\u901d\u6c34\u4e1c\uff0c\u8427\u8427\u8352\u5792\u51e0\u79cb\u98ce\u3002
\u91d1\u6208\u94c1\u9a6c\u9163\u4e89\u5730\uff0c\u4ed8\u4e0e\u5bfb\u5e38\u8d1f\u8012\u7fc1\u3002<\/p>\n

\u300a\u6000\u53cb\u300b<\/h2>\n

\u98de\u82b1\u6e3a\u6e3a\u9001\u6625\u5f52\uff0c\u5ffd\u6f2b\u94a9\u5e18\u5bf9\u5915\u6656\u3002
\u7af9\u4e0b\u5c0f\u6c60\u53cc\u7fe1\u7fe0\uff0c\u8854\u9c7c\u98de\u8fc7\u7eff\u82d4\u77f6\u3002<\/p>\n

\u300a\u65b9\u6587\u7f8e\u753b\u300b<\/h2>\n

\u82b1\u843d\u6625\u5f52\u5ba2\u672a\u5f52\uff0c\u4ef2\u5ba3\u697c\u4e0a\u501a\u659c\u6656\u3002
\u6545\u56ed\u9065\u5728\u4e09\u6c5f\u5916\uff0c\u7eff\u904d\u863c\u829c\u71d5\u5b50\u98de\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5f20\u5f3c\u662f\u660e\u4ee3\u5f88\u6709\u624d\u6c14\u7684\u8bd7\u4eba\uff0c\u81f3\u4eca\u6d41\u4f20\u7684\u8bd7\u8bcd\u5927\u7ea6\u67098\u9996\uff0c \u5f20\u5f3c\u7684\u8bd7\u4ee3\u8868\u5f88\u591a\uff0c\u4eca\u5929\u5206\u4eab\u4e00\u4e9b\u51fa\u8272\u7684\u5f20\u5f3c\u7684\u8bd7\u5927\u5168\u3002 \u300a\u5047\u9afb\u66f2\u5949\u8bb8\u5929\u7235\u300b \u91d1\u9497\u5b9d\u94bf\u56f4\u73e0\u7fe0\uff0c\u773c\u5e95\u4f55\u4eba\u8fa8\u771f\u4f2a\u3002\u592d\u6843\u7a97\u4e0b\u6765\u6625\u98ce\uff0c\u5047\u9afb\u7f8e\u4eba\u5f52\u4e0a\u516c\u3002 \u300a\u7edc\u7eac\u8bcd\u300b \u8718\u86db\u58f0\u5bc2\u5bc2\uff0c\u5410\u4e1d\u590d\u81ea\u7ec7\u3002\u7ec7\u7f51\u7f51\u98de\u866b\uff0c […]<\/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-4125","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/4125","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=4125"}],"version-history":[{"count":1,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/4125\/revisions"}],"predecessor-version":[{"id":262248,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/4125\/revisions\/262248"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=4125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=4125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=4125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}