Skip to content

Commit

Permalink
Add plugin suppression info in plugins screen (#6959)
Browse files Browse the repository at this point in the history
Co-authored-by: Dhaval Parekh <dmparekh007@gmail.com>
  • Loading branch information
westonruter and dhaval-parekh committed Mar 10, 2022
1 parent 3ba9db9 commit 9dce6a0
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/PluginSuppression.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function __construct( PluginRegistry $plugin_registry, CallbackReflection
public function register() {
add_filter( 'amp_default_options', [ $this, 'filter_default_options' ] );
add_filter( 'amp_options_updating', [ $this, 'sanitize_options' ], 10, 2 );
add_filter( 'plugin_row_meta', [ $this, 'filter_plugin_row_meta' ], 10, 2 );

add_filter(
'register_block_type_args',
Expand Down Expand Up @@ -491,4 +492,33 @@ private function is_callback_plugin_suppressed( $callback, $suppressed_plugins )
in_array( $source['name'], $suppressed_plugins, true )
);
}

/**
* Add meta if plugin is suppressed in AMP page.
*
* @param array $plugin_meta An array of the plugin's metadata.
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
*
* @return array An array of the plugin's metadata
*/
public function filter_plugin_row_meta( $plugin_meta, $plugin_file ) {

// Do not show on the network plugins screen.
if ( is_network_admin() ) {
return $plugin_meta;
}

$suppressed_plugins = AMP_Options_Manager::get_option( 'suppressed_plugins' );
$plugin_slug = $this->plugin_registry->get_plugin_slug_from_file( $plugin_file );
if ( isset( $suppressed_plugins[ $plugin_slug ] ) ) {
$plugin_meta[] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
esc_url( admin_url( 'admin.php?page=amp-options' ) . '#plugin-suppression' ),
esc_attr__( 'Visit AMP Settings', 'amp' ),
esc_html__( 'Suppressed on AMP Pages', 'amp' )
);
}

return $plugin_meta;
}
}
69 changes: 69 additions & 0 deletions tests/php/src/PluginSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,73 @@ static function ( $plugin_file_slug ) {
}
return $r;
}

/**
* Data provider for $this->test_filter_plugin_row_meta()
*
* @return array[]
*/
public function data_provider_for_filter_plugin_row_meta() {

return [
'plugin is not suppressed' => [
'plugin_file' => 'plugin.php',
'current_screen' => 'plugins',
'should_have_meta' => false,
],
'plugin is suppressed' => [
'plugin_file' => 'plugin-one/plugin-one.php',
'current_screen' => 'plugins',
'should_have_meta' => true,
],
'plugin is suppressed without slug' => [
'plugin_file' => 'plugin-one/plugin-one.php',
'current_screen' => 'plugins',
'should_have_meta' => true,
],
'plugin is suppressed on different screen' => [
'plugin_file' => 'plugin-one/plugin-one.php',
'current_screen' => 'plugins-network',
'should_have_meta' => false,
],
];
}

/**
* @dataProvider data_provider_for_filter_plugin_row_meta()
* @covers ::filter_plugin_row_meta()
*/
public function test_filter_plugin_row_meta( $plugin_file, $current_screen, $should_have_meta ) {

set_current_screen( $current_screen );

// Mock AMP option.
AMP_Options_Manager::update_option(
'suppressed_plugins',
[
'plugin-one' => [
'last_version' => '1.0',
'timestamp' => 1646316249,
'username' => 'user1',
],
]
);

$output = $this->instance->filter_plugin_row_meta( [], $plugin_file );

if ( $should_have_meta ) {

$this->assertEquals(
sprintf(
'<a href="%s" aria-label="%s">%s</a>',
esc_url( admin_url( 'admin.php?page=amp-options#plugin-suppression' ) ),
esc_attr__( 'Visit AMP Settings', 'amp' ),
__( 'Suppressed on AMP Pages', 'amp' )
),
$output[0]
);
} else {
$this->assertEmpty( $output );
}
}
}

0 comments on commit 9dce6a0

Please sign in to comment.