plugin-visibility.php 13 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
<?php
class amePluginVisibility {
	const OPTION_NAME = 'ws_ame_plugin_visibility';
	const TAB_SLUG = 'plugin-visibility';

	const HIDE_USAGE_NOTICE_FLAG = 'ws_ame_hide_pv_notice';

	private static $lastInstance = null;

	/**
	 * @var WPMenuEditor
	 */
	private $menuEditor;
	private $settings = array();

	public function __construct($menuEditor) {
		$this->menuEditor = $menuEditor;
		self::$lastInstance = $this;

		//Remove "hidden" plugins from the list on the "Plugins -> Installed Plugins" page.
		add_filter('all_plugins', array($this, 'filterPluginList'));

		//It's not possible to completely prevent a user from (de)activating "hidden" plugins because plugin API
		//functions like activate_plugin() and deactivate_plugins() don't provide a way to abort (de)activation.
		//However, we can still block edits and *some* other actions that WP verifies with check_admin_referer().
		add_action('check_admin_referer', array($this, 'authorizePluginAction'));

		//Register the plugin visibility tab.
		add_action('admin_menu_editor-tabs', array($this, 'addSettingsTab'), 20);
		add_action('admin_menu_editor-section-' . self::TAB_SLUG, array($this, 'displayUi'));
		add_action('admin_menu_editor-header', array($this, 'handleFormSubmission'), 10, 2);

		//Enqueue scripts and styles.
		add_action('admin_menu_editor-enqueue_scripts-' . self::TAB_SLUG, array($this, 'enqueueScripts'));
		add_action('admin_menu_editor-enqueue_styles-' . self::TAB_SLUG, array($this, 'enqueueStyles'));

		//Display a usage hint in our tab.
		add_action('admin_notices', array($this, 'displayUsageNotice'));
		$dismissNoticeAction = new ameAjaxAction('ws_ame_dismiss_pv_usage_notice');
		$dismissNoticeAction
			->setAuthCallback(array($this->menuEditor, 'current_user_can_edit_menu'))
			->setHandler(array($this, 'ajaxDismissUsageNotice'));
	}

	public function getSettings() {
		if (!empty($this->settings)) {
			return $this->settings;
		}

		if ( $this->menuEditor->get_plugin_option('menu_config_scope') === 'site' ) {
			$json = get_option(self::OPTION_NAME, null);
		} else {
			$json = get_site_option(self::OPTION_NAME, null);
		}

		if ( is_string($json) ) {
			$settings = json_decode($json, true);
		} else {
			$settings = array();
		}

		$this->settings = array_merge(
			array(
				'plugins' => array(),
				'grantAccessByDefault' => array(),
			),
			$settings
		);

		return $this->settings;
	}

	private function saveSettings() {
		//Save per site or site-wide based on plugin configuration.
		$settings = json_encode($this->settings);
		if ($this->menuEditor->get_plugin_option('menu_config_scope') === 'site') {
			update_option(self::OPTION_NAME, $settings);
		} else {
			WPMenuEditor::atomic_update_site_option(self::OPTION_NAME, $settings);
		}
	}

	/**
	 * Check if a plugin is visible to the current user.
	 *
	 * Goals:
	 *  - You can easily hide a plugin from everyone, including new roles. See: isVisibleByDefault
	 *  - You can configure a role so that new plugins are hidden by default. See: grantAccessByDefault
	 *  - You can change visibility per role and per user, just like with admin menus.
	 *  - Roles that don't have access to plugins are not considered when deciding visibility.
	 *  - Precedence order: user > super admin > all roles.
	 *
	 * @param string $pluginFileName Plugin file name as returned by plugin_basename().
	 * @param WP_User $user Current user.
	 * @return bool
	 */
	private function isPluginVisible($pluginFileName, $user = null) {
		//TODO: Can we refactor this to be shorter?
		static $isMultisite = null;
		if (!isset($isMultisite)) {
			$isMultisite = is_multisite();
		}

		if ($user === null) {
			$user = wp_get_current_user();
		}
		$settings = $this->getSettings();

		//Do we have custom settings for this plugin?
		if (isset($settings['plugins'][$pluginFileName])) {
			$isVisibleByDefault = $settings['plugins'][$pluginFileName]['isVisibleByDefault'];
			$grantAccess = $settings['plugins'][$pluginFileName]['grantAccess'];

			if ($isVisibleByDefault) {
				$grantAccess = array_merge($settings['grantAccessByDefault'], $grantAccess);
			}
		} else {
			$isVisibleByDefault = true;
			$grantAccess = $settings['grantAccessByDefault'];
		}

		//User settings take precedence over everything else.
		$userActor = 'user:' . $user->get('user_login');
		if (isset($grantAccess[$userActor])) {
			return $grantAccess[$userActor];
		}

		//Super Admin is next.
		if ($isMultisite && is_super_admin($user->ID)) {
			//By default the Super Admin has access to everything.
			return ameUtils::get($grantAccess, 'special:super_admin', true);
		}

		//Finally, the user can see the plugin if at least one of their roles can.
		$roles = $this->menuEditor->get_user_roles($user);
		foreach ($roles as $roleId) {
			if (ameUtils::get($grantAccess, 'role:' . $roleId, $isVisibleByDefault && $this->canManagePlugins($roleId))) {
				return true;
			}
		}

		return false;
	}


	/**
	 * @param string $roleId
	 * @param WP_Role $role
	 * @return bool
	 */
	private function canManagePlugins($roleId, $role = null) {
		static $cache = array();

		if (isset($cache[$roleId])) {
			return $cache[$roleId];
		}

		//Any role that has any of the following capabilities has some degree of control over plugins,
		//so plugin visibility settings apply to that role.
		$pluginCaps = array(
			'activate_plugins', 'install_plugins', 'edit_plugins', 'update_plugins', 'delete_plugins',
			'manage_network_plugins',
		);

		if (!isset($role)) {
			$role = get_role($roleId);
		}

		$result = false;
		foreach ($pluginCaps as $cap) {
			if ($role->has_cap($cap)) {
				$result = true;
				break;
			}
		}

		$cache[$roleId] = $result;

		return $result;
	}

	/**
	 * Filter a plugin list by removing plugins that are not visible to the current user.
	 *
	 * @param array $plugins
	 * @return array
	 */
	public function filterPluginList($plugins) {
		$user = wp_get_current_user();

		//Remove all hidden plugins.
		$pluginFileNames = array_keys($plugins);
		foreach($pluginFileNames as $fileName) {
			if ( !$this->isPluginVisible($fileName, $user) ) {
				unset($plugins[$fileName]);
			}
		}

		return $plugins;
	}

	/**
	 * Verify that the current user is allowed to see the plugin that they're trying to edit, activate or deactivate.
	 * Note that this doesn't catch bulk (de-)activation or various plugin management plugins.
	 *
	 * This is a callback for the "check_admin_referer" action.
	 * @param string $action
	 */
	public function authorizePluginAction($action) {
		//Is the user trying to edit a plugin?
		if (preg_match('@^edit-plugin_(?P<file>.+)$@', $action, $matches)) {

			//The file that's being edited is part of a plugin. Find that plugin.
			$fileName = wp_normalize_path($matches['file']);
			$fileDirectory = ameUtils::getFirstDirectory($fileName);
			$selectedPlugin = null;

			$pluginFiles = array_keys(get_plugins());
			foreach ($pluginFiles as $pluginFile) {
				//Is the user editing the main plugin file?
				if ($pluginFile === $fileName) {
					$selectedPlugin = $pluginFile;
					break;
				}

				//Is the file inside this plugin's directory?
				$pluginDirectory = ameUtils::getFirstDirectory($pluginFile);
				if (($pluginDirectory !== null) && ($pluginDirectory === $fileDirectory)) {
					$selectedPlugin = $pluginFile;
					break;
				}
			}

			if ($selectedPlugin !== null) {
				//Can the current user see the selected plugin?
				$isVisible = $this->isPluginVisible($selectedPlugin);

				if (!$isVisible) {
					wp_die('You do not have sufficient permissions to edit this plugin.');
				}
			}

			//Is the user trying to (de-)activate a single plugin?
		} elseif (preg_match('@(?P<action>deactivate|activate)-plugin_(?P<plugin>.+)$@', $action, $matches)) {
			//Can the current user see this plugin?
			$isVisible = $this->isPluginVisible($matches['plugin']);

			if (!$isVisible) {
				wp_die(sprintf(
					'You do not have sufficient permissions to %s this plugin.',
					$matches['action']
				));
			}

			//Are they acting on multiple plugins? One of them might be hidden.
		} elseif (($action === 'bulk-plugins') && isset($_POST['checked']) && is_array($_POST['checked'])) {

			$user = wp_get_current_user();
			foreach ($_POST['checked'] as $pluginFile) {
				if (!$this->isPluginVisible(strval($pluginFile), $user)) {
					wp_die(sprintf(
						'You do not have sufficient permissions to manage this plugin: "%s".',
						$pluginFile
					));
				}
			}
		}
	}

	public function addSettingsTab($tabs) {
		$tabs[self::TAB_SLUG] = 'Plugins';
		return $tabs;
	}

	public function displayUi() {
		require dirname(__FILE__) . '/plugin-visibility-template.php';
	}

	public function handleFormSubmission($action, $post = array()) {
		//Note: We don't need to check user permissions here because plugin core already did.
		if ( $action === 'save_plugin_visibility' ) {
			check_admin_referer($action);

			$this->settings = json_decode($post['settings'], true);
			$this->saveSettings();

			$params = array('updated' => 1);

			//Re-select the same actor.
			if ( !empty($post['selected_actor']) ) {
				$params['selected_actor'] = strval($post['selected_actor']);
			}

			wp_redirect($this->getTabUrl($params));
			exit;
		}
	}

	private function getTabUrl($queryParameters = array()) {
		$queryParameters = array_merge(
			array(
				'page' => 'menu_editor',
				'sub_section' => self::TAB_SLUG
			),
			$queryParameters
		);
		return add_query_arg($queryParameters, admin_url('options-general.php'));
	}

	public function enqueueScripts() {
		wp_register_auto_versioned_script(
			'ame-plugin-visibility',
			plugins_url('plugin-visibility.js', __FILE__),
			array('ame-lodash', 'knockout', 'ame-actor-selector', 'jquery-json',)
		);
		wp_enqueue_script('ame-plugin-visibility');

		//Reselect the same actor.
		$query = $this->menuEditor->get_query_params();
		$selectedActor = null;
		if ( isset($query['selected_actor']) ) {
			$selectedActor = strval($query['selected_actor']);
		}

		$scriptData = $this->getScriptData();
		$scriptData['selectedActor'] = $selectedActor;
		wp_localize_script('ame-plugin-visibility', 'wsPluginVisibilityData', $scriptData);
	}

	public function getScriptData(){
		//Pass the list of installed plugins and their state (active/inactive) to UI JavaScript.
		$installedPlugins = get_plugins();

		$activePlugins = array_map('plugin_basename', wp_get_active_and_valid_plugins());
		$activeNetworkPlugins = array();
		if (function_exists('wp_get_active_network_plugins')) {
			//This function is only available on Multisite.
			$activeNetworkPlugins = array_map('plugin_basename', wp_get_active_network_plugins());
		}

		$plugins = array();
		foreach($installedPlugins as $pluginFile => $header) {
			$isActiveForNetwork = in_array($pluginFile, $activeNetworkPlugins);
			$isActive = in_array($pluginFile, $activePlugins);

			$plugins[] = array(
				'fileName' => $pluginFile,
				'name' => $header['Name'],
				'description' => isset($header['Description']) ? $header['Description'] : '',
				'isActive' => $isActive || $isActiveForNetwork,
			);
		}

		//Flag roles that can manage plugins.
		$canManagePlugins = array();
		$wpRoles = ameRoleUtils::get_roles();
		foreach($wpRoles->role_objects as $id => $role) {
			$canManagePlugins[$id] = $this->canManagePlugins($id, $role);
		}

		return array(
			'settings' => $this->getSettings(),
			'installedPlugins' => $plugins,
			'canManagePlugins' => $canManagePlugins,
			'isMultisite' => is_multisite(),
			'isProVersion' => $this->menuEditor->is_pro_version(),

			'dismissNoticeNonce' => wp_create_nonce('ws_ame_dismiss_pv_usage_notice'),
			'adminAjaxUrl' => admin_url('admin-ajax.php'),
		);
	}

	public function enqueueStyles() {
		wp_enqueue_auto_versioned_style(
			'ame-plugin-visibility-css',
			plugins_url('plugin-visibility.css', __FILE__)
		);
	}

	public function displayUsageNotice() {
		if ( !$this->menuEditor->is_tab_open(self::TAB_SLUG) ) {
			return;
		}

		//If the user has already made some changes, they probably don't need to see this notice any more.
		$settings = $this->getSettings();
		if ( !empty($settings['plugins']) ) {
			return;
		}

		//The notice is dismissible.
		if ( get_site_option(self::HIDE_USAGE_NOTICE_FLAG, false) ) {
			return;
		}

		echo '<div class="notice notice-info is-dismissible" id="ame-pv-usage-notice">
				<p>
					<strong>Tip:</strong> This screen lets you hide plugins from other users. 
					These settings only affect the "Plugins" page, not the admin menu or the dashboard.
				</p>
			 </div>';
	}

	public function ajaxDismissUsageNotice() {
		$result = update_site_option(self::HIDE_USAGE_NOTICE_FLAG, true);
		return array('success' => true, 'updateResult' => $result);
	}

	/**
	 * Get the most recently created instance of this class.
	 * Note: This function should only be used for testing purposes.
	 *
	 * @return amePluginVisibility|null
	 */
	public static function getLastCreatedInstance() {
		return self::$lastInstance;
	}

	/**
	 * Remove any visibility settings associated with the specified plugin.
	 *
	 * @param string $pluginFile
	 */
	public function forgetPlugin($pluginFile) {
		$settings = $this->getSettings();
		unset($settings['plugins'][$pluginFile]);
		$this->settings = $settings;
		$this->saveSettings();
	}
}