| 1 |
<?php |
| 2 |
defined('ABSPATH') || exit; |
| 3 |
|
| 4 |
if(!class_exists('WP_List_Table')) { |
| 5 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php'); |
| 6 |
} |
| 7 |
|
| 8 |
class Vision_List_Table_Items extends WP_List_Table { |
| 9 |
private $count_all = 0; |
| 10 |
private $count_mine = 0; |
| 11 |
private $count_trash = 0; |
| 12 |
private $url_preview_base = ''; |
| 13 |
|
| 14 |
function __construct() { |
| 15 |
parent::__construct([ |
| 16 |
'singular'=> 'vision_item', |
| 17 |
'plural' => 'vision_items', |
| 18 |
'ajax' => false |
| 19 |
]); |
| 20 |
|
| 21 |
$this->url_preview_base = rtrim(parse_url(home_url(), PHP_URL_PATH), '/') . '/vision/map/'; |
| 22 |
} |
| 23 |
|
| 24 |
function handle_row_actions($post, $column_name, $primary) { |
| 25 |
return ''; |
| 26 |
} |
| 27 |
|
| 28 |
function joinPaths() { |
| 29 |
$paths = []; |
| 30 |
|
| 31 |
foreach(func_get_args() as $arg) { |
| 32 |
if($arg !== '') { |
| 33 |
$paths[] = $arg; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return preg_replace('#/+#','/',join('/', $paths)); |
| 38 |
} |
| 39 |
|
| 40 |
function filesystem_method() { |
| 41 |
return 'direct'; |
| 42 |
} |
| 43 |
|
| 44 |
function request_filesystem_credentials() { |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
function getFileSystem() { |
| 49 |
global $wp_filesystem; |
| 50 |
$result = true; |
| 51 |
|
| 52 |
if(!$wp_filesystem) { |
| 53 |
require_once(ABSPATH . '/wp-admin/includes/file.php'); |
| 54 |
|
| 55 |
add_filter('filesystem_method', [$this, 'filesystem_method']); |
| 56 |
add_filter('request_filesystem_credentials', [$this, 'request_filesystem_credentials']); |
| 57 |
|
| 58 |
$credentials = request_filesystem_credentials(site_url(), '', true, false, null); |
| 59 |
|
| 60 |
$result = WP_Filesystem($credentials); |
| 61 |
|
| 62 |
remove_filter('filesystem_method', [$this, 'filesystem_method']); |
| 63 |
remove_filter('request_filesystem_credentials', [$this, 'request_filesystem_credentials']); |
| 64 |
} |
| 65 |
|
| 66 |
if($result) |
| 67 |
return $wp_filesystem; |
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
function column_default($item, $column_name){ |
| 72 |
switch($column_name){ |
| 73 |
case 'title': |
| 74 |
case 'active': |
| 75 |
case 'shortcode': |
| 76 |
case 'author': |
| 77 |
case 'editor': |
| 78 |
case 'created': |
| 79 |
case 'modified': |
| 80 |
case 'id': |
| 81 |
return $item[$column_name]; |
| 82 |
default: |
| 83 |
return print_r($item, true); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
function column_cb($item) { |
| 88 |
return sprintf( |
| 89 |
'<input type="checkbox" name="%1$s[]" value="%2$s">', |
| 90 |
esc_attr($this->_args['singular']), // let's simply repurpose the table's singular label |
| 91 |
esc_attr($item['id']) // the value of the checkbox should be the record's ID |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
function column_title($item) { |
| 96 |
$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_STRING); |
| 97 |
$item_status = filter_input(INPUT_GET, 'item_status', FILTER_SANITIZE_STRING); |
| 98 |
|
| 99 |
if(current_user_can('manage_options') || get_current_user_id()==$item['author']) { |
| 100 |
$actions = []; |
| 101 |
|
| 102 |
switch($item_status) { |
| 103 |
case 'trash': { |
| 104 |
$args = [ |
| 105 |
'page' => $page, |
| 106 |
'item_status' => $item_status, |
| 107 |
'action' => 'restore', |
| 108 |
'id' => $item['id'] |
| 109 |
]; |
| 110 |
$url = add_query_arg($args, 'admin.php'); |
| 111 |
|
| 112 |
$actions['restore'] = sprintf( |
| 113 |
'<a href="%1$s">%2$s</a>', |
| 114 |
esc_url(wp_nonce_url($url, 'restore_' . $item['id'])), |
| 115 |
esc_html__('Restore', 'vision') |
| 116 |
); |
| 117 |
|
| 118 |
$args = [ |
| 119 |
'page' => $page, |
| 120 |
'item_status' => $item_status, |
| 121 |
'action' => 'delete', |
| 122 |
'id' => $item['id'] |
| 123 |
]; |
| 124 |
$url = add_query_arg($args, 'admin.php'); |
| 125 |
|
| 126 |
$actions['delete'] = sprintf( |
| 127 |
'<a href="%1$s">%2$s</a>', |
| 128 |
esc_url(wp_nonce_url($url, 'delete_' . $item['id'])), |
| 129 |
esc_html__('Delete Permanently', 'vision') |
| 130 |
); |
| 131 |
} break; |
| 132 |
default: { |
| 133 |
$args = [ |
| 134 |
'page' => 'vision_item', |
| 135 |
'item_status' => $item_status, |
| 136 |
'id' => $item['id'] |
| 137 |
]; |
| 138 |
$url = add_query_arg($args, 'admin.php'); |
| 139 |
|
| 140 |
$actions['edit'] = sprintf( |
| 141 |
'<a href="%1$s">%2$s</a>', |
| 142 |
esc_url($url), |
| 143 |
esc_html__('Edit', 'vision') |
| 144 |
); |
| 145 |
|
| 146 |
$args = [ |
| 147 |
'page' => $page, |
| 148 |
'item_status' => $item_status, |
| 149 |
'action' => 'copy', |
| 150 |
'id' => $item['id'] |
| 151 |
]; |
| 152 |
$url = add_query_arg($args, 'admin.php'); |
| 153 |
|
| 154 |
$actions['copy'] = sprintf( |
| 155 |
'<a href="%1$s">%2$s</a>', |
| 156 |
esc_url(wp_nonce_url($url, 'copy_' . $item['id'])), |
| 157 |
esc_html__('Duplicate', 'vision') |
| 158 |
); |
| 159 |
|
| 160 |
$args = [ |
| 161 |
'page' => $page, |
| 162 |
'item_status' => $item_status, |
| 163 |
'action' => 'trash', |
| 164 |
'id' => $item['id'] |
| 165 |
]; |
| 166 |
$url = add_query_arg($args, 'admin.php'); |
| 167 |
|
| 168 |
$actions['trash'] = sprintf( |
| 169 |
'<a href="%1$s">%2$s</a>', |
| 170 |
esc_url(wp_nonce_url($url, 'trash_' . $item['id'])), |
| 171 |
esc_html__('Move to Trash', 'vision') |
| 172 |
); |
| 173 |
|
| 174 |
$args = [ |
| 175 |
'preview' => true |
| 176 |
]; |
| 177 |
$url = add_query_arg($args, $this->url_preview_base . $item['id']); |
| 178 |
|
| 179 |
$actions['view'] = sprintf( |
| 180 |
'<a href="%1$s" target="_blank">%2$s</a>', |
| 181 |
esc_url($url), |
| 182 |
esc_html__('Preview', 'vision') |
| 183 |
); |
| 184 |
} break; |
| 185 |
} |
| 186 |
|
| 187 |
return sprintf('<a href="?page=%1$s&id=%2$s" class="row-title">%3$s</a> %4$s', |
| 188 |
'vision_item', |
| 189 |
$item['id'], |
| 190 |
$item['title'], |
| 191 |
$this->row_actions($actions) |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
return sprintf('<strong>%1$s</strong>', $item['title']); |
| 196 |
} |
| 197 |
|
| 198 |
function column_active($item) { |
| 199 |
if(current_user_can('manage_options') || get_current_user_id()==$item['author']) { |
| 200 |
return sprintf( |
| 201 |
'<div class="vision-toggle vision-%1$s" data-id="%2$s"> </div>', |
| 202 |
($item['active'] ? 'checked' : 'unchecked'), |
| 203 |
$item['id'] |
| 204 |
); |
| 205 |
} else { |
| 206 |
return sprintf( |
| 207 |
'<div class="vision-toggle vision-readonly vision-%1$s" data-id="%2$s"> </div>', |
| 208 |
($item['active'] ? 'checked' : 'unchecked'), |
| 209 |
$item['id'] |
| 210 |
); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
function column_shortcode($item) { |
| 215 |
return sprintf('<code>[vision id="%1$s"]</code>', $item['id']); |
| 216 |
} |
| 217 |
|
| 218 |
function column_author($item) { |
| 219 |
$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_STRING); |
| 220 |
$args = [ |
| 221 |
'page' => $page, |
| 222 |
'author' => $item['author'] |
| 223 |
]; |
| 224 |
$url = add_query_arg($args, 'admin.php'); |
| 225 |
|
| 226 |
return sprintf( |
| 227 |
'<a href="%1$s">%2$s</a>', |
| 228 |
esc_url( $url ), |
| 229 |
get_the_author_meta('display_name', $item['author']) |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
function column_editor($item) { |
| 234 |
$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_STRING); |
| 235 |
$args = [ |
| 236 |
'page' => $page, |
| 237 |
'editor' => $item['editor'] |
| 238 |
]; |
| 239 |
$url = add_query_arg($args, 'admin.php'); |
| 240 |
|
| 241 |
return sprintf( |
| 242 |
'<a href="%1$s">%2$s</a>', |
| 243 |
esc_url( $url ), |
| 244 |
get_the_author_meta('display_name', $item['editor']) |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
function column_created( $item ) { |
| 249 |
$m_time = mysql2date('Y/m/d g:i:s a', $item['created']); |
| 250 |
$h_time = mysql2date('Y/m/d', $item['created']); |
| 251 |
|
| 252 |
return sprintf('<abbr title="%1$s">%2$s</abbr>', esc_attr($m_time), esc_html($h_time)); |
| 253 |
} |
| 254 |
|
| 255 |
function column_modified( $item ) { |
| 256 |
$m_time = mysql2date('Y/m/d g:i:s a', $item['modified']); |
| 257 |
$h_time = mysql2date('Y/m/d', $item['modified']); |
| 258 |
|
| 259 |
return sprintf('<abbr title="%1$s">%2$s</abbr>', esc_attr($m_time), esc_html($h_time)); |
| 260 |
} |
| 261 |
|
| 262 |
function get_views() { |
| 263 |
$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_STRING); |
| 264 |
$item_status = filter_input(INPUT_GET, 'item_status', FILTER_SANITIZE_STRING); |
| 265 |
|
| 266 |
$args_all = ['page' => $page]; |
| 267 |
$args_mine = ['page' => $page, 'item_status' => 'mine']; |
| 268 |
$args_trash = ['page' => $page, 'item_status' => 'trash']; |
| 269 |
|
| 270 |
$url_all = add_query_arg($args_all, 'admin.php'); |
| 271 |
$url_mine = add_query_arg($args_mine, 'admin.php'); |
| 272 |
$url_trash = add_query_arg($args_trash, 'admin.php'); |
| 273 |
|
| 274 |
$status_links = [ |
| 275 |
'all' => sprintf('<a href="%1$s" %2$s>%3$s <span class="count">(%4$d)</span></a>', esc_url($url_all), ($item_status ? '' : 'class="current"'), esc_html__('All','vision'), $this->count_all), |
| 276 |
'mine' => sprintf('<a href="%1$s" %2$s>%3$s <span class="count">(%4$d)</span></a>', esc_url($url_mine), ($item_status == 'mine' ? 'class="current"' : ''), esc_html__('Mine','vision'), $this->count_mine), |
| 277 |
'trash' => sprintf('<a href="%1$s" %2$s>%3$s <span class="count">(%4$d)</span></a>', esc_url($url_trash), ($item_status == 'trash' ? 'class="current"' : ''), esc_html__('Trash','vision'), $this->count_trash) |
| 278 |
]; |
| 279 |
|
| 280 |
return $status_links; |
| 281 |
} |
| 282 |
|
| 283 |
function get_columns() { |
| 284 |
$columns = [ |
| 285 |
'cb' => '<input type="checkbox">', |
| 286 |
'title' => esc_html__('Title', 'vision'), |
| 287 |
'active' => esc_html__('Active', 'vision'), |
| 288 |
'shortcode' => esc_html__('Shortcode', 'vision'), |
| 289 |
'author' => esc_html__('Author', 'vision'), |
| 290 |
'editor' => esc_html__('Editor', 'vision'), |
| 291 |
'created' => esc_html__('Created', 'vision'), |
| 292 |
'modified' => esc_html__('Modified', 'vision') |
| 293 |
]; |
| 294 |
return $columns; |
| 295 |
} |
| 296 |
|
| 297 |
function get_sortable_columns() { |
| 298 |
$columns = [ |
| 299 |
'title' => ['title', false], |
| 300 |
'active' => ['active', false], |
| 301 |
'author' => ['author', false], |
| 302 |
'editor' => ['editor', false], |
| 303 |
'created' => ['created', false], |
| 304 |
'modified' => ['modified', false] |
| 305 |
]; |
| 306 |
return $columns; |
| 307 |
} |
| 308 |
|
| 309 |
function get_bulk_actions() { |
| 310 |
$item_status = filter_input(INPUT_GET, 'item_status', FILTER_SANITIZE_STRING); |
| 311 |
$actions = []; |
| 312 |
|
| 313 |
switch($item_status) { |
| 314 |
case 'trash': { |
| 315 |
$actions = [ |
| 316 |
'restore' => esc_html__('Restore', 'vision'), |
| 317 |
'delete' => esc_html__('Delete Permanently', 'vision') |
| 318 |
]; |
| 319 |
} break; |
| 320 |
default: { |
| 321 |
$actions = [ |
| 322 |
'copy' => esc_html__('Duplicate', 'vision'), |
| 323 |
'trash' => esc_html__('Move to Trash', 'vision') |
| 324 |
]; |
| 325 |
} break; |
| 326 |
} |
| 327 |
return $actions; |
| 328 |
} |
| 329 |
|
| 330 |
function process_trash_action($id, $flag) { |
| 331 |
global $wpdb; |
| 332 |
$table = $wpdb->prefix . VISION_PLUGIN_NAME; |
| 333 |
$author = get_current_user_id(); |
| 334 |
|
| 335 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE id=%d", $id); |
| 336 |
$item = $wpdb->get_row($sql, OBJECT); |
| 337 |
|
| 338 |
if($item && (current_user_can('manage_options') || $author==$item->author)) { |
| 339 |
$wpdb->update($table, ['deleted' => $flag], ['id' => $id]); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
function process_copy_action($id) { |
| 344 |
global $wpdb; |
| 345 |
$table = $wpdb->prefix . VISION_PLUGIN_NAME; |
| 346 |
$author = get_current_user_id(); |
| 347 |
|
| 348 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE id=%d AND NOT deleted", $id); |
| 349 |
$item = $wpdb->get_row($sql, OBJECT); |
| 350 |
|
| 351 |
if($item && (current_user_can('manage_options') || $author==$item->author)) { |
| 352 |
$itemData = unserialize($item->data); |
| 353 |
$itemData->slug = sanitize_title(($itemData->slug ? $itemData->slug : $itemData->title)); |
| 354 |
$itemData->title = esc_html__('[Duplicate] ', 'vision') . $itemData->title; |
| 355 |
$itemConfig = unserialize($item->config); |
| 356 |
|
| 357 |
$result = $wpdb->insert( |
| 358 |
$table, |
| 359 |
[ |
| 360 |
'title' => $itemData->title, |
| 361 |
'slug' => $itemData->slug, |
| 362 |
'active' => $itemData->active, |
| 363 |
'data' => serialize($itemData), |
| 364 |
'config' => serialize($itemConfig), |
| 365 |
'author' => $author, |
| 366 |
'editor' => $author, |
| 367 |
'created' => current_time('mysql', 1), |
| 368 |
'modified' => current_time('mysql', 1) |
| 369 |
] |
| 370 |
); |
| 371 |
|
| 372 |
// [filemanager] create an external file |
| 373 |
if($result) { |
| 374 |
if(wp_is_writable(VISION_PLUGIN_UPLOAD_DIR)) { |
| 375 |
$dir_src_root = $this->joinPaths(VISION_PLUGIN_UPLOAD_DIR, $id); |
| 376 |
$dir_dst_root = $this->joinPaths(VISION_PLUGIN_UPLOAD_DIR, $wpdb->insert_id); |
| 377 |
$wp_filesystem = $this->getFileSystem(); |
| 378 |
|
| 379 |
if($wp_filesystem) { |
| 380 |
if(!$wp_filesystem->is_dir($dir_dst_root)) { |
| 381 |
$wp_filesystem->mkdir($dir_dst_root); |
| 382 |
} |
| 383 |
copy_dir($dir_src_root, $dir_dst_root); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
function process_delete_action($id) { |
| 391 |
global $wpdb; |
| 392 |
$table = $wpdb->prefix . VISION_PLUGIN_NAME; |
| 393 |
$author = get_current_user_id(); |
| 394 |
|
| 395 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE id=%d AND deleted", $id); |
| 396 |
$item = $wpdb->get_row($sql, OBJECT); |
| 397 |
|
| 398 |
if($item && (current_user_can('manage_options') || $author==$item->author) ) { |
| 399 |
$result = $wpdb->delete($table, ['id' => $id], ['%d']); |
| 400 |
|
| 401 |
// [filemanager] delete file |
| 402 |
if($result) { |
| 403 |
if (wp_is_writable(VISION_PLUGIN_UPLOAD_DIR)) { |
| 404 |
$dir_root = $this->joinPaths(VISION_PLUGIN_UPLOAD_DIR, $id); |
| 405 |
$wp_filesystem = $this->getFileSystem(); |
| 406 |
if ($wp_filesystem) { |
| 407 |
if ($wp_filesystem->is_dir($dir_root)) { |
| 408 |
$wp_filesystem->rmdir($dir_root, true); |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
function process_actions() { |
| 417 |
switch($this->current_action()) { |
| 418 |
case 'trash': { |
| 419 |
if(isset($_POST['_wpnonce']) && !empty($_POST['_wpnonce'])) { |
| 420 |
$nonce = filter_input(INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING); |
| 421 |
$nonce_key = 'bulk-' . $this->_args['plural']; |
| 422 |
|
| 423 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 424 |
$items = filter_input(INPUT_POST, $this->_args['singular'], FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
| 425 |
|
| 426 |
foreach($items as $id) { |
| 427 |
$this->process_trash_action($id, true); |
| 428 |
} |
| 429 |
} |
| 430 |
} else if(isset($_GET['_wpnonce']) && !empty($_GET['_wpnonce'])) { |
| 431 |
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 432 |
$nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); |
| 433 |
$nonce_key = 'trash_' . $id; |
| 434 |
|
| 435 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 436 |
$this->process_trash_action($id, true); |
| 437 |
} |
| 438 |
} |
| 439 |
} break; |
| 440 |
case 'restore': { |
| 441 |
if(isset($_POST['_wpnonce']) && !empty($_POST['_wpnonce'])) { |
| 442 |
$nonce = filter_input(INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING); |
| 443 |
$nonce_key = 'bulk-' . $this->_args['plural']; |
| 444 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 445 |
$items = filter_input(INPUT_POST, $this->_args['singular'], FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
| 446 |
|
| 447 |
foreach($items as $id) { |
| 448 |
$this->process_trash_action($id, false); |
| 449 |
} |
| 450 |
} |
| 451 |
} else if(isset($_GET['_wpnonce']) && !empty($_GET['_wpnonce'])) { |
| 452 |
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 453 |
$nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); |
| 454 |
$nonce_key = 'restore_' . $id; |
| 455 |
|
| 456 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 457 |
$this->process_trash_action($id, false); |
| 458 |
} |
| 459 |
} |
| 460 |
} break; |
| 461 |
case 'copy': { |
| 462 |
if(isset($_POST['_wpnonce']) && !empty($_POST['_wpnonce'])) { |
| 463 |
$nonce = filter_input(INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING); |
| 464 |
$nonce_key = 'bulk-' . $this->_args['plural']; |
| 465 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 466 |
$items = filter_input(INPUT_POST, $this->_args['singular'], FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
| 467 |
|
| 468 |
foreach($items as $id) { |
| 469 |
$this->process_copy_action($id); |
| 470 |
} |
| 471 |
} |
| 472 |
} else if(isset($_GET['_wpnonce']) && !empty($_GET['_wpnonce'])) { |
| 473 |
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 474 |
$nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); |
| 475 |
$nonce_key = 'copy_' . $id; |
| 476 |
|
| 477 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 478 |
$this->process_copy_action($id); |
| 479 |
} |
| 480 |
} |
| 481 |
} break; |
| 482 |
case 'delete': { |
| 483 |
if(isset($_POST['_wpnonce']) && !empty($_POST['_wpnonce'])) { |
| 484 |
$nonce = filter_input(INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING); |
| 485 |
$nonce_key = 'bulk-' . $this->_args['plural']; |
| 486 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 487 |
$items = filter_input(INPUT_POST, $this->_args['singular'], FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
| 488 |
|
| 489 |
foreach($items as $id) { |
| 490 |
$this->process_delete_action($id); |
| 491 |
} |
| 492 |
} |
| 493 |
} else if(isset($_GET['_wpnonce']) && !empty($_GET['_wpnonce'])) { |
| 494 |
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 495 |
$nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); |
| 496 |
$nonce_key = 'delete_' . $id; |
| 497 |
|
| 498 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 499 |
$this->process_delete_action($id); |
| 500 |
} |
| 501 |
} |
| 502 |
} break; |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
function prepare_items() { |
| 507 |
$this->process_actions(); |
| 508 |
|
| 509 |
$columns = $this->get_columns(); |
| 510 |
$sortable = $this->get_sortable_columns(); |
| 511 |
$hidden = []; |
| 512 |
|
| 513 |
$itemsPerPage = 25; |
| 514 |
$currentPage = ($this->get_pagenum()-1) * $itemsPerPage; |
| 515 |
|
| 516 |
$this->_column_headers = [$columns, $hidden, $sortable]; |
| 517 |
|
| 518 |
global $wpdb; |
| 519 |
|
| 520 |
$table = $wpdb->prefix . VISION_PLUGIN_NAME; |
| 521 |
$item_status = filter_input(INPUT_GET, 'item_status', FILTER_SANITIZE_STRING); |
| 522 |
$orderby = (isset($_GET['orderby']) ? filter_input(INPUT_GET, 'orderby', FILTER_SANITIZE_STRING ) : 'id'); |
| 523 |
$order = (isset($_GET['order']) ? filter_input(INPUT_GET, 'order', FILTER_SANITIZE_STRING ) : 'desc'); |
| 524 |
$author = (isset($_GET['author']) ? filter_input(INPUT_GET, 'author', FILTER_SANITIZE_NUMBER_INT ) : NULL); |
| 525 |
$editor = (isset($_GET['editor']) ? filter_input(INPUT_GET, 'editor', FILTER_SANITIZE_NUMBER_INT ) : NULL); |
| 526 |
$search = (isset($_POST['s']) ? filter_input(INPUT_POST, 's', FILTER_SANITIZE_STRING) : NULL); |
| 527 |
$current_user = get_current_user_id(); |
| 528 |
|
| 529 |
$sql = ''; |
| 530 |
$total_items = 0; |
| 531 |
$sql_search = ($search ? sprintf(' AND title LIKE "%%%s%%"', $search) : ''); |
| 532 |
|
| 533 |
// database operations |
| 534 |
if(current_user_can('manage_options')) { // by default, the manage_options permission is only given to 'Super Users' and 'Administrators' |
| 535 |
$this->count_all = $wpdb->query("SELECT id FROM {$table} WHERE NOT deleted"); |
| 536 |
$this->count_mine = $wpdb->query($wpdb->prepare("SELECT id FROM {$table} WHERE NOT deleted AND author=%s", $current_user)); |
| 537 |
$this->count_trash = $wpdb->query("SELECT id FROM {$table} WHERE deleted"); |
| 538 |
|
| 539 |
switch($item_status) { |
| 540 |
case 'trash': { |
| 541 |
if($author) { |
| 542 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $author, $orderby, $order, $currentPage, $itemsPerPage); |
| 543 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND deleted", $author); |
| 544 |
} else if($editor) { |
| 545 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE editor=%s AND deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $editor, $orderby, $order, $currentPage, $itemsPerPage); |
| 546 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE editor=%s AND deleted", $editor); |
| 547 |
} else { |
| 548 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $orderby, $order, $currentPage, $itemsPerPage); |
| 549 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE deleted"); |
| 550 |
} |
| 551 |
$sql_total_items = $sql_total_items . $sql_search; |
| 552 |
} break; |
| 553 |
case 'mine': { |
| 554 |
if($author) { |
| 555 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND NOT deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $current_user, $orderby, $order, $currentPage, $itemsPerPage); |
| 556 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted", $current_user); |
| 557 |
} else if($editor) { |
| 558 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND editor=%s AND NOT deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $current_user, $editor, $orderby, $order, $currentPage, $itemsPerPage); |
| 559 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND editor=%s AND NOT deleted", $current_user, $editor); |
| 560 |
} else { |
| 561 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND NOT deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $current_user, $orderby, $order, $currentPage, $itemsPerPage); |
| 562 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted", $current_user); |
| 563 |
} |
| 564 |
$sql_total_items = $sql_total_items . $sql_search; |
| 565 |
} break; |
| 566 |
default: { |
| 567 |
if($author) { |
| 568 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND NOT deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $author, $orderby, $order, $currentPage, $itemsPerPage); |
| 569 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted {$sql_search}", $author); |
| 570 |
} else if($editor) { |
| 571 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE editor=%s AND NOT deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $editor, $orderby, $order, $currentPage, $itemsPerPage); |
| 572 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE editor=%s AND NOT deleted {$sql_search}", $editor); |
| 573 |
} else { |
| 574 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE NOT deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $orderby, $order, $currentPage, $itemsPerPage); |
| 575 |
$sql_total_items = "SELECT id FROM {$table} WHERE NOT deleted"; |
| 576 |
} |
| 577 |
$sql_total_items = $sql_total_items . $sql_search; |
| 578 |
} break; |
| 579 |
} |
| 580 |
} else { // the current user view |
| 581 |
$this->count_all = $wpdb->query($wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted", $current_user)); |
| 582 |
$this->count_mine = $wpdb->query($wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted", $current_user)); |
| 583 |
$this->count_trash = $wpdb->query($wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND deleted", $current_user)); |
| 584 |
|
| 585 |
switch($item_status) { |
| 586 |
case 'trash': { |
| 587 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $current_user, $orderby, $order, $currentPage, $itemsPerPage); |
| 588 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND deleted {$sql_search}", $current_user); |
| 589 |
} break; |
| 590 |
case 'mine': { |
| 591 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND NOT deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $current_user, $orderby, $order, $currentPage, $itemsPerPage); |
| 592 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted {$sql_search}", $current_user); |
| 593 |
} break; |
| 594 |
default: { |
| 595 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND NOT deleted {$sql_search} ORDER BY %s %s LIMIT %d, %d", $current_user, $orderby, $order, $currentPage, $itemsPerPage); |
| 596 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted {$sql_search}", $current_user); |
| 597 |
} break; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
$this->items = $wpdb->get_results($sql, 'ARRAY_A'); |
| 602 |
$total_items = $wpdb->query($sql_total_items); |
| 603 |
|
| 604 |
$this->set_pagination_args([ |
| 605 |
'total_items' => $total_items, |
| 606 |
'total_pages' => ceil($total_items / $itemsPerPage) |
| 607 |
]); |
| 608 |
} |
| 609 |
} |
| 610 |
?> |