PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.1
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / front-end / rss-footer-embed.php

rss-footer-embed.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.1, at src/integrations/front-end/rss-footer-embed.php

198 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Integrations\Front_End;
4
5 use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
6 use Yoast\WP\SEO\Helpers\Options_Helper;
7 use Yoast\WP\SEO\Integrations\Integration_Interface;
8
9 /**
10 * Class RSS_Footer_Embed.
11 */
12 class RSS_Footer_Embed implements Integration_Interface {
13
14 /**
15 * The options helper.
16 *
17 * @var Options_Helper
18 */
19 protected $options;
20
21 /**
22 * Returns the conditionals based in which this loadable should be active.
23 *
24 * @return array
25 */
26 public static function get_conditionals() {
27 return [ Front_End_Conditional::class ];
28 }
29
30 /**
31 * Sets the required helpers.
32 *
33 * @codeCoverageIgnore It only handles dependencies.
34 *
35 * @param Options_Helper $options The options helper.
36 */
37 public function __construct( Options_Helper $options ) {
38 $this->options = $options;
39 }
40
41 /**
42 * Initializes the integration.
43 *
44 * This is the place to register hooks and filters.
45 *
46 * @return void
47 */
48 public function register_hooks() {
49 \add_filter( 'the_content_feed', [ $this, 'embed_rssfooter' ] );
50 \add_filter( 'the_excerpt_rss', [ $this, 'embed_rssfooter_excerpt' ] );
51 }
52
53 /**
54 * Adds the RSS footer (or header) to the full RSS feed item.
55 *
56 * @param string $content Feed item content.
57 *
58 * @return string
59 */
60 public function embed_rssfooter( $content ) {
61 if ( ! $this->include_rss_footer( 'full' ) ) {
62 return $content;
63 }
64
65 return $this->embed_rss( $content );
66 }
67
68 /**
69 * Adds the RSS footer (or header) to the excerpt RSS feed item.
70 *
71 * @param string $content Feed item excerpt.
72 *
73 * @return string
74 */
75 public function embed_rssfooter_excerpt( $content ) {
76 if ( ! $this->include_rss_footer( 'excerpt' ) ) {
77 return $content;
78 }
79
80 return $this->embed_rss( \wpautop( $content ) );
81 }
82
83 /**
84 * Checks if the RSS footer should included.
85 *
86 * @param string $context The context of the RSS content.
87 *
88 * @return bool Whether or not the RSS footer should included.
89 */
90 protected function include_rss_footer( $context ) {
91 if ( ! \is_feed() ) {
92 return false;
93 }
94
95 /**
96 * Filter: 'wpseo_include_rss_footer' - Allow the RSS footer to be dynamically shown/hidden.
97 *
98 * @api boolean $show_embed Indicates if the RSS footer should be shown or not.
99 *
100 * @param string $context The context of the RSS content - 'full' or 'excerpt'.
101 */
102 if ( ! \apply_filters( 'wpseo_include_rss_footer', true, $context ) ) {
103 return false;
104 }
105
106 return $this->is_configured();
107 }
108
109 /**
110 * Checks if the RSS feed fields are configured.
111 *
112 * @return bool True when one of the fields has a value.
113 */
114 protected function is_configured() {
115 return ( $this->options->get( 'rssbefore', '' ) !== '' || $this->options->get( 'rssafter', '' ) );
116 }
117
118 /**
119 * Adds the RSS footer and/or header to an RSS feed item.
120 *
121 * @param string $content Feed item content.
122 *
123 * @return string The content to add.
124 */
125 protected function embed_rss( $content ) {
126 $before = $this->rss_replace_vars( $this->options->get( 'rssbefore', '' ) );
127 $after = $this->rss_replace_vars( $this->options->get( 'rssafter', '' ) );
128 $content = $before . $content . $after;
129
130 return $content;
131 }
132
133 /**
134 * Replaces the possible RSS variables with their actual values.
135 *
136 * @param string $content The RSS content that should have the variables replaced.
137 *
138 * @return string
139 */
140 protected function rss_replace_vars( $content ) {
141 if ( $content === '' ) {
142 return $content;
143 }
144
145 $replace_vars = $this->get_replace_vars( $this->get_link_template(), \get_post() );
146
147 $content = \stripslashes( \trim( $content ) );
148 $content = \str_ireplace( \array_keys( $replace_vars ), \array_values( $replace_vars ), $content );
149
150 return \wpautop( $content );
151 }
152
153 /**
154 * Retrieves the replacement variables.
155 *
156 * @codeCoverageIgnore It just contains too much WordPress functions.
157 *
158 * @param string $link_template The link template.
159 * @param mixed $post The post to use.
160 *
161 * @return array The replacement variables.
162 */
163 protected function get_replace_vars( $link_template, $post ) {
164 $author_link = '';
165 if ( \is_object( $post ) ) {
166 $author_link = \sprintf( $link_template, \esc_url( \get_author_posts_url( $post->post_author ) ), \esc_html( \get_the_author() ) );
167 }
168
169 return [
170 '%%AUTHORLINK%%' => $author_link,
171 '%%POSTLINK%%' => \sprintf( $link_template, \esc_url( \get_permalink() ), \esc_html( \get_the_title() ) ),
172 '%%BLOGLINK%%' => \sprintf( $link_template, \esc_url( \get_bloginfo( 'url' ) ), \esc_html( \get_bloginfo( 'name' ) ) ),
173 '%%BLOGDESCLINK%%' => \sprintf( $link_template, \esc_url( \get_bloginfo( 'url' ) ), \esc_html( \get_bloginfo( 'name' ) ) . ' - ' . \esc_html( \get_bloginfo( 'description' ) ) ),
174 ];
175 }
176
177 /**
178 * Retrieves the link template.
179 *
180 * @return string The link template.
181 */
182 protected function get_link_template() {
183 /**
184 * Filter: 'nofollow_rss_links' - Allow the developer to determine whether or not to follow the links in
185 * the bits Yoast SEO adds to the RSS feed, defaults to true.
186 *
187 * @api bool $unsigned Whether or not to follow the links in RSS feed, defaults to true.
188 *
189 * @since 1.4.20
190 */
191 if ( \apply_filters( 'nofollow_rss_links', true ) ) {
192 return '<a rel="nofollow" href="%1$s">%2$s</a>';
193 }
194
195 return '<a href="%1$s">%2$s</a>';
196 }
197 }
198