rss-footer-embed.php
5.22 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Yoast\WP\SEO\Integrations\Front_End;
use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
use Yoast\WP\SEO\Helpers\Options_Helper;
use Yoast\WP\SEO\Integrations\Integration_Interface;
/**
* Class RSS_Footer_Embed.
*/
class RSS_Footer_Embed implements Integration_Interface {
/**
* The options helper.
*
* @var Options_Helper
*/
protected $options;
/**
* Returns the conditionals based in which this loadable should be active.
*
* @return array
*/
public static function get_conditionals() {
return [ Front_End_Conditional::class ];
}
/**
* Sets the required helpers.
*
* @codeCoverageIgnore It only handles dependencies.
*
* @param Options_Helper $options The options helper.
*/
public function __construct( Options_Helper $options ) {
$this->options = $options;
}
/**
* Initializes the integration.
*
* This is the place to register hooks and filters.
*
* @return void
*/
public function register_hooks() {
\add_filter( 'the_content_feed', [ $this, 'embed_rssfooter' ] );
\add_filter( 'the_excerpt_rss', [ $this, 'embed_rssfooter_excerpt' ] );
}
/**
* Adds the RSS footer (or header) to the full RSS feed item.
*
* @param string $content Feed item content.
*
* @return string
*/
public function embed_rssfooter( $content ) {
if ( ! $this->include_rss_footer( 'full' ) ) {
return $content;
}
return $this->embed_rss( $content );
}
/**
* Adds the RSS footer (or header) to the excerpt RSS feed item.
*
* @param string $content Feed item excerpt.
*
* @return string
*/
public function embed_rssfooter_excerpt( $content ) {
if ( ! $this->include_rss_footer( 'excerpt' ) ) {
return $content;
}
return $this->embed_rss( \wpautop( $content ) );
}
/**
* Checks if the RSS footer should included.
*
* @param string $context The context of the RSS content.
*
* @return bool Whether or not the RSS footer should included.
*/
protected function include_rss_footer( $context ) {
if ( ! \is_feed() ) {
return false;
}
/**
* Filter: 'wpseo_include_rss_footer' - Allow the RSS footer to be dynamically shown/hidden.
*
* @api boolean $show_embed Indicates if the RSS footer should be shown or not.
*
* @param string $context The context of the RSS content - 'full' or 'excerpt'.
*/
if ( ! \apply_filters( 'wpseo_include_rss_footer', true, $context ) ) {
return false;
}
return $this->is_configured();
}
/**
* Checks if the RSS feed fields are configured.
*
* @return bool True when one of the fields has a value.
*/
protected function is_configured() {
return ( $this->options->get( 'rssbefore', '' ) !== '' || $this->options->get( 'rssafter', '' ) );
}
/**
* Adds the RSS footer and/or header to an RSS feed item.
*
* @param string $content Feed item content.
*
* @return string The content to add.
*/
protected function embed_rss( $content ) {
$before = $this->rss_replace_vars( $this->options->get( 'rssbefore', '' ) );
$after = $this->rss_replace_vars( $this->options->get( 'rssafter', '' ) );
$content = $before . $content . $after;
return $content;
}
/**
* Replaces the possible RSS variables with their actual values.
*
* @param string $content The RSS content that should have the variables replaced.
*
* @return string
*/
protected function rss_replace_vars( $content ) {
if ( $content === '' ) {
return $content;
}
$replace_vars = $this->get_replace_vars( $this->get_link_template(), \get_post() );
$content = \stripslashes( \trim( $content ) );
$content = \str_ireplace( \array_keys( $replace_vars ), \array_values( $replace_vars ), $content );
return \wpautop( $content );
}
/**
* Retrieves the replacement variables.
*
* @codeCoverageIgnore It just contains too much WordPress functions.
*
* @param string $link_template The link template.
* @param mixed $post The post to use.
*
* @return array The replacement variables.
*/
protected function get_replace_vars( $link_template, $post ) {
$author_link = '';
if ( \is_object( $post ) ) {
$author_link = \sprintf( $link_template, \esc_url( \get_author_posts_url( $post->post_author ) ), \esc_html( \get_the_author() ) );
}
return [
'%%AUTHORLINK%%' => $author_link,
'%%POSTLINK%%' => \sprintf( $link_template, \esc_url( \get_permalink() ), \esc_html( \get_the_title() ) ),
'%%BLOGLINK%%' => \sprintf( $link_template, \esc_url( \get_bloginfo( 'url' ) ), \esc_html( \get_bloginfo( 'name' ) ) ),
'%%BLOGDESCLINK%%' => \sprintf( $link_template, \esc_url( \get_bloginfo( 'url' ) ), \esc_html( \get_bloginfo( 'name' ) ) . ' - ' . \esc_html( \get_bloginfo( 'description' ) ) ),
];
}
/**
* Retrieves the link template.
*
* @return string The link template.
*/
protected function get_link_template() {
/**
* Filter: 'nofollow_rss_links' - Allow the developer to determine whether or not to follow the links in
* the bits Yoast SEO adds to the RSS feed, defaults to false.
*
* @api bool $unsigned Whether or not to follow the links in RSS feed, defaults to true.
*
* @since 1.4.20
*/
if ( \apply_filters( 'nofollow_rss_links', false ) ) {
return '<a rel="nofollow" href="%1$s">%2$s</a>';
}
return '<a href="%1$s">%2$s</a>';
}
}