get_configured_subreddits(); $limit = $config['limit'] ?? 25; $time_filter = $config['time_filter'] ?? 'day'; $include_comments = $config['include_comments'] ?? true; $comments_limit = $config['comments_per_post'] ?? 5; $client = new PRAutoBlogger_Reddit_JSON_Client(); $collected = array(); PRAutoBlogger_Logger::instance()->info( sprintf( 'Reddit collect_data: %d subreddits=%s, limit=%d, time=%s', count( $subreddits ), wp_json_encode( $subreddits ), $limit, $time_filter ), 'reddit' ); foreach ( $subreddits as $subreddit ) { $subreddit = sanitize_text_field( $subreddit ); try { $before_count = count( $collected ); // Fetch posts — client tries RSS first, .json fallback internally. $posts = $client->fetch_posts( $subreddit, $limit, $time_filter ); foreach ( $posts as $post ) { $data_source = $post['data_source'] ?? 'reddit'; $collected[] = new PRAutoBlogger_Source_Data( array( 'source_type' => 'reddit', 'source_id' => 't3_' . ( $post['id'] ?? '' ), 'subreddit' => $subreddit, 'title' => $post['title'] ?? '', 'content' => $post['selftext'] ?? '', 'author' => $post['author'] ?? '[deleted]', 'score' => (int) ( $post['score'] ?? 0 ), 'comment_count' => (int) ( $post['num_comments'] ?? 0 ), 'permalink' => 'https://reddit.com' . ( $post['permalink'] ?? '' ), 'collected_at' => current_time( 'mysql' ), 'metadata' => array( 'post_type' => ( $post['is_self'] ?? false ) ? 'self' : 'link', 'flair' => $post['link_flair_text'] ?? null, 'created_utc' => $post['created_utc'] ?? null, 'upvote_ratio' => $post['upvote_ratio'] ?? null, 'is_original' => $post['is_original_content'] ?? false, 'data_source' => $data_source, ), ) ); // Fetch comments for high-engagement posts via .json // (comments are not available in RSS feeds). if ( $include_comments && ( $post['num_comments'] ?? 0 ) > 0 ) { $post_id = $post['id'] ?? ''; $comments = $client->fetch_comments( $subreddit, $post_id, $comments_limit ); foreach ( $comments as $comment ) { $collected[] = new PRAutoBlogger_Source_Data( array( 'source_type' => 'reddit', 'source_id' => 't1_' . ( $comment['id'] ?? '' ), 'subreddit' => $subreddit, 'title' => null, 'content' => $comment['body'] ?? '', 'author' => $comment['author'] ?? '[deleted]', 'score' => (int) ( $comment['score'] ?? 0 ), 'comment_count' => 0, 'permalink' => 'https://reddit.com' . ( $comment['permalink'] ?? '' ), 'collected_at' => current_time( 'mysql' ), 'metadata' => array( 'parent_post_id' => 't3_' . $post_id, 'parent_title' => $post['title'] ?? '', 'is_comment' => true, 'created_utc' => $comment['created_utc'] ?? null, 'data_source' => 'reddit_json', ), ) ); } } } $added = count( $collected ) - $before_count; PRAutoBlogger_Logger::instance()->info( sprintf( 'Reddit r/%s: fetched %d items', $subreddit, $added ), 'reddit' ); } catch ( \Throwable $e ) { PRAutoBlogger_Logger::instance()->error( sprintf( 'Reddit: %s collecting from r/%s: %s', get_class( $e ), $subreddit, $e->getMessage() ), 'reddit' ); } } return $collected; } /** * @return string */ public function get_source_type(): string { return 'reddit'; } /** * Validate that at least one data source is reachable. * * No credentials needed — RSS and .json are both unauthenticated. * We just check if either endpoint is responding. * * Side effects: HTTP requests to Reddit. * * @return bool True if at least one data source is available. */ public function validate_credentials(): bool { $client = new PRAutoBlogger_Reddit_JSON_Client(); return $client->is_available(); } /** * Get current rate limit status from the Reddit client. * * @return array{remaining: int, limit: int, resets_at: string} */ public function get_rate_limit_status(): array { $client = new PRAutoBlogger_Reddit_JSON_Client(); return $client->get_rate_limit_status(); } /** * Get the list of subreddits configured in plugin settings. * * @return string[] */ private function get_configured_subreddits(): array { $json = get_option( 'prautoblogger_target_subreddits', '' ); $list = json_decode( $json, true ); return is_array( $list ) ? $list : array(); } }