-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpu_better_rss.php
166 lines (143 loc) · 6.11 KB
/
wpu_better_rss.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
defined('ABSPATH') || die;
/*
Plugin Name: WPU Better RSS
Plugin URI: https://github.com/WordPressUtilities/wpu_better_rss
Update URI: https://github.com/WordPressUtilities/wpu_better_rss
Description: Better RSS feeds
Version: 0.4.0
Author: Darklg
Author URI: https://darklg.me/
Text Domain: wpu_better_rss
Domain Path: /lang
Requires at least: 6.2
Requires PHP: 8.0
Network: Optional
License: MIT License
License URI: https://opensource.org/licenses/MIT
*/
class WPUBetterRSS {
private $plugin_version = '0.4.0';
private $plugin_settings = array(
'id' => 'wpu_better_rss',
'name' => 'WPU Better RSS'
);
private $basetoolbox;
private $plugin_description;
public function __construct() {
add_action('plugins_loaded', array(&$this, 'load_dependencies'));
add_action('init', array(&$this, 'load_translation'));
add_action('plugins_loaded', array(&$this, 'rss_features'));
}
# Translations
public function load_translation(){
$lang_dir = dirname(plugin_basename(__FILE__)) . '/lang/';
if (strpos(__DIR__, 'mu-plugins') !== false) {
load_muplugin_textdomain('wpu_better_rss', $lang_dir);
} else {
load_plugin_textdomain('wpu_better_rss', false, $lang_dir);
}
$this->plugin_description = __('Better RSS feeds', 'wpu_better_rss');
}
public function load_dependencies() {
# TOOLBOX
require_once __DIR__ . '/inc/WPUBaseToolbox/WPUBaseToolbox.php';
$this->basetoolbox = new \wpu_better_rss\WPUBaseToolbox(array(
'need_form_js' => false
));
}
/* ----------------------------------------------------------
Features
---------------------------------------------------------- */
public function rss_features() {
$hook_content = get_option('rss_use_excerpt') ? 'the_excerpt_rss' : 'the_content_feed';
if (apply_filters('wpu_better_rss__display_thumbnail_before_content__enable', false)) {
add_filter($hook_content, array(&$this, 'display_thumbnail_before_content'), 20, 1);
}
if (apply_filters('wpu_better_rss__add_enclosure_field__enable', false)) {
add_action('rss2_item', array(&$this, 'add_enclosure_field'));
}
if (apply_filters('wpu_better_rss__force_sitename_as_author__enable', false)) {
add_filter('the_author', array(&$this, 'force_sitename_as_author'), 40);
}
if (apply_filters('wpu_better_rss__add_copyright_in_feed__enable', false)) {
add_filter($hook_content, array(&$this, 'add_copyright_in_feed'), 50, 1);
}
if (apply_filters('wpu_better_rss__add_tracking_to_links__enable', true)) {
add_filter('the_permalink_rss', array(&$this, 'add_tracking_to_links'));
}
}
/* ----------------------------------------------------------
Display thumb before content
---------------------------------------------------------- */
public function display_thumbnail_before_content($text) {
add_filter('max_srcset_image_width', function () {
return 1;
});
$thumbnail = '';
$thumbnail_size = apply_filters('wpu_better_rss__display_thumbnail_before_content__thumbnail_size', 'medium');
if (get_post_type() == 'post') {
$image = wp_get_attachment_image(get_post_thumbnail_id(get_the_ID()), $thumbnail_size);
$thumbnail = wpautop($image);
}
return $thumbnail . $text;
}
/* ----------------------------------------------------------
Add tracking to links
---------------------------------------------------------- */
public function add_tracking_to_links($permalink) {
$utm_params = array();
if (apply_filters('wpu_better_rss__add_tracking_to_links__enable', false)) {
$utm_params = array(
'utm_source' => 'rss',
'utm_medium' => 'rss',
'utm_campaign' => 'rss_feed_campaign'
);
}
return add_query_arg($utm_params, $permalink);
}
/* ----------------------------------------------------------
Display enclosure field
Thanks to https://github.com/kasparsd/feed-image-enclosure/blob/master/feed-image-enclosure.php
---------------------------------------------------------- */
public function add_enclosure_field() {
if (!has_post_thumbnail()) {
return;
}
$thumbnail_id = get_post_thumbnail_id(get_the_ID());
$thumbnail = image_get_intermediate_size($thumbnail_id, 'medium');
if (empty($thumbnail)) {
return;
}
$filepath = get_attached_file($thumbnail_id);
$filesize = 0;
if (file_exists($filepath)) {
$filesize = filesize($filepath);
}
printf(
'<enclosure url="%s" length="%s" type="%s" />',
$thumbnail['url'],
$filesize,
get_post_mime_type($thumbnail_id)
);
}
/* ----------------------------------------------------------
Force sitename as author
---------------------------------------------------------- */
public function force_sitename_as_author($display_name) {
return is_feed() ? get_bloginfo('name') : $display_name;
}
/* ----------------------------------------------------------
Add copyright in feed
---------------------------------------------------------- */
public function add_copyright_in_feed($content) {
$copyright_before = apply_filters('wpu_better_rss__add_copyright_in_feed__before_content', '<hr /><p>');
$copyright_after = apply_filters('wpu_better_rss__add_copyright_in_feed__after_content', '</p>');
$copyright_parts = apply_filters('wpu_better_rss__add_copyright_in_feed__copyright_parts', array(
'copy' => '© ' . date('Y') . ' ' . get_bloginfo('name'),
'title' => '<a href="' . $this->add_tracking_to_links(get_permalink()) . '">' . get_the_title() . '</a>'
));
return $content . $copyright_before . implode(' - ', $copyright_parts) . $copyright_after;
}
}
$WPUBetterRSS = new WPUBetterRSS();