SampleModule.php 17.1 KB
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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
<?php

namespace YahnisElsts\AdminMenuEditor\Customizable\Design;

require_once __DIR__ . '/constants.php';

use ameModule;
use WPMenuEditor;
use YahnisElsts\AdminMenuEditor\Customizable\Builders\ElementBuilderFactory;
use YahnisElsts\AdminMenuEditor\Customizable\Builders\SettingFactory;
use YahnisElsts\AdminMenuEditor\Customizable\Controls\Tooltip;
use YahnisElsts\AdminMenuEditor\Customizable\Settings\BooleanSetting;
use YahnisElsts\AdminMenuEditor\Customizable\SettingsForm;
use YahnisElsts\AdminMenuEditor\Customizable\Storage\AbstractSettingsDictionary;
use YahnisElsts\AdminMenuEditor\Customizable\Storage\CompressedStorage;
use YahnisElsts\AdminMenuEditor\Customizable\Storage\ScopedOptionStorage;
use YahnisElsts\AdminMenuEditor\Customizable\Storage\StorageInterface;

//todo: This can be in a namespaces because that requires PHP 5.3 and we already require PHP 5.6.

//TODO: This could be a "Core Settings Module" that is always loaded, defines the UI, and handles form submission.
class SampleModule extends ameModule {
	protected $tabSlug = 'settings-refactor';
	protected $tabTitle = 'Settings X';

	protected $settingsFormAction = 'save-settings-v2';

	/**
	 * @var SettingsForm|null
	 */
	private $form = null;

	/**
	 * @var AmeCoreSettings|null
	 */
	private $settings = null;

	protected function outputMainTemplate() {
		$this->getSettingsForm()->output();
		return true;
	}

	public function handleSettingsForm($post = array()) {
		$this->getSettingsForm()->handleUpdateRequest($post);
	}

	private function getSettingsForm() {
		if ( $this->form === null ) {
			$this->form = SettingsForm::builder($this->settingsFormAction)
				->id('ws_plugin_settings_form')
				->structure($this->getInterfaceStructure())
				->settings($this->getSettingsDictionary()->getRegisteredSettings())
				->submitUrl($this->getTabUrl(array('noheader' => 1)))
				->redirectAfterSaving($this->getTabUrl(), array('message' => '1'))
				->treatMissingFieldsAsEmpty()
				->postProcessSettings(
				/**
				 * @param $values
				 * @param array<string,\YahnisElsts\AdminMenuEditor\Customizable\Settings\AbstractSetting> $settingsById
				 * @return void
				 */
					function ($values, $settingsById) {
						//Update the allowed user ID when changing "who can access this plugin".
						if ( isset($settingsById['plugin_access'], $settingsById['allowed_user_id']) ) {
							if ( $settingsById['plugin_access']->getValue() === 'specific_user' ) {
								$settingsById['allowed_user_id']->update(get_current_user_id());
							} else {
								$settingsById['allowed_user_id']->update(null);
							}
						}
					}
				)
				->build();
		}
		return $this->form;
	}

	private function getSettingsDictionary() {
		if ( $this->settings === null ) {
			$optionName = $this->menuEditor->is_pro_version() ? 'ws_menu_editor_pro' : 'ws_menu_editor';

			$store = new ScopedOptionStorage(
				$optionName,
				//Core settings are always global even if menu settings are per-site.
				ScopedOptionStorage::GLOBAL_SCOPE
			);

			$this->settings = new AmeCoreSettings($this->menuEditor, $store);
		}
		return $this->settings;
	}

	private function getInterfaceStructure() {
		$isProVersion = $this->menuEditor->is_pro_version();
		$settings = $this->getSettingsDictionary();

		$b = new ElementBuilderFactory($settings);
		$structure = $b->structure(
			$b->group(
				'Who can access this plugin',
				$b->radioGroup('plugin_access'),
				$b->auto('hide_plugin_from_others')
			)->stacked(),

			$b->auto('menu_config_scope'),

			$this->createModulesGroup($b),

			$b->group(
				'Interface',
				$b->auto('hide_advanced_settings'),
				$b->auto('show_deprecated_hide_button')->onlyIf($isProVersion)
			)->stacked(),

			$b->group(
				'Editor colour scheme',
				$b->radioGroup('ui_colour_scheme')
			),

			$b->auto('submenu_icons_enabled')->onlyIf($isProVersion),

			$b->group(
				'New menu visibility',
				$b->radioGroup('unused_item_permissions')
			)
				->onlyIf($isProVersion)
				->tooltip(
					"This setting controls the default permissions of menu items that are
					 not present in the last saved menu configuration.
					 <br><br>
					 This includes new menus added by plugins and themes.
					 In Multisite, it also applies to menus that exist on some sites but not others.
					 It doesn't affect menu items that you add through the Admin Menu Editor interface."
				),

			$b->auto('unused_item_position')
				->tooltip(
					"This setting controls the position of menu items that are not present 
					 in the last saved menu	configuration.
					 <br><br>
					 This includes new menus added by plugins and themes.
					 In Multisite, it also applies to menus that exist only on certain sites but not on all sites.
					 It doesn't affect menu items that you add through the Admin Menu Editor interface."
				),

			$b->auto('deep_nesting_enabled')
				->tooltip(
					"Caution: Experimental feature.<br>
					 This feature might not work as expected, and it could cause conflicts with other plugins or themes.",
					Tooltip::EXPERIMENTAL
				)
				//The free version lacks the ability to render deeply nested menus in the dashboard, so the nesting
				//options are hidden by default. However, if the user somehow acquires a configuration where this
				//feature is enabled (e.g. by importing config from the Pro version), the free version can display
				//and even edit that configuration to a limited extent.
				->onlyIf(
					$isProVersion || $settings->get('was_nesting_ever_changed')
				),

			$b->group(
				'WPML support',
				$b->auto('wpml_support_enabled')
			)->stacked(),

			$b->group(
				'bbPress override',
				$b->auto('bbpress_override_enabled')
			)->stacked(),

			$b->auto('error_verbosity'),

			$b->group(
				'Debugging',
				$b->auto('security_logging_enabled'),
				$b->auto('force_custom_dashicons'),
				$b->auto('compress_custom_menu')
			)->stacked(),

			$b->group(
				'Server info',
				$b->html(
					"<figure>
						<figcaption>PHP error log:</figcaption>

						<code>" . esc_html(ini_get('error_log')) . "</code>
					</figure>

					<figure>
						<figcaption>PHP memory usage:</figcaption> "
					. sprintf(
						'%.2f MiB of %s',
						memory_get_peak_usage() / (1024 * 1024),
						esc_html(ini_get('memory_limit'))
					)
					. " </figure>"
				)
			)
		);

		return $structure->build();
	}

	private function createModulesGroup(ElementBuilderFactory $b) {
		$activeModulesGroup = $b->group('Modules')
			->id('ame-available-modules')
			->stacked()
			->fieldset()
			->tooltip(
				'Modules are plugin features that can be turned on or off. <br>'
				. 'Turning off unused features will slightly increase performance '
				. 'and may help with certain compatibility issues.'
			);

		foreach ($this->menuEditor->get_available_modules() as $id => $module) {
			if ( !empty($module['isAlwaysActive']) ) {
				continue;
			}

			$isCompatible = $this->menuEditor->is_module_compatible($module);
			$compatibilityNote = '';
			if ( !$isCompatible && !empty($module['requiredPhpVersion']) ) {
				if ( version_compare(phpversion(), $module['requiredPhpVersion'], '<') ) {
					$compatibilityNote = sprintf(
						'Required PHP version: %1$s or later. Installed PHP version: %2$s',
						htmlspecialchars($module['requiredPhpVersion']),
						htmlspecialchars(phpversion())
					);
				}
			}

			$activeModulesGroup->add(
				$b->checkbox()
					->label(htmlspecialchars(!empty($module['title']) ? $module['title'] : $id))
					->description($compatibilityNote)
					->enabled($isCompatible)
					->params(array(
						'inputAttributes' => array(
							'name'    => 'is_active_module[]',
							'value'   => $id,
							'checked' => $this->menuEditor->is_module_active($id, $module),
						),
					))
			);
		}
		return $activeModulesGroup;
	}
}

class AmeCoreSettings extends AbstractSettingsDictionary {
	/**
	 * @var WPMenuEditor
	 */
	protected $menuEditor;

	public function __construct(WPMenuEditor $menuEditor, StorageInterface $store = null) {
		if ( !isset($store) ) {
			$store = new ScopedOptionStorage(
				'ws_menu_editor_pro',
				ScopedOptionStorage::GLOBAL_SCOPE
			);
		}
		$this->menuEditor = $menuEditor;
		parent::__construct($store);

		if ( $this->store instanceof CompressedStorage ) {
			$this->store->setCompressionEnabled($this->get('compress_custom_menu', false));
		}
	}

	protected function createDefaults() {
		return $this->menuEditor->get_default_options();
	}

	protected function createSettings() {
		$factory = new SettingFactory($this->store, $this->defaults);

		$isMultisite = is_multisite();
		$isSuperAdmin = is_super_admin();
		$isProVersion = $this->menuEditor->is_pro_version();
		$currentUser = wp_get_current_user();
		$menuEditor = $this->menuEditor;

		$settings = array(
			$factory->stringEnum(
				'plugin_access',
				array('super_admin', 'manage_options', 'specific_user'),
				'Who can access this plugin'
			)
				->describeChoice(
					'super_admin',
					'Super Admin',
					$isMultisite ? null : 'On a single site installation this is usually the same as the Administrator role.',
					$isSuperAdmin
				)
				->describeChoice(
					'manage_options',
					'Anyone with the "manage_options" capability',
					'By default only Administrators have this capability.',
					current_user_can('manage_options')
				)
				->describeChoice(
					'specific_user',
					'Only the current user',
					'Login: ' . esc_html($currentUser->user_login) . ', user ID: ' . esc_html(get_current_user_id()),
					//In Multisite only Super Admins can choose this option.
					$isSuperAdmin || !$isMultisite
				),
			$factory->integer(
				'allowed_user_id',
				'(Internal) ID of the user that can access the plugin when "plugin_access" is "specific_user".',
				array(
					'default'    => null,
					'isEditable' => '__return_false', //Not directly editable by the user.
				)
			),
			new AmeHidePluginSetting('hide_plugin_from_others', $this->store),
			$factory->stringEnum(
				'menu_config_scope',
				array('global', 'site'),
				'Multisite settings',
				array(
					'isEditable' => function () use ($isMultisite) {
						return $isMultisite && is_super_admin();
					},
				)
			)
				->describeChoice(
					'global',
					'Global &mdash;	Use the same admin menu settings for all network sites.'
				)
				->describeChoice(
					'site',
					'Per-site &mdash; Use different admin menu settings for each site.'
				),
			$factory->boolean(
				'hide_advanced_settings',
				'Hide advanced menu options by default'
			),
			$factory->boolean(
				'security_logging_enabled',
				'Show menu access checks performed by the plugin on every admin page',
				array(
					'description' => "This can help track down configuration problems 
						and figure out why your menu permissions don't work the way they should.

						Note: It's not recommended to use this option on a live site as
						it can reveal information about your menu configuration.",
				)
			),
			$factory->boolean(
				'show_deprecated_hide_button',
				'Enable the "Hide (cosmetic)" toolbar button',
				array(
					'description' => "This button hides the selected menu item without making it inaccessible.",
				)
			),
			$factory->stringEnum(
				'submenu_icons_enabled',
				array('always', 'if_custom', 'never'),
				'Show submenu icons'
			)->describeChoice('if_custom', 'Only when manually selected'),
			$factory->boolean(
				'force_custom_dashicons',
				'Attempt to override menu icon CSS that was added by other plugins'
			),
			$factory->stringEnum(
				'ui_colour_scheme',
				array('classic', 'modern-one', 'wp-grey'),
				'Editor colour scheme'
			)
				->describeChoice('classic', 'Blue and yellow')
				->describeChoice('modern-one', 'Modern')
				->describeChoice('wp-grey', 'Grey'),
			$factory->stringEnum(
				'unused_item_position',
				array('relative', 'bottom'),
				'New menu position'
			)
				->describeChoice(
					'relative',
					'Maintain relative order',
					'Attempts to put new items in the same relative positions '
					. 'as they would be in in the default admin menu.'
				)
				->describeChoice(
					'bottom',
					'Bottom',
					'Puts new items at the bottom of the admin menu.'
				),
			$factory->stringEnum(
				'unused_item_permissions',
				array('unchanged', 'match_plugin_access'),
				'New menu visibility'
			)
				->describeChoice(
					'unchanged',
					'Leave unchanged (default)',
					'No special restrictions. Visibility will depend on the plugin that added the menus.'
				)
				->describeChoice(
					'match_plugin_access',
					'Show only to users who can access this plugin',
					'Automatically hides all new and unrecognized menus from regular users. '
					. 'To make new menus visible, you have to manually enable them in the menu editor.'
				),
			$factory->enum(
				'error_verbosity',
				array(
					WPMenuEditor::VERBOSITY_LOW,
					WPMenuEditor::VERBOSITY_NORMAL,
					WPMenuEditor::VERBOSITY_VERBOSE,
				),
				'Error verbosity level'
			)
				->describeChoice(
					WPMenuEditor::VERBOSITY_LOW,
					'Low',
					'Shows a generic error message without any details.'
				)
				->describeChoice(
					WPMenuEditor::VERBOSITY_NORMAL,
					'Normal',
					'Shows a one or two sentence explanation. For example: "The current'
					. ' user doesn\'t have the "manage_options" capability that is required'
					. ' to access the "Settings" menu item."'
				)
				->describeChoice(
					WPMenuEditor::VERBOSITY_VERBOSE,
					'Verbose',
					'Like "normal", but also includes a log of menu settings and permissions
					 that caused the current menu to be hidden. Useful for debugging.'
				),
			$factory->boolean(
				'compress_custom_menu',
				"Compress menu configuration data that's stored in the database",
				array(
					'description' => sprintf(
						"Significantly reduces the size of the <code>%s</code> DB option,
						but adds decompression overhead to every page.",
						esc_html($this->store->getStorageKey())
					),
				)
			),
			$factory->boolean(
				'wpml_support_enabled',
				'Make edited menu titles translatable with WPML',
				array(
					'description' => 'The titles will appear in the "Strings" section in WPML. '
						. 'If you don\'t use WPML or a similar translation plugin, '
						. 'you can safely disable this option.',
				)
			),
			$factory->boolean(
				'bbpress_override_enabled',
				'Prevent bbPress from resetting role capabilities',
				array(
					'description' => 'By default, bbPress will automatically undo any '
						. 'changes that are made to dynamic bbPress roles. Enable this '
						. 'option to override that behaviour and make it possible to '
						. 'change bbPress role capabilities.',
				)
			),
			$factory->enum(
				'deep_nesting_enabled',
				array(null, true, false),
				'Three level menus',
				array('default' => null)
			)
				->describeChoice(null, 'Ask on first use')
				->describeChoice(true, 'Enabled' . ($isProVersion ? '' : ' (only in editor)'))
				->describeChoice(false, 'Disabled'),
			$factory->custom(
				'is_active_module',
				'array',
				function ($inputValue) use ($menuEditor) {
					if ( empty($inputValue) ) {
						return array();
					}

					//Convert to [$moduleId => $enabled].
					$activeModules = (array)$inputValue;
					$activeModules = array_fill_keys(array_map('strval', $activeModules), true);

					//Filter out modules that are invalid or not installed.
					$availableModules = $menuEditor->get_available_modules();
					$activeModules = array_intersect_key($activeModules, $availableModules);

					//Explicitly set disabled modules to false.
					return array_merge(
						array_map('__return_false', $availableModules),
						$activeModules
					);
				},
				'Modules'
			),
		);

		//Index settings by ID.
		$result = array();
		foreach ($settings as $setting) {
			$result[$setting->getId()] = $setting;
		}
		return $result;
	}
}

class AmeHidePluginSetting extends BooleanSetting {
	const SETTING_KEY = 'plugins_page_allowed_user_id';

	protected $defaultValue = false;

	public function __construct($id, StorageInterface $store = null, $params = array()) {
		$isProVersion = self::isProVersion();
		if ( !isset($params['label']) ) {
			$label = 'Hide "Admin Menu Editor' . ($isProVersion ? ' Pro' : '') . '"';
			if ( defined('WS_ADMIN_BAR_EDITOR_FILE') || defined('AME_BRANDING_ADD_ON_FILE') ) {
				$label .= ' and its add-ons ';
			}
			$label .= ' from the "Plugins" page for other users';
			if ( !$isProVersion ) {
				$label .= ' (Pro version only)';
			}
			$params['label'] = $label;
		}

		parent::__construct($id, $store, $params);
	}

	public function getValue($customDefault = null) {
		/** @noinspection PhpRedundantOptionalArgumentInspection */
		$userId = $this->store->getPath(self::SETTING_KEY, null);
		return ($userId !== null);
	}

	public function update($validValue) {
		if ( !$this->store ) {
			return false;
		}
		$success = $this->store->setPath(
			self::SETTING_KEY,
			$validValue ? get_current_user_id() : null
		);
		$this->notifyUpdated();
		return $success;
	}

	public function isEditableByUser() {
		if ( !self::isProVersion() ) {
			return false;
		}

		if ( is_multisite() ) {
			$allowed = is_super_admin();
		} else {
			$allowed = current_user_can('manage_options');
		}
		return $allowed && parent::isEditableByUser();
	}

	protected static function isProVersion() {
		static $isPro = null;
		if ( $isPro === null ) {
			$isPro = apply_filters('admin_menu_editor_is_pro', false);
		}
		return $isPro;
	}
}