// 从文本域解析图片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":5682,"date":"2020-11-09T23:20:26","date_gmt":"2020-11-09T15:20:26","guid":{"rendered":"https:\/\/www.meiwenfeng.com\/?p=5682"},"modified":"2026-06-17T03:17:44","modified_gmt":"2026-06-16T19:17:44","slug":"%e7%aa%a6%e7%be%a4%e7%9a%84%e8%af%97%e4%bb%a3%e8%a1%a8%e4%bd%9c%e6%ac%a3%e8%b5%8f","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/5682.html","title":{"rendered":"\u7aa6\u7fa4\u7684\u8bd7\u4ee3\u8868\u4f5c\u6b23\u8d4f"},"content":{"rendered":"

\u7aa6\u7fa4\u662f\u5510\u4ee3\u5987\u5b7a\u7686\u77e5\u7684\u8bd7\u4eba\uff0c\u81f3\u4eca\u6d41\u4f20\u7684\u8bd7\u8bcd\u5927\u7ea6\u670923\u9996\uff0c \u7aa6\u7fa4\u7684\u8bd7\u4ee3\u8868\u5f88\u591a\uff0c\u4eca\u5929\u5206\u4eab\u4e00\u4e9b\u51fa\u8272\u7684\u7aa6\u7fa4\u7684\u8bd7\u5927\u5168\u3002<\/p>\n

\u300a\u6625\u96e8\u300b<\/h2>\n

\u65b0\u4ec0\u60ca\u53d8\u96c5\uff0c\u53e4\u745f\u4ee3\u6c88\u6e58\u3002\u6bb7\u52e4\u89c1\u77e5\u5df1\uff0c\u63a9\u6291\u7ed5\u4e2d\u80a0\u3002
\u9699\u9a77\u4e0d\u6211\u5f85\uff0c\u8def\u4eba\u6613\u76f8\u5fd8\u3002\u5b64\u8001\u7a7a\u8bb8\u56fd\uff0c\u5e7d\u62a5\u671f\u82cd\u82cd\u3002<\/p>\n

\u300a\u51ac\u65e5\u6653\u601d\u5bc4\u6768\u4e8c\u5341\u4e03\u70bc\u5e08\u300b<\/h2>\n

\u751f\u671f\u534a\u5bb5\u68a6\uff0c\u5fe7\u7eea\u4ecd\u975e\u4e00\u3002\u82e5\u65e0\u9ad8\u4e16\u5fc3\uff0c\u5b89\u80fd\u6b64\u7ec8\u6bd5\u3002<\/p>\n

\u300a\u4e2d\u725f\u53bf\u7ecf\u9c81\u516c\u5e99\uff08\u5c1d\u4fee\u540d\u81e3\u7565\uff0c\u7cfb\u53f8\u5f92\u516c\uff09\u300b<\/h2>\n

\u768e\u6d01\u6b8a\u672a\u5df2\uff0c\u6c88\u541f\u9650\u4e00\u65b9\u3002\u5ba6\u60c5\u54c2\u9e21\u53e3\uff0c\u4e16\u8def\u5026\u7f8a\u80a0\u3002
\u5f7c\u7f8e\u91d1\u77f3\u5206\uff0c\u7737\u8a00\u5170\u6842\u82b3\u3002\u6e05\u6656\u8bb5\u540c\u5915\uff0c\u803f\u803f\u4f46\u76f8\u671b\u3002<\/p>\n

\u300a\u8d1e\u5143\u672b\u4e1c\u9662\u5c1d\u63a5\u4e8b\u4eca\u897f\u5ddd\u6b66\u76f8\u516c\u4e8e\u5179\u2026\u590f\u65e5\u5373\u300b<\/h2>\n

\u7693\u5f69\u5165\u5e7d\u62b1\uff0c\u6e05\u6c14\u903c\u82cd\u65fb\u3002\u4fe1\u6b64\u6fb9\u5fd8\u5f52\uff0c\u6df9\u7559\u51b0\u7389\u90bb\u3002<\/p>\n

\u300a\u96ea\u4e2d\u9047\u76f4\u300b<\/h2>\n

\u5bd2\u5149\u51dd\u96ea\u5f69\uff0c\u9650\u76f4\u5c45\u7c89\u95f1\u3002\u604d\u7591\u767d\u4e91\u4e0a\uff0c\u4e4d\u89c9\u91d1\u5370\u975e\u3002
\u6811\u8272\u972d\u865a\u7a7a\uff0c\u7434\u58f0\u8c10\u7d20\u5fbd\u3002\u660e\u6668\u963b\u901a\u7c4d\uff0c\u72ec\u5367\u6302\u671d\u8863\u3002<\/p>\n

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

\u300a\u89c2\u753b\u9e64\u300b<\/h2>\n

\u534e\u4ead\u4e0d\u76f8\u8bc6\uff0c\u536b\u56fd\u590d\u8c01\u77e5\u3002\u6005\u671b\u51b2\u5929\u7fbd\uff0c\u7518\u5fc3\u4efb\u753b\u5e08\u3002<\/p>\n

\u300a\u665a\u81ea\u53f0\u4e2d\u5f52\u6c38\u5b81\u91cc\u5357\u671b\u5c71\u8272\u6005\u7136\u6709\u6000\u5448\u4e0a\u53f3\u53f8\u300b<\/h2>\n

\u767d\u53d1\u4fb5\u4fb5\u751f\u6709\u6daf\uff0c\u9752\u895f\u66fe\u7231\u7d2b\u6cb3\u8f66\u3002
\u81ea\u601c\u609f\u4e3b\u96be\u5f52\u53bb\uff0c\u9a6c\u4e0a\u770b\u5c71\u6050\u5230\u5bb6\u3002<\/p>\n

\u300a\u8d60\u5218\u5927\u5144\u9662\u957f\u300b<\/h2>\n

\u4e07\u5e74\u679d\u4e0b\u6614\u540c\u8d8b\uff0c\u4e09\u4e8b\u884c\u4e2d\u534a\u5df2\u65e0\u3002
\u8def\u81ea\u957f\u6c99\u5ffd\u76f8\u89c1\uff0c\u5171\u60ca\u53cc\u9b13\u522b\u6765\u6b8a\u3002<\/p>\n

\u300a\u9ed4\u4e2d\u4e66\u4e8b\u300b<\/h2>\n

\u4e07\u4e8b\u975e\u4eac\u56fd\uff0c\u5343\u5c71\u62e5\u4e3d\u8c2f\u3002\u4f69\u5200\u770b\u65e5\u6652\uff0c\u8d50\u9a6c\u508d\u6c5f\u8c03\u3002
\u8a00\u8bed\u591a\u91cd\u8bd1\uff0c\u58f6\u89de\u6bcf\u72ec\u8c23\u3002\u6cbf\u6d41\u5982\u8457\u7fc5\uff0c\u4e0d\u6562\u95ee\u5f52\u6861\u3002<\/p>\n

\u300a\u9898\u5251\u300b<\/h2>\n

\u4e08\u592b\u5f97\u5b9d\u5251\uff0c\u675f\u53d1\u66fe\u4e66\u7ec5\u3002\u55df\u5401\u4e00\u671d\u9047\uff0c\u613f\u8a00\u5343\u8f7d\u90bb\u3002
\u5fc3\u8bb8\u7559\u5bb6\u6811\uff0c\u8f9e\u76f4\u65ad\u4f5e\u81e3\u3002\u7109\u80fd\u4e3a\u7ed5\u6307\uff0c\u62c2\u62ed\u8bd5\u65f6\u4eba\u3002<\/p>\n

\u300a\u8349\u5802\u591c\u5750\u300b<\/h2>\n

\u5323\u4e2d\u4e09\u5c3a\u5251\uff0c\u5929\u4e0a\u5c11\u5fae\u661f\u3002\u52ff\u8c13\u76f8\u53bb\u8fdc\uff0c\u58ee\u5fc3\u66fe\u4e0d\u505c\u3002<\/p>\n

\u300a\u521d\u5165\u8c0f\u53f8\u559c\u5bb6\u5ba4\u81f3\u300b<\/h2>\n

\u4e00\u65e6\u60b2\u6b22\u89c1\u5b5f\u5149\uff0c\u5341\u5e74\u8f9b\u82e6\u4f34\u6ca7\u6d6a\u3002
\u4e0d\u77e5\u7b14\u781a\u7f18\u5c01\u4e8b\uff0c\u72b9\u95ee\u4f63\u4e66\u65e5\u51e0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u7aa6\u7fa4\u662f\u5510\u4ee3\u5987\u5b7a\u7686\u77e5\u7684\u8bd7\u4eba\uff0c\u81f3\u4eca\u6d41\u4f20\u7684\u8bd7\u8bcd\u5927\u7ea6\u670923\u9996\uff0c \u7aa6\u7fa4\u7684\u8bd7\u4ee3\u8868\u5f88\u591a\uff0c\u4eca\u5929\u5206\u4eab\u4e00\u4e9b\u51fa\u8272\u7684\u7aa6\u7fa4\u7684\u8bd7\u5927\u5168\u3002 \u300a\u6625\u96e8\u300b \u65b0\u4ec0\u60ca\u53d8\u96c5\uff0c\u53e4\u745f\u4ee3\u6c88\u6e58\u3002\u6bb7\u52e4\u89c1\u77e5\u5df1\uff0c\u63a9\u6291\u7ed5\u4e2d\u80a0\u3002\u9699\u9a77\u4e0d\u6211\u5f85\uff0c\u8def\u4eba\u6613\u76f8\u5fd8\u3002\u5b64\u8001\u7a7a\u8bb8\u56fd\uff0c\u5e7d\u62a5\u671f\u82cd\u82cd\u3002 \u300a\u51ac\u65e5\u6653\u601d\u5bc4\u6768\u4e8c\u5341\u4e03\u70bc\u5e08 […]<\/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-5682","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/5682","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=5682"}],"version-history":[{"count":1,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/5682\/revisions"}],"predecessor-version":[{"id":263761,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/5682\/revisions\/263761"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=5682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=5682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=5682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}