- exclude core pages from uninstall (issue #940)

This commit is contained in:
ashubawork
2021-11-15 11:40:47 +02:00
parent 070b0f4017
commit 4f34a9dc74
+70 -74
View File
@@ -5,16 +5,21 @@
*/
// Exit if accessed directly.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) exit;
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
if ( ! defined( 'um_path' ) )
if ( ! defined( 'um_path' ) ) {
define( 'um_path', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'um_url' ) )
if ( ! defined( 'um_url' ) ) {
define( 'um_url', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'um_plugin' ) )
if ( ! defined( 'um_plugin' ) ) {
define( 'um_plugin', plugin_basename( __FILE__ ) );
}
//for delete Email options only for Core email notifications
remove_all_filters( 'um_email_notifications' );
@@ -31,13 +36,6 @@ if ( ! empty( $delete_options ) ) {
$upl_folder = UM()->files()->upload_basedir;
UM()->files()->remove_dir( $upl_folder );
//remove core pages
foreach ( UM()->config()->core_pages as $page_key => $page_value ) {
$page_id = UM()->options()->get( UM()->options()->get_core_page_id( $page_key ) );
if ( ! empty( $page_id ) )
wp_delete_post( $page_id, true );
}
//remove core settings
$settings_defaults = UM()->config()->settings_defaults;
foreach ( $settings_defaults as $k => $v ) {
@@ -45,24 +43,26 @@ if ( ! empty( $delete_options ) ) {
}
//delete UM Custom Post Types posts
$um_posts = get_posts( array(
'post_type' => array(
'um_form',
'um_directory',
'um_role',
'um_private_content',
'um_mailchimp',
'um_profile_tabs',
'um_social_login',
'um_review',
'um_frontend_posting',
'um_notice'
),
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
'numberposts' => -1
) );
$um_posts = get_posts(
array(
'post_type' => array(
'um_form',
'um_directory',
'um_role',
'um_private_content',
'um_mailchimp',
'um_profile_tabs',
'um_social_login',
'um_review',
'um_frontend_posting',
'um_notice',
),
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
'numberposts' => -1,
)
);
foreach ( $um_posts as $um_post ){
foreach ( $um_posts as $um_post ) {
delete_option( 'um_existing_rows_' . $um_post->ID );
delete_option( 'um_form_rowdata_' . $um_post->ID );
wp_delete_post( $um_post->ID, 1 );
@@ -84,17 +84,6 @@ if ( ! empty( $delete_options ) ) {
global $wpdb;
//remove extensions core pages
$ext_pages = $wpdb->get_results( "
SELECT post_id
FROM {$wpdb->postmeta}
WHERE meta_key = '_um_core'
", ARRAY_A );
foreach ( $ext_pages as $page_id ) {
wp_delete_post( $page_id['post_id'], 1 );
}
$wpdb->query(
"DELETE
FROM {$wpdb->usermeta}
@@ -132,12 +121,12 @@ if ( ! empty( $delete_options ) ) {
//remove all tables from extensions
$all_tables = "SHOW TABLES LIKE '{$wpdb->prefix}um\_%'";
$results = $wpdb->get_results( $all_tables );
if( $results ){
foreach( $results as $index => $value ) {
foreach( $value as $table_name ) {
$um_groups_members = $wpdb->prefix.'um_groups_members';
if( $table_name == $um_groups_members ){
$results = $wpdb->get_results( $all_tables );
if ( $results ) {
foreach ( $results as $index => $value ) {
foreach ( $value as $table_name ) {
$um_groups_members = $wpdb->prefix . 'um_groups_members';
if ( $table_name === $um_groups_members ) {
$wpdb->query( "
DELETE posts, term_rel, pmeta, terms, tax, commetns
FROM {$wpdb->posts} posts
@@ -154,6 +143,7 @@ if ( ! empty( $delete_options ) ) {
WHERE posts.post_type = 'um_groups' OR posts.post_type = 'um_groups_discussion'"
);
}
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table_name is static variable
$wpdb->query( "DROP TABLE IF EXISTS $table_name" );
}
}
@@ -161,14 +151,16 @@ if ( ! empty( $delete_options ) ) {
//remove options from extensions
//user photos
$um_user_photos = get_posts( array(
'post_type' => array(
'um_user_photos'
),
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
'numberposts' => -1
) );
if( $um_user_photos ) {
$um_user_photos = get_posts(
array(
'post_type' => array(
'um_user_photos',
),
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
'numberposts' => -1,
)
);
if ( $um_user_photos ) {
foreach ( $um_user_photos as $um_user_photo ) {
$attachments = get_attached_media( 'image', $um_user_photo->ID );
foreach ( $attachments as $attachment ) {
@@ -179,17 +171,19 @@ if ( ! empty( $delete_options ) ) {
}
//user notes
$um_notes = get_posts( array(
'post_type' => array(
'um_notes'
),
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
'numberposts' => -1
) );
if( $um_notes ){
foreach ( $um_notes as $um_note ){
$um_notes = get_posts(
array(
'post_type' => array(
'um_notes',
),
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
'numberposts' => -1,
)
);
if ( $um_notes ) {
foreach ( $um_notes as $um_note ) {
$attachments = get_attached_media( 'image', $um_note->ID );
foreach ( $attachments as $attachment ){
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, 1 );
}
wp_delete_post( $um_note->ID, 1 );
@@ -219,22 +213,24 @@ if ( ! empty( $delete_options ) ) {
option_name LIKE 'widget_um%' OR
option_name LIKE 'ultimatemember_%'" );
foreach( $um_options as $um_option ) {
foreach ( $um_options as $um_option ) {
delete_option( $um_option->option_name );
}
//social activity
$um_activities = get_posts( array(
'post_type' => array(
'um_activity'
),
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
'numberposts' => -1
) );
$um_activities = get_posts(
array(
'post_type' => array(
'um_activity',
),
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
'numberposts' => -1,
)
);
foreach ( $um_activities as $um_activity ) {
$image = get_post_meta( $um_activity->ID, '_photo', true );
if( $image ){
$user_id = get_post_meta( $um_activity->ID, '_user_id', true );
if ( $image ) {
$user_id = get_post_meta( $um_activity->ID, '_user_id', true );
$upload_dir = wp_upload_dir();
$image_path = $upload_dir['basedir'] . '/ultimatemember/' . $user_id . '/' . $image;
if ( file_exists( $image_path ) ) {
@@ -243,4 +239,4 @@ if ( ! empty( $delete_options ) ) {
}
wp_delete_post( $um_activity->ID, 1 );
}
}
}