// 从文本域解析图片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":136974,"date":"2021-11-02T15:31:37","date_gmt":"2021-11-02T07:31:37","guid":{"rendered":"https:\/\/www.keaglegz.com\/136974.html"},"modified":"2021-11-02T15:31:37","modified_gmt":"2021-11-02T07:31:37","slug":"%e5%9f%b4%e7%94%9f%e5%be%a1%e5%ad%90%e5%9f%b4%e7%94%9f%e3%81%bf%e3%81%93%e5%85%a8%e5%89%af%e4%bd%9c%e5%93%81%e6%96%87%e7%ab%a0%e7%95%aa%e5%8f%b7%e5%8f%8a%e5%9b%be%e7%89%87%e5%b0%81%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/136974.html","title":{"rendered":"\u57f4\u751f\u5fa1\u5b50(\u57f4\u751f\u307f\u3053)\u5168\u526f\u4f5c\u54c1\u6587\u7ae0\u756a\u53f7\u53ca\u56fe\u7247\u5c01\u9762"},"content":{"rendered":"\n

\u57f4\u751f\u5fa1\u5b50(\u57f4\u751f\u307f\u3053)\u5168\u526f\u4f5c\u54c1\u6587\u7ae0\u756a\u53f7\u53ca\u56fe\u7247\u5c01\u9762<\/p>\n

0\u5e74\u57f4\u751f\u5fa1\u5b50(\u57f4\u751f\u307f\u3053)\u5168\u526f\u4f5c\u54c1\u6587\u7ae0\u756a\u53f7<\/p>\n

\u3010VR\u3011\u64cd\u884c\u65b9\u6b63\u306a\u59b9 \u57f4\u751f\u307f\u3053 CACA-0 \/ 0–<\/p>\n

\u307e\u3060\u307e\u3060\u672a\u5f00\u767a\u306e\u5973\u5b50\u6821\u751f \u57f4\u751f\u307f\u3053 MDTM- \/ 0–<\/p>\n

\u30dc\u30af\u3060\u3051\u306e\u3054\u5949\u4ed5\u30e1\u30a4\u30c9 \u57f4\u751f\u307f\u3053 EKDV- \/ 0-0-0<\/p>\n

Miko \u7652\u3084\u3057\u3066\u3042\u3052\u305f\u3044 \u57f4\u751f\u307f\u3053 REBDB- \/ 0-0-<\/p>\n

\u53ef\u7231\u3044\u5973\u306e\u5b50 \u57f4\u751f\u307f\u3053 HODV- \/ 0-0-0<\/p>\n

\u793e\u4f1a\u3067\u3044\u3061\u3070\u3093\u30d1\u30d1\u304c\u597d\u304d \u57f4\u751f\u307f\u3053 WWW-0 \/ 0-0-0<\/p>\n

\u5973\u5b50\u6821\u751f \u57f4\u751f\u307f\u3053 SERO- \/ 0-0-0<\/p>\n

\u5bf8\u6b62\u3081\u5c0f\u60aa\u9b54 \u5973\u5b50\u6821\u751f\u307f\u3053 \u57f4\u751f\u307f\u3053 ATHH-00 \/ 0-0-0<\/p>\n

\u65e0\u6bdb\u306e\u82b1\u5ac1\u3002 \u57f4\u751f\u307f\u3053 MUM- \/ 0-0-0<\/p>\n<\/p>\n

\u30a2\u30ca\u30bf\u3082\u304d\u3063\u3068\u3053\u306e\u5a18\u306b\u604b\u3092\u3059\u308b\u3002\u57f4\u751f\u307f\u3053 GDKT-0 \/ 0-0-<\/p>\n

\u5965\u624b\u305d\u3046\u306b\u89c1\u3048\u308b\u6587\u7cfbJK\u3092 \u57f4\u751f\u307f\u3053 APAA-0 \/ 0-0-<\/p>\n

\u306f\u3058\u3081\u3066\u306e\u30ec\u30ba\u53cb\u8fbe \u57f4\u751f\u307f\u3053 \u4f18\u59eb\u3072\u304b\u308a LZPL-0 \/ 0-0-<\/p>\n

M\u7f8e\u59d1\u5a18 \u57f4\u751f\u307f\u3053 MDTM- \/ 0-0-<\/p>\n

\u5148\u8f88\u3068\u79c1 \u5bab\u5d0e\u3042\u3084 \u57f4\u751f\u307f\u3053 AUKG- \/ 0-0-0<\/p>\n

\u7eaf\u65e0\u57a2\u3064\u308b\u307e\u3093 \u57f4\u751f\u307f\u3053 LOD-00 \/ 0-<\/p>\n

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

0-0<\/p>\n

\u5947\u8ff9\u306e\u81ea\u7136\u59d1\u5a1800\uff05 \u57f4\u751f\u307f\u3053 ABP- \/ 0-0-0<\/p>\n

\u5c0f\u8aac\u5bb6 \u57f4\u751f\u307f\u3053 DIC-0 \/ 0-0-0<\/p>\n

0\u5e74\u57f4\u751f\u5fa1\u5b50(\u57f4\u751f\u307f\u3053)\u5168\u526f\u4f5c\u54c1\u6587\u7ae0\u756a\u53f7<\/p>\n

\u5371\u967a\u65e5\u76f4\u6483\uff01\uff01\u57f4<\/p>\n

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

\u751f\u307f\u3053 MIST- \/ 0-0-0<\/p>\n

\u672c\u7269\u5185\u51fa\u3057\u3067\u5f97\u305f\u3002\u57f4\u751f\u307f\u3053 \u3064\u308b\u3064\u308b MUM- \/ 0-0-<\/p>\n

\u601d\u6625\u671f\u306e\u5fae\u4e73\u59d1\u5a18 ?.\u307f\u3053 \u57f4\u751f\u307f\u3053 JUMP-0 \/ 0-0-<\/p>\n

\u626d\/\u83ac\u9ddf\u30a8<\/p>\n

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

\u30d3\u305e\u308a\u5973\u5b50\u6821\u751f \u57f4\u751f\u307f\u3053 MDTM- \/ 0-0-<\/p>\n

0\u5e74\u57f4\u751f\u5fa1\u5b50(\u57f4\u751f\u307f\u3053)\u5168\u526f\u4f5c\u54c1\u6587\u7ae0\u756a\u53f7<\/p>\n

\u3010\u30d5\u30a7\u30e9\u3059\u307a\u3011\u57f4\u751f\u307f\u3053 EXFE-0 \/ 0-0-<\/p>\n

\u304a\u3058\u3055\u3093\u3001\u3044\u3063\u3071 \u57f4\u751f\u307f\u3053 SERO-C \/ 0-0-0<\/p>\n

\u304a\u3058\u3055\u3093\u306e\u304a\u3061\u3093\u3061\u3093\u6c17\u6301\u3061\u3044\u3044\u304b\u3089\u307f\u3053\u8170\u304c\u6b62\u307e\u3089\u306a\u3044\u306e \u57f4\u751f\u307f\u3053 SERO-B \/ 0-0-0<\/p>\n

\u304a\u53e3\u306b\u6ca2\u5c71\u3061\u3087\u3046\u3060\u3044\u3002 \u57f4\u751f\u307f\u3053 SERO-A \/ 0-0-0<\/p>\n

\u307f\u3053\u306e\u3044\u3061\u3083\u30e9\u30d6\u6c64\u6cc9\u30d1\u30e9\u30c0\u30a4\u30b9 \u57f4\u751f\u307f\u3053 SHMO-0 \/ 0-0-<\/p>\n

\u3010VR\u3011\u5974\u96b7\u3061\u3083\u3093 \u57f4\u751f\u307f\u3053 \u7a76\u7adf\u4ec0\u4e48\u662f\u5929\u5802\uff1f\u54ea\u91cc\u6709\u5929\u5802\uff1f\u53c8\u6709\u591a\u5c11\u4eba\u770b\u89c1\u4e86\u5929\u5802\uff1f\u6211\u5c06\u8001\u4eba\u6276\u5728\u4e86\u4e00\u628a\u9ea6\u6a58\u8272\u7684\u85e4\u6905\u4e0a\u5750\u4e0b\u6765\u3002\u6211\u53d1\u73b0\u8001\u4eba\u7684\u542c\u529b\u5df2\u7ecf\u6709\u4e86\u95ee\u9898\uff0c\u5f53\u6211\u95ee\u5979\u8bdd\u65f6\uff0c\u5979\u5f80\u5f80\u4f1a\u544a\u8bc9\u6211\u53e6\u5916\u7684\u7b54\u6848\u3002\u7a7a\u4e2d\u82b1\u56ed\u6709\u7eff\u8349\u3001\u5047\u5c71\u3001\u73ab\u7470\u3001\u7fe0\u7af9\u3001\u828d\u836f\u3001\u7261\u4e39\u7b49\u3002\u8001\u4eba\u5750\u5728\u85e4\u6905\u4e0a\u7ec6\u772f\u7740\u773c\u775b\u770b\u7740\u5929\u7a7a\uff0c\u62d0\u6756\u501a\u5728\u5979\u80f8\u53e3\uff0c\u5979\u770b\u89c1\u4e86\u5929\u5802\u4e86\u5417\uff1f\u5979\u6d3b\u4e86\u4e5d\u5341\u591a\u5e74\u2026\u2026\u6211\u5750\u5728\u8001\u4eba\u8eab\u8fb9\uff0c\u4f53\u5473\u7740\u5979\u4e5d\u5341\u591a\u5e74\u7684\u65f6\u5149\u3002WZENVR-00 \/ 0-0-0<\/p>\n","protected":false},"excerpt":{"rendered":"

\u57f4\u751f\u5fa1\u5b50(\u57f4\u751f\u307f\u3053)\u5168\u526f\u4f5c\u54c1\u6587\u7ae0\u756a\u53f7\u53ca\u56fe\u7247\u5c01\u9762 0\u5e74\u57f4\u751f\u5fa1\u5b50(\u57f4\u751f\u307f\u3053)\u5168\u526f\u4f5c\u54c1\u6587\u7ae0\u756a\u53f7 \u3010VR\u3011\u64cd\u884c\u65b9\u6b63\u306a\u59b9 \u57f4\u751f\u307f\u3053 CACA-0 \/ 0– \u307e\u3060\u307e\u3060\u672a\u5f00\u767a\u306e\u5973\u5b50\u6821\u751f \u57f4\u751f\u307f\u3053 MDTM- \/ 0– \u30dc\u30af\u3060\u3051\u306e\u3054 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66015],"tags":[],"class_list":["post-136974","post","type-post","status-publish","format-standard","hentry","category-mxx"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/136974","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=136974"}],"version-history":[{"count":0,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/136974\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=136974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=136974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=136974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}