| 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( Redirect_Helper $redirect, Robots_Helper $robots ) { |
| 47 |
$this->redirect = $redirect; |
| 48 |
$this->robots = $robots; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Initializes the integration. |
| 53 |
* |
| 54 |
* This is the place to register hooks and filters. |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function register_hooks() { |
| 59 |
if ( $this->clean_reply_to_com() ) { |
| 60 |
\add_filter( 'comment_reply_link', [ $this, 'remove_reply_to_com' ] ); |
| 61 |
\add_action( 'template_redirect', [ $this, 'replytocom_redirect' ], 1 ); |
| 62 |
} |
| 63 |
|
| 64 |
// When users view a reply to a comment, this URL parameter is set. These should never be indexed separately. |
| 65 |
if ( $this->get_replytocom_parameter() !== null ) { |
| 66 |
\add_filter( 'wpseo_robots_array', [ $this->robots, 'set_robots_no_index' ] ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Checks if the url contains the ?replytocom query parameter. |
| 72 |
* |
| 73 |
* @codeCoverageIgnore Wraps the filter input. |
| 74 |
* |
| 75 |
* @return string|null The value of replytocom or null if it does not exist. |
| 76 |
*/ |
| 77 |
protected function get_replytocom_parameter() { |
| 78 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 79 |
if ( isset( $_GET['replytocom'] ) && \is_string( $_GET['replytocom'] ) ) { |
| 80 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 81 |
return \sanitize_text_field( \wp_unslash( $_GET['replytocom'] ) ); |
| 82 |
} |
| 83 |
return null; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Removes the ?replytocom variable from the link, replacing it with a #comment-<number> anchor. |
| 88 |
* |
| 89 |
* @todo Should this function also allow for relative urls ? |
| 90 |
* |
| 91 |
* @param string $link The comment link as a string. |
| 92 |
* |
| 93 |
* @return string The modified link. |
| 94 |
*/ |
| 95 |
public function remove_reply_to_com( $link ) { |
| 96 |
return \preg_replace( '`href=(["\'])(?:.*(?:\?|&|&)replytocom=(\d+)#respond)`', 'href=$1#comment-$2', $link ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Redirects out the ?replytocom variables. |
| 101 |
* |
| 102 |
* @return bool True when redirect has been done. |
| 103 |
*/ |
| 104 |
public function replytocom_redirect() { |
| 105 |
if ( isset( $_GET['replytocom'] ) && \is_singular() ) { |
| 106 |
$url = \get_permalink( $GLOBALS['post']->ID ); |
| 107 |
$hash = \sanitize_text_field( \wp_unslash( $_GET['replytocom'] ) ); |
| 108 |
$query_string = ''; |
| 109 |
if ( isset( $_SERVER['QUERY_STRING'] ) ) { |
| 110 |
$query_string = \remove_query_arg( 'replytocom', \sanitize_text_field( \wp_unslash( $_SERVER['QUERY_STRING'] ) ) ); |
| 111 |
} |
| 112 |
if ( ! empty( $query_string ) ) { |
| 113 |
$url .= '?' . $query_string; |
| 114 |
} |
| 115 |
$url .= '#comment-' . $hash; |
| 116 |
|
| 117 |
$this->redirect->do_safe_redirect( $url, 301 ); |
| 118 |
|
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Checks whether we can allow the feature that removes ?replytocom query parameters. |
| 127 |
* |
| 128 |
* @codeCoverageIgnore It just wraps a call to a filter. |
| 129 |
* |
| 130 |
* @return bool True to remove, false not to remove. |
| 131 |
*/ |
| 132 |
private function clean_reply_to_com() { |
| 133 |
/** |
| 134 |
* Filter: 'wpseo_remove_reply_to_com' - Allow disabling the feature that removes ?replytocom query parameters. |
| 135 |
* |
| 136 |
* @param bool $return True to remove, false not to remove. |
| 137 |
*/ |
| 138 |
return (bool) \apply_filters( 'wpseo_remove_reply_to_com', true ); |
| 139 |
} |
| 140 |
} |
| 141 |
|