PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.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 / comment-link-fixer.php

comment-link-fixer.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at src/integrations/front-end/comment-link-fixer.php

138 lines 3.6 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\Redirect_Helper;
7 use Yoast\WP\SEO\Helpers\Robots_Helper;
8 use Yoast\WP\SEO\Integrations\Integration_Interface;
9
10 /**
11 * Class Comment_Link_Fixer.
12 */
13 class Comment_Link_Fixer implements Integration_Interface {
14
15 /**
16 * The redirects helper.
17 *
18 * @var Redirect_Helper
19 */
20 protected $redirect;
21
22 /**
23 * The robots helper.
24 *
25 * @var Robots_Helper
26 */
27 protected $robots;
28
29 /**
30 * Returns the conditionals based in which this loadable should be active.
31 *
32 * @return array
33 */
34 public static function get_conditionals() {
35 return [ Front_End_Conditional::class ];
36 }
37
38 /**
39 * Comment_Link_Fixer constructor.
40 *
41 * @codeCoverageIgnore It only sets depedencies.
42 *
43 * @param Redirect_Helper $redirect The redirect helper.
44 * @param Robots_Helper $robots The robots helper.
45 */
46 public function __construct(
47 Redirect_Helper $redirect, Robots_Helper $robots
48 ) {
49 $this->redirect = $redirect;
50 $this->robots = $robots;
51 }
52
53 /**
54 * Initializes the integration.
55 *
56 * This is the place to register hooks and filters.
57 *
58 * @return void
59 */
60 public function register_hooks() {
61 if ( $this->clean_reply_to_com() ) {
62 \add_filter( 'comment_reply_link', [ $this, 'remove_reply_to_com' ] );
63 \add_action( 'template_redirect', [ $this, 'replytocom_redirect' ], 1 );
64 }
65
66 // When users view a reply to a comment, this URL parameter is set. These should never be indexed separately.
67 if ( $this->has_replytocom_parameter() ) {
68 \add_filter( 'wpseo_robots_array', [ $this->robots, 'set_robots_no_index' ] );
69 }
70 }
71
72 /**
73 * Checks if the url contains the ?replytocom query parameter.
74 *
75 * @codeCoverageIgnore Wraps the filter input.
76 *
77 * @return string The value of replytocom.
78 */
79 protected function has_replytocom_parameter() {
80 return \filter_input( \INPUT_GET, 'replytocom' );
81 }
82
83 /**
84 * Removes the ?replytocom variable from the link, replacing it with a #comment-<number> anchor.
85 *
86 * @todo Should this function also allow for relative urls ?
87 *
88 * @param string $link The comment link as a string.
89 *
90 * @return string The modified link.
91 */
92 public function remove_reply_to_com( $link ) {
93 return \preg_replace( '`href=(["\'])(?:.*(?:\?|&|&#038;)replytocom=(\d+)#respond)`', 'href=$1#comment-$2', $link );
94 }
95
96 /**
97 * Redirects out the ?replytocom variables.
98 *
99 * @return bool True when redirect has been done.
100 */
101 public function replytocom_redirect() {
102 if ( isset( $_GET['replytocom'] ) && \is_singular() ) {
103 $url = \get_permalink( $GLOBALS['post']->ID );
104 $hash = \sanitize_text_field( \wp_unslash( $_GET['replytocom'] ) );
105 $query_string = '';
106 if ( isset( $_SERVER['QUERY_STRING'] ) ) {
107 $query_string = \remove_query_arg( 'replytocom', \sanitize_text_field( \wp_unslash( $_SERVER['QUERY_STRING'] ) ) );
108 }
109 if ( ! empty( $query_string ) ) {
110 $url .= '?' . $query_string;
111 }
112 $url .= '#comment-' . $hash;
113
114 $this->redirect->do_safe_redirect( $url, 301 );
115
116 return true;
117 }
118
119 return false;
120 }
121
122 /**
123 * Checks whether we can allow the feature that removes ?replytocom query parameters.
124 *
125 * @codeCoverageIgnore It just wraps a call to a filter.
126 *
127 * @return bool True to remove, false not to remove.
128 */
129 private function clean_reply_to_com() {
130 /**
131 * Filter: 'wpseo_remove_reply_to_com' - Allow disabling the feature that removes ?replytocom query parameters.
132 *
133 * @param bool $return True to remove, false not to remove.
134 */
135 return (bool) \apply_filters( 'wpseo_remove_reply_to_com', true );
136 }
137 }
138