array(...), * 'subsite' => array(...), * ) * * By default, an array of transients will be treated as site wide. * */ protected $transients; /** * @var array Blog(s) in site */ protected $blog_ids; /** * WP_AWS_Uninstall constructor. * * @param array|string $options * @param array|string $postmeta * @param array|string $crons * @param array|string $transients */ public function __construct( $options = array(), $postmeta = array(), $crons = array(), $transients = array() ) { $this->options = $this->maybe_convert_to_array( $options ); $this->postmeta = $this->maybe_convert_to_array( $postmeta ); $this->crons = $this->maybe_convert_to_array( $crons ); $this->transients = $this->maybe_convert_to_array( $transients ); $this->set_blog_ids(); $this->delete_options(); $this->delete_postmeta(); $this->clear_crons(); $this->delete_transients(); } /** * Set the blog id(s) for a site */ private function set_blog_ids() { $blog_ids = array( 1 ); if ( function_exists( 'is_multisite' ) && is_multisite() ) { $args = array( 'limit' => false, 'spam' => 0, 'deleted' => 0, 'archived' => 0, ); $blogs = wp_get_sites( $args ); $blog_ids = wp_list_pluck( $blogs, 'blog_id' ); } $this->blog_ids = $blog_ids; } /** * Is the current blog ID that specified in wp-config.php * * @param int $blog_id * * @return bool */ private function is_current_blog( $blog_id ) { $default = defined( 'BLOG_ID_CURRENT_SITE' ) ? BLOG_ID_CURRENT_SITE : 1; if ( $default === $blog_id ) { return true; } return false; } /** * Helper to ensure a value is an array * * @param array|string $data * * @return array */ private function maybe_convert_to_array( $data ) { if ( ! is_array( $data ) ) { // Convert a string to an array $data = array( $data ); } return $data; } /** * Delete site wide options */ public function delete_options() { foreach ( $this->options as $option ) { delete_site_option( $option ); } } /** * Delete post meta data for all blogs */ public function delete_postmeta() { global $wpdb; foreach ( $this->blog_ids as $blog_id ) { $prefix = $wpdb->get_blog_prefix( $blog_id ); foreach ( $this->postmeta as $postmeta ) { $sql = $wpdb->prepare( "DELETE FROM {$prefix}postmeta WHERE meta_key = %s", $postmeta ); $wpdb->query( $sql ); } } } /** * Clear any scheduled cron jobs */ public function clear_crons() { foreach ( $this->crons as $cron ) { $timestamp = wp_next_scheduled( $cron ); if ( $timestamp ) { wp_unschedule_event( $timestamp, $cron ); } } } /** * Delete transients */ public function delete_transients() { if ( ! isset( $this->transients['site'] ) && ! isset( $this->transients['subsite'] ) ) { // Single array of site wide transients foreach ( $this->transients as $transient ) { delete_site_transient( $transient ); } return; } // Deal with site wide transients if ( isset( $this->transients['site'] ) ) { $site_transients = $this->maybe_convert_to_array( $this->transients['site'] ); foreach ( $site_transients as $transient ) { delete_site_transient( $transient ); } } // Deal with subsite specific transients if ( isset( $this->transients['subsite'] ) ) { $subsite_transients = $this->maybe_convert_to_array( $this->transients['subsite'] ); foreach ( $this->blog_ids as $blog_id ) { if ( is_multisite() && ! $this->is_current_blog( $blog_id ) ) { switch_to_blog( $blog_id ); } foreach ( $subsite_transients as $transient ) { delete_transient( $transient ); } } if ( is_multisite() && ! $this->is_current_blog( $blog_id ) ) { restore_current_blog(); } } } } }