/
AddActionsAndFilters_AdminPageController.php
201 lines (176 loc) · 6.85 KB
/
AddActionsAndFilters_AdminPageController.php
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
<?php
/*
"Add Shortcodes, Actions and Filters" Copyright (C) 2013-2023 Michael Simpson (email : michael.d.simpson@gmail.com)
This file is part of Add Actions and Filters for WordPress.
Add Actions and Filters is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Add Actions and Filters is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Contact Form to Database Extension.
If not, see <http://www.gnu.org/licenses/>.
*/
class AddActionsAndFilters_AdminPageController
{
/**
* @var AddActionsAndFilters_Plugin
*/
var $plugin;
/**
* AddActionsAndFilters_AdminPageController constructor.
* @param AddActionsAndFilters_Plugin $plugin
*/
public function __construct(AddActionsAndFilters_Plugin $plugin)
{
$this->plugin = $plugin;
}
/**
* Handle the URL request for the WP dashboard admin page.
* Handle any actions indicated in the URL GET parameters
*/
function handleAdminPageUrl()
{
$this->plugin->securityCheck();
// set-screen-option callback - does not work
// this is the work-around
if (isset($_REQUEST['wp_screen_options']) && is_array($_REQUEST['wp_screen_options'])) {
AddActionsAndFilters_ViewAdminPage::setScreenOptionCallback(
$_REQUEST['wp_screen_options']['option'],
$_REQUEST['wp_screen_options']['value']
);
}
require_once('AddActionsAndFilters_DataModelConfig.php');
require_once('AddActionsAndFilters_DataModel.php');
// Look for Sorting, ordering and searching
$config = new AddActionsAndFilters_DataModelConfig();
if (isset($_REQUEST['orderby'])) {
$config->setOrderby($_REQUEST['orderby']);
}
if (isset($_REQUEST['order'])) {
$config->setAsc($_REQUEST['order'] != 'desc');
}
if (isset($_REQUEST['s'])) {
$config->setSearch($_REQUEST['s']);
}
// Init DataModel and Table
$dataModel = new AddActionsAndFilters_DataModel($this->plugin, $config);
require_once('AddActionsAndFilters_CodeListTable.php');
$table = new AddActionsAndFilters_CodeListTable($dataModel);
// May be changed if a different page is to be displayed
$showAdminPage = true;
// Look for actions to be performed
$action = $table->current_action();
if ($action && $action != -1) {
require_once('AddActionsAndFilters_AdminPageActions.php');
$actions = new AddActionsAndFilters_AdminPageActions();
$ids = $this->getIds();
// Perform Actions
if ($action == $actions->getEditKey()) {
$item = isset($_REQUEST['id']) ?
$dataModel->getDataItem($_REQUEST['id']) :
array();
$showAdminPage = false; // show edit page instead
$this->plugin->displayEditPage($item);
} else if ($ids) {
$nonce_ok =$table->verifyAdminPageNonce($_REQUEST['_wpnonce']);
if ($nonce_ok) {
$this->doMutationAction($action, $actions, $dataModel, $ids);
}
}
}
// Display Admin Page
if ($showAdminPage) {
$table->prepare_items();
$this->displayAdminTable($table);
}
}
/**
* Display the Plugin's administration page in the WordPress Dashboard
* @param $table AddActionsAndFilters_CodeListTable
*/
public function displayAdminTable(&$table)
{
require_once('AddActionsAndFilters_ViewAdminPage.php');
$view = new AddActionsAndFilters_ViewAdminPage($this->plugin, $table);
$view->display();
}
public function ajaxSave()
{
if (current_user_can('manage_options')) {
if (ob_get_length()) {
// eliminate any debug output from polluting the ajax return value
// https://codex.wordpress.org/AJAX_in_Plugins
ob_clean();
}
if (!headers_sent()) {
// Don't let IE cache this request
header('Pragma: no-cache');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
header('Content-type: text/plain');
}
require_once('AddActionsAndFilters_DataModelConfig.php');
require_once('AddActionsAndFilters_DataModel.php');
$config = new AddActionsAndFilters_DataModelConfig();
$dataModel = new AddActionsAndFilters_DataModel($this->plugin, $config);
$id = $dataModel->saveItem($_REQUEST);
echo $id;
die();
} else {
die(-1);
}
}
/**
* @return array|false|string[]|null
*/
public function getIds()
{
$ids = null;
if (isset($_REQUEST['cb']) && is_array($_REQUEST['cb'])) {
$ids = $_REQUEST['cb']; // for bulk action
}
if (isset($_REQUEST['id'])) {
$ids = array($_REQUEST['id']);
} else if (isset($_REQUEST['ids'])) {
$ids = explode(',', $_REQUEST['ids']);
}
return $ids;
}
/**
* @param $action
* @param AddActionsAndFilters_AdminPageActions $actions
* @param AddActionsAndFilters_DataModel $dataModel
* @param array $ids
* @return void
*/
public function doMutationAction($action,
AddActionsAndFilters_AdminPageActions $actions,
AddActionsAndFilters_DataModel
$dataModel, array $ids)
{
switch ($action) {
case $actions->getActivateKey():
$dataModel->activate($ids, true);
break;
case $actions->getDeactivateKey():
$dataModel->activate($ids, false);
break;
case $actions->getDeleteKey();
$dataModel->delete($ids);
break;
case $actions->getExportKey();
if (!empty($ids)) {
require_once('AddActionsAndFilters_ViewImportExport.php');
$view = new AddActionsAndFilters_ViewImportExport($this->plugin);
$view->outputBulkExport($ids);
}
break;
default:
break;
}
}
}