PluginProbe
SQLite Database Integration / 3.0.1
SQLite Database Integration v3.0.1
3.0.1 trunk 2.1.13 2.1.14 2.1.15 2.1.16 2.2.0 2.2.1 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.2.2 2.2.20 2.2.21 2.2.22 2.2.23 2.2.3 2.2.4 All 31 releases
sqlite-database-integration / capabilities.php

capabilities.php in SQLite Database Integration 3.0.1, at capabilities.php

81 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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