/** * Filters the meta map process to handle complex capability checks. * * Global admins have all capabilities. This function ensures that as well (in the rare case that * a global admin is not technically a super admin for a specific network). Regarding this hacky * approach in the function, there might be a better alternative... * * @since 1.0.0 * * @param array $caps Returns the user's actual capabilities. * @param string $cap Capability name. * @param int $user_id The user ID. * @param array $args Adds the context to the cap. Typically the object ID. * @return array The mapped capabilities. */ function ga_map_meta_cap($caps, $cap, $user_id, $args) { if (!is_multinetwork()) { return $caps; } switch ($cap) { case 'list_networks': case 'create_networks': case 'delete_networks': $caps = array('manage_networks'); break; case 'create_user_signups': case 'edit_user_signups': case 'activate_signup': case 'delete_signup': case 'edit_signup': case 'resend_signup': $caps = array('manage_user_signups'); break; case 'edit_user': if (!current_user_can('manage_global_users') && isset($args[0])) { $user = get_userdata($args[0]); if ($user->has_cap('manage_global_users')) { $caps[] = 'do_not_allow'; } } break; } return $caps; }
/** * Adjusts the class that the WP Network Roles plugin uses to replace any WP_User instances. * * @since 1.0.0 * @access private * * @param string $class_name Original class name. * @return string Modified class name. */ function _ga_adjust_user_class_name($class_name) { if (!is_multinetwork()) { return $class_name; } return 'WP_User_With_Network_And_Global_Roles'; }
/** * Adjusts the URL to the networks admin page to be part of the Global Administration panel. * * @since 1.0.0 * @access private * * @param string $url The original URL. * @return string The adjusted URL. */ function _ga_adjust_signups_admin_url($url, $admin_url, $r, $args) { if (!is_multinetwork()) { return $url; } $admin_url = global_admin_url('admin.php'); return add_query_arg($r, $admin_url); }
function _ga_initialize_admin_bar_changes() { if (!is_multinetwork()) { return; } add_action('admin_bar_menu', '_ga_adjust_admin_bar_my_sites_menu', 19, 1); if (!is_global_admin()) { return; } remove_action('admin_bar_menu', 'wp_admin_bar_site_menu', 30); remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60); remove_action('admin_bar_menu', 'wp_admin_bar_new_content_menu', 70); add_action('admin_bar_menu', '_ga_adjust_admin_bar_site_menu', 30, 1); }
/** * Initializes the plugin. * * Loads the required files, registers the new DB table, global cache groups and global capabilities. * * @since 1.0.0 */ function ga_init() { if (!function_exists('wp_network_roles')) { add_action('admin_notices', 'ga_requirements_notice_network_roles'); add_action('network_admin_notices', 'ga_requirements_notice_network_roles'); return; } define('GA_PATH', plugin_dir_path(__FILE__)); define('GA_URL', plugin_dir_url(__FILE__)); require_once GA_PATH . 'global-admin/wp-includes/load.php'; require_once GA_PATH . 'global-admin/wp-includes/option.php'; require_once GA_PATH . 'global-admin/wp-includes/class-wp-global-role.php'; require_once GA_PATH . 'global-admin/wp-includes/class-wp-global-roles.php'; require_once GA_PATH . 'global-admin/wp-includes/capabilities.php'; require_once GA_PATH . 'global-admin/wp-includes/class-wp-user-with-network-and-global-roles.php'; require_once GA_PATH . 'global-admin/wp-includes/user.php'; require_once GA_PATH . 'global-admin/wp-includes/link-template.php'; require_once GA_PATH . 'global-admin/wp-includes/admin-bar.php'; require_once GA_PATH . 'global-admin/wp-includes/ms-functions.php'; require_once GA_PATH . 'global-admin/wp-includes/ms-default-filters.php'; require_once GA_PATH . 'global-admin/wp-admin/includes/schema.php'; require_once GA_PATH . 'global-admin/wp-admin/includes/hacks.php'; require_once ABSPATH . 'wp-admin/includes/plugin.php'; if (is_plugin_active('wp-multi-network/wpmn-loader.php')) { require_once GA_PATH . 'global-admin/multi-network-compat.php'; } if (is_plugin_active('wp-user-signups/wp-user-signups.php')) { require_once GA_PATH . 'global-admin/user-signups-compat.php'; } if (is_multinetwork()) { ga_register_table(); add_action('setup_theme', 'ga_setup_wp_global_roles', 1); } if (function_exists('wp_cache_add_global_groups')) { wp_cache_add_global_groups(array('global-options', 'global-transient')); } }
/** * Adjusts the detection of which networks belong to a user. * * Users who are a global admin have full capabilities on all networks. * * @since 1.0.0 * @access private * * @param array|null $networks Original array of network IDs or null. * @param int $user_id User ID to get networks for. * @return array|false Array of network IDs or false if no IDs. */ function _ga_user_has_networks($networks, $user_id) { if (!is_multinetwork()) { return $networks; } $all_networks = get_networks(array('fields' => 'ids')); $user = get_user_by('id', $user_id); if ($user->has_cap('manage_networks')) { $user_networks = $all_networks; } else { $user = get_userdata($user_id); $user_networks = array(); foreach ($all_networks as $network_id) { $network_admins = get_network_option($network_id, 'site_admins', array()); if (in_array($user->user_login, $network_admins, true)) { $user_networks[] = $network_id; } } } if (empty($user_networks)) { $user_networks = false; } return $user_networks; }
<?php /** * Global Freedoms administration panel. * * @package GlobalAdmin * @since 1.0.0 */ /** Load WordPress Administration Bootstrap */ require_once dirname(__FILE__) . '/admin.php'; if (!is_multinetwork()) { wp_die(__('Multinetwork support is not enabled.', 'global-admin')); } require ABSPATH . 'wp-admin/freedoms.php';
/** * Retrieve the SQL for creating database tables. * * @since 1.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $scope Optional. The tables for which to retrieve SQL. Can be all, global, ms_global, or blog tables. Defaults to all. * @param int $blog_id Optional. The site ID for which to retrieve SQL. Default is the current site ID. * @return string The SQL needed to create the requested tables. */ function ga_get_db_schema($scope = 'all', $blog_id = null) { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); // Engage multinetwork if in the middle of turning it on from global.php. $is_multinetwork = is_multinetwork() || defined('WP_INSTALLING_GLOBAL') && WP_INSTALLING_GLOBAL; $max_index_length = 191; $mn_global_tables = "CREATE TABLE {$wpdb->global_options} (\n option_id bigint(20) unsigned NOT NULL auto_increment,\n option_name varchar(191) NOT NULL default '',\n option_value longtext NOT NULL,\n autoload varchar(20) NOT NULL default 'yes',\n PRIMARY KEY (option_id),\n UNIQUE KEY option_name (option_name)\n) {$charset_collate};\n"; switch ($scope) { case 'blog': $queries = ''; break; case 'global': $queries = ''; if ($is_multinetwork) { $queries .= $mn_global_tables; } break; case 'mn_global': $queries = $mn_global_tables; break; case 'all': default: $queries = ''; if ($is_multinetwork) { $queries .= $mn_global_tables; } break; } return $queries; }
<?php /** * Sets up the default filters and actions for Multisite. * * If you need to remove a default hook, this file will give you the priority * for which to use to remove the hook. * * Not all of the Multisite default hooks are found in ms-default-filters.php * * @package GlobalAdmin * @since 1.0.0 */ if (is_multinetwork()) { add_action('admin_init', 'wp_schedule_update_global_counts'); add_action('update_global_counts', 'wp_update_global_counts'); foreach (array('user_register', 'deleted_user', 'wpmu_new_user', 'make_spam_user', 'make_ham_user') as $action) { add_action($action, 'wp_maybe_update_global_user_counts'); } foreach (array('make_spam_blog', 'make_ham_blog', 'archive_blog', 'unarchive_blog', 'make_delete_blog', 'make_undelete_blog') as $action) { add_action($action, 'wp_maybe_update_global_site_counts'); } unset($action); }
?> <div class="wrap"> <h1><?php echo esc_html($title); ?> </h1> <?php if ($_POST) { check_admin_referer('install-global-1'); require_once ABSPATH . 'wp-admin/includes/upgrade.php'; // Create global table. install_global(); $result = populate_global(sanitize_email($_POST['email']), wp_unslash($_POST['global_name'])); if (is_wp_error($result)) { global_step1($result); } else { global_step2(); } } elseif (is_multinetwork() || global_table_check()) { global_step2(); } else { global_step1(); } ?> </div> <?php if ($_ga_load_admin_files) { include ABSPATH . 'wp-admin/admin-footer.php'; }
function delete_global_option($option) { global $wpdb; $option = trim($option); if (empty($option)) { return false; } wp_protect_special_option($option); // Get the ID, if no ID then return $row = $wpdb->get_row($wpdb->prepare("SELECT autoload FROM {$wpdb->global_options} WHERE option_name = %s", $option)); if (is_null($row)) { return false; } /** * Fires immediately before a global option is deleted. * * The dynamic portion of the hook name, `$option`, refers to the option name. * * @since 1.0.0 * * @param string $option Name of the option to delete. */ do_action('pre_delete_global_option_' . $option, $option); if (!is_multinetwork()) { $result = delete_network_option(null, $option); } else { $result = $wpdb->delete($wpdb->global_options, array('option_name' => $option)); if (!wp_installing()) { if ('yes' == $row->autoload) { $alloptions = wp_load_global_alloptions(); if (is_array($alloptions) && isset($alloptions[$option])) { unset($alloptions[$option]); wp_cache_set('alloptions', $alloptions, 'global-options'); } } else { wp_cache_delete($option, 'global-options'); } } } if ($result) { /** * Fires after a specific global option has been deleted. * * The dynamic portion of the hook name, `$option`, refers to the option name. * * @since 1.0.0 * * @param string $option Name of the deleted option. */ do_action("delete_global_option_{$option}", $option); /** * Fires after a global option has been deleted. * * @since 1.0.0 * * @param string $option Name of the deleted option. */ do_action('deleted_global_option', $option); return true; } return false; }
/** * Determine whether this is a multinetwork install or not * Will only return true if the is_multinetwork() & the add_mnetwork_option() functions exist * @return bool whether this is a multi-network install capable of handling multi-network options */ function is_multinetwork() { return function_exists('is_multinetwork') && function_exists('add_mnetwork_option') && is_multinetwork(); }
function global_step2() { $main_network_id = get_main_network_id(); $main_network = get_network($main_network_id); $hostname = $main_network->domain; $abspath_fix = str_replace('\\', '/', ABSPATH); $location_of_wp_config = $abspath_fix; if (!file_exists(ABSPATH . 'wp-config.php') && file_exists(dirname(ABSPATH) . '/wp-config.php')) { $location_of_wp_config = dirname($abspath_fix); } $location_of_wp_config = trailingslashit($location_of_wp_config); if ($_POST || !is_multinetwork()) { ?> <h3><?php esc_html_e('Enabling the Multinetwork'); ?> </h3> <p><?php _e('Complete the following steps to enable the global admin panel for creating multiple networks.'); ?> </p> <div class="updated inline"><p> <?php printf(__('We recommend you back up your existing %s file.', 'global-admin'), '<code>wp-config.php</code>'); ?> </p></div> <?php } else { ?> <p><?php _e('The original configuration steps are shown here for reference.', 'global-admin'); ?> </p> <?php } ?> <ol> <li> <p><?php printf(__('Add the following to your %1$s file in %2$s <strong>above</strong> the line reading %3$s:', 'global-admin'), '<code>wp-config.php</code>', '<code>' . $location_of_wp_config . '</code>', '<code>/* ' . __('That’s all, stop editing! Happy blogging.', 'global-admin') . ' */</code>'); ?> </p> <textarea class="code" readonly="readonly" cols="100" rows="2"> define('MULTINETWORK', true); </textarea> </li> </ol> <?php if (!is_multinetwork()) { ?> <p><?php _e('Once you complete these steps, your multinetwork is enabled and configured.', 'global-admin'); ?> </p> <?php } }
/** * Adjusts the user count option to only include users in the network. * * This is a hacky way to fix `wp_update_network_user_counts()` which does not count users in the current network, * but instead counts all users in the entire setup which is incorrect. * * @since 1.0.0 * @access private * * @param int $user_count The original number of users. * @return int The modified number of users. */ function _ga_fix_network_user_counts($user_count) { global $wpdb; if (!is_multinetwork()) { return $user_count; } //TODO: When the time is right, this function should use the network_id argument that WP Network Roles introduces. $site_ids = get_sites(array('fields' => 'ids', 'network_id' => get_current_network_id())); $args = array('number' => 20, 'meta_query' => array('relation' => 'OR')); foreach ($site_ids as $site_id) { $args['meta_query'][] = array('key' => $wpdb->get_blog_prefix($site_id) . 'capabilities', 'compare' => 'EXISTS'); } $user_query = new WP_User_Query($args); return $user_query->total_users; }
function global_admin_url($path = '', $scheme = 'admin') { if (!is_multinetwork()) { return network_admin_url($path, $scheme); } $plugin_dir_relative = str_replace(home_url('/'), '', GA_URL); $url = global_home_url($plugin_dir_relative . 'global-admin/wp-admin/global/', $scheme); // This would be used if it was part of Core. //$url = global_site_url( 'wp-admin/global/', $scheme ); if ($path && is_string($path)) { $url .= ltrim($path, '/'); } /** * Filters the global admin URL. * * @since 1.0.0 * * @param string $url The complete global admin URL including scheme and path. * @param string $path Path relative to the global admin URL. Blank string if * no path is specified. */ return apply_filters('global_admin_url', $url, $path); }
/** * Adds the Global Setup screen to the network administration menu if necessary. * * If it was in Core, that would happen directly in `wp-admin/network/menu.php`. * * @since 1.0.0 * @access private */ function _ga_add_global_setup_menu_item() { if (is_multinetwork() && !is_main_network()) { //TODO: add Delete Network screen } if (defined('WP_ALLOW_MULTINETWORK') && WP_ALLOW_MULTINETWORK && !is_multinetwork()) { add_submenu_page('settings.php', __('Global Setup', 'global-admin'), __('Global Setup', 'global-admin'), 'manage_networks', GA_PATH . 'global-admin/wp-admin/network/global.php'); } }