Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions admin/class-options-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ private function render_general_tab( $tab, $options ) {
</td>
</tr>

<tr>
<th scope="row">
<?php _e( 'Additional attributes', 'remove-noreferrer' ); ?>
</th>
<td>
<fieldset>
<?php
$this->render_checkbox_where_should_the_plugin_work(
$options,
'target_blank',
__( 'Remove target blank', 'remove-noreferrer' )
);

?>
</fieldset>
</td>
</tr>

<tr>
<th scope="row">
<?php _e( 'Widgets' ); ?>
Expand Down
58 changes: 55 additions & 3 deletions frontend/class-links-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ class Links_Processor {
* @static
*
* @param string $input Input.
* @param string $attribute_to_remove defines which attribute to remove.
*
* @return string
*/
public static function call( $input ) {
public static function call( $input, $attribute_to_remove = 'noreferrer' ) {
if ( ! self::is_links_found( $input ) ) {
return $input;
}

if ( ! self::is_noreferrer_found( $input ) ) {
return $input;
if ( 'target' === $attribute_to_remove ) {
if ( ! self::is_target_blank_found( $input ) ) {
return $input;
}

return self::remove_target_blank( $input );
}

return self::remove_noreferrer( $input );
Expand Down Expand Up @@ -70,6 +75,21 @@ private static function is_noreferrer_found( $input ) {
return ! ( false === stripos( $input, 'noreferrer' ) );
}

/**
* Checks is target blank found
*
* @since 2.0.1
* @access private
* @static
*
* @param string $input Input.
*
* @return bool
*/
private static function is_target_blank_found( $input ) {
return ! ( false === stripos( $input, '_blank' ) );
}

/**
* Removes noreferrer
*
Expand Down Expand Up @@ -102,6 +122,38 @@ private static function remove_noreferrer( $input ) {
return preg_replace_callback( $regex, $replace, $input );
}

/**
* Removes target blank attribute.
*
* @since 2.0.1
* @access private
* @static
*
* @param string $input Input.
*
* @return string
*/
private static function remove_target_blank( $input ) {
// phpcs:disable Squiz.Commenting.InlineComment.InvalidEndChar
$replace = function( $matches ) {
// For input string '<a class="test" target="_blank" name="test">test</a>' it returns:
return sprintf(
'%s%s%s%s%s%s',
$matches[1], // returns: '<a class="test" target='
$matches[2], // returns: '"'
self::remove_extra_spaces( str_ireplace( '_blank', '', $matches[3] ) ), // returns: '_blank'
$matches[2], // returns: '"'
$matches[4], // returns: ' name="test"'
$matches[5] // returns: '>test</a>'
);
};
// phpcs:enable Squiz.Commenting.InlineComment.InvalidEndChar

$regex = "/(<a\s.*target=)([\"\']??)(.+)\\2([^>]*)(>.*<\/a>)/siU";

return preg_replace_callback( $regex, $replace, $input );
}

/**
* Removes extra spaces from the string
*
Expand Down
33 changes: 32 additions & 1 deletion frontend/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ public function remove_noreferrer_from_content( $content ) {
return $content;
}

return $this->remove_noreferrer( $content );
$filtered_content = $this->remove_noreferrer( $content );
if ( $this->can_remove_target_blank() ) {
$filtered_content = $this->remove_target_blank( $filtered_content );
}
return $filtered_content;
}

/**
Expand Down Expand Up @@ -177,6 +181,20 @@ private function is_current_page_allowed() {
|| $this->is_posts_page_processable( $where_should_the_plugin_work );
}

/**
* Checks if the target blank attribute can be removed
*
* @since 2.0.1
* @access private
*
* @return bool
*/
private function can_remove_target_blank() {
$where_should_the_plugin_work = $this->get_option( GRN_WHERE_SHOULD_THE_PLUGIN_WORK_KEY );

return in_array( 'target_blank', $where_should_the_plugin_work, true );
}

/**
* Return option's value by key
*
Expand Down Expand Up @@ -268,5 +286,18 @@ private function is_widgets_processable( $options, $key ) {
private function remove_noreferrer( $content ) {
return $this->_links_processor->call( $content );
}

/**
* Remove target blank attribute from the links
*
* @since 2.0.1
* @access private
*
* @param string $content Content.
* @return string
*/
private function remove_target_blank( $content ) {
return $this->_links_processor->call( $content, 'target' );
}
}

1 change: 1 addition & 0 deletions remove-noreferrer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function grn_allowed_values() {
'page',
'comments',
'text_widget',
'target_blank',
'custom_html_widget',
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/admin/class-options-page-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function test_render_general_tab_has_valid_checkboxes_count() {

preg_match_all( '/<input\s+type="checkbox"\s+name="remove_noreferrer\[where_should_the_plugin_work\]\[\]"[^>]*>/', $content, $matches );

$this->assertCount( 6, $matches[0] );
$this->assertCount( 7, $matches[0] );
}

/**
Expand Down
55 changes: 53 additions & 2 deletions tests/phpunit/tests/frontend/class-links-processor-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ protected function setUp() {
*
* @param string $input The input string.
* @param string $expected The expected function output.
* @param string $attribute_to_remove The attribute to remove.
*
* @return void
*/
public function test_call( $input, $expected ) {
$result = $this->_processor->call( $input );
public function test_call( $input, $expected, $attribute_to_remove ) {
$result = $this->_processor->call( $input, $attribute_to_remove );

$this->assertSame( $expected, $result );
}
Expand All @@ -73,39 +74,89 @@ public function data_call() {
'no links found' => array(
'test',
'test',
'',
),
'no noreferrer found' => array(
'<a rel="nofollow">test</a>',
'<a rel="nofollow">test</a>',
'',
),
'noreferrer found' => array(
'<a rel="nofollow noreferrer">test</a>',
'<a rel="nofollow">test</a>',
'',
),
'rel values inside single quotes' => array(
'<a class="test" rel=\'nofollow noreferrer\' name="test">test</a>',
'<a class="test" rel=\'nofollow\' name="test">test</a>',
'',
),
'link has attributes before rel' => array(
'<a class="test" rel="nofollow noreferrer">test</a>',
'<a class="test" rel="nofollow">test</a>',
'',
),
'link has attributes after rel' => array(
'<a rel="nofollow noreferrer" name="test">test</a>',
'<a rel="nofollow" name="test">test</a>',
'',
),
'removes extra spaces in rel' => array(
'<a rel=" nofollow noreferrer ugc ">test</a>',
'<a rel="nofollow ugc">test</a>',
'',
),
'two links' => array(
'<a rel="nofollow noreferrer">test</a> <a rel="ugc noreferrer nofollow">test2</a>',
'<a rel="nofollow">test</a> <a rel="ugc nofollow">test2</a>',
'',
),
'rel="nofollow" exists outside the link' => array(
'<a rel="nofollow noreferrer">rel="noreferrer"</a>',
'<a rel="nofollow">rel="noreferrer"</a>',
'',
),
'no target blank found' => array(
'<a rel="nofollow">test</a>',
'<a rel="nofollow">test</a>',
'target',
),
'target blank found' => array(
'<a target="_blank">test</a>',
'<a target="">test</a>',
'target',
),
'target values inside single quotes' => array(
'<a class="test" target=\'_blank\' name="test">test</a>',
'<a class="test" target=\'\' name="test">test</a>',
'target',
),
'link has attributes before target' => array(
'<a class="test" target="_blank">test</a>',
'<a class="test" target="">test</a>',
'target',
),
'link has attributes after target' => array(
'<a target="_blank" name="test">test</a>',
'<a target="" name="test">test</a>',
'target',
),
'removes extra spaces in target' => array(
'<a target=" _blank ">test</a>',
'<a target="">test</a>',
'target',
),
'target two links' => array(
'<a target="_blank">test</a> <a target="_blank">test2</a>',
'<a target="">test</a> <a target="">test2</a>',
'target',
),
'target="_blank" exists outside the link' => array(
'<a target="_blank">target="_blank"</a>',
'<a target="">target="_blank"</a>',
'target',
),

);
// phpcs:enable WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned
}
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/remove-noreferrer-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function test_has_grn_allowed_values_function() {
'page',
'comments',
'text_widget',
'target_blank',
'custom_html_widget',
),
\Remove_Noreferrer\grn_allowed_values()
Expand Down