|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\Admin\TableActions; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\UserSettingsManagerFacade; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Module List Table Action |
|
11
|
|
|
*/ |
|
12
|
|
|
class ModuleListTableAction extends AbstractListTableAction |
|
13
|
|
|
{ |
|
14
|
|
|
public const ACTION_ENABLE = 'enable'; |
|
15
|
|
|
public const ACTION_DISABLE = 'disable'; |
|
16
|
|
|
public const INPUT_BULK_ACTION_IDS = 'bulk-action-items'; |
|
17
|
|
|
|
|
18
|
|
|
private bool $processed = false; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private array $mutatedModuleIDs = []; |
|
23
|
|
|
private bool $mutatedEnabled = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Please notice that creatin a new instance carries side effects |
|
27
|
|
|
* |
|
28
|
|
|
* @param boolean $allowSideEffects |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(bool $allowSideEffects = true) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($allowSideEffects) { |
|
33
|
|
|
/** |
|
34
|
|
|
* If executing an operation, print a success message |
|
35
|
|
|
*/ |
|
36
|
|
|
\add_action('admin_notices', function () { |
|
37
|
|
|
$this->maybeAddAdminNotice(); |
|
38
|
|
|
}); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Show an admin notice with the successful message |
|
44
|
|
|
* Executing this function from within `setModulesEnabledValue` is too late, |
|
45
|
|
|
* since hook "admin_notices" will have been executed by then |
|
46
|
|
|
* Then, deduce if there will be an operation, and always say "successful" |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function maybeAddAdminNotice(): void |
|
51
|
|
|
{ |
|
52
|
|
|
// If processes, `maybeProcessAction` has already been executed, |
|
53
|
|
|
// so we have the results to show in the admin notice |
|
54
|
|
|
$message = ''; |
|
55
|
|
|
if ($this->processed) { |
|
56
|
|
|
/** |
|
57
|
|
|
* Executing at the beginning (in Plugin.php): Add a precise message |
|
58
|
|
|
*/ |
|
59
|
|
|
if (!empty($this->mutatedModuleIDs)) { |
|
60
|
|
|
if (count($this->mutatedModuleIDs) == 1 && $this->mutatedEnabled) { |
|
61
|
|
|
$message = \__('Module enabled successfully', 'graphql-api'); |
|
62
|
|
|
} elseif (count($this->mutatedModuleIDs) > 1 && $this->mutatedEnabled) { |
|
63
|
|
|
$message = \__('Modules enabled successfully', 'graphql-api'); |
|
64
|
|
|
} elseif (count($this->mutatedModuleIDs) == 1 && !$this->mutatedEnabled) { |
|
65
|
|
|
$message = \__('Module disabled successfully', 'graphql-api'); |
|
66
|
|
|
} elseif (count($this->mutatedModuleIDs) > 1 && !$this->mutatedEnabled) { |
|
67
|
|
|
$message = \__('Modules disabled successfully', 'graphql-api'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} else { |
|
71
|
|
|
/** |
|
72
|
|
|
* Executed at the end (in ModuleListTable.php, `prepare_items`): Add a generic message |
|
73
|
|
|
*/ |
|
74
|
|
|
// See if executing any of the actions |
|
75
|
|
|
$actions = $this->getActions(); |
|
76
|
|
|
$isBulkAction = in_array($_POST['action'] ?? null, $actions) || in_array($_POST['action2'] ?? null, $actions); |
|
77
|
|
|
$isSingleAction = in_array($this->currentAction(), $actions); |
|
78
|
|
|
if ($isBulkAction || $isSingleAction) { |
|
79
|
|
|
$message = \__('Operation successful', 'graphql-api'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
if ($message) { |
|
83
|
|
|
_e(sprintf( |
|
84
|
|
|
'<div class="notice notice-success is-dismissible">' . |
|
85
|
|
|
'<p>%s</p>' . |
|
86
|
|
|
'</div>', |
|
87
|
|
|
$message |
|
88
|
|
|
)); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Process bulk and single actions. |
|
94
|
|
|
* This function can be executed only once, from either the Plugin class or the table |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function maybeProcessAction() |
|
99
|
|
|
{ |
|
100
|
|
|
// Process only once |
|
101
|
|
|
if ($this->processed) { |
|
102
|
|
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
$this->processed = true; |
|
105
|
|
|
|
|
106
|
|
|
$actions = $this->getActions(); |
|
107
|
|
|
$isBulkAction = in_array($_POST['action'] ?? null, $actions) || in_array($_POST['action2'] ?? null, $actions); |
|
108
|
|
|
/** |
|
109
|
|
|
* The Bulk takes precedence, because it's executed as a POST on the current URL |
|
110
|
|
|
* Then, the URL can contain an ?action=... which was just executed, |
|
111
|
|
|
* and we don't want to execute it again |
|
112
|
|
|
*/ |
|
113
|
|
|
if ($isBulkAction) { |
|
114
|
|
|
$moduleIDs = (array) \esc_sql($_POST[self::INPUT_BULK_ACTION_IDS] ?? []); |
|
115
|
|
|
if (!empty($moduleIDs)) { |
|
116
|
|
|
// Enable or disable |
|
117
|
|
|
if (($_POST['action'] ?? null) == self::ACTION_ENABLE || ($_POST['action2'] ?? null) == self::ACTION_ENABLE) { |
|
118
|
|
|
$this->setModulesEnabledValue($moduleIDs, true); |
|
119
|
|
|
} elseif (($_POST['action'] ?? null) == self::ACTION_DISABLE || ($_POST['action2'] ?? null) == self::ACTION_DISABLE) { |
|
120
|
|
|
$this->setModulesEnabledValue($moduleIDs, false); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
return; |
|
124
|
|
|
} |
|
125
|
|
|
$isSingleAction = in_array($this->currentAction(), $actions); |
|
126
|
|
|
if ($isSingleAction) { |
|
127
|
|
|
// Verify the nonce |
|
128
|
|
|
$nonce = \esc_attr($_REQUEST['_wpnonce'] ?? ''); |
|
129
|
|
|
if (!\wp_verify_nonce($nonce, 'graphql_api_enable_or_disable_module')) { |
|
130
|
|
|
$noParamsCurrentURL = \admin_url(sprintf( |
|
131
|
|
|
'admin.php?page=%s', |
|
132
|
|
|
$_GET['page'] ?? '' |
|
133
|
|
|
)); |
|
134
|
|
|
\wp_die(sprintf( |
|
135
|
|
|
__('This URL is either stale or not valid. Please <a href="%s">click here to reload the page</a>, and try again', 'graphql-api'), |
|
136
|
|
|
$noParamsCurrentURL |
|
137
|
|
|
)); |
|
138
|
|
|
} |
|
139
|
|
|
if ($moduleID = $_GET['item'] ?? null) { |
|
140
|
|
|
// Enable or disable |
|
141
|
|
|
if (self::ACTION_ENABLE === $this->currentAction()) { |
|
142
|
|
|
$this->setModulesEnabledValue([$moduleID], true); |
|
143
|
|
|
} elseif (self::ACTION_DISABLE === $this->currentAction()) { |
|
144
|
|
|
$this->setModulesEnabledValue([$moduleID], false); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Enable or Disable a list of modules |
|
152
|
|
|
* |
|
153
|
|
|
* @param string[] $moduleIDs |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function setModulesEnabledValue(array $moduleIDs, bool $isEnabled): void |
|
156
|
|
|
{ |
|
157
|
|
|
$userSettingsManager = UserSettingsManagerFacade::getInstance(); |
|
158
|
|
|
$moduleIDValues = []; |
|
159
|
|
|
foreach ($moduleIDs as $moduleID) { |
|
160
|
|
|
$moduleIDValues[$moduleID] = $isEnabled; |
|
161
|
|
|
} |
|
162
|
|
|
$userSettingsManager->setModulesEnabled($moduleIDValues); |
|
163
|
|
|
|
|
164
|
|
|
// Flags to indicate that data was mutated, which and how |
|
165
|
|
|
$this->mutatedModuleIDs = $moduleIDs; |
|
166
|
|
|
$this->mutatedEnabled = $isEnabled; |
|
167
|
|
|
|
|
168
|
|
|
// If modifying a CPT, must flush the rewrite rules |
|
169
|
|
|
// But do it at the end! Once the new configuration has been applied |
|
170
|
|
|
\add_action( |
|
171
|
|
|
'init', |
|
172
|
|
|
function () { |
|
173
|
|
|
\flush_rewrite_rules(); |
|
174
|
|
|
|
|
175
|
|
|
// Update the timestamp |
|
176
|
|
|
$userSettingsManager = UserSettingsManagerFacade::getInstance(); |
|
177
|
|
|
$userSettingsManager->storeTimestamp(); |
|
178
|
|
|
}, |
|
179
|
|
|
PHP_INT_MAX |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return string[] |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getActions(): array |
|
187
|
|
|
{ |
|
188
|
|
|
return [ |
|
189
|
|
|
self::ACTION_ENABLE, |
|
190
|
|
|
self::ACTION_DISABLE, |
|
191
|
|
|
]; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|