• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 5066322038

pending completion
5066322038

push

github

GitHub
Merge pull request #20316 from Yoast/JRF/ghactions-run-more-selectively

2550 of 29012 relevant lines covered (8.79%)

0.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/integrations/front-end/comment-link-fixer.php
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->get_replytocom_parameter() !== null ) {
×
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|null The value of replytocom or null if it does not exist.
78
         */
79
        protected function get_replytocom_parameter() {
80
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
81
                if ( isset( $_GET['replytocom'] ) && \is_string( $_GET['replytocom'] ) ) {
82
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
83
                        return \sanitize_text_field( \wp_unslash( $_GET['replytocom'] ) );
84
                }
85
                return null;
86
        }
87

88
        /**
89
         * Removes the ?replytocom variable from the link, replacing it with a #comment-<number> anchor.
90
         *
91
         * @todo Should this function also allow for relative urls ?
92
         *
93
         * @param string $link The comment link as a string.
94
         *
95
         * @return string The modified link.
96
         */
97
        public function remove_reply_to_com( $link ) {
98
                return \preg_replace( '`href=(["\'])(?:.*(?:\?|&|&#038;)replytocom=(\d+)#respond)`', 'href=$1#comment-$2', $link );
×
99
        }
100

101
        /**
102
         * Redirects out the ?replytocom variables.
103
         *
104
         * @return bool True when redirect has been done.
105
         */
106
        public function replytocom_redirect() {
107
                if ( isset( $_GET['replytocom'] ) && \is_singular() ) {
×
108
                        $url          = \get_permalink( $GLOBALS['post']->ID );
×
109
                        $hash         = \sanitize_text_field( \wp_unslash( $_GET['replytocom'] ) );
×
110
                        $query_string = '';
×
111
                        if ( isset( $_SERVER['QUERY_STRING'] ) ) {
×
112
                                $query_string = \remove_query_arg( 'replytocom', \sanitize_text_field( \wp_unslash( $_SERVER['QUERY_STRING'] ) ) );
×
113
                        }
114
                        if ( ! empty( $query_string ) ) {
×
115
                                $url .= '?' . $query_string;
×
116
                        }
117
                        $url .= '#comment-' . $hash;
×
118

119
                        $this->redirect->do_safe_redirect( $url, 301 );
×
120

121
                        return true;
×
122
                }
123

124
                return false;
×
125
        }
126

127
        /**
128
         * Checks whether we can allow the feature that removes ?replytocom query parameters.
129
         *
130
         * @codeCoverageIgnore It just wraps a call to a filter.
131
         *
132
         * @return bool True to remove, false not to remove.
133
         */
134
        private function clean_reply_to_com() {
135
                /**
136
                 * Filter: 'wpseo_remove_reply_to_com' - Allow disabling the feature that removes ?replytocom query parameters.
137
                 *
138
                 * @param bool $return True to remove, false not to remove.
139
                 */
140
                return (bool) \apply_filters( 'wpseo_remove_reply_to_com', true );
141
        }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc