|
20 | 20 | * https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate/pull/123#issuecomment-28541913
|
21 | 21 | *
|
22 | 22 | * @link https://github.com/Books4Languages/pressbooks-metadata
|
23 |
| - * @since 0.1 |
| 23 | + * @since 0.16 |
24 | 24 | *
|
25 | 25 | * @package Pressbooks_Metadata
|
26 | 26 | */
|
|
29 | 29 | if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
|
30 | 30 | exit;
|
31 | 31 | }
|
| 32 | + |
| 33 | +//declaring global DB connection variable |
| 34 | +global $wpdb; |
| 35 | + |
| 36 | +//get all the sites for multisite, if not a multisite, set blog id to 1 |
| 37 | +if (is_multisite()) { |
| 38 | + $blogs_ids = get_sites(); |
| 39 | +} else { |
| 40 | + $blogs_ids = [1]; |
| 41 | +} |
| 42 | + |
| 43 | +//delete plugin options and posts/chapter related metadata from every site |
| 44 | +foreach( $blogs_ids as $b ){ |
| 45 | + //if multisite, each iteration changes site |
| 46 | + if (is_multisite()) { |
| 47 | + switch_to_blog( $b->blog_id ); |
| 48 | + } |
| 49 | + |
| 50 | + //get all the options from database |
| 51 | + $all_options = wp_load_alloptions(); |
| 52 | + $plugin_options = []; |
| 53 | + $related_meta = []; |
| 54 | + |
| 55 | + //check if PressBooks plugin is used |
| 56 | + if ((@include_once( WP_PLUGIN_DIR . '/pressbooks/pressbooks.php')) && |
| 57 | + //Checking if the plugin is active |
| 58 | + is_plugin_active('pressbooks/pressbooks.php')) { |
| 59 | + $pb = true; |
| 60 | + } |
| 61 | + else{ |
| 62 | + $pb = false; |
| 63 | + } |
| 64 | + |
| 65 | + //gather all post types, including built-in of without PressBooks |
| 66 | + if($pb){ |
| 67 | + $allPostTypes = get_post_types( array( 'public' => true, '_builtin' => false )) ; |
| 68 | + }else{ |
| 69 | + $allPostTypes = get_post_types( array( 'public' => true)) ; |
| 70 | + } |
| 71 | + |
| 72 | + $allPostTypes['site-meta'] = 'site-meta'; |
| 73 | + $allPostTypes['metadata'] = 'metadata'; |
| 74 | + |
| 75 | + |
| 76 | + //extract plugin options from all options |
| 77 | + foreach ( $all_options as $name => $value ) { |
| 78 | + foreach ($allPostTypes as $postType) { |
| 79 | + if ( stristr( $name, '_type_' . $postType ) || stristr( $name, '_type_' . $postType . '_level' ) |
| 80 | + || stristr( $name, '_type_overwrite' ) || stristr($name, 'saoverwr') || stristr($name, $postType.'_checkbox') |
| 81 | + || stristr( $name, 'dublin_checkbox' ) || stristr($name, 'coins_checkbox') || stristr($name, 'educational_checkbox')) { |
| 82 | + $plugin_options[ $name ] = $value; |
| 83 | + |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + //delete plugin options |
| 90 | + foreach ( $plugin_options as $key => $value ) { |
| 91 | + if ( get_option( $key ) || get_option($key, 'nonex') !== 'nonex') { |
| 92 | + delete_option( $key ); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Delete plugin related posts' meta |
| 97 | + //if blog is root, do not add blog number to table name |
| 98 | + $blog_id = $b->blog_id == 1 || $b = 1 ? '' : $b->blog_id.'_'; |
| 99 | + //DELETE query to postmeta database |
| 100 | + $wpdb->query( "DELETE FROM `".$wpdb->prefix.$blog_id."postmeta` WHERE `meta_key` LIKE 'pb%type%'"); |
| 101 | + $wpdb->query( "DELETE FROM `".$wpdb->prefix.$blog_id."postmeta` WHERE `meta_key` LIKE 'pb%vocab%'"); |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | + |
0 commit comments