model = new
protected $template = 'view-other-media';
protected static $instance;
// Pagination .
protected $items_per_page = 20;
protected $currentPage = 1;
protected $total_items = 0;
protected $order;
protected $orderby;
protected $search;
protected $show_hidden = false;
protected $has_hidden_items = false;
public function __construct()
{
parent::__construct();
// 2015: https://github.com/WordPress/WordPress-Coding-Standards/issues/426 !
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is not a form
$this->currentPage = isset($_GET['paged']) ? intval($_GET['paged']) : 1;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is not a form
$this->orderby = ( ! empty( $_GET['orderby'] ) ) ? $this->filterAllowedOrderBy(sanitize_text_field(wp_unslash($_GET['orderby']))) : 'id';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is not a form
$this->order = ( ! empty($_GET['order'] ) ) ? sanitize_text_field( wp_unslash($_GET['order'])) : 'desc'; // If no order, default to asc
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is not a form
$this->search = (isset($_GET["s"]) && strlen($_GET["s"]) > 0) ? sanitize_text_field( wp_unslash($_GET['s'])) : false;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is not a form
$this->show_hidden = isset($_GET['show_hidden']) ? sanitize_text_field(wp_unslash($_GET['show_hidden'])) : false;
}
/** Controller default action - overview */
public function load()
{
// $this->process_actions();
$this->view->items = $this->getItems();
$this->view->folders = $this->getItemFolders($this->view->items);
$this->view->headings = $this->getHeadings();
$this->view->pagination = $this->getPagination();
$this->view->filter = $this->getFilter();
$this->view->title = __('Custom Media optimized by ShortPixel', 'shortpixel-image-optimiser');
$this->view->show_search = true;
// $this->checkQueue();
$this->loadView();
}
protected function getHeadings()
{
$headings = array(
'checkbox' => array('title' => '', 'sortable' => false),
'thumbnails' => array('title' => __('Thumbnail', 'shortpixel-image-optimiser'),
'sortable' => false,
'orderby' => 'id', // placeholder to allow sort on this.
),
'name' => array('title' => __('Name', 'shortpixel-image-optimiser'),
'sortable' => true,
'orderby' => 'name',
),
'folder' => array('title' => __('Folder', 'shortpixel-image-optimiser'),
'sortable' => true,
'orderby' => 'path',
),
'type' => array('title' => __('Type', 'shortpixel-image-optimiser'),
'sortable' => false,
),
'date' => array('title' => __('Date', 'shortpixel-image-optimiser'),
'sortable' => true,
'orderby' => 'ts_optimized',
),
'status' => array('title' => __('Status', 'shortpixel-image-optimiser'),
'sortable' => true,
'orderby' => 'status',
),
/* 'actions' => array('title' => __('Actions', 'shortpixel-image-optimiser'),
'sortable' => false,
), */
);
$keyControl = ApiKeyController::getInstance();
if (! $keyControl->keyIsVerified())
{
$headings['actions']['title'] = '';
}
return $headings;
}
protected function getItems()
{
$fs = \wpSPIO()->filesystem();
// [BS] Moving this from ts_added since often images get added at the same time, resulting in unpredictable sorting
$items = $this->queryItems();
$removed = array();
foreach($items as $index => $item)
{
$mediaItem = $fs->getImage($item->id, 'custom');
if (! $mediaItem->exists()) // remove image if it doesn't exist.
{
$mediaItem->onDelete();
$removed[] = $item->path;
unset($items[$index]);
}
$items[$index] = $mediaItem;
}
if (count($removed) > 0)
{
Notices::addWarning(sprintf(__('Some images were missing. They have been removed from the Custom Media overview : %s %s'),
'
', implode('
', $removed)));
}
return $items;
}
protected function getItemFolders($items)
{
$folderArray = array();
$otherMedia = OtherMediaController::getInstance();
foreach ($items as $item) // mediaItem;
{
$folder_id = $item->get('folder_id');
if (! isset($folderArray[$folder_id]))
{
$folderArray[$folder_id] = $otherMedia->getFolderByID($folder_id);
}
}
return $folderArray;
}
/* Check which folders are in result, and load them. */
protected function loadFolders($items)
{
$folderArray = array();
$otherMedia = OtherMediaController::getInstance();
foreach($items as $item)
{
$folder_id = $item->get('folder_id');
if (! isset($folderArray[$folder_id]))
{
$folderArray[$folder_id] = $otherMedia->getFolderByID($folder_id);
}
}
return $folderArray;
}
protected function getFilter() {
$filter = array();
$this->view->hasFilter = false;
$this->view->hasSearch = false;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is not a form
$search = (isset($_GET['s'])) ? sanitize_text_field(wp_unslash($_GET['s'])) : '';
if(strlen($search) > 0) {
$this->view->hasSearch = true;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is not a form
$filter['path'] = (object)array("operator" => "like", "value" =>"'%" . esc_sql($search) . "%'");
}
$folderFilter = (isset($_GET['folder_id'])) ? intval($_GET['folder_id']) : false;
if (false !== $folderFilter)
{
$this->view->hasFilter = true;
$filter['folder_id'] = (object)array("operator" => "=", "value" =>"'" . esc_sql($folderFilter) . "'");
}
$statusFilter = isset($_GET['custom-status']) ? sanitize_text_field($_GET['custom-status']) : false;
if (false !== $statusFilter)
{
$operator = '=';
$value = false;
$this->view->hasFilter = true;
switch($statusFilter)
{
case 'optimized':
$value = ImageModel::FILE_STATUS_SUCCESS;
break;
case 'unoptimized':
$value = ImageModel::FILE_STATUS_UNPROCESSED;
break;
case 'prevented':
// $value = 0;
// $operator = '<';
$filter['status'] = (object) array('field' => 'status',
'operator' => "<", 'value' => "0");
$filter['status2'] = (object) array('field' => 'status',
'operator' => '<>', 'value' => ImageModel::FILE_STATUS_MARKED_DONE
);
break;
}
if (false !== $value)
{
$filter['status'] = (object)array("operator" => $operator, "value" =>"'" . esc_sql($value) . "'");
}
}
return $filter;
}
public function queryItems() {
$filters = $this->getFilter();
global $wpdb;
$page = $this->currentPage;
if ($page <= 0)
$page = 1;
$controller = OtherMediaController::getInstance();
$hidden_ids = $controller->getHiddenDirectoryIDS();
if (count($hidden_ids) > 0)
$this->has_hidden_items = true;
if ($this->show_hidden == true)
$dirs = implode(',', $hidden_ids );
else
$dirs = implode(',', $controller->getActiveDirectoryIDS() );
if (strlen($dirs) == 0)
return array();
$sql = "SELECT COUNT(id) as count FROM " . $wpdb->prefix . "shortpixel_meta where folder_id in ( " . $dirs . ") ";
foreach($filters as $field => $value) {
$field = property_exists($value, 'field') ? $value->field : $field;
$sql .= " AND $field " . $value->operator . " ". $value->value . " ";
}
$this->total_items = $wpdb->get_var($sql);
$sql = "SELECT * FROM " . $wpdb->prefix . "shortpixel_meta where folder_id in ( " . $dirs . ") ";
foreach($filters as $field => $value) {
$field = property_exists($value, 'field') ? $value->field : $field;
$sql .= " AND $field " . $value->operator . " ". $value->value . " ";
}
$sql .= ($this->orderby ? " ORDER BY " . $this->orderby . " " . $this->order . " " : "")
. " LIMIT " . $this->items_per_page . " OFFSET " . ($page - 1) * $this->items_per_page;
$results = $wpdb->get_results($sql);
return $results;
}
private function getPageArgs($args = array())
{
$defaults = array(
'orderby' => $this->orderby,
'order' => $this->order,
's' => $this->search,
'paged' => $this->currentPage
);
$page_args = array_filter(wp_parse_args($args, $defaults));
return $page_args; // has url
}
protected function filterAllowedOrderBy($orderby)
{
$headings = $this->getHeadings() ;
$filters = array();
foreach ($headings as $heading)
{
if (isset($heading['orderby']))
{
$filters[]= $heading['orderby'];
}
}
if (! in_array($orderby, $filters))
return '';
return $orderby;
}
protected function getPagination()
{
$parray = array();
$current = $this->currentPage;
$total = $this->total_items;
$per_page = $this->items_per_page;
$pages = ceil($total / $per_page);
if ($pages <= 1)
return false; // no pages.
$disable_first = $disable_last = $disable_prev = $disable_next = false;
$page_links = array();
if ( $current == 1 ) {
$disable_first = true;
$disable_prev = true;
}
if ( $current == 2 ) {
$disable_first = true;
}
if ( $current == $pages ) {
$disable_last = true;
$disable_next = true;
}
if ( $current == $pages - 1 ) {
$disable_last = true;
}
$total_pages_before = '';
$total_pages_after = '';
$page_args =$this->getPageArgs(); // has url
if (isset($page_args['paged']))
unset($page_args['paged']);
// Try with controller URL, if not present, try with upload URL and page param.
$admin_url = admin_url('upload.php');
$url = (is_null($this->url)) ? add_query_arg('page','wp-short-pixel-custom', $admin_url) : $this->url; // has url
$current_url = add_query_arg($page_args, $url);
$url = remove_query_arg('page', $url);
$page_args['page'] = 'wp-short-pixel-custom';
$output = '