Skip to content

Add option to keep AMP data upon uninstalling of plugin #6632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion amp.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Plugin Name: AMP
* Description: An easier path to great Page Experience for everyone. Powered by AMP.
* Description: An easier path to great Page Experience for everyone. Powered by AMP. <em class="amp-deletion-notice"><strong>Deletion Note:</strong> To delete all plugin data with uninstallation, first activate the plugin, <strong>Go to Settings screen > Scroll to “Other” section in Advanced settings > Enable “Delete plugin data upon uninstall” toggle</strong> (if you haven't done so already).</em>
* Plugin URI: https://amp-wp.org
* Author: AMP Project Contributors
* Author URI: https://github.com/ampproject/amp-wp/graphs/contributors
Expand Down
40 changes: 40 additions & 0 deletions assets/src/settings-page/data-removal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* WordPress dependencies
*/
import { useContext } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { AMPSettingToggle } from '../components/amp-setting-toggle';
import { Options } from '../components/options-context-provider';
import { Loading } from '../components/loading';

/**
* Developer tools section of the settings page.
*/
export function DataRemoval() {
const { editedOptions, fetchingOptions, updateOptions } = useContext( Options );

const keepAmpData = editedOptions?.keep_amp_data;

if ( fetchingOptions ) {
return <Loading />;
}

return (
<section className="developer-tools">
<AMPSettingToggle
checked={ true === keepAmpData }
title={ __( 'Keep my data', 'amp' ) }
onChange={ () => {
updateOptions( { keep_amp_data: ! keepAmpData } );
} }
/>
<p>
{ __( 'When you delete the plugin you have the choice to have the data remain or removed.', 'amp' ) }
</p>
</section>
);
}
2 changes: 2 additions & 0 deletions assets/src/settings-page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { Analytics } from './analytics';
import { PairedUrlStructure } from './paired-url-structure';
import { MobileRedirection } from './mobile-redirection';
import { DeveloperTools } from './developer-tools';
import { DataRemoval } from './data-removal';

const { ajaxurl: wpAjaxUrl } = global;

Expand Down Expand Up @@ -250,6 +251,7 @@ function Root( { appRoot } ) {
>
<MobileRedirection />
<DeveloperTools />
<DataRemoval />
</AMPDrawer>
<SettingsFooter />
</form>
Expand Down
19 changes: 19 additions & 0 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ function amp_bootstrap_plugin() {
add_action( 'after_setup_theme', 'amp_after_setup_theme', 5 );

add_action( 'plugins_loaded', '_amp_bootstrap_customizer', 9 ); // Should be hooked before priority 10 on 'plugins_loaded' to properly unhook core panels.

// @Todo Eliminate this once https://core.trac.wordpress.org/ticket/20578 has finally landed.
add_filter( 'all_plugins', 'amp_modify_plugin_description' );
}

/**
Expand Down Expand Up @@ -219,6 +222,22 @@ function() {
);
}

/**
* When AMP plugin is active remove instruction of plugin data removal steps.
*
* @param array $meta An array of plugins to display in the list table.
*
* @return array An array of plugins to display in the list table.
*/
function amp_modify_plugin_description( $meta ) {

if ( isset( $meta['amp/amp.php']['Description'] ) ) {
$meta['amp/amp.php']['Description'] = preg_replace( ':\s*<em class=\"amp-deletion-notice\">.+?</em>:', '', $meta['amp/amp.php']['Description'] );
}

return $meta;
}

/**
* Set up AMP.
*
Expand Down
5 changes: 5 additions & 0 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AMP_Options_Manager {
Option::READER_THEME => ReaderThemes::DEFAULT_READER_THEME,
Option::PAIRED_URL_STRUCTURE => Option::PAIRED_URL_STRUCTURE_QUERY_VAR,
Option::PLUGIN_CONFIGURED => false,
Option::KEEP_AMP_DATA => false,
];

/**
Expand Down Expand Up @@ -320,6 +321,10 @@ public static function validate_options( $new_options ) {
$options[ Option::PLUGIN_CONFIGURED ] = (bool) $new_options[ OPTION::PLUGIN_CONFIGURED ];
}

if ( isset( $new_options[ Option::KEEP_AMP_DATA ] ) ) {
$options[ Option::KEEP_AMP_DATA ] = (bool) $new_options[ OPTION::KEEP_AMP_DATA ];
}

// Validate analytics.
if ( isset( $new_options[ Option::ANALYTICS ] ) && $new_options[ Option::ANALYTICS ] !== $options[ Option::ANALYTICS ] ) {
$new_analytics_option = [];
Expand Down
14 changes: 9 additions & 5 deletions includes/uninstall-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,13 @@ function delete_transients() {
*/
function remove_plugin_data() {

delete_options();
delete_user_metadata();
delete_posts();
delete_terms();
delete_transients();
$keep_amp_data = \AMP_Options_Manager::get_option( Option::KEEP_AMP_DATA );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Humm, did you test this on an actual site? The AMP_Options_Manager class is not loaded at uninstallation so this causes a fatal error:

image

(Perhaps the plugin's autoloader should get initialized during uninstallation.)


if ( true !== $keep_amp_data ) {
delete_options();
delete_user_metadata();
delete_posts();
delete_terms();
delete_transients();
}
}
7 changes: 7 additions & 0 deletions src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ interface Option {
*/
const PLUGIN_CONFIGURED = 'plugin_configured';

/**
* The key of the option storing whether the to delete AMP data upon uninstalling of plugin or not.
*
* @var string
*/
const KEEP_AMP_DATA = 'keep_amp_data';

/**
* Cached slug when it is defined late.
*
Expand Down
4 changes: 4 additions & 0 deletions src/OptionsRESTController.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ public function get_item_schema() {
Option::ANALYTICS => [
'type' => 'object',
],
Option::KEEP_AMP_DATA => [
'type' => 'boolean',
'default' => false,
],
self::SUPPORTABLE_POST_TYPES => [
'type' => 'array',
'readonly' => true,
Expand Down
1 change: 1 addition & 0 deletions tests/php/src/OptionsRESTControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function test_get_items() {
'mobile_redirect',
'plugin_configured',
'all_templates_supported',
'keep_amp_data',
'suppressed_plugins',
'supported_templates',
'supported_post_types',
Expand Down
22 changes: 22 additions & 0 deletions tests/php/test-amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function test_amp_bootstrap_plugin() {

$this->assertEquals( PHP_INT_MAX, has_filter( 'script_loader_tag', 'amp_filter_script_loader_tag' ) );
$this->assertEquals( 10, has_filter( 'style_loader_tag', 'amp_filter_font_style_loader_tag_with_crossorigin_anonymous' ) );
$this->assertEquals( 10, has_filter( 'all_plugins', 'amp_modify_plugin_description' ) );
}

/** @covers ::amp_bootstrap_plugin() */
Expand Down Expand Up @@ -2417,6 +2418,27 @@ public function test_amp_has_paired_endpoint_passed( $paired_url_structure, $suf
$this->assertEquals( $is_amp, amp_has_paired_endpoint( $url ) );
}

/**
* @covers amp_modify_plugin_description
*/
public function test_amp_modify_plugin_description() {

$input = [
'amp/amp.php' => [
'Description' => 'An easier path to great Page Experience for everyone. Powered by AMP. <em class="amp-deletion-notice"><strong>Deletion Note:</strong> To delete all plugin data with uninstallation, first activate the plugin, <strong>Go to Settings screen > Scroll to “Other” section in Advanced settings > Enable “Delete plugin data upon uninstall” toggle</strong> (if you haven\'t done so already).</em>',
],
];

$expected = [
'amp/amp.php' => [
'Description' => 'An easier path to great Page Experience for everyone. Powered by AMP.',
],
];

$this->assertEquals( $expected, amp_modify_plugin_description( $input ) );

}

/**
* Get a mock publisher logo URL, to test that the filter works as expected.
*
Expand Down
5 changes: 5 additions & 0 deletions tests/php/test-class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public function test_get_and_set_options() {
Option::PLUGIN_CONFIGURED => false,
Option::PAIRED_URL_STRUCTURE => Option::PAIRED_URL_STRUCTURE_QUERY_VAR,
Option::LATE_DEFINED_SLUG => null,
Option::KEEP_AMP_DATA => false,
],
AMP_Options_Manager::get_options()
);
Expand Down Expand Up @@ -264,19 +265,23 @@ public function test_get_options_changing_plugin_configured_default() {
[
Option::VERSION => AMP__VERSION,
Option::PLUGIN_CONFIGURED => false,
Option::KEEP_AMP_DATA => false,
]
);
$this->assertFalse( AMP_Options_Manager::get_option( Option::PLUGIN_CONFIGURED ) );
$this->assertFalse( AMP_Options_Manager::get_option( Option::KEEP_AMP_DATA ) );

// Ensure plugin_configured is false when explicitly set as such in the DB.
update_option(
AMP_Options_Manager::OPTION_NAME,
[
Option::VERSION => AMP__VERSION,
Option::PLUGIN_CONFIGURED => true,
Option::KEEP_AMP_DATA => true,
]
);
$this->assertTrue( AMP_Options_Manager::get_option( Option::PLUGIN_CONFIGURED ) );
$this->assertTrue( AMP_Options_Manager::get_option( Option::KEEP_AMP_DATA ) );
}

/** @return array */
Expand Down
14 changes: 13 additions & 1 deletion tests/php/test-uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,23 @@ public function test_uninstall_php() {
define( 'WP_UNINSTALL_PLUGIN', 'Yes' );
}

// Test 1: With option to keep AMP data ON.
AMP_Options_Manager::update_option( \AmpProject\AmpWP\Option::KEEP_AMP_DATA, true );

require AMP__DIR__ . '/uninstall.php';

$this->flush_cache();

$this->assertNotEmpty( get_option( AMP_Options_Manager::OPTION_NAME, false ) );

// Test 2: With option to keep AMP data OFF.
AMP_Options_Manager::update_option( \AmpProject\AmpWP\Option::KEEP_AMP_DATA, false );

require AMP__DIR__ . '/uninstall.php';

$this->flush_cache();

$this->assertEmpty( get_option( 'amp-option', false ) );
$this->assertEmpty( get_option( AMP_Options_Manager::OPTION_NAME, false ) );
$this->assertEmpty( get_post( $amp_validated_post->ID ) );
$this->assertEmpty( get_term( $amp_error_term->term_id ) );

Expand Down