// 从文本域解析图片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 = '
\u5f20\u5f3c\u662f\u660e\u4ee3\u5f88\u6709\u624d\u6c14\u7684\u8bd7\u4eba\uff0c\u81f3\u4eca\u6d41\u4f20\u7684\u8bd7\u8bcd\u5927\u7ea6\u67098\u9996\uff0c \u5f20\u5f3c\u7684\u8bd7\u4ee3\u8868\u5f88\u591a\uff0c\u4eca\u5929\u5206\u4eab\u4e00\u4e9b\u51fa\u8272\u7684\u5f20\u5f3c\u7684\u8bd7\u5927\u5168\u3002<\/p>\n
\u91d1\u9497\u5b9d\u94bf\u56f4\u73e0\u7fe0\uff0c\u773c\u5e95\u4f55\u4eba\u8fa8\u771f\u4f2a\u3002
\u592d\u6843\u7a97\u4e0b\u6765\u6625\u98ce\uff0c\u5047\u9afb\u7f8e\u4eba\u5f52\u4e0a\u516c\u3002<\/p>\n
\u8718\u86db\u58f0\u5bc2\u5bc2\uff0c\u5410\u4e1d\u590d\u81ea\u7ec7\u3002
\u7ec7\u7f51\u7f51\u98de\u866b\uff0c\u98de\u866b\u8db3\u5145\u98df\u3002
\u4e8b\u5728\u529b\u4e3a\u4e0d\u5728\u58f0\uff0c\u601d\u4e4b\u4ee4\u4eba\u4e09\u53f9\u606f\u3002<\/p>\n
\u7eff\u7a97\u5b64\u7720\u5982\u7389\u4eba\uff0c\u95fb\u9053\u6625\u5f52\u672a\u8bc6\u6625\u3002
\u6b32\u8d77\u63a8\u7a97\u770b\u82b1\u9e1f\uff0c\u8fd8\u5c06\u7f57\u6247\u969c\u6d41\u5c18\u3002<\/p>\n