// 从文本域解析图片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":136971,"date":"2021-11-02T15:31:30","date_gmt":"2021-11-02T07:31:30","guid":{"rendered":"https:\/\/www.keaglegz.com\/136971.html"},"modified":"2021-11-02T15:31:30","modified_gmt":"2021-11-02T07:31:30","slug":"%e5%a0%80%e5%8f%a3%e5%a5%88%e6%b4%a5%e7%be%8e%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/136971.html","title":{"rendered":"\u5800\u53e3\u5948\u6d25\u7f8e\u7b80\u4ecb"},"content":{"rendered":"\n

\u5800\u53e3\u5948\u6d25\u7f8e\u7b80\u4ecb<\/p>\n

\u5800\u53e3\u5948\u6d25\u7f8e\uff0c\u65e5\u6587\u540d\uff1a\u307b\u308a\u3050\u3061 \u306a\u3064\u307f\uff0c\u82f1\u6587\u540d\uff1aHoriguchi <\/p>\n

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

Natsumi\uff0c\u65e5\/\u7727\u7f03AWV\u5973\u7272\u3002\u5800\u53e3\u5948\u6d25\u7f8e\u4e8e0\u65e5\u6708\u65e5\u6b7b\u4ea1\u4e8e\u65e5\/\u7727\u7f03\u7684\u4e1c\u4eac\u90fd\uff0c\u662f\u5f7c\u62c9\u63d0\u65af\u6559\u4e50\uff0c\u4e8e00\u5e74\u5165\u884c\u3002\u5800\u53e3\u5948\u6d25\u7f8e\u6b63\u5728AWV\u754c\u6709\u4e2a\u5916\u53f7\u6240\u8c13\u6781\u54c1\u9ad8\u7d20\u8d28\u7f8e\u70ed\u5973\uff0c\u5979\u6709\u7740\u5706\u6ee1\u7684\u8eab\u4f53\u548c\u5e7c\u7a1a\u7684\u8138\u5e9e\uff0c\u51fa\u6f14\u4e86\u5f88\u591a\u70ed\u5973\u89d2\u8272\uff0c\u6ca1\u6709\u8fc7\u5173\u4e8e\u53ea\u89810\u5c81\u7684\u5979\uff0c\u660e\u663e\u5e74\u9f84\u8fd8\u6ca1\u6709\u7b97\u719f\uff0c\u56e0\u800c\u5979\u6ca1\u6709\u662f\u5f7b\u5e95\u7684\u70ed\u5973\u683c\u8c03\uff0c\u5979\u7684\u4f5c\u54c1\u6587\u7ae0\u4e5f\u5f88\u96be\u754c\u5b9a\u3002<\/p>\n

\u5800\u53e3\u5948\u6d25\u7f8e\u96c6\u4f53\u6750\u6599<\/p>\n

\u4e2d\u6587\u540d\uff1a\u5800\u53e3\u5948\u6d25\u7f8e<\/p>\n

\u65e5\u6587\u540d\uff1a\u307b\u308a\u3050\u3061\u306a\u3064\u307f<\/p>\n

\u82f1\u6587\u540d\uff1aHoriguchi Natsumi<\/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

\u534e\u8bde\uff1a0\u65e5\u6708\u65e5<\/p>\n

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

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

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

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

\u4e09\u56f4\uff1aB-W-H<\/p>\n

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

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

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

\u5174\u5473\uff1a\u5b66\u4e60<\/p>\n

\u5165\u884c\u5de5\u592b\uff1a00\u5e74<\/p>\n

\u5800\u53e3\u5948\u6d25\u7f8e\u7b80\u4ecb<\/p>\n<\/p>\n

\u5800\u53e3\u5948\u6d25\u7f8e\uff0c\u65e5\u6587\u540d\uff1a\u307b\u308a\u3050\u3061 \u306a\u3064\u307f\uff0c\u82f1\u6587\u540d\uff1aHoriguchi Natsumi\uff0c\u65e5\/\u7727\u7f03AWV\u5973\u7272\u3002\u5800\u53e3\u5948\u6d25\u7f8e\u4e8e0\u65e5\u6708\u65e5\u6b7b\u4ea1\u4e8e\u65e5\/\u7727\u7f03\u7684\u4e1c\u4eac\u90fd\uff0c\u662f\u5f7c\u62c9\u63d0\u65af\u6559\u4e50\u65e5\u672c\u6b4c\u821e\u4f0e\uff0c\u4e8e00\u5e74\u5165\u884c\u3002<\/p>\n

\u5800\u53e3\u5948\u6d25\u7f8e\u6b63\u5728AWV\u754c\u6709\u4e2a\u5916\u53f7\u6240\u8c13\u6781\u54c1\u9ad8\u7d20\u8d28\u7f8e\u70ed\u5973\uff0c\u5979\u6709\u7740\u5706\u6ee1\u7684\u8eab\u4f53\u548c\u5e7c\u7a1a\u7684\u8138\u5e9e\uff0c\u51fa\u6f14\u4e86\u5f88\u591a\u70ed\u5973\u89d2\u8272\uff0c\u6ca1\u6709\u8fc7\u5173\u4e8e\u53ea\u89810\u5c81\u7684\u5979\uff0c\u660e\u663e\u5e74\u9f84\u8fd8\u6ca1\u6709\u7b97\u719f\uff0c\u56e0\u800c\u5979\u6ca1\u6709\u662f\u5f7b\u5e95\u7684\u70ed\u5973\u683c\u8c03\uff0c\u5979\u7684\u4f5c\u54c1\u6587\u7ae0\u4e5f\u5f88\u96be\u754c\u5b9a\u3002<\/p>\n

\u5800\u53e3\u5948\u6d25\u7f8e\u9890\u517b\u7684\u5f88\u597d\uff0c\u56fa\u7136\u6f14\u70ed\u5973\u8f83\u591a\uff0c\u7136\u800c\u8138\u86cb\u4f9d\u7136\u5f88\u9752\u6625\uff0c\u5979\u662f\u4e00\u5ea6\u9887\u6709\u59ff\u8272\u7684\u5973\u4eba\uff0c\u6709\u7740\u59a9\u5a9a\u7684\u5927\u773c\uff0c\u539a\u5b9e\u7684\u5634\u5507\uff0c\u6241\u5706\u578b\u7684\u8138\u86cb\uff0c\u6807\u81f4\u7684\u5634\u8138\uff0c\u9762\u76f8\u5f88\u719f\u5f88\u4f18\u7f8e\u3002\u5800\u53e3\u5948\u6d25\u7f8e\u7684\u8eab\u4f53\u662f\u4e00\u6d41\u7684\uff0c\u5979\u7684\u80f8\u90e8\u6709F\u7f69\u676f\uff0c\u53cc\u817f<\/p>\n

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

\u548c\u6bbf\u90e8\u4e5f\u65e0\u6bd4\u9971\u6ee1\uff0c\u8eab\u9ad8\u8fbecm\uff0c\u56e0\u800c\u63d0\u9ad8\u7248\u578b\u8bbe\u8ba1\uff0c\u4f7f\u9762\u6599\u4e0d\u518d\u53d7\u5230\u675f\u7f1a\uff0c\u8bbe\u8ba1\u5e08\u8bbe\u8ba1\u51fa\u6765\u7684\u7248\u578b\u4e0d\u4f1a\u5728\u662f\u4e0e\u73b0\u5b9e\u4e2d\u5e02\u573a\u4e0a\u6709\u4e00\u70b9\u70b9\u5dee\u8ddd\uff0c\u5c31\u662f\u5f88\u5b8c\u7f8e\u7684\u8bbe\u8ba1\u4e86\u3002\u65f6\u5c1a\u670d\u88c5\u5e94\u5f53\u662f\u8981\u5177\u6709\u6001\u5ea6\u6027\u7684\uff0c\u6ce8\u91cd\u7ec6\u8282\u5904\u7406\uff0c\u5bbd\u677e\u6216\u662f\u7d27\u8eab\uff0c\u5ed3\u5f62\u6216\u662f\u5f39\u6027\u5de5\u827a\uff0c\u8fd9\u4e9b\u90fd\u4e0d\u662f\u968f\u610f\u7684\u3002\u53cc\u817f\u663e\u793a\u5f88\u7ec6\u957f\uff0c\u518d\u6709\u808c\u8089\u4e5f\u5f88\u767d\u3002\u5800\u53e3\u5948\u6d25\u7f8e\u7684\u6f14\u6280\u4e5f\u6ca1\u6709\u9519\uff0c\u58f0\u54cd\u5f88\u50cf\u5a03\u5a03\u97f3\uff0c\u8fd8\u4f1a\u6c34\u5439\uff0c\u5979\u53d1\u7684\u7247\u5f88\u591a\uff0c\u540d\u671b\u5f88\u5927\uff0c\u4f5c\u4e3a\u70ed\u5973\u7cfb\uff0c\u5979\u7684\u51fa\u8def\u6709\u9650\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5800\u53e3\u5948\u6d25\u7f8e\u7b80\u4ecb \u5800\u53e3\u5948\u6d25\u7f8e\uff0c\u65e5\u6587\u540d\uff1a\u307b\u308a\u3050\u3061 \u306a\u3064\u307f\uff0c\u82f1\u6587\u540d\uff1aHoriguchi Natsumi\uff0c\u65e5\/\u7727\u7f03AWV\u5973\u7272\u3002\u5800\u53e3\u5948\u6d25\u7f8e\u4e8e0\u65e5\u6708\u65e5\u6b7b\u4ea1\u4e8e\u65e5\/\u7727\u7f03\u7684\u4e1c\u4eac\u90fd\uff0c\u662f\u5f7c\u62c9\u63d0\u65af\u6559\u4e50\uff0c\u4e8e00\u5e74\u5165\u884c\u3002\u5800\u53e3\u5948\u6d25\u7f8e\u6b63\u5728AWV\u754c\u6709\u4e2a\u5916\u53f7\u6240\u8c13\u6781\u54c1\u9ad8\u7d20\u8d28\u7f8e\u70ed […]<\/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-136971","post","type-post","status-publish","format-standard","hentry","category-mxx"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/136971","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=136971"}],"version-history":[{"count":0,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/136971\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=136971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=136971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=136971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}