| | 193 | /** |
|---|
| | 194 | * get_plugin_assets() retrieves the plugin's registered assets from the option |
|---|
| | 195 | * |
|---|
| | 196 | * Part of the uninstall process. |
|---|
| | 197 | * This function checks whether the plugin author has registered any assets |
|---|
| | 198 | * that can be uninstalled and if so returns a named array with those options. |
|---|
| | 199 | * |
|---|
| | 200 | * @link http://trac.wordpress.org/ticket/5625 |
|---|
| | 201 | * |
|---|
| | 202 | * @param string $plugin_file the path to the plugin file, usually given by __FILE__ |
|---|
| | 203 | * @return array|bool the plugin's uninstallable assets, or false if there are none |
|---|
| | 204 | * |
|---|
| | 205 | */ |
|---|
| | 206 | function get_plugin_assets( $plugin_file ) { |
|---|
| | 207 | $plugin_assets = get_option( 'plugin_assets' ); |
|---|
| | 208 | $plugin = plugin_basename( $plugin_file ); |
|---|
| | 209 | if ( !is_array( $plugin_assets ) ) |
|---|
| | 210 | return false; |
|---|
| | 211 | if ( isset( $plugin_assets[$plugin] ) ) { |
|---|
| | 212 | return $plugin_assets[$plugin]; |
|---|
| | 213 | } else { |
|---|
| | 214 | return false; |
|---|
| | 215 | } |
|---|
| | 216 | } |
|---|
| | 217 | |
|---|
| | 218 | /** |
|---|
| | 219 | * is_plugin_uninstallable() checks if a plugin has registered any uninstallable assets |
|---|
| | 220 | * |
|---|
| | 221 | * Part of the uninstall process. |
|---|
| | 222 | * This function checks whether the plugin author has registered any assets |
|---|
| | 223 | * that can be uninstalled. |
|---|
| | 224 | * |
|---|
| | 225 | * @link http://trac.wordpress.org/ticket/5625 |
|---|
| | 226 | * |
|---|
| | 227 | * @param string $plugin_file the path to the plugin file, usually given by __FILE__ |
|---|
| | 228 | * @return bool true if the plugin has assets that can be uninstalled |
|---|
| | 229 | * |
|---|
| | 230 | */ |
|---|
| | 231 | function is_plugin_uninstallable( $plugin_file ){ |
|---|
| | 232 | $uninstallable_plugins = get_option( 'plugin_assets' ); |
|---|
| | 233 | $plugin = plugin_basename( $plugin_file ); |
|---|
| | 234 | if ( isset( $uninstallable_plugins[$plugin] ) ) { |
|---|
| | 235 | return true; |
|---|
| | 236 | } else { |
|---|
| | 237 | return false; |
|---|
| | 238 | } |
|---|
| | 239 | } |
|---|
| | 240 | |
|---|
| | 241 | |
|---|
| | 242 | /** |
|---|
| | 243 | * delete_plugin_assets() - Delete the database tables and options created by a plugin |
|---|
| | 244 | * |
|---|
| | 245 | * Part of the uninstall process. |
|---|
| | 246 | * This function retrieves the list of assets the plugin author registered, using |
|---|
| | 247 | * register_plugin_assets and removes the database tables and options specified. |
|---|
| | 248 | * |
|---|
| | 249 | * @link http://trac.wordpress.org/ticket/5625 |
|---|
| | 250 | * |
|---|
| | 251 | * @param string $plugin_file the path to the plugin |
|---|
| | 252 | * @return bool returns true if the tables and options are no longer present |
|---|
| | 253 | */ |
|---|
| | 254 | function delete_plugin_assets( $plugin_file ){ |
|---|
| | 255 | global $wpdb; |
|---|
| | 256 | $deletion_complete = true; |
|---|
| | 257 | $uninstallable_plugins = get_option( 'plugin_assets' ); |
|---|
| | 258 | $plugin = plugin_basename( $plugin_file ); |
|---|
| | 259 | if ( isset( $uninstallable_plugins[$plugin] ) ) { |
|---|
| | 260 | $plugin_assets = $uninstallable_plugins[$plugin]; |
|---|
| | 261 | //remove tables |
|---|
| | 262 | $remove_table_sql = 'DROP TABLE IF EXISTS `%s`'; |
|---|
| | 263 | $check_table_sql = 'SELECT 1 FROM `%s` LIMIT 0'; |
|---|
| | 264 | foreach( $plugin_assets['tables'] as $table ){ |
|---|
| | 265 | //filter |
|---|
| | 266 | $table = str_replace( '`' , '' , $table); |
|---|
| | 267 | $table = str_replace( ';' , '' , $table); |
|---|
| | 268 | $table = str_replace( '*' , '' , $table); |
|---|
| | 269 | |
|---|
| | 270 | //check against core tables |
|---|
| | 271 | if ( !in_array( str_replace( $wpdb->prefix , '' , $table ) , $wpdb->tables ) ) { |
|---|
| | 272 | $sql = sprintf( $remove_table_sql , $table ); |
|---|
| | 273 | $wpdb->query( $sql ); |
|---|
| | 274 | //check if table is now gone |
|---|
| | 275 | if ( $wpdb->query( sprintf( $check_table_sql , $table ) ) ) { |
|---|
| | 276 | $deletion_complete = false; |
|---|
| | 277 | } |
|---|
| | 278 | } |
|---|
| | 279 | } |
|---|
| | 280 | //remove options |
|---|
| | 281 | foreach( $plugin_assets['options'] as $option ){ |
|---|
| | 282 | delete_option( $option ); |
|---|
| | 283 | if ( get_option( $option ) ) { |
|---|
| | 284 | $deletion_complete = false; |
|---|
| | 285 | }; |
|---|
| | 286 | } |
|---|
| | 287 | //remove assets from asset list |
|---|
| | 288 | if ( $deletion_complete ) { |
|---|
| | 289 | unregister_plugin_assets( $plugin_file ); |
|---|
| | 290 | } |
|---|
| | 291 | } |
|---|
| | 292 | return $deletion_complete; |
|---|
| | 293 | } |
|---|
| | 294 | |
|---|
| | 295 | |
|---|