hooked = true;
}
public function load()
{
if (! $this->hooked)
$this->loadHooks();
$fs = \wpSPIO()->filesystem();
$fs->startTrustedMode();
}
public function addMetaBox()
{
add_meta_box(
'shortpixel_info_box', // this is HTML id of the box on edit screen
__('ShortPixel Info', 'shortpixel-image-optimiser'), // title of the box
array( $this, 'doMetaBox'), // function to be called to display the info
null,//, // on which edit screen the box should appear
'side'//'normal', // part of page where the box should appear
//'default' // priority of the box
);
}
public function dometaBox($post)
{
$this->post_id = $post->ID;
$this->view->debugInfo = array();
$this->view->id = $this->post_id;
$this->view->list_actions = '';
$fs = \wpSPIO()->filesystem();
$this->imageModel = $fs->getMediaImage($this->post_id);
// Asking for something non-existing.
if ($this->imageModel === false)
{
$this->view->status_message = __('File Error. This could be not an image or the file is missing', 'shortpixel-image-optimiser');
$this->loadView();
return false;
}
$this->view->status_message = null;
$this->view->text = UiHelper::getStatusText($this->imageModel);
$this->view->list_actions = UiHelper::getListActions($this->imageModel);
if ( count($this->view->list_actions) > 0)
$this->view->list_actions = UiHelper::renderBurgerList($this->view->list_actions, $this->imageModel);
else
$this->view->list_actions = '';
//$this->imageModel->cancelUserExclusions();
$this->view->actions = UiHelper::getActions($this->imageModel);
$this->view->stats = $this->getStatistics();
if (! $this->userIsAllowed)
{
$this->view->actions = array();
$this->view->list_actions = '';
}
if(true === \wpSPIO()->env()->is_debug )
{
$this->view->debugInfo = $this->getDebugInfo();
}
$this->loadView();
}
protected function getStatusMessage()
{
return UIHelper::renderSuccessText($this->imageModel);
}
protected function getStatistics()
{
//$data = $this->data;
$stats = array();
$imageObj = $this->imageModel;
$did_keepExif = $imageObj->getMeta('did_keepExif');
$did_convert = $imageObj->getMeta()->convertMeta()->isConverted();
$resize = $imageObj->getMeta('resize');
// Not optimized, not data.
if (! $imageObj->isOptimized())
return array();
if ($did_keepExif)
$stats[] = array(__('EXIF kept', 'shortpixel-image-optimiser'), '');
elseif ( $did_keepExif === false) {
$stats[] = array(__('EXIF removed', 'shortpixel-image-optimiser'), '');
}
if (true === $did_convert )
{
$ext = $imageObj->getMeta()->convertMeta()->getFileFormat();
$stats[] = array( sprintf(__('Converted from %s','shortpixel-image-optimiser'), $ext), '');
}
elseif (false !== $imageObj->getMeta()->convertMeta()->didTry()) {
$ext = $imageObj->getMeta()->convertMeta()->getFileFormat();
$error = $imageObj->getMeta()->convertMeta()->getError(); // error code.
$stats[] = array(UiHelper::getConvertErrorReason($error, $ext), '');
}
if ($resize == true)
{
$from = $imageObj->getMeta('originalWidth') . 'x' . $imageObj->getMeta('originalHeight');
$to = $imageObj->getMeta('resizeWidth') . 'x' . $imageObj->getMeta('resizeHeight');
$type = ($imageObj->getMeta('resizeType') !== null) ? '(' . $imageObj->getMeta('resizeType') . ')' : '';
$stats[] = array(sprintf(__('Resized %s %s to %s'), $type, $from, $to), '');
}
$tsOptimized = $imageObj->getMeta('tsOptimized');
if ($tsOptimized !== null)
$stats[] = array(__("Optimized on :", 'shortpixel-image-optimiser') . "
", UiHelper::formatTS($tsOptimized) );
if ($imageObj->isOptimized())
{
$stats[] = array( sprintf(__('%s %s Read more about theses stats %s ', 'shortpixel-image-optimiser'), '
', '', '
'), ''); } return $stats; } protected function getDebugInfo() { if(! \wpSPIO()->env()->is_debug ) { return array(); } $meta = \wp_get_attachment_metadata($this->post_id); $fs = \wpSPIO()->filesystem(); $imageObj = $this->imageModel; if ($imageObj->isProcessable()) { $optimizeData = $imageObj->getOptimizeData(); $urls = $optimizeData['urls']; } $thumbnails = $imageObj->get('thumbnails'); $processable = ($imageObj->isProcessable()) ? 'Yes' : 'No (' . $imageObj->getReason('processable') . ')'; $anyFileType = ($imageObj->isProcessableAnyFileType()) ? 'Yes' : 'No'; $restorable = ($imageObj->isRestorable()) ? 'Yes' : 'No (' . $imageObj->getReason('restorable') . ')'; $hasrecord = ($imageObj->hasDBRecord()) ? 'Yes' : 'No '; $debugInfo = array(); $debugInfo[] = array(__('URL (get attachment URL)', 'shortpixel_image_optiser'), wp_get_attachment_url($this->post_id)); $debugInfo[] = array(__('File (get attached)'), get_attached_file($this->post_id)); if ($imageObj->is_virtual()) { $virtual = $imageObj->get('virtual_status'); if($virtual == FileModel::$VIRTUAL_REMOTE) $vtext = 'Remote'; elseif($virtual == FileModel::$VIRTUAL_STATELESS) $vtext = 'Stateless'; else $vtext = 'Not set'; $debugInfo[] = array(__('Is Virtual: ') . $vtext, $imageObj->getFullPath() ); } $debugInfo[] = array(__('Size and Mime (ImageObj)'), $imageObj->get('width') . 'x' . $imageObj->get('height'). ' (' . $imageObj->get('mime') . ')'); $debugInfo[] = array(__('Status (ShortPixel)'), $imageObj->getMeta('status') . ' ' ); $debugInfo[] = array(__('Processable'), $processable); $debugInfo[] = array(__('Avif/Webp needed'), $anyFileType); $debugInfo[] = array(__('Restorable'), $restorable); $debugInfo[] = array(__('Record'), $hasrecord); if ($imageObj->getMeta()->convertMeta()->didTry()) { $debugInfo[] = array(__('Converted'), ($imageObj->getMeta()->convertMeta()->isConverted() ?'Yes' : 'No ')); $debugInfo[] = array(__('Checksum'), $imageObj->getMeta()->convertMeta()->didTry()); $debugInfo[] = array(__('Error'), $imageObj->getMeta()->convertMeta()->getError()); } $debugInfo[] = array(__('WPML Duplicates'), json_encode($imageObj->getWPMLDuplicates()) ); if ($imageObj->getParent() !== false) { $debugInfo[] = array(__('WPML duplicate - Parent: '), $imageObj->getParent()); } if (isset($urls)) { $debugInfo[] = array(__('To Optimize URLS'), $urls); } if (isset($optimizeData)) { $debugInfo[] = array(__('Optimize Data'), $optimizeData); $optControl = new optimizeController(); $q = $optControl->getQueue($imageObj->get('type')); $debugInfo[] = array(__('Image to Queue'), $q->_debug_imageModelToQueue($imageObj) ); } $debugInfo['imagemetadata'] = array(__('ImageModel Metadata (ShortPixel)'), $imageObj); $debugInfo[] = array('', '
URL: $url ( $display_size - $width X $height )
FileName: $filename
$backupText $backup
Processable: $processable
Restorable: $restorable
Record: $hasrecord ($dbid)