// 从文本域解析图片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":134738,"date":"2021-11-02T13:56:57","date_gmt":"2021-11-02T05:56:57","guid":{"rendered":"https:\/\/www.keaglegz.com\/134738.html"},"modified":"2021-11-02T13:56:57","modified_gmt":"2021-11-02T05:56:57","slug":"%e3%80%902016%e5%b9%b4%e3%80%91%e9%ab%98%e5%8d%83%e7%a9%97%e9%93%83%ef%bc%88%e9%ab%98%e5%8d%83%e7%a9%82%e3%81%99%e3%81%9a%ef%bc%89%e4%b8%aa%e4%ba%ba%e4%bd%9c%e5%93%81%e6%96%87%e7%ab%a0%e5%9b%be","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/134738.html","title":{"rendered":"\u30102016\u5e74\u3011\u9ad8\u5343\u7a57\u94c3\uff08\u9ad8\u5343\u7a42\u3059\u305a\uff09\u4e2a\u4eba\u4f5c\u54c1\u6587\u7ae0\u56fe\u7247\u5c01\u9762\u4ee5\u53ca\u756a\u53f7 \u5ef6\u7eed\u66f4\u65b0"},"content":{"rendered":"\n

\u30102016\u5e74\u3011\u9ad8\u5343\u7a57\u94c3\uff08\u9ad8\u5343\u7a42\u3059\u305a\uff09\u4e2a\u4eba\u4f5c\u54c1\u6587\u7ae0\u56fe\u7247\u5c01\u9762\u4ee5\u53ca\u756a\u53f7 \u5ef6\u7eed\u66f4\u65b0
\n\u30102016\u5e74\u3011\u9ad8\u5343\u7a57\u94c3\uff08\u9ad8\u5343\u7a42\u3059\u305a\uff09\u4e2a\u4eba\u4f5c\u54c1\u6587\u7ae0\u56fe\u7247\u5c01\u9762\u4ee5\u53ca\u756a\u53f7 \u5ef6\u7eed\u66f4\u65b0
\n\u65e5\/\u7727\u7f03AWV\u5973\u7272\u7167\u7247\u5199\u5b9e\u65f6\u9ae6\u56fe\u7247\u5c01\u9762\uff0c\u6700\u65b0\u4f5c\u54c1\u6587\u7ae0\u756a\u53f7\u5927\u5168\u9700\u8981\u65e5\/\u7727\u7f03\u5973\u4eba\u756a\u53f7\u9009\u96c6\u5927\u5168<\/p>\n

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

,\u76f8\u4f3c\u5987\u4eba\u5b66\u751f\u7a7a\u59d0\u4ee5\u53ca\u62a4\u58eb\u7b49\u5404\u65cf\u89d2\u8272\u8868\u6f14\u4e3a\u6b21\u8981\u5f62\u5f0fSNIS-602SNIS-602\u9ad8\u5343\u7a57\u94c3\u53c2\u6f14\u7684\u4f5c\u54c1\u6587\u7ae0\u756a\u53f7SNIS-602\uff0c\u8be5\u7247\uff08\u4ea4\u308f\u308b\u6708\u7ecf\u3001\u6d53\u5bc6\u30bb\u30c3\u30af\u30b9 \u9ad8\u5343\u7a42\u3059\u7ecf\u5e38\u4ece\u4e8b\u4f53\u529b\u52b3\u52a8\u7684\u4eba\u305a\uff09\u65f6\u957f120\u79d2\u949f\uff0c\u672c\u756a\u53f7\u4f5c\u54c1\u6587\u7ae0\u603b\u7ed3\u754c\u8bf4\u4e8e\uff1a\u5973\u4e0a\u5ea7\u3001\u76b1\u7709\u3001 \u6b63\u5f0f\u53d1\u7247\u65e5\u5b50\u662f2<\/p>\n

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

016-02-04\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"

\u30102016\u5e74\u3011\u9ad8\u5343\u7a57\u94c3\uff08\u9ad8\u5343\u7a42\u3059\u305a\uff09\u4e2a\u4eba\u4f5c\u54c1\u6587\u7ae0\u56fe\u7247\u5c01\u9762\u4ee5\u53ca\u756a\u53f7 \u5ef6\u7eed\u66f4\u65b0 \u30102016\u5e74\u3011\u9ad8\u5343\u7a57\u94c3\uff08\u9ad8\u5343\u7a42\u3059\u305a\uff09\u4e2a\u4eba\u4f5c\u54c1\u6587\u7ae0\u56fe\u7247\u5c01\u9762\u4ee5\u53ca\u756a\u53f7 \u5ef6\u7eed\u66f4\u65b0 \u65e5\/\u7727\u7f03AWV\u5973\u7272\u7167\u7247\u5199\u5b9e\u65f6\u9ae6\u56fe\u7247\u5c01\u9762\uff0c\u6700\u65b0\u4f5c\u54c1\u6587\u7ae0\u756a\u53f7\u5927\u5168\u9700\u8981\u65e5\/\u7727\u7f03\u5973\u4eba\u756a\u53f7\u9009\u96c6\u5927\u5168 […]<\/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-134738","post","type-post","status-publish","format-standard","hentry","category-mxx"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/134738","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=134738"}],"version-history":[{"count":0,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/134738\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=134738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=134738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=134738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}