| 1 |
<?php |
| 2 |
/** |
| 3 |
* Feed Manager Class |
| 4 |
* |
| 5 |
* Handles RSS/Atom feed management |
| 6 |
* |
| 7 |
* @package Vigilante |
| 8 |
*/ |
| 9 |
|
| 10 |
// Prevent direct access |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Vigilante_Feed_Manager |
| 17 |
* |
| 18 |
* Manages RSS/Atom feeds |
| 19 |
*/ |
| 20 |
class Vigilante_Feed_Manager { |
| 21 |
|
| 22 |
/** |
| 23 |
* Settings instance |
| 24 |
* |
| 25 |
* @var Vigilante_Settings |
| 26 |
*/ |
| 27 |
private $settings; |
| 28 |
|
| 29 |
/** |
| 30 |
* Feed manager options |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $options; |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructor |
| 38 |
* |
| 39 |
* @param Vigilante_Settings $settings Settings instance. |
| 40 |
*/ |
| 41 |
public function __construct( $settings ) { |
| 42 |
$this->settings = $settings; |
| 43 |
$this->options = $settings->get_section( 'wp_hardening' ); |
| 44 |
|
| 45 |
$this->init_hooks(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Initialize hooks |
| 50 |
*/ |
| 51 |
private function init_hooks() { |
| 52 |
// Completely disable feeds |
| 53 |
if ( ! empty( $this->options['disable_feeds'] ) ) { |
| 54 |
add_action( 'do_feed', array( $this, 'disable_feed' ), 1 ); |
| 55 |
add_action( 'do_feed_rdf', array( $this, 'disable_feed' ), 1 ); |
| 56 |
add_action( 'do_feed_rss', array( $this, 'disable_feed' ), 1 ); |
| 57 |
add_action( 'do_feed_rss2', array( $this, 'disable_feed' ), 1 ); |
| 58 |
add_action( 'do_feed_atom', array( $this, 'disable_feed' ), 1 ); |
| 59 |
|
| 60 |
// Remove feed links from head |
| 61 |
remove_action( 'wp_head', 'feed_links', 2 ); |
| 62 |
remove_action( 'wp_head', 'feed_links_extra', 3 ); |
| 63 |
|
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
// Disable only if no content |
| 68 |
if ( ! empty( $this->options['disable_if_no_content'] ) ) { |
| 69 |
add_action( 'template_redirect', array( $this, 'maybe_disable_feed' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
// Delay feed publication |
| 73 |
if ( ! empty( $this->options['delay_feed_publish'] ) && $this->options['delay_feed_publish'] > 0 ) { |
| 74 |
add_filter( 'posts_where', array( $this, 'delay_feed_posts' ), 10, 2 ); |
| 75 |
} |
| 76 |
|
| 77 |
// Disable comment feeds |
| 78 |
if ( ! empty( $this->options['disable_feed_comments'] ) ) { |
| 79 |
add_action( 'do_feed_rss2_comments', array( $this, 'disable_feed' ), 1 ); |
| 80 |
add_action( 'do_feed_atom_comments', array( $this, 'disable_feed' ), 1 ); |
| 81 |
} |
| 82 |
|
| 83 |
// Remove WordPress version from feeds |
| 84 |
if ( ! empty( $this->options['remove_feed_version'] ) ) { |
| 85 |
add_filter( 'the_generator', '__return_empty_string' ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Disable feed - redirect to homepage |
| 91 |
*/ |
| 92 |
public function disable_feed() { |
| 93 |
wp_safe_redirect( home_url(), 301 ); |
| 94 |
exit; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Disable feeds if no content exists |
| 99 |
*/ |
| 100 |
public function maybe_disable_feed() { |
| 101 |
if ( ! is_feed() ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$post_count = wp_count_posts( 'post' ); |
| 106 |
|
| 107 |
if ( ! $post_count || 0 === (int) $post_count->publish ) { |
| 108 |
wp_safe_redirect( home_url(), 301 ); |
| 109 |
exit; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Delay posts from appearing in feeds |
| 115 |
* |
| 116 |
* @param string $where WHERE clause. |
| 117 |
* @param WP_Query $wp_query Query object. |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
public function delay_feed_posts( $where, $wp_query ) { |
| 121 |
if ( ! $wp_query->is_feed() ) { |
| 122 |
return $where; |
| 123 |
} |
| 124 |
|
| 125 |
global $wpdb; |
| 126 |
|
| 127 |
$minutes = absint( $this->options['delay_feed_publish'] ); |
| 128 |
$delay = gmdate( 'Y-m-d H:i:s', strtotime( "-{$minutes} minutes" ) ); |
| 129 |
|
| 130 |
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date_gmt < %s", $delay ); |
| 131 |
|
| 132 |
return $where; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get feed statistics |
| 137 |
* |
| 138 |
* @return array |
| 139 |
*/ |
| 140 |
public function get_feed_stats() { |
| 141 |
$post_count = wp_count_posts( 'post' ); |
| 142 |
|
| 143 |
return array( |
| 144 |
'published_posts' => $post_count ? (int) $post_count->publish : 0, |
| 145 |
'feeds_disabled' => ! empty( $this->options['disable_feeds'] ), |
| 146 |
'delay_minutes' => $this->options['delay_feed_publish'] ?? 0, |
| 147 |
); |
| 148 |
} |
| 149 |
} |
| 150 |
|