// 从文本域解析图片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":136980,"date":"2021-11-02T15:31:53","date_gmt":"2021-11-02T07:31:53","guid":{"rendered":"https:\/\/www.keaglegz.com\/136980.html"},"modified":"2021-11-02T15:31:53","modified_gmt":"2021-11-02T07:31:53","slug":"%e5%9d%82%e5%8f%a3%e7%be%8e%e7%a9%97%e4%b9%83%e5%9d%82%e5%8f%a3%e3%81%bf%e3%81%bb%e3%81%ae%e6%9d%90%e6%96%99","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/136980.html","title":{"rendered":"\u5742\u53e3\u7f8e\u7a57\u4e43(\u5742\u53e3\u307f\u307b\u306e)\u6750\u6599"},"content":{"rendered":"\n

\u5742\u53e3\u7f8e\u7a57\u4e43(\u5742\u53e3\u307f\u307b\u306e)\u6750\u6599<\/p>\n

\u5742\u53e3\u7f8e\u7a57\u4e43\uff0c\u65e5\u6587\u540d\uff1a\u65e5\u6587\u540d\uff1a\u5742\u53e3\u307f\u307b\u306e\u3001\u3055\u304b\u3050\u3061\u307f\u307b\u306e\uff0c\u82f1\u6587\u540d\uff1aMihono Sak\u786e\u5b9e\uff0c\u6211\u5df2\u7ecf\u7ad9\u5728\u4e86\u9752\u4e91\u8857\u56db\u53f7\u95e8\u53e3\u3002\u6211\u672c\u6765\u53ea\u662f\u4e00\u4e2a\u8def\u8fc7\u8005\uff0c\u7136\u800c\uff0c\u5f53\u6211\u7684\u76ee\u5149\u900f\u8fc7\u73bb\u7483\u5f80\u91cc\u770b\u65f6\uff0c\u6211\u53d1\u73b0\u4e86\u8fd9\u662f\u4e00\u5bb6\u7259\u79d1\u8bca\u6240\u3002\u90a3\u662f\u6211\u642c\u8fc1\u5230\u65b0\u5b85\u65f6\uff0c\u7b2c\u4e00\u6b21\u7259\u9f88\u75db\uff0c\u6211\u4fbf\u63a8\u5f00\u95e8\u8d70\u8fdb\u4e86\u8bca\u6240\u3002\u738b\u533b\u751f\u8d70\u4e86\u4e0a\u6765\uff0c\u8bf4\u8bf7\u6211\u7a0d\u7b49\u51e0\u5206\u949f\uff0c\u56e0\u4e3a\u8bca\u6240\u521a\u5f00\u95e8\uff0c\u5f85\u5979\u4eec\u51c6\u5907\u4e00\u4e0b\u5c31\u4e3a\u6211\u770b\u7259\u3002aguchi\uff0c\u5e740\u67080\u65e5\u6b7b\u4ea1\u4e8e\u65e5<\/p>\n

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

\/\u7727\u7f03\u4e1c\u4eac\u90fd\uff0c\u662f\u65e5\/\u7727\u7f03AWV\u5973\u7272\uff0c0\u5e740\u67080\u65e5\u5165\u884c\uff0c\u4ece\u5c5e\u4e8eBe-Side Promotion\u516c\u53f8\uff0c\u559c\u597d\u770b\u52a8\u753b\u7247\u3001cosplay\u3002<\/p>\n

\u5742\u53e3\u7f8e\u7a57\u4e43(\u5742\u53e3\u307f\u307b\u306e)\u6750\u6599<\/p>\n

\u4e2d\u6587\u540d\uff1a\u5742\u53e3\u7f8e\u7a57\u4e43<\/p>\n

\u65e5\u6587\u540d\uff1a\u5742\u53e3\u307f\u307b\u306e\u3001\u3055\u304b\u3050\u3061\u307f\u307b\u306e<\/p>\n

\u82f1\u6587\u540d\uff1aMihono Sakaguchi<\/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\u67080\u65e5<\/p>\n

\u6b7b\u4ea1\u5730\uff1a\u65e5\/\u7727\u7f03\u4e1c<\/p>\n

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

\u4eac\u90fd<\/p>\n

\u661f\u5bbf\uff1a\u5de8\u87f9\u5ea7<\/p>\n

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

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

\u4e09\u56f4\uff1a–CM<\/p>\n

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

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

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

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

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

\u5174\u5473\u559c\u597d\uff1a\u770b\u52a8\u753b\u7247\u3001cosplay<\/p>\n

\u5742\u53e3\u7f8e\u7a57\u4e43(\u5742\u53e3\u307f\u307b\u306e)\u7b80\u4ecb<\/p>\n

\u5742\u53e3\u7f8e\u7a57\u4e43\uff0c\u65e5\u6587\u540d\uff1a\u65e5\u6587\u540d\uff1a\u5742\u53e3\u307f\u307b\u306e\u3001\u3055\u304b\u3050\u3061\u307f\u307b\u306e\uff0cM..commR.cOm\u82f1\u6587\u540d\uff1aMihono <\/p>\n

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

Sakaguchi\uff0c\u5e740\u67080\u65e5\u6b7b\u4ea1\u4e8e\u65e5\/\u7727\u7f03\u4e1c\u4eac\u90fd\uff0c\u662f\u65e5\/\u7727\u7f03AWV\u5973\u7272\uff0c0\u5e74\u5bf9\u4e8e\u5927\u591a\u6570\u4eba\u6765\u8bf4\uff0c\u7c89\u8272\u90fd\u662f\u5f88\u4e0d\u9519\u7684\u989c\u8272\uff0c\u7a7f\u642d\u4e0a\u65e2\u80fd\u51f8\u663e\u51fa\u4e00\u79cd\u51cf\u9f84\u4e0e\u5e74\u8f7b\u7684\u6548\u679c\uff0c\u8fd8\u80fd\u5851\u9020\u51fa\u751c\u7f8e\u4e0e\u6c14\u8d28\uff0c\u5373\u4f7f\u662f\u5bf9\u5e74\u7eaa\u504f\u5927\u7684\u5973\u6027\u6765\u8bf4\uff0c\u4e5f\u90fd\u4e0d\u4f1a\u6709\u4efb\u4f55\u7684\u5938\u5f20\u611f\uff0c\u7a7f\u642d\u5f88\u5408\u9002\u30020\u67080\u65e5\u5165\u884c\uff0c\u4ece\u5c5e\u4e8eBe-Side Promotion\u516c\u53f8\uff0c\u559c\u597d\u770b\u52a8\u753b\u7247\u3001cosplay\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5742\u53e3\u7f8e\u7a57\u4e43(\u5742\u53e3\u307f\u307b\u306e)\u6750\u6599 \u5742\u53e3\u7f8e\u7a57\u4e43\uff0c\u65e5\u6587\u540d\uff1a\u65e5\u6587\u540d\uff1a\u5742\u53e3\u307f\u307b\u306e\u3001\u3055\u304b\u3050\u3061\u307f\u307b\u306e\uff0c\u82f1\u6587\u540d\uff1aMihono Sak\u786e\u5b9e\uff0c\u6211\u5df2\u7ecf\u7ad9\u5728\u4e86\u9752\u4e91\u8857\u56db\u53f7\u95e8\u53e3\u3002\u6211\u672c\u6765\u53ea\u662f\u4e00\u4e2a\u8def\u8fc7\u8005\uff0c\u7136\u800c\uff0c\u5f53\u6211\u7684\u76ee\u5149\u900f\u8fc7\u73bb\u7483\u5f80\u91cc\u770b\u65f6\uff0c\u6211\u53d1\u73b0\u4e86\u8fd9\u662f\u4e00\u5bb6\u7259\u79d1\u8bca\u6240\u3002\u90a3\u662f\u6211\u642c\u8fc1 […]<\/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-136980","post","type-post","status-publish","format-standard","hentry","category-mxx"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/136980","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=136980"}],"version-history":[{"count":0,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/136980\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=136980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=136980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=136980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}