|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Distributor uninstall script. |
| 4 | + * |
| 5 | + * @package distributor |
| 6 | + * @since x.x.x |
| 7 | + */ |
| 8 | + |
| 9 | + // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber |
| 10 | + |
| 11 | +if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { |
| 12 | + die; |
| 13 | +} |
| 14 | + |
| 15 | +/* |
| 16 | + * Only remove ALL data if DT_REMOVE_ALL_DATA constant is set to true in user's |
| 17 | + * wp-config.php. This is to prevent data loss when deleting the plugin from the backend |
| 18 | + * and to ensure only the site owner can perform this action. |
| 19 | + */ |
| 20 | +if ( defined( 'DT_REMOVE_ALL_DATA' ) && true === DT_REMOVE_ALL_DATA ) { |
| 21 | + global $wpdb; |
| 22 | + |
| 23 | + /** |
| 24 | + * Function to delete all relevant data from the site. |
| 25 | + */ |
| 26 | + function dt_delete_data() { |
| 27 | + global $wpdb; |
| 28 | + |
| 29 | + // Delete post meta and posts of type 'dt_subscription'. |
| 30 | + $subscription_post_ids = $wpdb->get_col( |
| 31 | + $wpdb->prepare( |
| 32 | + "SELECT ID FROM $wpdb->posts WHERE post_type = %s", |
| 33 | + 'dt_subscription' |
| 34 | + ) |
| 35 | + ); |
| 36 | + |
| 37 | + if ( ! empty( $subscription_post_ids ) ) { |
| 38 | + $ids_string = implode( ',', array_map( 'intval', $subscription_post_ids ) ); |
| 39 | + |
| 40 | + // Delete subscription meta. |
| 41 | + $wpdb->query( |
| 42 | + "DELETE FROM $wpdb->postmeta WHERE post_id IN ($ids_string)" |
| 43 | + ); |
| 44 | + |
| 45 | + // Delete subscription posts. |
| 46 | + $wpdb->query( |
| 47 | + $wpdb->prepare( |
| 48 | + "DELETE FROM $wpdb->posts WHERE ID IN (%s)", |
| 49 | + $ids_string |
| 50 | + ) |
| 51 | + ); |
| 52 | + |
| 53 | + // Clear the post cache. |
| 54 | + wp_cache_set_posts_last_changed(); |
| 55 | + wp_cache_delete_multiple( $subscription_post_ids, 'posts' ); |
| 56 | + wp_cache_delete_multiple( $subscription_post_ids, 'post_meta' ); |
| 57 | + } |
| 58 | + |
| 59 | + // Delete relevant options from the options table. |
| 60 | + delete_site_options(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Delete all relevant options from the options table. |
| 65 | + */ |
| 66 | + function delete_site_options() { |
| 67 | + global $wpdb; |
| 68 | + |
| 69 | + $option_prefixes = array( |
| 70 | + 'dt_', |
| 71 | + '_transient_dt_', |
| 72 | + '_transient_timeout_dt_', |
| 73 | + ); |
| 74 | + |
| 75 | + // Site transients can be used on single sites too. |
| 76 | + // See https://github.com/10up/distributor/pull/540#discussion_r1759587692 |
| 77 | + if ( ! is_multisite() ) { |
| 78 | + $option_prefixes = array_merge( |
| 79 | + $option_prefixes, |
| 80 | + array( |
| 81 | + '_site_transient_dt_', |
| 82 | + '_site_transient_timeout_dt_', |
| 83 | + ) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + // Prepare the WHERE clause for the options table. |
| 88 | + $where_clause = implode( ' OR ', array_fill( 0, count( $option_prefixes ), 'option_name LIKE %s' ) ); |
| 89 | + |
| 90 | + // Prepare the query. |
| 91 | + $query = $wpdb->prepare( |
| 92 | + sprintf( |
| 93 | + "SELECT option_id, option_name FROM $wpdb->options WHERE %s;", |
| 94 | + $where_clause |
| 95 | + ), |
| 96 | + array_map( |
| 97 | + function( $prefix ) use ( $wpdb ) { |
| 98 | + return $wpdb->esc_like( $prefix ) . '%'; |
| 99 | + }, |
| 100 | + $option_prefixes |
| 101 | + ) |
| 102 | + ); |
| 103 | + |
| 104 | + // Fetch the options to delete. |
| 105 | + $options_to_delete = $wpdb->get_results( $query, ARRAY_A ); |
| 106 | + |
| 107 | + if ( ! empty( $options_to_delete ) ) { |
| 108 | + // Collect IDs from fetched options. |
| 109 | + $ids = array_column( $options_to_delete, 'option_id' ); |
| 110 | + $ids_string = implode( ',', array_map( 'intval', $ids ) ); |
| 111 | + |
| 112 | + // Delete the options using the retrieved IDs. |
| 113 | + $wpdb->query( |
| 114 | + $wpdb->prepare( |
| 115 | + "DELETE FROM $wpdb->options WHERE option_id IN ($ids_string)" |
| 116 | + ) |
| 117 | + ); |
| 118 | + |
| 119 | + // Flush the options cache. |
| 120 | + $option_names = array_column( $options_to_delete, 'option_name' ); |
| 121 | + wp_cache_delete_multiple( $option_names, 'options' ); |
| 122 | + |
| 123 | + // Flush the alloptions cache. |
| 124 | + wp_cache_delete( 'alloptions', 'options' ); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Delete all relevant options from the sitemeta table (multisite only). |
| 130 | + */ |
| 131 | + function delete_sitemeta_options() { |
| 132 | + global $wpdb; |
| 133 | + |
| 134 | + $option_prefixes = array( |
| 135 | + 'dt_', |
| 136 | + '_site_transient_dt_', |
| 137 | + '_site_transient_timeout_dt_', |
| 138 | + ); |
| 139 | + |
| 140 | + // Prepare the WHERE clause for the sitemeta table. |
| 141 | + $where_clause = implode( ' OR ', array_fill( 0, count( $option_prefixes ), 'meta_key LIKE %s' ) ); |
| 142 | + |
| 143 | + $site_id = get_current_network_id(); |
| 144 | + |
| 145 | + $query = $wpdb->prepare( |
| 146 | + sprintf( |
| 147 | + "SELECT meta_id, meta_key FROM $wpdb->sitemeta WHERE site_id = %%d AND (%s);", |
| 148 | + $where_clause |
| 149 | + ), |
| 150 | + array_merge( |
| 151 | + [ $site_id ], |
| 152 | + array_map( |
| 153 | + function( $prefix ) use ( $wpdb ) { |
| 154 | + return $wpdb->esc_like( $prefix ) . '%'; |
| 155 | + }, |
| 156 | + $option_prefixes |
| 157 | + ) |
| 158 | + ) |
| 159 | + ); |
| 160 | + |
| 161 | + // Fetch the sitemeta to delete. |
| 162 | + $sitemeta_to_delete = $wpdb->get_results( $query, ARRAY_A ); |
| 163 | + |
| 164 | + if ( ! empty( $sitemeta_to_delete ) ) { |
| 165 | + // Collect IDs from fetched options. |
| 166 | + $ids = array_column( $sitemeta_to_delete, 'meta_id' ); |
| 167 | + $ids_string = implode( ',', array_map( 'intval', $ids ) ); |
| 168 | + |
| 169 | + // Delete the sitemeta using the retrieved IDs. |
| 170 | + $wpdb->query( |
| 171 | + $wpdb->prepare( |
| 172 | + "DELETE FROM $wpdb->sitemeta WHERE meta_id IN ($ids_string)" |
| 173 | + ) |
| 174 | + ); |
| 175 | + |
| 176 | + // Flush the site options cache. |
| 177 | + $key_names = array_column( $sitemeta_to_delete, 'meta_key' ); |
| 178 | + $key_names = array_map( |
| 179 | + function( $key ) use ( $site_id ) { |
| 180 | + return $site_id . ':' . $key; |
| 181 | + }, |
| 182 | + $key_names |
| 183 | + ); |
| 184 | + wp_cache_delete_multiple( $key_names, 'site-options' ); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + // Check if it's a multisite installation. |
| 189 | + if ( is_multisite() ) { |
| 190 | + // Loop through each site in the network. |
| 191 | + $sites = get_sites(); |
| 192 | + foreach ( $sites as $site ) { |
| 193 | + switch_to_blog( $site->blog_id ); |
| 194 | + dt_delete_data(); |
| 195 | + restore_current_blog(); |
| 196 | + } |
| 197 | + |
| 198 | + // Delete network-wide sitemeta options. |
| 199 | + delete_sitemeta_options(); |
| 200 | + } else { |
| 201 | + // Single site. |
| 202 | + dt_delete_data(); |
| 203 | + } |
| 204 | +} |
| 205 | + |
0 commit comments