<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$last_deactivation = get_option( 'gnpub_last_deactivation', 0 );
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$last_activation = get_option( 'gnpub_last_activation', 0 );

/**
 * RSS2 Feed Template for displaying RSS2 Posts feed specifically for Google News Publisher.
 * 
 * This template is based on wp-includes/feed-rss2.php
 */

header( 'Content-Type: ' . feed_content_type( 'rss2' ) . '; charset=' . esc_attr( get_option( 'blog_charset' ) ), true );
$more = 1;

///////////////
// Disable caching @since 1.0.2 -ca
//////////////
header('Expires: Wed, 01 Jan 2014 00:00:00 GMT');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

if ( ! defined('DONOTCACHEPAGE') ) {
	define( 'DONOTCACHEPAGE', true);
}

echo '<?xml version="1.0" encoding="' . esc_attr( get_option( 'blog_charset' ) ) . '"?' . '>';
/**
 * Fires between the xml and rss tags in a feed.
 *
 * @since 4.0.0
 *
 * @param string $context Type of feed. Possible values include 'rss2', 'rss2-comments',
 *                        'rdf', 'atom', and 'atom-comments'.
 */
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
do_action( 'rss_tag_pre', 'rss2' );
?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/" <?php
	/**
	 * Fires at the end of the RSS root to add namespaces.
	 *
	 * @since 2.0.0
	 */
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
	do_action( 'rss2_ns' );
	echo '>';
	?> 

	<channel>
		<title><?php gnpub_wp_title_rss(); ?></title>
		<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
		<link><?php gnpub_feed_channel_link(); ?></link>
		<description><?php gnpub_bloginfo_rss( 'description' ); ?></description>
		<lastBuildDate><?php
			// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			$date = get_lastpostmodified( 'GMT' );
			echo $date ? esc_html( mysql2date( 'D, d M Y H:i:s +0000', $date, false ) ) : esc_html( gmdate( 'r' ) );
		?></lastBuildDate>
		<language><?php bloginfo_rss( 'language' ); ?></language>
		<sy:updatePeriod> 
			<?php 
			// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
			$duration = 'hourly'; echo esc_html( apply_filters( 'rss_update_period', $duration ) );
			?> 
		</sy:updatePeriod>
		<sy:updateFrequency> 
			<?php 
			// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
			$gnpub_frequency = '1'; echo esc_html( apply_filters( 'rss_update_frequency', $gnpub_frequency ) );
			?> 
		</sy:updateFrequency>
		<atom:link rel="hub" href="https://pubsubhubbub.appspot.com/" />
		<generator>GN Publisher v<?php echo esc_html(GNPUB_VERSION);?> https://wordpress.org/plugins/gn-publisher/</generator>
<?php
	$gnpub_feed_support_flag = apply_filters('gnpub_enable_feed_support_filter', 0); // create selected post type feeds
	if($gnpub_feed_support_flag == 0){
		while ( have_posts() ) :
			the_post();
			$post_id = get_the_ID();
			$gnpub_mod_counter = intval( get_post_meta( $post_id, 'gnpub_modified_count', true ) );
			$gnpub_last_modified = get_post_modified_time( 'U', true );
			if ( $gnpub_last_modified > $last_deactivation && $gnpub_last_modified < $last_activation ) {
				$gnpub_mod_counter++;
			}

			if ( $gnpub_mod_counter ) {
				$gnpub_pub_date_object = new DateTime;
				$gnpub_pub_date_object->setTimestamp( get_post_time( 'U', true ) );
				$gnpub_pub_date_object->modify( '+' . $gnpub_mod_counter . ' seconds' );

				$gnpub_pub_date = gmdate( 'D, d M Y H:i:s +0000', $gnpub_pub_date_object->getTimestamp() );
			} else {
				 $gnpub_pub_date = mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false );

			}

			?>

			<item>
				<title><?php gnpub_the_title_rss(); ?></title>
				<link><?php gnpub_feed_post_link(get_the_permalink()); ?></link>
				<pubDate><?php echo esc_attr( $gnpub_pub_date ); ?></pubDate>
				<?php $gnpub_authors_escaped = '<dc:creator><![CDATA['. esc_html( get_the_author() ) .']]></dc:creator>'; ?>
				<?php $gnpub_authors_escaped = apply_filters('gnpub_pp_authors_compat',$gnpub_authors_escaped );
					  $gnpub_authors_escaped = apply_filters('gnpub_molongui_authors_compat',$gnpub_authors_escaped );
					  echo $gnpub_authors_escaped; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --reason: already escaped
				?>
				<guid isPermaLink="false"><?php the_guid() ?></guid>
	<?php 
	$gnpub_content = get_the_content_feed( GNPUB_Feed::FEED_ID );
	$gnpub_content = gnpub_remove_potentially_dangerous_tags($gnpub_content);

	if( function_exists( 'gnpub_pp_translate' ) )
		$gnpub_content = gnpub_pp_translate( $gnpub_content );
	 if ( $gnpub_content && strlen( $gnpub_content ) > 0 ) : 
	?>
				<description><![CDATA[<?php echo wp_trim_words($gnpub_content,15,'...'); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>]]></description>

				<content:encoded><![CDATA[<?php echo $gnpub_content; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>]]></content:encoded>
	<?php 		else : ?>
				<content:encoded><![CDATA[<?php the_excerpt_rss(); ?>]]></content:encoded>
	<?php 		endif; ?>
	<?php 		gnpub_rss_enclosure(); 
			// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
			do_action( 'rss2_item',  $post_id);
	?>
			</item>
	<?php 	endwhile; 
	}
	?>
	</channel>
</rss>
<!-- last GN Pub feeds fetch (not specifically this feed): <?php echo (get_option( 'gnpub_google_last_fetch', null )) ? esc_html(date_i18n( 'Y-m-d H:i:s', get_option( 'gnpub_google_last_fetch' ) )) : esc_html__( 'has not fetched' , 'gn-publisher' ); ?> -->
