MenuConfigurationWrapper.php
2.11 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Storage;
class MenuConfigurationWrapper extends LazyArrayStorage implements StorageInterface {
/**
* @var \WPMenuEditor
*/
private $menuEditor;
/**
* @var string|null
*/
private $menuConfigId;
private static $wrappersById = [];
public function __construct(\WPMenuEditor $menuEditor, $menuConfigId) {
$this->menuEditor = $menuEditor;
$this->menuConfigId = $menuConfigId;
parent::__construct();
}
protected function loadData() {
$data = $this->menuEditor->load_custom_menu($this->menuConfigId);
if ( $data === null ) {
$data = [];
}
return $data;
}
protected function storeData($newData) {
//Caution: Currently, the underlying implementation doesn't support configs
//without a "tree" key. This may need to be changed to allow configurations
//that only specify menu styles, not menu items.
$this->menuEditor->set_custom_menu($newData, $this->menuConfigId);
}
protected function deleteStoredData() {
throw new \LogicException('Cannot delete the menu configuration via this interface.');
}
public function setValue($value) {
throw new \LogicException('Cannot replace the menu configuration via this interface.');
}
/**
* @param string $menuConfigId
* @param \WPMenuEditor|null $menuEditor
* @return self
*/
public static function getStoreByConfigId($menuConfigId, $menuEditor = null) {
if ( isset(self::$wrappersById[$menuConfigId]) ) {
return self::$wrappersById[$menuConfigId];
}
if ( $menuEditor === null ) {
$menuEditor = $GLOBALS['wp_menu_editor'];
}
$wrapper = new self($menuEditor, $menuConfigId);
self::$wrappersById[$menuConfigId] = $wrapper;
return $wrapper;
}
/**
* @param string|null $optionalConfigId
* @param \WPMenuEditor|null $menuEditor
* @return \YahnisElsts\AdminMenuEditor\Customizable\Storage\StorageInterface
*/
public static function getStore($optionalConfigId = null, $menuEditor = null) {
if ( $optionalConfigId === null ) {
return ActiveMenuConfiguration::getInstance($menuEditor);
} else {
return self::getStoreByConfigId($optionalConfigId, $menuEditor);
}
}
}