Skip to content

Commit

Permalink
added the uninstall file
Browse files Browse the repository at this point in the history
  • Loading branch information
amirition committed Nov 14, 2024
1 parent 424f60f commit 48c1ebf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 31 deletions.
49 changes: 18 additions & 31 deletions src/Admin/Settings_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,37 +101,6 @@ public function support_links(): void {
);
}

/**
* Product Tab setting lists.
*
* @since 2.0.2
*
* @return array Add custom settings for product tabs.
*/
public function wpt_get_settings() {
$settings = [
'tab_section' => [
'name' => esc_html__( 'Product Tab Settings', 'woocommerce-product-tabs' ),
'type' => 'title',
'id' => 'wpt_tab_section',
],
'wpt_disable_content_filter' => [
'name' => esc_html__( 'Disable the_content Filter', 'woocommerce-product-tabs' ),
'type' => 'checkbox',
'desc' => esc_html__( 'Enable this checkbox if you are using a page builder and have problems with the content preview.', 'woocommerce-product-tabs' ),
'default' => 'no',
'class' => 'wpt_disable_content_filter',
'id' => 'wpt_disable_content_filter',
],
'tab_section_end' => [
'type' => 'sectionend',
'id' => 'wpt_tab_section_end',
],
];

return $settings;
}

public function get_settings_page_footer() {
do_action( 'barn2_after_plugin_settings', $this->plugin->get_id() );
?>
Expand Down Expand Up @@ -166,6 +135,13 @@ function register_plugin_option_fields() {
register_setting( 'wpt_group', 'wpt_options', 'validate_plugin_options' );
add_settings_section( 'wpt_option_section', __( 'Tab options', 'woocommerce-product-tabs' ), [], 'wpt-options' );
add_settings_field( 'disable_content_filter', __( 'Page builder support', 'woocommerce-product-tabs' ), [ $this, 'disable_content_filter' ], 'wpt-options', 'wpt_option_section' );
add_settings_field(
'delete_data',
__( 'Uninstalling ' . $this->plugin->get_name(), 'woocommerce-product-tabs' ),
[ $this, 'delete_data' ],
'wpt-options',
'wpt_option_section'
);
}

/**
Expand Down Expand Up @@ -250,6 +226,17 @@ function admin_product_tabs_options_page() {
$this->get_settings_page_footer();
}

public function delete_data() {
echo '<fieldset>';
$delete_data = Util::get_option( 'delete_data' );
echo '<label class="checkbox-row" for="delete_data">';
?>
<input type="checkbox" name="wpt_options[delete_data]" id="delete_data" value="1" <?php checked( 1, $delete_data ); ?> />
<?php
_e( 'Permanently delete all ' . $this->plugin->get_name() . ' settings and data when uninstalling the plugin', 'woocommerce-product-tabs' );
echo ' </label><br /></fieldset>';
}

/**
* Change the default support link to the WordPress repository
*/
Expand Down
61 changes: 61 additions & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Fired when the plugin is uninstalled.
*
* @package Barn2\woocommerce-product-tabs
* @author Barn2 Plugins <[email protected]>
* @license GPL-3.0
* @copyright Barn2 Media Ltd
*/

// If uninstall not called from WordPress, then exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}

$settings = get_option( 'wpt_options', [] );

if ( ! isset( $settings['delete_data'] ) || ! $settings['delete_data'] ) {
return;
}

foreach ( $settings as $option ) {
delete_option( $option );
}

// Delete all the tabs
$tabs = get_posts( array(
'post_type' => 'woo_product_tab',
'posts_per_page' => -1,
'post_status' => 'any',
) );

foreach ( $tabs as $tab ) {
wp_delete_post( $tab->ID, true );
}

// Delete all the custom tabs including content and icon
global $wpdb;

// Get all WooCommerce products
$product_ids = $wpdb->get_col( "
SELECT ID FROM {$wpdb->posts}
WHERE post_type = 'product'
" );
foreach ( $product_ids as $product_id ) {
// Delete meta keys containing '_wpt_override_', '_wpt_field_', '_wpt_icon_'
$meta_keys = array(
'_wpt_override_',
'_wpt_field_',
'_wpt_icon_'
);

foreach ( $meta_keys as $key ) {
$wpdb->query( $wpdb->prepare(
"DELETE FROM {$wpdb->postmeta}
WHERE post_id = %d
AND meta_key LIKE %s",
$product_id, '%' . $wpdb->esc_like($key) . '%'
) );
}
}

0 comments on commit 48c1ebf

Please sign in to comment.