| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Require SQLite management permission to deactivate an active integration. |
| 5 |
* |
| 6 |
* WordPress checks this capability when one plugin is deactivated, including |
| 7 |
* each plugin in a site-level batch. It skips this per-plugin check for a |
| 8 |
* Network Admin batch, so that case is handled separately below. |
| 9 |
* |
| 10 |
* @access private |
| 11 |
* |
| 12 |
* @param string[] $caps Primitive capabilities required of the user. |
| 13 |
* @param string $cap Capability being checked. |
| 14 |
* @param int $user_id User ID. |
| 15 |
* @param mixed[] $args Additional capability arguments. |
| 16 |
* @return string[] Required primitive capabilities. |
| 17 |
*/ |
| 18 |
function sqlite_plugin_map_deactivation_capability( $caps, $cap, $user_id, $args ) { |
| 19 |
if ( |
| 20 |
'deactivate_plugin' === $cap |
| 21 |
&& isset( $args[0] ) |
| 22 |
&& plugin_basename( SQLITE_MAIN_FILE ) === $args[0] |
| 23 |
&& sqlite_plugin_has_active_dropin() |
| 24 |
) { |
| 25 |
$caps[] = sqlite_plugin_get_manage_capability(); |
| 26 |
} |
| 27 |
return $caps; |
| 28 |
} |
| 29 |
add_filter( 'map_meta_cap', 'sqlite_plugin_map_deactivation_capability', 10, 4 ); |
| 30 |
|
| 31 |
/** |
| 32 |
* WordPress does not check each plugin's permission when deactivating a Network |
| 33 |
* Admin batch. Remove SQLite from the batch when the user cannot manage the |
| 34 |
* network-wide drop-in, while allowing the other selected plugins to continue. |
| 35 |
* |
| 36 |
* @access private |
| 37 |
*/ |
| 38 |
function sqlite_plugin_filter_network_bulk_deactivation() { |
| 39 |
$is_network_bulk_deactivation = is_network_admin() |
| 40 |
&& isset( $_REQUEST['action'] ) |
| 41 |
&& is_string( $_REQUEST['action'] ) |
| 42 |
&& 'deactivate-selected' === wp_unslash( $_REQUEST['action'] ); |
| 43 |
|
| 44 |
if ( |
| 45 |
$is_network_bulk_deactivation |
| 46 |
&& sqlite_plugin_has_active_dropin() |
| 47 |
&& ! current_user_can( sqlite_plugin_get_manage_capability() ) |
| 48 |
) { |
| 49 |
/* |
| 50 |
* Remove only SQLite from the submitted list. WordPress will still check |
| 51 |
* whether the user may run bulk deactivation and whether the request is |
| 52 |
* valid before it deactivates the plugins left in the list. |
| 53 |
*/ |
| 54 |
$plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array(); |
| 55 |
$_POST['checked'] = array_values( array_diff( $plugins, array( plugin_basename( SQLITE_MAIN_FILE ) ) ) ); |
| 56 |
} |
| 57 |
} |
| 58 |
add_action( 'load-plugins.php', 'sqlite_plugin_filter_network_bulk_deactivation' ); |
| 59 |
|
| 60 |
/** |
| 61 |
* Check whether the SQLite database drop-in is active. |
| 62 |
* |
| 63 |
* @access private |
| 64 |
* |
| 65 |
* @return bool Whether the SQLite database drop-in is active. |
| 66 |
*/ |
| 67 |
function sqlite_plugin_has_active_dropin() { |
| 68 |
return defined( 'SQLITE_DB_DROPIN_VERSION' ) && file_exists( WP_CONTENT_DIR . '/db.php' ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get the capability required to manage the SQLite integration. |
| 73 |
* |
| 74 |
* @access private |
| 75 |
* |
| 76 |
* @return string Required capability. |
| 77 |
*/ |
| 78 |
function sqlite_plugin_get_manage_capability() { |
| 79 |
return is_multisite() ? 'manage_network_options' : 'manage_options'; |
| 80 |
} |
| 81 |
|