'ids', 'number' => 0 ) ) as $site_id ) { switch_to_blog( $site_id ); waymorphic_map_make_backup(); restore_current_blog(); } } else { waymorphic_map_make_backup(); } } /** * Records a copy of the plugin that is active but does not operate, because * WordPress loaded a different copy before it. * * @param string $file The main file of the copy. Empty: record nothing. * @return string[] The recorded copies, each as plugin_basename() gives it. */ function waymorphic_map_idle_copies( $file = '' ) { static $files = array(); if ( '' !== $file ) { $files[] = plugin_basename( $file ); } return $files; } /** * The active copies of the plugin that are not necessary: all the active * copies but the one with the highest version. * * @return array> For each copy: file (as plugin_basename() gives it), name and version. */ function waymorphic_map_extra_copies() { if ( ! defined( 'WAYMORPHIC_MAP_FILE' ) || ! function_exists( 'is_plugin_active' ) || ! function_exists( 'get_plugins' ) ) { return array(); } // The copy that operates is first. Then the copies of version 1.2.2 or later that do not // operate, and the two versions with other folder names. Version 1.1.0 loads after this // version: it does nothing but write PHP warnings in each request. $files = array_merge( array( plugin_basename( WAYMORPHIC_MAP_FILE ) ), waymorphic_map_idle_copies(), array( 'wp-free-maps/wp-free-maps.php', 'free-maps-waymorphic-map/free-maps-waymorphic-map.php' ) ); // The copies that are active. One copy is the usual condition: there is nothing more to do. $active = array(); foreach ( array_unique( $files ) as $file ) { if ( is_plugin_active( $file ) ) { $active[] = $file; } } if ( count( $active ) < 2 ) { return array(); } // The name and the version of each copy, from the list of the plugins that WordPress // has. A copy that is not in the list has no file: WordPress does not load it. $plugins = get_plugins(); $copies = array(); foreach ( $active as $file ) { if ( isset( $plugins[ $file ]['Name'], $plugins[ $file ]['Version'] ) ) { $copies[] = array( 'file' => $file, 'name' => $plugins[ $file ]['Name'], 'version' => $plugins[ $file ]['Version'], ); } } if ( count( $copies ) < 2 ) { return array(); } // Keep the copy with the highest version. Two copies with the same version: keep the copy // in the folder of the plugin directory, because it gets the updates. If there is none, // keep the first copy, which operates. $keep = 0; foreach ( $copies as $index => $copy ) { $order = version_compare( $copy['version'], $copies[ $keep ]['version'] ); if ( $order > 0 || ( 0 === $order && 'waymorphic-map/waymorphic-map.php' === $copy['file'] ) ) { $keep = $index; } } unset( $copies[ $keep ] ); return array_values( $copies ); } /** * Tells the administrator that more than one copy of the plugin is active, * and which copy to deactivate. The notice shows on the Plugins screen, the * Dashboard and the settings page of the plugin, to a user who can * deactivate that copy. It goes away when one copy stays active. */ function waymorphic_map_extra_copies_notice() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; $screens = array( 'plugins', 'plugins-network', 'dashboard', 'dashboard-network', 'settings_page_waymorphic-map' ); if ( ! $screen || ! in_array( $screen->id, $screens, true ) ) { return; } $text = array(); $network = false; foreach ( waymorphic_map_extra_copies() as $copy ) { // A copy that is active for the network is deactivated in the network administration. $for_network = is_multisite() && is_plugin_active_for_network( $copy['file'] ); if ( ! current_user_can( $for_network ? 'manage_network_plugins' : 'activate_plugins' ) ) { continue; } $network = $network || $for_network; $text[] = sprintf( /* translators: 1: the name of a plugin, 2: its version number, 3: the name of its folder. */ __( 'Deactivate "%1$s" (version %2$s, in the folder %3$s), then delete it.', 'waymorphic-map' ), $copy['name'], $copy['version'], dirname( $copy['file'] ) ); } if ( ! $text ) { return; } array_unshift( $text, __( 'More than one copy of this plugin is active, and only one copy can operate.', 'waymorphic-map' ) ); $text[] = __( 'Your settings and your maps stay.', 'waymorphic-map' ); echo '

' . esc_html__( 'Waymorphic Map:', 'waymorphic-map' ) . ' ' . esc_html( implode( ' ', $text ) ); if ( ( $network ? 'plugins-network' : 'plugins' ) !== $screen->id ) { // The user is not on the Plugins screen that has the copy. echo ' ' . esc_html__( 'Go to the Plugins screen', 'waymorphic-map' ) . ''; } echo '

'; }