// 从文本域解析图片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":136976,"date":"2021-11-02T15:31:42","date_gmt":"2021-11-02T07:31:42","guid":{"rendered":"https:\/\/www.keaglegz.com\/136976.html"},"modified":"2021-11-02T15:31:42","modified_gmt":"2021-11-02T07:31:42","slug":"%e5%9f%8e%e5%b4%8e%e6%a1%90%e5%ad%90%e6%9d%90%e6%96%99","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/136976.html","title":{"rendered":"\u57ce\u5d0e\u6850\u5b50\u6750\u6599"},"content":{"rendered":"\n

\u98df\u7528\u7389\u7c73\u8fd8\u6709\u5ef6\u7f13\u8870\u8001\u7684\u4f5c\u7528\uff0c\u6bd4\u8f83\u9002\u5408\u5973\u6027\uff0c\u5f88\u591a\u7684\u5973\u6027\u90fd\u662f\u4e0d\u65ad\u7684\u5728\u8ffd\u6c42\u5e74\u8f7b\uff0c\u60f3\u8981\u4fdd\u6301\u5e74\u8f7b\uff0c\u5ef6\u7f13\u8870\u8001\uff0c\u90a3\u5c31\u53ef\u4ee5\u98df\u7528\u70b9\u7389\u7c73\uff0c\u7389\u7c73\u4e2d\u7684\u8425\u517b\u4ef7\u503c\u662f\u975e\u5e38\u7684\u4e30\u5bcc\uff0c\u5176\u7ef4\u751f\u7d20E\u662f\u6709\u5ef6\u7f13\u8870\u8001\u7684\u4f5c\u7528\uff0c\u80fd\u591f\u9632\u6b62\u76ae\u80a4\u75c5\u53d8\uff0c\u51cf\u8f7b\u52a8\u8109\u786c\u5316\u548c\u8111\u529f\u80fd\u8870\u9000\u7684\u60c5\u51b5\uff0c\u8fdb\u5165\u53ef\u4ee5\u8d77\u5230\u5ef6\u7f13\u8870\u8001\u7684\u6548\u679c\u3002
\n\u57ce\u5d0e\u6850\u5b50\u6750\u6599<\/p>\n

\u57ce\u5d0e\u6850\u5b50\uff0c\u65e5<\/p>\n

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

\u6587\u540d\uff1a\u3057\u308d\u3055\u304d\u3068\u3046\u3053\uff0c\u82f1\u6587\u540d\uff1aShirosaki Touko\uff0c\u5e740\u6708\u65e5\u6b7b\u4ea1\u4e8e\u65e5\/\u7727\u7f03\u795e\u5948\u5ddd\u53bf\uff0c\u662f\u65e5\/\u7727\u7f03AWV\u5973\u7272\uff0c0\u65e5\u67080\u65e5\u5165\u884c\uff0c\u4ece\u5c5e\u4e8eBardot Model Agency\u516c\u53f8\uff0c\u559c\u597d\u8d2d\u7269\u3001\u5361\u62c9OK\u3001\u9ad8\u5c14\u592b\u3001\u6ed1\u96ea\u677f\u3001\u98ce\u7434\u3002<\/p>\n

\u57ce\u5d0e\u6850\u5b50\u6750\u6599<\/p>\n

\u4e2d\u6587\u540d\uff1a\u57ce\u5d0e\u6850\u5b50<\/p>\n

\u65e5\u6587\u540d\uff1a\u3057\u308d\u3055\u304d\u3068\u3046\u3053<\/p>\n

\u82f1\u6587\u540d\uff1aShirosaki <\/p>\n

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

Touko<\/p>\n

\u56fd\u522b\uff1a\u5973<\/p>\n

\u4eba\u79cd\uff1a\u5927\u548c\u4eba\u79cd<\/p>\n

\u515a\u7c4d\uff1a\u65e5\/\u7727\u7f03<\/p>\n

\u51fa\u534e\u8bde\u671f\uff1a\u5e740\u6708\u65e5<\/p>\n

\u6b7b\u4ea1\u5730\uff1a\u65e5\/\u7727\u7f03\u795e\u5948\u5ddd\u53bf<\/p>\n

\u661f\u5bbf\uff1a\u53cc\u5b50\u5ea7<\/p>\n

\u4f53\u91cd\uff1akg<\/p>\n

\u8eab\u9ad8\uff1acm<\/p>\n

\u4e09\u56f4\uff1a-0-0CM<\/p>\n

\u7f69\u676f\uff1aC\u676f<\/p>\n

\u97f3\u578b\uff1aA\u578b<\/p>\n

\u804c\u4e1a\uff1a\u65e5\/\u7727\u7f03AWV\u5973\u7272<\/p>\n

\u5165\u884c\u5de5\u592b\uff1a0\u65e5\u67080\u65e5<\/p>\n

\u5f15\u9000\u5de5\u592b\uff1a\u8fc4\u4eca<\/p>\n<\/p>\n

\u5174\u5473\u559c\u597d\uff1a\u8d2d\u7269\u3001\u5361\u62c9OK\u3001\u9ad8\u5c14\u592b\u3001\u6ed1\u96ea\u677f\u3001\u98ce\u7434<\/p>\n

\u57ce\u5d0e\u6850\u5b50\u7b80\u4ecb<\/p>\n

\u57ce\u5d0e\u6850\u5b50\uff0c\u65e5\u6587\u540d\uff1a\u3057\u308d\u3055\u304d\u3068\u3046\u3053\uff0c\u82f1\u6587\u540d\uff1aShirosaki Touko\uff0c\u5e740\u6708\u65e5\u6b7b\u4ea1\u4e8e\u65e5\/\u7727\u7f03\u795e\u5948\u5ddd\u53bf\uff0c\u662f\u65e5\/\u7727\u7f03AWV\u5973\u7272\u7c89\u8272\u886c\u886b+\u9ec4\u8272\u62fc\u63a5\u534a\u8eab\u88d9\uff0c0\u65e5\u67080\u65e5\u5165\u884c\uff0c\u4ece\u5c5e\u4e8eBardot Model Agency\u516c\u53f8\uff0c\u559c\u597d\u8d2d\u7269\u3001\u5361\u62c9OK\u3001\u9ad8\u5c14\u592b\u3001\u6ed1\u96ea\u677f\u3001\u98ce\u7434\u3002\u57ce\u5d0e\u6850\u5b50\u6765\u81ea\u4e00\u5ea6\u7ba1\u675f\u65e0\u6bd4\u4e25\u5389\u7684\u5bb6\u65cf\uff0c\u6709\u5982\u6696\u623f\u91cc\u6735\u513f\u7684\u5979\u5207\u9898\u6765\u8bf4\u5c31\u662f\u8981\u6309\u7167\u53cc\u4eb2\u7684\u610f\u4e49\u597d<\/p>\n

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

\u597d\u8fdb\u4fee\u800c\u540e\u627e\u4e2a\u574f\u4eba\u5bb6\u5ac1\u4e86\uff0c\u4f46\u5979\u660e\u663e\u6ca1\u6709\u7518\u613f\u4eba\u751f\u88ab\u6a7e\u63a7\uff0c\u6ca1\u6709\u4ec5\u6b63\u5728\u8bfb\u5927\u5b66\u7684\u65f6\u5206\u548c\u53cc\u4eb2\u62e5\u62a4\u7684\u7537\u751f\u6765\u5f80\u3001\u6700\u540e\u66f4\u5ac1\u7ed9\u4e86\u4e00\u5ea6\u6bd4\u672c\u4eba\u59270\u5c81\u7684\u9152\u5427\u8001\u677f\uff0c\u540e\u6765\u66f4\u662f\u5927\u9006\u6ca1\u6709\u9053\u7684\u53d6\u820d\u4e86\u53cd\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u98df\u7528\u7389\u7c73\u8fd8\u6709\u5ef6\u7f13\u8870\u8001\u7684\u4f5c\u7528\uff0c\u6bd4\u8f83\u9002\u5408\u5973\u6027\uff0c\u5f88\u591a\u7684\u5973\u6027\u90fd\u662f\u4e0d\u65ad\u7684\u5728\u8ffd\u6c42\u5e74\u8f7b\uff0c\u60f3\u8981\u4fdd\u6301\u5e74\u8f7b\uff0c\u5ef6\u7f13\u8870\u8001\uff0c\u90a3\u5c31\u53ef\u4ee5\u98df\u7528\u70b9\u7389\u7c73\uff0c\u7389\u7c73\u4e2d\u7684\u8425\u517b\u4ef7\u503c\u662f\u975e\u5e38\u7684\u4e30\u5bcc\uff0c\u5176\u7ef4\u751f\u7d20E\u662f\u6709\u5ef6\u7f13\u8870\u8001\u7684\u4f5c\u7528\uff0c\u80fd\u591f\u9632\u6b62\u76ae\u80a4\u75c5\u53d8\uff0c\u51cf\u8f7b\u52a8\u8109\u786c\u5316\u548c\u8111\u529f\u80fd\u8870\u9000\u7684\u60c5\u51b5\uff0c\u8fdb\u5165\u53ef\u4ee5\u8d77\u5230 […]<\/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-136976","post","type-post","status-publish","format-standard","hentry","category-mxx"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/136976","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=136976"}],"version-history":[{"count":0,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/136976\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=136976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=136976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=136976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}