/** * Initialize the thumber class for use in thumbnail generation. */ public static function init() { $options = DG_Thumber::getOptions(); $active = $options['active']; if ($active['imagick']) { parent::init(); } }
/** * @param string $ID The attachment ID to retrieve thumbnail from. * * @return bool|string False on failure, URL to thumb on success. */ private static function getImageThumbnail($ID) { $options = DG_Thumber::getOptions(); $ret = false; if ($icon = image_downsize($ID, array($options['width'], $options['height']))) { $ret = $icon[0]; } return $ret; }
/** * The default DG options to be used on install and when validating structure of options. * * @param $skeleton bool When true, expensive values are not calculated. Only keys may be trusted when returning skeleton. * * @return mixed[][] Contains default options for DG. */ public static function getDefaultOptions($skeleton = false) { include_once DG_PATH . 'inc/class-thumber.php'; $gs = $donate_link = null; if (!$skeleton) { $gs = DG_GhostscriptThumber::getGhostscriptExecutable(); $donate_link = self::getDonateLink(); } return array('thumber' => array('gs' => $gs, 'active' => DG_Thumber::getDefaultThumbers($skeleton), 'width' => 200, 'height' => 200), 'thumber-co' => array('uid' => null, 'secret' => null, 'subscription' => array(), 'direct_upload' => false, 'mime_types' => array()), 'gallery' => array('attachment_pg' => false, 'columns' => 4, 'descriptions' => false, 'fancy' => true, 'limit' => -1, 'mime_types' => implode(',', self::getDefaultMimeTypes()), 'new_window' => false, 'order' => 'ASC', 'orderby' => 'menu_order', 'paginate' => true, 'post_status' => 'any', 'post_type' => 'attachment', 'relation' => 'AND', 'skip' => 0), 'css' => array('text' => ''), 'meta' => array('version' => DG_VERSION, 'items_per_page' => 10, 'donate_link' => $donate_link), 'logging' => array('enabled' => defined('WP_DEBUG') && WP_DEBUG, 'purge_interval' => 7)); }
/** * Accepts AJAX request containing list of IDs to be generated and returned. * Returns associative array mapping ID to thumbnail URL for all icons that were generated, * skipping any that could not be processed. */ public static function generateIcons() { $ret = array(); if (isset($_REQUEST['ids'])) { foreach ($_REQUEST['ids'] as $id) { // only return URL if different from default -- default image is already displayed on the client side $url = DG_Thumber::getInstance()->getThumbnail($id, 1, true, $is_default); if (!$is_default) { $ret[$id] = $url; } } } wp_send_json($ret); }
/** * Returns HTML representing this Document. * @filter dg_icon_template Filters the DG icon HTML. Passes a single * bool value indicating whether the gallery is using descriptions or not. * @return string */ public function __toString() { include_once DG_PATH . 'inc/class-thumber.php'; $thumb = $this->gallery->useFancyThumbs() ? DG_Thumber::getThumbnail($this->ID) : DG_Thumber::getDefaultThumbnail($this->ID); $target = $this->gallery->openLinkInNewWindow() ? '_blank' : '_self'; $repl = array($this->link, $thumb, $this->title_attribute, $this->title, $target, $this->extension, $this->size, $this->path); $find = array('%link%', '%img%', '%title_attribute%', '%title%', '%target%', '%extension%', '%size%', '%path%'); $description = ''; // if descriptions then add filterable tag and value to replaced tag if ($this->gallery->useDescriptions()) { $repl[] = $this->description; $find[] = '%description%'; $description = ' <p>%description%</p>'; } $doc_icon = ' <div class="document-icon">' . PHP_EOL . ' <a href="%link%" target="%target%"><img src="%img%" title="%title_attribute%" alt="%title_attribute%" /><br>%title%</a>' . PHP_EOL . ' </div>' . PHP_EOL . $description; // allow developers to filter icon output $doc_icon = apply_filters('dg_icon_template', $doc_icon, $this->gallery->useDescriptions(), $this->ID); return str_replace($find, $repl, $doc_icon); }
/** * @param int $ID The attachment ID. * @param int $pg The page to thumbnail. * * @return bool Always false. Asynchronously set the thumbnail in webhook later. */ public function getThumbnail($ID, $pg = 1) { global $dg_options; include_once DG_PATH . 'inc/thumbers/thumber-co/thumber-client/client.php'; include_once DG_PATH . 'inc/thumbers/thumber-co/thumber-client/thumb-request.php'; $options = DG_Thumber::getOptions(); $url_or_path = get_attached_file($ID); if (!self::checkGeometry($options['width'], $options['height'])) { DG_Logger::writeLog(DG_LogLevel::Detail, "Skipping attachment #{$ID} as it exceeds Thumber.co subscription geometry limit."); return false; } if (!self::checkFileSize($url_or_path)) { DG_Logger::writeLog(DG_LogLevel::Detail, "Skipping attachment #{$ID} as it exceeds Thumber.co subscription file size limit."); return false; } $mime_type = get_post_mime_type($ID); if (!$dg_options['thumber-co']['direct_upload']) { $url_or_path = wp_get_attachment_url($ID); } if (!$url_or_path || !$mime_type) { return false; } $req = new ThumberThumbReq(); $req->setCallback(self::$webhook); $req->setMimeType($mime_type); $req->setNonce($ID . self::NonceSeparator . md5(microtime())); $req->setPg($pg); $req->setGeometry($options['width'] . 'x' . $options['height']); if ($dg_options['thumber-co']['direct_upload']) { $req->setDecodedData(file_get_contents($url_or_path)); } else { $req->setUrl($url_or_path); } $resp = self::$client->sendThumbRequest($req); if ($resp['http_code'] < 200 || $resp['http_code'] > 399) { DG_Logger::writeLog(DG_LogLevel::Error, 'Failed to transmit to server: ' . $resp['body']); } // always returns false -- we set the thumbnail later when webhook is hit return false; }
/** * * @param int $ID The attachment ID. * @param int $pg The page number to use (1-based numbering). * @param bool $generate_if_missing Whether to generate the thumbnail if it has not yet been generated. * * @return string The URL for the thumbnail NULL. Note that if generate_if_missing * is true then you will never get NULL -- you will get a default icon if generation fails. */ public static function getThumbnail($ID, $pg = 1, $generate_if_missing = false) { include_once DG_PATH . 'inc/class-thumber.php'; return DG_Thumber::getInstance()->getThumbnail($ID, $pg, $generate_if_missing); }
/** * Runs when DG is uninstalled for an individual blog. */ private static function _uninstall($blog) { $options = DG_Thumber::getOptions($blog); if (is_null($options)) { return; } foreach ($options['thumbs'] as $val) { if (isset($val['thumber'])) { @unlink($val['thumb_path']); } } DocumentGallery::deleteOptions($blog); }
/** * Processes the POST request, generating a ThumberResponse, validating, and passing the result to $callback. * If not using client.php as the webhook, whoever receives webhook response should first invoke this method to * validate response. */ public function receiveThumbResponse() { $resp = parent::receiveThumbResponse(); if (is_null($resp)) { return; } $nonce = $resp->getNonce(); $split = explode(DG_ThumberCoThumber::NonceSeparator, $nonce); if ($resp->getSuccess() && count($split) === 2) { $ID = absint($split[0]); $tmpfile = DG_Util::getTempFile(); file_put_contents($tmpfile, $resp->getDecodedData()); DG_Thumber::setThumbnail($ID, $tmpfile, array(__CLASS__, 'getThumberThumbnail')); DG_Logger::writeLog(DG_LogLevel::Detail, "Received thumbnail from Thumber for attachment #{$split[0]}."); } else { $ID = count($split) > 0 ? $split[0] : $nonce; DG_Logger::writeLog(DG_LogLevel::Warning, "Thumber was unable to process attachment #{$ID}: " . $resp->getError()); } }
/** * Save a Meta Box. */ public static function saveMetaBox($post_id) { // Check if our nonce is set. // Verify that the nonce is valid. // If this is an autosave, our form has not been submitted, so we don't want to do anything. if (!isset($_POST[DG_OPTION_NAME . '_meta_box_nonce']) || !wp_verify_nonce($_POST[DG_OPTION_NAME . '_meta_box_nonce'], DG_OPTION_NAME . '_meta_box') || defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } global $dg_options; $responseArr = array('result' => false); if (isset($_POST[DG_OPTION_NAME]['entry'])) { $ID = intval($_POST[DG_OPTION_NAME]['entry']); } else { $ID = -1; } if (isset($_POST[DG_OPTION_NAME]['upload']) && isset($_FILES['file']) && isset($dg_options['thumber']['thumbs'][$ID])) { $old_path = $dg_options['thumber']['thumbs'][$ID]['thumb_path']; $uploaded_filename = self::validateUploadedFile(); if ($uploaded_filename && DG_Thumber::setThumbnail($ID, $uploaded_filename)) { if ($dg_options['thumber']['thumbs'][$ID]['thumb_path'] !== $old_path) { @unlink($old_path); } $responseArr['result'] = true; $responseArr['url'] = $dg_options['thumber']['thumbs'][$ID]['thumb_url']; } } if (isset($_POST[DG_OPTION_NAME]['ajax'])) { wp_send_json($responseArr); } }
/** * Render the Thumbnail table. */ function dg_render_thumbnail_section() { global $dg_url_params; include_once DG_PATH . 'inc/class-thumber.php'; static $limit_options = array(10, 25, 75); static $order_options = array('asc', 'desc'); static $orderby_options = array('date', 'title'); $options = DG_Thumber::getOptions(); // find subset of thumbs to be included $orderby = $dg_url_params['orderby'] = dg_get_orderby_param($orderby_options); $order = $dg_url_params['order'] = dg_get_order_param($order_options); $limit = $dg_url_params['limit'] = dg_get_limit_param(); /** @var DG_Thumb[] $thumbs */ $thumbs = DG_Thumb::getThumbs($options['width'] . 'x' . $options['height']); uasort($thumbs, 'dg_cmp_thumb'); $thumbs_number = count($thumbs); $lastsheet = ceil($thumbs_number / $limit); $sheet = isset($_REQUEST['sheet']) ? absint($_REQUEST['sheet']) : 1; if ($sheet === 0 || $sheet > $lastsheet) { $sheet = 1; } $offset = ($sheet - 1) * $limit; $thumbs = array_slice($thumbs, $offset, $limit, true); // https://core.trac.wordpress.org/ticket/12212 $posts = array(); if (!empty($thumbs)) { $posts = get_posts(array('post_type' => 'any', 'post_status' => 'any', 'numberposts' => -1, 'post__in' => array_keys($thumbs), 'orderby' => 'post__in')); } foreach ($posts as $post) { $path_parts = pathinfo($post->guid); $thumb = $thumbs[$post->ID]; $thumbs[$post->ID] = array(); $t =& $thumbs[$post->ID]; $t['timestamp'] = $thumb->getTimestamp(); $t['title'] = dg_get_thumb_title($post); $t['ext'] = isset($path_parts['extension']) ? $path_parts['extension'] : ''; $t['description'] = $post->post_content; $t['icon'] = $thumb->isSuccess() ? $thumb->getUrl() : DG_DefaultThumber::getInstance()->getThumbnail($post->ID); } unset($posts); $select_limit = ''; foreach ($limit_options as $l_o) { $select_limit .= '<option value="' . $l_o . '"' . selected($limit, $l_o, false) . '>' . $l_o . '</option>' . PHP_EOL; } $thead = '<tr>' . '<th scope="col" class="manage-column column-cb check-column">' . '<label class="screen-reader-text" for="cb-select-all-%1$d">' . __('Select All', 'document-gallery') . '</label>' . '<input id="cb-select-all-%1$d" type="checkbox">' . '</th>' . '<th scope="col" class="manage-column column-icon">' . __('Thumbnail', 'document-gallery') . '</th>' . '<th scope="col" class="manage-column column-title ' . ($orderby != 'title' ? 'sortable desc' : 'sorted ' . $order) . '"><a href="?' . http_build_query(array_merge($dg_url_params, array('orderby' => 'title', 'order' => $orderby != 'title' ? 'asc' : ($order == 'asc' ? 'desc' : 'asc')))) . '"><span>' . __('File name', 'document-gallery') . '</span><span class="sorting-indicator"></span></th>' . '<th scope="col" class="manage-column column-description">' . __('Description', 'document-gallery') . '</th>' . '<th scope="col" class="manage-column column-thumbupload"></th>' . '<th scope="col" class="manage-column column-date ' . ($orderby != 'date' ? 'sortable asc' : 'sorted ' . $order) . '"><a href="?' . http_build_query(array_merge($dg_url_params, array('orderby' => 'date', 'order' => $orderby != 'date' ? 'desc' : ($order == 'asc' ? 'desc' : 'asc')))) . '"><span>' . __('Date', 'document-gallery') . '</span><span class="sorting-indicator"></span></th>' . '</tr>'; $pagination = '<div class="alignleft bulkactions"><button class="button action deleteSelected">' . __('Delete Selected', 'document-gallery') . '</button></div><div class="tablenav-pages">' . '<span class="displaying-num">' . $thumbs_number . ' ' . _n('item', 'items', $thumbs_number, 'document-gallery') . '</span>' . ($lastsheet > 1 ? '<span class="pagination-links">' . '<a class="first-page' . ($sheet == 1 ? ' disabled' : '') . '" title="' . __('Go to the first page', 'document-gallery') . '"' . ($sheet == 1 ? '' : ' href="?' . http_build_query($dg_url_params) . '"') . '>«</a>' . '<a class="prev-page' . ($sheet == 1 ? ' disabled' : '') . '" title="' . __('Go to the previous page', 'document-gallery') . '"' . ($sheet == 1 ? '' : ' href="?' . http_build_query(array_merge($dg_url_params, array('sheet' => $sheet - 1))) . '"') . '>‹</a>' . '<span class="paging-input">' . '<input class="current-page" title="' . __('Current page', 'document-gallery') . '" type="text" name="paged" value="' . $sheet . '" size="' . strlen($sheet) . '" maxlength="' . strlen($sheet) . '"> ' . __('of', 'document-gallery') . ' <span class="total-pages">' . $lastsheet . '</span></span>' . '<a class="next-page' . ($sheet == $lastsheet ? ' disabled' : '') . '" title="' . __('Go to the next page', 'document-gallery') . '"' . ($sheet == $lastsheet ? '' : ' href="?' . http_build_query(array_merge($dg_url_params, array('sheet' => $sheet + 1))) . '"') . '>›</a>' . '<a class="last-page' . ($sheet == $lastsheet ? ' disabled' : '') . '" title="' . __('Go to the last page', 'document-gallery') . '"' . ($sheet == $lastsheet ? '' : ' href="?' . http_build_query(array_merge($dg_url_params, array('sheet' => $lastsheet))) . '"') . '>»</a>' . '</span>' : ' <b>|</b> ') . '<span class="displaying-num"><select dir="rtl" class="limit_per_page">' . $select_limit . '</select> ' . __('items per page', 'document-gallery') . '</span>' . '</div>' . '<br class="clear" />'; ?> <script type="text/javascript"> var URL_params = <?php echo wp_json_encode($dg_url_params); ?> ; </script> <div class="thumbs-list-wrapper"> <div> <div class="tablenav top"><?php echo $pagination; ?> </div> <table id="ThumbsTable" class="wp-list-table widefat fixed media" cellpadding="0" cellspacing="0"> <thead> <?php printf($thead, 1); ?> </thead> <tfoot> <?php printf($thead, 2); ?> </tfoot> <tbody><?php foreach ($thumbs as $tid => $thumb) { $icon = $thumb['icon']; $title = $thumb['title']; $ext = $thumb['ext']; $description = $thumb['description']; $date = DocumentGallery::localDateTimeFromTimestamp($thumb['timestamp']); ?> <tr data-entry="<?php echo $tid; ?> "> <td scope="row" class="check-column"> <input type="checkbox" class="cb-ids" name="<?php echo DG_OPTION_NAME; ?> [ids][]" value="<?php echo $tid; ?> "> </td> <td class="column-icon media-icon"><img src="<?php echo $icon; ?> " /></td> <td class="title column-title"> <strong> <a href="<?php echo home_url('/?attachment_id=' . $tid); ?> " target="_blank" title="<?php sprintf(__("View '%s' attachment page", 'document-gallery'), $title); ?> "> <span class="editable-title"><?php echo $title; ?> </span> <sup><?php echo $ext; ?> </sup> </a> </strong> <span class="dashicons dashicons-edit"></span> <span class="edit-controls"> <span class="dashicons dashicons-yes"></span> <span class="dashicons dashicons-no"></span> </span> </td> <td class="column-description"> <div class="editable-description"><?php echo $description; ?> </div> <span class="dashicons dashicons-edit"></span> <span class="edit-controls"> <span class="dashicons dashicons-yes"></span> <span class="dashicons dashicons-no"></span> <span class="dashicons dashicons-update"></span> </span> </td> <td class="column-thumbupload"> <span class="manual-download"> <span class="dashicons dashicons-upload"></span> <span class="html5dndmarker">Drop file here<span> or </span></span> <span class="buttons-area"> <input id="upload-button<?php echo $tid; ?> " type="file" /> <input id="trigger-button<?php echo $tid; ?> " type="button" value="Select File" class="button" /> </span> </span> <div class="progress animate invis"> <span><span></span></span> </div> </td> <td class="date column-date"><?php echo $date; ?> </td> </tr> <?php } ?> </tbody> </table> <div class="tablenav bottom"><?php echo $pagination; ?> </div> </div> </div> <?php }
/** * @return DG_Thumber The singleton instance. */ public static function getInstance() { return isset(self::$instance) ? self::$instance : (self::$instance = new DG_Thumber()); }
/** * @return bool Whether we can use the GS executable. */ public static function isGhostscriptAvailable() { static $ret = null; if (is_null($ret)) { $options = DG_Thumber::getOptions(); $ret = $options['gs'] && self::isExecAvailable(); } return $ret; }