PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
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 28.2, at src/integrations/front-end/rss-footer-embed.php

197 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 * @param bool $show_embed Indicates if the RSS footer should be shown or not.
99 * @param string $context The context of the RSS content - 'full' or 'excerpt'.
100 */
101 if ( ! \apply_filters( 'wpseo_include_rss_footer', true, $context ) ) {
102 return false;
103 }
104
105 return $this->is_configured();
106 }
107
108 /**
109 * Checks if the RSS feed fields are configured.
110 *
111 * @return bool True when one of the fields has a value.
112 */
113 protected function is_configured() {
114 return ( $this->options->get( 'rssbefore', '' ) !== '' || $this->options->get( 'rssafter', '' ) );
115 }
116
117 /**
118 * Adds the RSS footer and/or header to an RSS feed item.
119 *
120 * @param string $content Feed item content.
121 *
122 * @return string The content to add.
123 */
124 protected function embed_rss( $content ) {
125 $before = $this->rss_replace_vars( $this->options->get( 'rssbefore', '' ) );
126 $after = $this->rss_replace_vars( $this->options->get( 'rssafter', '' ) );
127 $content = $before . $content . $after;
128
129 return $content;
130 }
131
132 /**
133 * Replaces the possible RSS variables with their actual values.
134 *
135 * @param string $content The RSS content that should have the variables replaced.
136 *
137 * @return string
138 */
139 protected function rss_replace_vars( $content ) {
140 if ( $content === '' ) {
141 return $content;
142 }
143
144 $replace_vars = $this->get_replace_vars( $this->get_link_template(), \get_post() );
145
146 $content = \stripslashes( \trim( $content ) );
147 $content = \str_ireplace( \array_keys( $replace_vars ), \array_values( $replace_vars ), $content );
148
149 return \wpautop( $content );
150 }
151
152 /**
153 * Retrieves the replacement variables.
154 *
155 * @codeCoverageIgnore It just contains too much WordPress functions.
156 *
157 * @param string $link_template The link template.
158 * @param mixed $post The post to use.
159 *
160 * @return array The replacement variables.
161 */
162 protected function get_replace_vars( $link_template, $post ) {
163 $author_link = '';
164 if ( \is_object( $post ) ) {
165 $author_link = \sprintf( $link_template, \esc_url( \get_author_posts_url( $post->post_author ) ), \esc_html( \get_the_author() ) );
166 }
167
168 return [
169 '%%AUTHORLINK%%' => $author_link,
170 '%%POSTLINK%%' => \sprintf( $link_template, \esc_url( \get_permalink() ), \esc_html( \get_the_title() ) ),
171 '%%BLOGLINK%%' => \sprintf( $link_template, \esc_url( \get_bloginfo( 'url' ) ), \esc_html( \get_bloginfo( 'name' ) ) ),
172 '%%BLOGDESCLINK%%' => \sprintf( $link_template, \esc_url( \get_bloginfo( 'url' ) ), \esc_html( \get_bloginfo( 'name' ) ) . ' - ' . \esc_html( \get_bloginfo( 'description' ) ) ),
173 ];
174 }
175
176 /**
177 * Retrieves the link template.
178 *
179 * @return string The link template.
180 */
181 protected function get_link_template() {
182 /**
183 * Filter: 'nofollow_rss_links' - Allow the developer to determine whether or not to follow the links in
184 * the bits Yoast SEO adds to the RSS feed, defaults to false.
185 *
186 * @since 1.4.20
187 *
188 * @param bool $unsigned Whether or not to follow the links in RSS feed, defaults to true.
189 */
190 if ( \apply_filters( 'nofollow_rss_links', false ) ) {
191 return '<a rel="nofollow" href="%1$s">%2$s</a>';
192 }
193
194 return '<a href="%1$s">%2$s</a>';
195 }
196 }
197