// 从文本域解析图片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":4286,"date":"2020-11-06T08:29:40","date_gmt":"2020-11-06T00:29:40","guid":{"rendered":"https:\/\/www.meiwenfeng.com\/?p=4286"},"modified":"2026-06-16T19:44:05","modified_gmt":"2026-06-16T11:44:05","slug":"%e9%bb%84%e6%9c%ba%e7%9a%84%e8%af%97%e6%9c%89%e5%93%aa%e4%ba%9b","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/4286.html","title":{"rendered":"\u9ec4\u673a\u7684\u8bd7\u6709\u54ea\u4e9b"},"content":{"rendered":"

\u9ec4\u673a\u662f\u5b8b\u4ee3\u540d\u58f0\u663e\u8d6b\u7684\u8bd7\u4eba\uff0c\u81f3\u4eca\u6d41\u4f20\u7684\u8bd7\u8bcd\u5927\u7ea6\u670994\u9996\uff0c \u9ec4\u673a\u7684\u8bd7\u4ee3\u8868\u5f88\u591a\uff0c\u4eca\u5929\u5206\u4eab\u4e00\u4e9b\u4f18\u79c0\u7684\u9ec4\u673a\u7684\u8bd7\u5927\u5168\u3002<\/p>\n

\u300a\u6ee1\u6c5f\u7ea2\u00b7\u82bd\u9f13\u58f0\u4e2d\u300b<\/h2>\n

\u9022\u677e\u4e24\u9b13\u98de\u4e91\u5f71\uff0c\u94bf\u5408\u672a\u68b3\u5986\u3002
\u9611\u5e72\u4fa7\u7554\uff0c\u95f2\u629b\u8354\u5b50\uff0c\u60ca\u6563\u9e33\u9e2f\u3002<\/p>\n

\u300a\u773c\u513f\u5a9a\u00b7\u4e1c\u98ce\u631f\u96e8\u82e6\u65e0\u7aef\u300b<\/h2>\n

\u4e1c\u98ce\u631f\u96e8\u82e6\u65e0\u7aef\u3002\u607b\u607b\u9001\u8f7b\u5bd2\u3002\u90a3\u582a\u66f4\u5411\uff0c\u6e58\u6e7e\u516d\u516d\uff0c\u6d45\u5904\u7559\u8239\u3002\u8bd7\u9604\u9152\u620f\u6210\u5b64\u8d1f\uff0c\u6625\u4e8b\u5df2\u9611\u73ca\u3002\u79bb\u6101\u90fd\u5728\uff0c\u843d\u82b1\u679d\u4e0a\uff0c\u675c\u5b87\u58f0\u8fb9\u3002<\/p>\n

\u300a\u773c\u513f\u5a9a\u00b7\u83ab\u55d4\u65e5\u65e5\u8bdd\u601d\u5f52\u300b<\/h2>\n

\u83ab\u55d4\u65e5\u65e5\u8bdd\u601d\u5f52\u3002\u5f52\u4e5f\u5374\u4fbf\u5b9c\u3002\u4e1c\u90bb\u62db\u8317\uff0c\u897f\u90bb\u5524\u9152\uff0c\u4e00\u7b11\u5f00\u7709\u3002\u4eba\u751f\u4e07\u4e8b\u65e0\u7f18\u8db3\uff0c\u5f85\u8db3\u662f\u4f55\u65f6\u3002\u59bb\u80fd\u7eba\u7ee9\uff0c\u513f\u80fd\u8015\u83b7\uff0c\u672a\u5fc5\u5bd2\u9965\u3002<\/p>\n

\u300a\u6d63\u6eaa\u6c99\u300b<\/h2>\n

\u7eff\u9501\u7a97\u524d\u53cc\u51e4\u5941\u3002\u8c03\u6731\u5300\u7c89\u7389\u7ea4\u7ea4\u3002\u5986\u6210\u8c01\u89e3\u5c3d\u60c5\u770b\u3002\u67f3\u8f6c\u5149\u98ce\u4e1d\u8885\u5a1c\uff0c\u82b1\u660e\u6674\u65e5\u9526\u6593\u6591\u3002\u4e00\u6625\u5fc3\u4e8b\u5728\u7709\u5c16\u3002<\/p>\n

\u300a\u6d63\u6eaa\u6c99\u00b7\u7eff\u7eee\u7a7a\u5f39\u6068\u672a\u5e73\u300b<\/h2>\n

\u7eff\u7eee\u7a7a\u5f39\u6068\u672a\u5e73\u3002\u53ef\u582a\u6267\u624b\u9001\u884c\u4eba\u3002\u78a7\u9152\u8c29\u5c06\u73cd\u91cd\u610f\uff0c\u83ab\u8f9e\u659f\u3002\u6211\u5b9a\u5fc6\u541b\u541f\u6e2d\u5317\uff0c\u541b\u987b\u601d\u6211\u8d4b\u505c\u4e91\u3002\u672a\u4fe1\u9ad8\u5c71\u6d41\u6c34\u66f2\uff0c\u65ad\u77e5\u97f3\u3002<<\/p>\n

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

\/p><\/p>\n

\u300a\u6d63\u6eaa\u6c99\u00b7\u6d41\u8f6c\u6625\u5149\u53c8\u4e00\u5e74\u300b<\/h2>\n

\u6d41\u8f6c\u6625\u5149\u53c8\u4e00\u5e74\u3002\u6625\u6101\u5c3d\u65e5\u4e24\u7709\u5c16\u3002\u8349\u8349\u5e7d\u6b22\u80fd\u51e0\u8bb8\uff0c\u5df2\u5929\u8fb9\u3002\u4f1a\u5f97\u97f3\u4e66\u751f\u7fbd\u7ffc\uff0c\u514d\u6559\u9b42\u68a6\u5f79\u5173\u5c71\u3002\u5e18\u5377\u843d\u82b1\u5343\u4e07\u70b9\uff0c\u96e8\u5982\u70df\u3002<\/p>\n

\u300a\u6d63\u6eaa\u6c99\u300b<\/h2>\n

\u65e5\u8f6c\u96d5\u680f\u5348\u6f0f\u5206\u3002\u4e95\u68a7\u843d\u5c3d\u5c0f\u7a97\u660e\u3002\u5b9d\u5e8a\u4e1d\u7d22\u61d2\u5173\u5fc3\u3002\u6101\u538b\u6625\u5c71\u5e94\u8109\u8109\uff0c\u56f0\u51dd\u79cb\u6c34\u60f3\u6c88\u6c88\u3002\u4f4e\u5934\u65f6\u9732\u4e00\u6e7e\u91d1\u3002<\/p>\n

\u300a\u6d63\u6eaa\u6c99\u00b7\u58a8\u7eff\u886b\u513f\u7a84\u7a84\u88c1\u300b<\/h2>\n

\u58a8\u7eff\u886b\u513f\u7a84\u7a84\u88c1\u3002\u7fe0\u8377\u659c?\u9886\u4e91\u5806\u3002\u51e0\u65f6\u8e2a\u8ff9\u4e0b\u9633\u53f0\u3002\u6b4c\u7f62\u6a31\u6843\u548c\u9732\u5c0f\uff0c\u821e\u9980\u6768\u67f3\u8d81\u98ce\u56de\u3002\u5524\u4eba\u4f11\u8bc9\u5341\u5206\u676f\u3002<\/p>\n

\u300a\u6d63\u6eaa\u6c99\u300b<\/h2>\n

\u8457\u7834\u6625\u886b\u8d70\u8def\u5c18\u3002\u5b50\u89c4\u557c\u65ad\u4e0d\u7981\u95fb\u3002\u529f\u540d\u4f3c\u6211\u5374\u7f9e\u4eba\u3002\u8c61\u677f\u4e14\u987b\u6b4c\u7693\u9f7f\uff0c\u8885\u8e44\u4f55\u82e6\u60dc\u9ec4\u91d1\u3002\u5c0a\u524d\u4f11\u8d1f\u6b64\u751f\u8eab\u3002<\/p>\n

\u300a\u6c34\u8c03\u6b4c\u5934\u00b7\u6b64\u65e5\u8db3\u53ef\u60dc\u300b<\/h2>\n

\u6b64\u65e5\u8db3\u53ef\u60dc\uff0c\u5fc3\u4e8b\u6b63\u5d14\u5d6c\u3002\u6c5f\u6dee\u8e0f\u904d\uff0c\u7ecf\u5c81\u76f8\u8bc6\u5b9a\u8c01\u6765\u3002\u6bcf\u5411\u9152\u8fb9\u957f\u53f9\uff0c\u66f4\u5411\u82b1\u8fb9\u957f\u7b11\uff0c\u610f\u8651\u53f5\u80fd\u731c\u3002\u9082\u9005\u5ffd\u76f8\u9047\uff0c\u6709\u5ba2\u5728\u5c18\u57c3\u3002
\u8131\u5112\u51a0\uff0c\u8457\u6b66\u5f01\uff0c\u592a\u591a\u624d\u3002\u7b14\u58a8\u4e89\u4f3c\uff0c\u94a9\u621f\u5bb9\u6613\u5230\u4e91\u53f0\u3002\u4f59\u5b50\u4f55\u987b\u8f6c\u624b\uff0c\u4fbf\u628a\u5e73\u751f\u80f8\u81c6\uff0c\u52c7\u53bb\u83ab\u5f98\u5f8a\u3002\u4e8b\u4e1a\u4e0a\u91d1\u77f3\uff0c\u4eba\u4e16\u81ea\u6b22\u54c0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u9ec4\u673a\u662f\u5b8b\u4ee3\u540d\u58f0\u663e\u8d6b\u7684\u8bd7\u4eba\uff0c\u81f3\u4eca\u6d41\u4f20\u7684\u8bd7\u8bcd\u5927\u7ea6\u670994\u9996\uff0c \u9ec4\u673a\u7684\u8bd7\u4ee3\u8868\u5f88\u591a\uff0c\u4eca\u5929\u5206\u4eab\u4e00\u4e9b\u4f18\u79c0\u7684\u9ec4\u673a\u7684\u8bd7\u5927\u5168\u3002 \u300a\u6ee1\u6c5f\u7ea2\u00b7\u82bd\u9f13\u58f0\u4e2d\u300b \u9022\u677e\u4e24\u9b13\u98de\u4e91\u5f71\uff0c\u94bf\u5408\u672a\u68b3\u5986\u3002\u9611\u5e72\u4fa7\u7554\uff0c\u95f2\u629b\u8354\u5b50\uff0c\u60ca\u6563\u9e33\u9e2f\u3002 \u300a\u773c\u513f\u5a9a\u00b7\u4e1c\u98ce\u631f\u96e8\u82e6\u65e0\u7aef\u300b \u4e1c\u98ce\u631f\u96e8\u82e6\u65e0\u7aef\u3002\u607b\u607b\u9001 […]<\/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-4286","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/4286","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=4286"}],"version-history":[{"count":1,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/4286\/revisions"}],"predecessor-version":[{"id":262403,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/4286\/revisions\/262403"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=4286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=4286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=4286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}