| 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 = '/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 |
} |
| 83 |
} |
| 84 |
|
| 85 |
function column_cb($item) { |
| 86 |
return sprintf( |
| 87 |
'<input type="checkbox" name="%1$s[]" value="%2$s">', |
| 88 |
esc_attr($this->_args['singular']), // let's simply repurpose the table's singular label |
| 89 |
esc_attr($item['id']) // the value of the checkbox should be the record's ID |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
function column_title($item) { |
| 94 |
$page = sanitize_key(filter_input(INPUT_GET, 'page', FILTER_DEFAULT)); |
| 95 |
$item_status = sanitize_key(filter_input(INPUT_GET, 'item_status', FILTER_DEFAULT)); |
| 96 |
|
| 97 |
if(current_user_can('manage_options') || get_current_user_id()==$item['author']) { |
| 98 |
$actions = []; |
| 99 |
|
| 100 |
switch($item_status) { |
| 101 |
case 'trash': { |
| 102 |
$args = [ |
| 103 |
'page' => $page, |
| 104 |
'item_status' => $item_status, |
| 105 |
'action' => 'restore', |
| 106 |
'id' => $item['id'] |
| 107 |
]; |
| 108 |
$url = add_query_arg($args, 'admin.php'); |
| 109 |
|
| 110 |
$actions['restore'] = sprintf( |
| 111 |
'<a href="%1$s">%2$s</a>', |
| 112 |
esc_url(wp_nonce_url($url, 'restore_' . $item['id'])), |
| 113 |
esc_html__('Restore', 'vision') |
| 114 |
); |
| 115 |
|
| 116 |
$args = [ |
| 117 |
'page' => $page, |
| 118 |
'item_status' => $item_status, |
| 119 |
'action' => 'delete', |
| 120 |
'id' => $item['id'] |
| 121 |
]; |
| 122 |
$url = add_query_arg($args, 'admin.php'); |
| 123 |
|
| 124 |
$actions['delete'] = sprintf( |
| 125 |
'<a href="%1$s">%2$s</a>', |
| 126 |
esc_url(wp_nonce_url($url, 'delete_' . $item['id'])), |
| 127 |
esc_html__('Delete Permanently', 'vision') |
| 128 |
); |
| 129 |
} break; |
| 130 |
default: { |
| 131 |
$args = [ |
| 132 |
'page' => 'vision_item', |
| 133 |
'item_status' => $item_status, |
| 134 |
'id' => $item['id'] |
| 135 |
]; |
| 136 |
$url = add_query_arg($args, 'admin.php'); |
| 137 |
|
| 138 |
$actions['edit'] = sprintf( |
| 139 |
'<a href="%1$s">%2$s</a>', |
| 140 |
esc_url($url), |
| 141 |
esc_html__('Edit', 'vision') |
| 142 |
); |
| 143 |
|
| 144 |
$args = [ |
| 145 |
'page' => $page, |
| 146 |
'item_status' => $item_status, |
| 147 |
'action' => 'copy', |
| 148 |
'id' => $item['id'] |
| 149 |
]; |
| 150 |
$url = add_query_arg($args, 'admin.php'); |
| 151 |
|
| 152 |
$actions['copy'] = sprintf( |
| 153 |
'<a href="%1$s">%2$s</a>', |
| 154 |
esc_url(wp_nonce_url($url, 'copy_' . $item['id'])), |
| 155 |
esc_html__('Duplicate', 'vision') |
| 156 |
); |
| 157 |
|
| 158 |
$args = [ |
| 159 |
'page' => $page, |
| 160 |
'item_status' => $item_status, |
| 161 |
'action' => 'trash', |
| 162 |
'id' => $item['id'] |
| 163 |
]; |
| 164 |
$url = add_query_arg($args, 'admin.php'); |
| 165 |
|
| 166 |
$actions['trash'] = sprintf( |
| 167 |
'<a href="%1$s">%2$s</a>', |
| 168 |
esc_url(wp_nonce_url($url, 'trash_' . $item['id'])), |
| 169 |
esc_html__('Move to Trash', 'vision') |
| 170 |
); |
| 171 |
|
| 172 |
$args = [ |
| 173 |
'preview' => true |
| 174 |
]; |
| 175 |
$url = add_query_arg($args, $this->url_preview_base . $item['id']); |
| 176 |
|
| 177 |
$actions['view'] = sprintf( |
| 178 |
'<a href="%1$s" target="_blank">%2$s</a>', |
| 179 |
esc_url($url), |
| 180 |
esc_html__('Preview', 'vision') |
| 181 |
); |
| 182 |
} break; |
| 183 |
} |
| 184 |
|
| 185 |
return sprintf('<a href="?page=%1$s&id=%2$s" class="row-title">%3$s</a> %4$s', |
| 186 |
'vision_item', |
| 187 |
$item['id'], |
| 188 |
$item['title'], |
| 189 |
$this->row_actions($actions) |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
return sprintf('<strong>%1$s</strong>', $item['title']); |
| 194 |
} |
| 195 |
|
| 196 |
function column_active($item) { |
| 197 |
if(current_user_can('manage_options') || get_current_user_id()==$item['author']) { |
| 198 |
return sprintf( |
| 199 |
'<div class="vision-toggle vision-%1$s" data-id="%2$s"> </div>', |
| 200 |
($item['active'] ? 'checked' : 'unchecked'), |
| 201 |
$item['id'] |
| 202 |
); |
| 203 |
} else { |
| 204 |
return sprintf( |
| 205 |
'<div class="vision-toggle vision-readonly vision-%1$s" data-id="%2$s"> </div>', |
| 206 |
($item['active'] ? 'checked' : 'unchecked'), |
| 207 |
$item['id'] |
| 208 |
); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
function column_shortcode($item) { |
| 213 |
return sprintf('<code>[vision id="%1$s"]</code>', $item['id']); |
| 214 |
} |
| 215 |
|
| 216 |
function column_author($item) { |
| 217 |
$page = sanitize_key(filter_input(INPUT_GET, 'page', FILTER_DEFAULT)); |
| 218 |
$args = [ |
| 219 |
'page' => $page, |
| 220 |
'author' => $item['author'] |
| 221 |
]; |
| 222 |
$url = add_query_arg($args, 'admin.php'); |
| 223 |
|
| 224 |
$actions = []; |
| 225 |
$select_html = ''; |
| 226 |
|
| 227 |
if (current_user_can('manage_options') || get_current_user_id() == $item['author']) { |
| 228 |
$actions['edit'] = sprintf( |
| 229 |
'<a href="#" class="vision-edit-author" data-item-id="%1$s">%2$s</a>', |
| 230 |
$item['id'], |
| 231 |
esc_html__('Edit', 'vision') |
| 232 |
); |
| 233 |
|
| 234 |
$users = get_users(['fields' => ['ID', 'display_name']]); |
| 235 |
$options = ''; |
| 236 |
foreach ($users as $u) { |
| 237 |
$options .= sprintf( |
| 238 |
'<option value="%d"%s>%s</option>', |
| 239 |
esc_attr($u->ID), |
| 240 |
selected($u->ID, $item['author'], false), |
| 241 |
esc_html($u->display_name) |
| 242 |
); |
| 243 |
} |
| 244 |
$select_html = sprintf( |
| 245 |
'<div class="vision-author-edit-form" data-item-id="%d" style="display:none;">' |
| 246 |
. '<select class="vision-author-select">%s</select> ' |
| 247 |
. '<button type="button" class="button vision-author-save">%s</button> ' |
| 248 |
. '<button type="button" class="button vision-author-cancel">%s</button>' |
| 249 |
. '</div>', |
| 250 |
$item['id'], |
| 251 |
$options, |
| 252 |
esc_html__('OK', 'vision'), |
| 253 |
esc_html__('Cancel', 'vision') |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
return sprintf( |
| 258 |
'<a href="%1$s" class="row-author">%2$s</a> %3$s %4$s', |
| 259 |
esc_url($url), |
| 260 |
get_the_author_meta('display_name', $item['author']), |
| 261 |
$this->row_actions($actions), |
| 262 |
$select_html |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
function column_editor($item) { |
| 267 |
$page = sanitize_key(filter_input(INPUT_GET, 'page', FILTER_DEFAULT)); |
| 268 |
$args = [ |
| 269 |
'page' => $page, |
| 270 |
'editor' => $item['editor'] |
| 271 |
]; |
| 272 |
$url = add_query_arg($args, 'admin.php'); |
| 273 |
|
| 274 |
return sprintf( |
| 275 |
'<a href="%1$s">%2$s</a>', |
| 276 |
esc_url( $url ), |
| 277 |
get_the_author_meta('display_name', $item['editor']) |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
function column_created( $item ) { |
| 282 |
$m_time = mysql2date('Y/m/d g:i:s a', $item['created']); |
| 283 |
$h_time = mysql2date('Y/m/d', $item['created']); |
| 284 |
|
| 285 |
return sprintf('<abbr title="%1$s">%2$s</abbr>', esc_attr($m_time), esc_html($h_time)); |
| 286 |
} |
| 287 |
|
| 288 |
function column_modified( $item ) { |
| 289 |
$m_time = mysql2date('Y/m/d g:i:s a', $item['modified']); |
| 290 |
$h_time = mysql2date('Y/m/d', $item['modified']); |
| 291 |
|
| 292 |
return sprintf('<abbr title="%1$s">%2$s</abbr>', esc_attr($m_time), esc_html($h_time)); |
| 293 |
} |
| 294 |
|
| 295 |
function get_views() { |
| 296 |
$page = sanitize_key(filter_input(INPUT_GET, 'page', FILTER_DEFAULT)); |
| 297 |
$item_status = sanitize_key(filter_input(INPUT_GET, 'item_status', FILTER_DEFAULT)); |
| 298 |
|
| 299 |
$args_all = ['page' => $page]; |
| 300 |
$args_mine = ['page' => $page, 'item_status' => 'mine']; |
| 301 |
$args_trash = ['page' => $page, 'item_status' => 'trash']; |
| 302 |
|
| 303 |
$url_all = add_query_arg($args_all, 'admin.php'); |
| 304 |
$url_mine = add_query_arg($args_mine, 'admin.php'); |
| 305 |
$url_trash = add_query_arg($args_trash, 'admin.php'); |
| 306 |
|
| 307 |
$status_links = [ |
| 308 |
'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), |
| 309 |
'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), |
| 310 |
'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) |
| 311 |
]; |
| 312 |
|
| 313 |
return $status_links; |
| 314 |
} |
| 315 |
|
| 316 |
function get_columns() { |
| 317 |
$columns = [ |
| 318 |
'cb' => '<input type="checkbox">', |
| 319 |
'title' => esc_html__('Title', 'vision'), |
| 320 |
'active' => esc_html__('Active', 'vision'), |
| 321 |
'shortcode' => esc_html__('Shortcode', 'vision'), |
| 322 |
'author' => esc_html__('Author', 'vision'), |
| 323 |
'editor' => esc_html__('Editor', 'vision'), |
| 324 |
'created' => esc_html__('Created', 'vision'), |
| 325 |
'modified' => esc_html__('Modified', 'vision') |
| 326 |
]; |
| 327 |
return $columns; |
| 328 |
} |
| 329 |
|
| 330 |
function get_sortable_columns() { |
| 331 |
$columns = [ |
| 332 |
'title' => ['title', false], |
| 333 |
'active' => ['active', false], |
| 334 |
'author' => ['author', false], |
| 335 |
'editor' => ['editor', false], |
| 336 |
'created' => ['created', false], |
| 337 |
'modified' => ['modified', false] |
| 338 |
]; |
| 339 |
return $columns; |
| 340 |
} |
| 341 |
|
| 342 |
function get_bulk_actions() { |
| 343 |
$item_status = sanitize_key(filter_input(INPUT_GET, 'item_status', FILTER_DEFAULT)); |
| 344 |
$actions = []; |
| 345 |
|
| 346 |
switch($item_status) { |
| 347 |
case 'trash': { |
| 348 |
$actions = [ |
| 349 |
'restore' => esc_html__('Restore', 'vision'), |
| 350 |
'delete' => esc_html__('Delete Permanently', 'vision') |
| 351 |
]; |
| 352 |
} break; |
| 353 |
default: { |
| 354 |
$actions = [ |
| 355 |
'copy' => esc_html__('Duplicate', 'vision'), |
| 356 |
'trash' => esc_html__('Move to Trash', 'vision') |
| 357 |
]; |
| 358 |
} break; |
| 359 |
} |
| 360 |
return $actions; |
| 361 |
} |
| 362 |
|
| 363 |
function process_trash_action($id, $flag) { |
| 364 |
global $wpdb; |
| 365 |
$table = $wpdb->prefix . VISION_PLUGIN_NAME; |
| 366 |
$author = get_current_user_id(); |
| 367 |
|
| 368 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 369 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE id=%d", $id); |
| 370 |
$item = $wpdb->get_row($sql, OBJECT); |
| 371 |
// phpcs:enable |
| 372 |
|
| 373 |
if($item && (current_user_can('manage_options') || $author==$item->author)) { |
| 374 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 375 |
$wpdb->update($table, ['deleted' => $flag], ['id' => $id]); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
function process_copy_action($id) { |
| 380 |
global $wpdb; |
| 381 |
$table = $wpdb->prefix . VISION_PLUGIN_NAME; |
| 382 |
$author = get_current_user_id(); |
| 383 |
|
| 384 |
if( VISION_PLUGIN_PLAN == 'lite' ) { |
| 385 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 386 |
$count = $wpdb->get_var("SELECT COUNT(*) FROM {$table}"); |
| 387 |
|
| 388 |
if ($count >= 1) { |
| 389 |
echo '<div class="notice notice-error is-dismissible">'; |
| 390 |
echo '<p>Vision: ' . esc_html__('You can create only 1 map. If you need more, upgrade to the pro version.', 'vision') . '</p>'; |
| 391 |
echo '</div>'; |
| 392 |
return; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 397 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE id=%d AND NOT deleted", $id); |
| 398 |
$item = $wpdb->get_row($sql, OBJECT); |
| 399 |
// phpcs:enable |
| 400 |
|
| 401 |
if($item && (current_user_can('manage_options') || $author==$item->author)) { |
| 402 |
$itemData = unserialize($item->data); |
| 403 |
$itemData->slug = sanitize_title(($itemData->slug ? $itemData->slug : $itemData->title)); |
| 404 |
$itemData->title = esc_html__('[Duplicate] ', 'vision') . $itemData->title; |
| 405 |
$itemConfig = unserialize($item->config); |
| 406 |
|
| 407 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 408 |
$result = $wpdb->insert( |
| 409 |
$table, |
| 410 |
[ |
| 411 |
'title' => $itemData->title, |
| 412 |
'slug' => $itemData->slug, |
| 413 |
'active' => $itemData->active, |
| 414 |
'data' => serialize($itemData), |
| 415 |
'config' => serialize($itemConfig), |
| 416 |
'author' => $author, |
| 417 |
'editor' => $author, |
| 418 |
'created' => current_time('mysql', 1), |
| 419 |
'modified' => current_time('mysql', 1) |
| 420 |
] |
| 421 |
); |
| 422 |
|
| 423 |
// [filemanager] create an external file |
| 424 |
if($result) { |
| 425 |
if(wp_is_writable(VISION_PLUGIN_UPLOAD_DIR)) { |
| 426 |
$dir_src_root = $this->joinPaths(VISION_PLUGIN_UPLOAD_DIR, $id); |
| 427 |
$dir_dst_root = $this->joinPaths(VISION_PLUGIN_UPLOAD_DIR, $wpdb->insert_id); |
| 428 |
$wp_filesystem = $this->getFileSystem(); |
| 429 |
|
| 430 |
if($wp_filesystem) { |
| 431 |
if(!$wp_filesystem->is_dir($dir_dst_root)) { |
| 432 |
$wp_filesystem->mkdir($dir_dst_root); |
| 433 |
} |
| 434 |
copy_dir($dir_src_root, $dir_dst_root); |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
function process_delete_action($id) { |
| 442 |
global $wpdb; |
| 443 |
$table = $wpdb->prefix . VISION_PLUGIN_NAME; |
| 444 |
$author = get_current_user_id(); |
| 445 |
|
| 446 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 447 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE id=%d AND deleted", $id); |
| 448 |
$item = $wpdb->get_row($sql, OBJECT); |
| 449 |
// phpcs:enable |
| 450 |
|
| 451 |
if($item && (current_user_can('manage_options') || $author==$item->author) ) { |
| 452 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 453 |
$result = $wpdb->delete($table, ['id' => $id], ['%d']); |
| 454 |
|
| 455 |
// [filemanager] delete file |
| 456 |
if($result) { |
| 457 |
if (wp_is_writable(VISION_PLUGIN_UPLOAD_DIR)) { |
| 458 |
$dir_root = $this->joinPaths(VISION_PLUGIN_UPLOAD_DIR, $id); |
| 459 |
$wp_filesystem = $this->getFileSystem(); |
| 460 |
if ($wp_filesystem) { |
| 461 |
if ($wp_filesystem->is_dir($dir_root)) { |
| 462 |
$wp_filesystem->rmdir($dir_root, true); |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
function process_actions() { |
| 471 |
switch($this->current_action()) { |
| 472 |
case 'trash': { |
| 473 |
if(isset($_POST['_wpnonce']) && !empty($_POST['_wpnonce'])) { |
| 474 |
$nonce = sanitize_key(filter_input(INPUT_POST, '_wpnonce', FILTER_DEFAULT)); |
| 475 |
$nonce_key = 'bulk-' . $this->_args['plural']; |
| 476 |
|
| 477 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 478 |
$items = filter_input(INPUT_POST, $this->_args['singular'], FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
| 479 |
|
| 480 |
foreach($items as $id) { |
| 481 |
$this->process_trash_action($id, true); |
| 482 |
} |
| 483 |
} |
| 484 |
} else if(isset($_GET['_wpnonce']) && !empty($_GET['_wpnonce'])) { |
| 485 |
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 486 |
$nonce = sanitize_key(filter_input(INPUT_GET, '_wpnonce', FILTER_DEFAULT)); |
| 487 |
$nonce_key = 'trash_' . $id; |
| 488 |
|
| 489 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 490 |
$this->process_trash_action($id, true); |
| 491 |
} |
| 492 |
} |
| 493 |
} break; |
| 494 |
case 'restore': { |
| 495 |
if(isset($_POST['_wpnonce']) && !empty($_POST['_wpnonce'])) { |
| 496 |
$nonce = sanitize_key(filter_input(INPUT_POST, '_wpnonce', FILTER_DEFAULT)); |
| 497 |
$nonce_key = 'bulk-' . $this->_args['plural']; |
| 498 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 499 |
$items = filter_input(INPUT_POST, $this->_args['singular'], FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
| 500 |
|
| 501 |
foreach($items as $id) { |
| 502 |
$this->process_trash_action($id, false); |
| 503 |
} |
| 504 |
} |
| 505 |
} else if(isset($_GET['_wpnonce']) && !empty($_GET['_wpnonce'])) { |
| 506 |
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 507 |
$nonce = sanitize_key(filter_input(INPUT_GET, '_wpnonce', FILTER_DEFAULT)); |
| 508 |
$nonce_key = 'restore_' . $id; |
| 509 |
|
| 510 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 511 |
$this->process_trash_action($id, false); |
| 512 |
} |
| 513 |
} |
| 514 |
} break; |
| 515 |
case 'copy': { |
| 516 |
if(isset($_POST['_wpnonce']) && !empty($_POST['_wpnonce'])) { |
| 517 |
$nonce = sanitize_key(filter_input(INPUT_POST, '_wpnonce', FILTER_DEFAULT)); |
| 518 |
$nonce_key = 'bulk-' . $this->_args['plural']; |
| 519 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 520 |
$items = filter_input(INPUT_POST, $this->_args['singular'], FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
| 521 |
|
| 522 |
foreach($items as $id) { |
| 523 |
$this->process_copy_action($id); |
| 524 |
} |
| 525 |
} |
| 526 |
} else if(isset($_GET['_wpnonce']) && !empty($_GET['_wpnonce'])) { |
| 527 |
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 528 |
$nonce = sanitize_key(filter_input(INPUT_GET, '_wpnonce', FILTER_DEFAULT)); |
| 529 |
$nonce_key = 'copy_' . $id; |
| 530 |
|
| 531 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 532 |
$this->process_copy_action($id); |
| 533 |
} |
| 534 |
} |
| 535 |
} break; |
| 536 |
case 'delete': { |
| 537 |
if(isset($_POST['_wpnonce']) && !empty($_POST['_wpnonce'])) { |
| 538 |
$nonce = sanitize_key(filter_input(INPUT_POST, '_wpnonce', FILTER_DEFAULT)); |
| 539 |
$nonce_key = 'bulk-' . $this->_args['plural']; |
| 540 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 541 |
$items = filter_input(INPUT_POST, $this->_args['singular'], FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY); |
| 542 |
|
| 543 |
foreach($items as $id) { |
| 544 |
$this->process_delete_action($id); |
| 545 |
} |
| 546 |
} |
| 547 |
} else if(isset($_GET['_wpnonce']) && !empty($_GET['_wpnonce'])) { |
| 548 |
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 549 |
$nonce = sanitize_key(filter_input(INPUT_GET, '_wpnonce', FILTER_DEFAULT)); |
| 550 |
$nonce_key = 'delete_' . $id; |
| 551 |
|
| 552 |
if(wp_verify_nonce($nonce, $nonce_key)) { |
| 553 |
$this->process_delete_action($id); |
| 554 |
} |
| 555 |
} |
| 556 |
} break; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
function prepare_items() { |
| 561 |
$this->process_actions(); |
| 562 |
|
| 563 |
$columns = $this->get_columns(); |
| 564 |
$sortable = $this->get_sortable_columns(); |
| 565 |
$hidden = []; |
| 566 |
|
| 567 |
$itemsPerPage = 25; |
| 568 |
$currentPage = ($this->get_pagenum()-1) * $itemsPerPage; |
| 569 |
|
| 570 |
$this->_column_headers = [$columns, $hidden, $sortable]; |
| 571 |
|
| 572 |
global $wpdb; |
| 573 |
$table = $wpdb->prefix . VISION_PLUGIN_NAME; |
| 574 |
|
| 575 |
$item_status = sanitize_key(filter_input(INPUT_GET, 'item_status', FILTER_DEFAULT)); |
| 576 |
|
| 577 |
// phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing |
| 578 |
$orderby = (isset($_GET['orderby']) ? strtolower(sanitize_key(filter_input(INPUT_GET, 'orderby', FILTER_DEFAULT))) : 'id'); |
| 579 |
$order = (isset($_GET['order']) ? strtolower(sanitize_key(filter_input(INPUT_GET, 'order', FILTER_DEFAULT))) : 'desc'); |
| 580 |
$author = (isset($_GET['author']) ? filter_input(INPUT_GET, 'author', FILTER_SANITIZE_NUMBER_INT ) : NULL); |
| 581 |
$editor = (isset($_GET['editor']) ? filter_input(INPUT_GET, 'editor', FILTER_SANITIZE_NUMBER_INT ) : NULL); |
| 582 |
$search = (isset($_POST['s']) ? '%' . $wpdb->esc_like(sanitize_text_field(filter_input(INPUT_POST, 's', FILTER_DEFAULT))) . '%' : '%'); |
| 583 |
// phpcs:enable |
| 584 |
|
| 585 |
$current_user = get_current_user_id(); |
| 586 |
|
| 587 |
// check user input |
| 588 |
$sortable_columns = array_keys($sortable); |
| 589 |
$orderby = in_array($orderby, $sortable_columns) ? $orderby : 'id'; |
| 590 |
|
| 591 |
$order_values = ['asc', 'desc']; |
| 592 |
$order = in_array($order, $order_values) ? $order : 'desc'; |
| 593 |
|
| 594 |
$sql = ''; |
| 595 |
$sql_total_items = ''; |
| 596 |
$total_items = 0; |
| 597 |
|
| 598 |
// database operations |
| 599 |
if(current_user_can('manage_options')) { // by default, the manage_options permission is only given to 'Super Users' and 'Administrators' |
| 600 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 601 |
$this->count_all = $wpdb->query("SELECT id FROM {$table} WHERE NOT deleted"); |
| 602 |
$this->count_mine = $wpdb->query($wpdb->prepare("SELECT id FROM {$table} WHERE NOT deleted AND author=%s", $current_user)); |
| 603 |
$this->count_trash = $wpdb->query("SELECT id FROM {$table} WHERE deleted"); |
| 604 |
// phpcs:enable |
| 605 |
|
| 606 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder |
| 607 |
switch($item_status) { |
| 608 |
case 'trash': { |
| 609 |
if($author) { |
| 610 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND deleted AND IFNULL(title,'') LIKE %s ORDER BY %1s %1s LIMIT %d, %d", $author, $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 611 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND deleted AND IFNULL(title,'') LIKE %s", $author, $search); |
| 612 |
} else if($editor) { |
| 613 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE editor=%s AND deleted AND IFNULL(title,'') LIKE %s ORDER BY %1s %1s LIMIT %d, %d", $editor, $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 614 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE editor=%s AND deleted AND IFNULL(title,'') LIKE %s", $editor, $search); |
| 615 |
} else { |
| 616 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE deleted AND IFNULL(title,'') LIKE %s ORDER BY %1s %1s LIMIT %d, %d", $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 617 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE deleted AND IFNULL(title,'') LIKE %s", $search); |
| 618 |
} |
| 619 |
} break; |
| 620 |
case 'mine': { |
| 621 |
if($author) { |
| 622 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND NOT deleted AND IFNULL(title,'') LIKE %s ORDER BY %1s %1s LIMIT %d, %d", $current_user, $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 623 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted AND IFNULL(title,'') LIKE %s", $current_user, $search); |
| 624 |
} else if($editor) { |
| 625 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND editor=%s AND NOT deleted AND IFNULL(title,'') LIKE %s ORDER BY %1s %1s LIMIT %d, %d", $current_user, $editor, $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 626 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND editor=%s AND NOT deleted AND IFNULL(title,'') LIKE %s", $current_user, $editor, $search); |
| 627 |
} else { |
| 628 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND NOT deleted AND IFNULL(title,'') LIKE %s ORDER BY %1s %1s LIMIT %d, %d", $current_user, $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 629 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted AND IFNULL(title,'') LIKE %s", $current_user, $search); |
| 630 |
} |
| 631 |
} break; |
| 632 |
default: { |
| 633 |
if($author) { |
| 634 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND NOT deleted AND IFNULL(title,'') LIKE %s ORDER BY %1s %1s LIMIT %d, %d", $author, $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 635 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted AND IFNULL(title,'') LIKE %s", $author, $search); |
| 636 |
} else if($editor) { |
| 637 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE editor=%s AND NOT deleted AND IFNULL(title,'') LIKE %s ORDER BY %1s %1s LIMIT %d, %d", $editor, $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 638 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE editor=%s AND NOT deleted AND IFNULL(title,'') LIKE %s", $editor, $search); |
| 639 |
} else { |
| 640 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE NOT deleted AND IFNULL(title,'') LIKE %s ORDER BY %1s %1s LIMIT %d, %d", $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 641 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE NOT deleted AND IFNULL(title,'') LIKE %s", $search); |
| 642 |
} |
| 643 |
} break; |
| 644 |
} |
| 645 |
// phpcs:enable |
| 646 |
} else { // the current user view |
| 647 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 648 |
$this->count_all = $wpdb->query($wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted", $current_user)); |
| 649 |
$this->count_mine = $wpdb->query($wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted", $current_user)); |
| 650 |
$this->count_trash = $wpdb->query($wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND deleted", $current_user)); |
| 651 |
// phpcs:enable |
| 652 |
|
| 653 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 654 |
switch($item_status) { |
| 655 |
case 'trash': { |
| 656 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND deleted AND IFNULL(title,'') LIKE %s ORDER BY %s %s LIMIT %d, %d", $current_user, $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 657 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND deleted AND IFNULL(title,'') LIKE %s", $current_user, $search); |
| 658 |
} break; |
| 659 |
case 'mine': { |
| 660 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND NOT deleted AND IFNULL(title,'') LIKE %s ORDER BY %s %s LIMIT %d, %d", $current_user, $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 661 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted AND IFNULL(title,'') LIKE %s", $current_user, $search); |
| 662 |
} break; |
| 663 |
default: { |
| 664 |
$sql = $wpdb->prepare("SELECT * FROM {$table} WHERE author=%s AND NOT deleted AND IFNULL(title,'') LIKE %s ORDER BY %s %s LIMIT %d, %d", $current_user, $search, $orderby, $order, $currentPage, $itemsPerPage); |
| 665 |
$sql_total_items = $wpdb->prepare("SELECT id FROM {$table} WHERE author=%s AND NOT deleted AND IFNULL(title,'') LIKE %s", $current_user, $search); |
| 666 |
} break; |
| 667 |
} |
| 668 |
// phpcs:enable |
| 669 |
} |
| 670 |
|
| 671 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 672 |
$this->items = $wpdb->get_results( $sql, 'ARRAY_A' ); |
| 673 |
$total_items = $wpdb->query( $sql_total_items ); |
| 674 |
// phpcs:enable |
| 675 |
|
| 676 |
$this->set_pagination_args([ |
| 677 |
'total_items' => $total_items, |
| 678 |
'total_pages' => ceil($total_items / $itemsPerPage) |
| 679 |
]); |
| 680 |
} |
| 681 |
} |
| 682 |
?> |