Skip to content

Instantly share code, notes, and snippets.

@foozlereducer
Last active September 11, 2017 18:12
Show Gist options
  • Save foozlereducer/9f21024ce3c2de1acd3004e83de618bf to your computer and use it in GitHub Desktop.
Save foozlereducer/9f21024ce3c2de1acd3004e83de618bf to your computer and use it in GitHub Desktop.
The abstract feed class
<?php
namespace Postmedia\Web;
abstract class Feed {
/**
* Constructor
* @return object instance
*/
public function __construct() {
add_action( 'pre_get_posts', array( $this, 'remove_sponsored_posts_from_feed' ), 1 );
}
/**
* Remove sponsored posts from feeds.
*/
public function remove_sponsored_posts_from_feed( $query ) {
if ( ! is_feed() ) {
return null;
}
if ( $query->is_main_query() ) {
$query->set( 'post_type', 'post' );
$query->set(
'meta_query', array(
'relation' => 'AND',
array(
'key' => 'advertorial_meta_box',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'pn_adv_sponsor_name',
'compare' => 'NOT EXISTS',
),
)
);
}
}
/**
* This Feed class allows a developer to use this default Feed header.
* When inheriting a developer can override the get_custom_feed_base_header() method to
* provide new content types, and namespaces.
* @return string
*/
public function get_custom_feed_base_header() {
header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . get_option( 'blog_charset' ), true );
$feed_header = '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>' . "\n\n";
$feed_header .= '<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/"
>' . "\n\n";
return $feed_header;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment