create_log( esc_html__( 'No RSS feed link is configured for this campaign. Edit the campaign and add the feed URL.', 'ai-content-writer' ), 'error', $campaign_id ); return false; } $response = wp_remote_get( $feed_link ); if ( is_wp_error( $response ) ) { ai_content_writer()->create_log( sprintf( /* translators: 1: RSS feed URL. 2: Error message. */ esc_html__( 'Could not fetch the RSS feed %1$s: %2$s', 'ai-content-writer' ), esc_url( $feed_link ), esc_html( $response->get_error_message() ) ), 'error', $campaign_id ); return false; } $response_code = wp_remote_retrieve_response_code( $response ); if ( $response_code < 200 || $response_code >= 300 ) { ai_content_writer()->create_log( sprintf( /* translators: 1: RSS feed URL. 2: HTTP status code. */ esc_html__( 'The RSS feed %1$s returned HTTP status %2$d. Check that the URL is correct and publicly reachable.', 'ai-content-writer' ), esc_url( $feed_link ), absint( $response_code ) ), 'error', $campaign_id ); return false; } $body = wp_remote_retrieve_body( $response ); if ( empty( $body ) ) { ai_content_writer()->create_log( sprintf( /* translators: %s: RSS feed URL. */ esc_html__( 'The RSS feed %s returned an empty response.', 'ai-content-writer' ), esc_url( $feed_link ) ), 'error', $campaign_id ); return false; } // Suppress warnings and manually handle XML errors. $previous_libxml_state = libxml_use_internal_errors( true ); $rss = simplexml_load_string( $body ); if ( ! $rss ) { // Keep the parser error — it is the only clue the user gets about why the feed failed. $xml_errors = libxml_get_errors(); $xml_message = ! empty( $xml_errors ) ? trim( $xml_errors[0]->message ) : ''; libxml_clear_errors(); libxml_use_internal_errors( $previous_libxml_state ); ai_content_writer()->create_log( sprintf( /* translators: 1: RSS feed URL. 2: XML parser error message. */ esc_html__( 'The RSS feed %1$s is not valid XML (%2$s). Make sure the URL points to the feed itself, for example https://example.com/feed, and not to a regular page.', 'ai-content-writer' ), esc_url( $feed_link ), esc_html( '' !== $xml_message ? $xml_message : __( 'no parser details available', 'ai-content-writer' ) ) ), 'error', $campaign_id ); return false; } libxml_clear_errors(); libxml_use_internal_errors( $previous_libxml_state ); if ( ! isset( $rss->channel->item ) || 0 === count( $rss->channel->item ) ) { ai_content_writer()->create_log( sprintf( /* translators: %s: RSS feed URL. */ esc_html__( 'The RSS feed %s contains no items. Only RSS feeds with entries are supported; Atom feeds are not.', 'ai-content-writer' ), esc_url( $feed_link ) ), 'error', $campaign_id ); return false; } $count = 0; foreach ( $rss->channel->item as $item ) { $title = wp_strip_all_tags( (string) $item->title ); $link = esc_url( (string) $item->link ); if ( empty( $title ) || empty( $link ) ) { continue; } // Check if the title already exists. $exists_post_ids = aicw_get_posts_by_title( $title ); $should_skip = false; foreach ( $exists_post_ids as $post_id ) { if ( get_post_meta( $post_id, '_aicw_content_source_link', true ) === $link ) { $should_skip = true; break; } } if ( $should_skip ) { continue; } // Create a new post. $post_id = wp_insert_post( array( 'post_type' => 'aicw_post', 'post_title' => $title, 'post_content' => '', 'post_status' => 'pending', 'post_author' => get_post_field( 'post_author', $campaign_id ), ) ); if ( is_wp_error( $post_id ) ) { continue; } // Add the post meta. update_post_meta( $post_id, '_aicw_campaign_id', $campaign_id ); update_post_meta( $post_id, '_aicw_content_source_link', $link ); $image = self::get_item_image( $item, $link ); if ( '' !== $image ) { update_post_meta( $post_id, '_aicw_source_image', $image ); } // Update the counter. ++$count; // If the counter is greater than or equal to the target then break the loop. if ( $count >= $posts_needed ) { break; } } // Update the last run time. aicw_update_last_run_time( $campaign_id ); // Logged after the loop so a partial run (fewer feed items than the target) // is reported too, not only a run that hit the target exactly. if ( $count > 0 ) { ai_content_writer()->create_log( sprintf( /* translators: %d: Number of posts generated. */ esc_html__( '%d post titles and source links generated from the RSS feed.', 'ai-content-writer' ), absint( $count ) ), 'success', $campaign_id ); return true; } // The feed parsed fine but produced nothing new — normally every item has // already been imported. Logged so the campaign does not look silently broken. ai_content_writer()->create_log( sprintf( /* translators: %s: RSS feed URL. */ esc_html__( 'No new items found in the RSS feed %s. Every item has already been imported.', 'ai-content-writer' ), esc_url( $feed_link ) ), 'info', $campaign_id ); return false; } /** * Generate content. * This function is responsible for generating content based on the source link. * * @param int $post_id The ID of the post for which content is being generated. * @param string $title The title of the post. * @param string $keyword The keyword that will be used to generate the content. * * @since 1.7.0 * @return bool Whether the content was generated successfully or not. */ public static function generate_content( $post_id, $title = '', $keyword = '' ) { return SourceArticle::import( $post_id, $title, $keyword ); } /** * The image a feed item carries, from Media RSS or an image enclosure. * * @param \SimpleXMLElement $item The feed item. * @param string $link The item link, to resolve relative URLs. * * @since 2.6.0 * @return string Image URL, or empty. */ protected static function get_item_image( $item, $link ) { $candidates = array(); $media = $item->children( 'http://search.yahoo.com/mrss/' ); $lists = array( $media->content, $media->thumbnail ); if ( isset( $media->group ) ) { $lists[] = $media->group->children( 'http://search.yahoo.com/mrss/' )->content; } foreach ( $lists as $elements ) { foreach ( $elements as $element ) { $attributes = $element->attributes(); $medium = (string) $attributes['medium']; $type = (string) $attributes['type']; if ( ( '' === $medium || 'image' === $medium ) && ( '' === $type || 0 === strpos( $type, 'image/' ) ) ) { $candidates[] = (string) $attributes['url']; } } } foreach ( $item->enclosure as $enclosure ) { if ( 0 === strpos( (string) $enclosure['type'], 'image/' ) ) { $candidates[] = (string) $enclosure['url']; } } foreach ( $candidates as $candidate ) { $url = '' !== trim( $candidate ) ? SourceArticle::absolute_url( trim( $candidate ), $link ) : ''; if ( '' !== $url ) { return $url; } } return ''; } }