/** * 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 The gallery HTML. */ public function __toString() { include_once DG_PATH . 'inc/class-thumber.php'; $data = ''; $description = ''; $target = $this->gallery->openLinkInNewWindow() ? '_blank' : '_self'; if ($this->gallery->useFancyThumbs()) { $thumb_obj = DG_Thumb::getThumb($this->ID); if (!is_null($thumb_obj)) { if ($thumb_obj->isSuccess()) { // icon has already been generated so include it in generated gallery $thumb = $thumb_obj->getUrl(); } } else { // include a data-* attribute for client side to asynchronously request icon after gallery load $data = 'data-id="' . $this->ID . '"'; } } if (!isset($thumb)) { $thumb = DG_DefaultThumber::init()->getThumbnail($this->ID); } $repl = array($this->link, $thumb, $this->title_attribute, $this->title, $target, $this->extension, $this->size, $this->path, $data); $find = array('%link%', '%img%', '%title_attribute%', '%title%', '%target%', '%extension%', '%size%', '%path%', '%data%'); // 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%">' . PHP_EOL . ' <img src="%img%" title="%title_attribute%" alt="%title_attribute%" %data%/>' . PHP_EOL . ' <span class="title">%title%</span>' . PHP_EOL . ' </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); }
/** * Validate settings for the tab. */ function dg_validate_settings($values) { global $dg_options; $ret = $dg_options; include_once DG_PATH . 'inc/class-gallery.php'; $thumbs_cleared = false; // handle gallery shortcode defaults $errs = array(); $ret['gallery'] = DG_Gallery::sanitizeDefaults(null, $values['gallery_defaults'], $errs); foreach ($errs as $k => $v) { add_settings_error(DG_OPTION_NAME, str_replace('_', '-', $k), $v); } // handle setting width if (isset($values['thumbnail_generation']['width'])) { $width = (int) $values['thumbnail_generation']['width']; if ($width > 0) { $ret['thumber']['width'] = $width; } else { add_settings_error(DG_OPTION_NAME, 'thumber-width', __('Invalid width given: ', 'document-gallery') . $values['thumbnail_generation']['width']); } unset($values['thumbnail_generation']['width']); } // handle setting height if (isset($values['thumbnail_generation']['height'])) { $height = (int) $values['thumbnail_generation']['height']; if ($height > 0) { $ret['thumber']['height'] = $height; } else { add_settings_error(DG_OPTION_NAME, 'thumber-height', __('Invalid height given: ', 'document-gallery') . $values['thumbnail_generation']['height']); } unset($values['thumbnail_generation']['width']); } // delete thumb cache to force regeneration if max dimensions changed if ($ret['thumber']['width'] !== $dg_options['thumber']['width'] || $ret['thumber']['height'] !== $dg_options['thumber']['height']) { DG_Thumb::purgeThumbs(); } // handle setting the active thumbers foreach (array_keys($ret['thumber']['active']) as $k) { $ret['thumber']['active'][$k] = isset($values['thumbnail_generation'][$k]); } // if new thumbers available, clear failed thumbnails for retry if (!$thumbs_cleared) { DG_Thumb::purgeFailedThumbs(); } // handle modified CSS if (trim($ret['css']['text']) !== trim($values['css'])) { $ret['css']['text'] = trim($values['css']); } return $ret; }
/** * Removes all metadata related to a thumbnail for the given attachment ID(s). This allows * the plugin to attempt to re-generate the thumbnail for this attachment next time it * is requested in a gallery or through some other means. * * @param int|int[] $ids Which thumbnails to delete. */ public static function deleteThumbnails($ids) { DG_Thumb::purgeThumbs($ids); }
/** * Delete all thumb entries that do not have an actual image associated with them due to failed generation. */ public static function purgeFailedThumbs() { global $wpdb; $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key = '" . self::$meta_key . "' AND meta_value LIKE '%:%::'"); // force re-query next time self::$thumbs = null; }
/** * Runs when DG is uninstalled for an individual blog. */ private static function _uninstall($blog) { DG_Thumb::purgeThumbs(null, $blog); DocumentGallery::deleteOptions($blog); }
/** * Render a Meta Box. * @param $post WP_Post The post. */ public static function renderMetaBox($post) { // Disabling scripts that have nothing to do with Edit Media pages wp_dequeue_script('dg-media-manager'); remove_filter('mce_external_plugins', array(__CLASS__, 'mce_external_plugins')); remove_filter('mce_css', array(__CLASS__, 'dg_plugin_mce_css')); wp_nonce_field(DG_OPTION_NAME . '_meta_box', DG_OPTION_NAME . '_meta_box_nonce'); $ID = $post->ID; $options = DG_Thumber::getOptions(); $thumb = DG_Thumb::getThumb($ID, $options['width'] . 'x' . $options['height']); $icon = !is_null($thumb) && $thumb->isSuccess() ? $thumb->getUrl() : DG_DefaultThumber::getInstance()->getThumbnail($ID); echo '<table id="ThumbsTable" class="wp-list-table widefat fixed media" cellpadding="0" cellspacing="0">' . '<tbody><tr data-entry="' . $ID . '"><td class="column-icon media-icon"><img src="' . $icon . '" />' . '</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' . $ID . '" type="file" />' . '<input id="trigger-button' . $ID . '" type="button" value="Select File" class="button" />' . '</span>' . '</span>' . '</td></tr></tbody></table>' . (is_null($thumb) ? '<span class="dashicons dashicons-info"></span><span class="">Please note this attachment hasn't been used in any Document Gallery instance and so there is no autogenerated thumbnail, in the meantime default one is used instead.</span>' : '') . PHP_EOL; }
/** * 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 }
/** * Template that handles generating a thumbnail. * * If image has already been generated through other means, $pg may be set to the system path where the * thumbnail is located. In this case, $generator will not be invoked, but *will* be kept for historical purposes. * * @param DG_AbstractThumber|string $generator Takes ID and pg and returns path to temp file or false. * @param int $ID ID for the attachment that we need a thumbnail for. * @param int|string $pg Page number of the attachment to get a thumbnail for or the system path to the image to be used. * * @return DG_Thumb|bool The generated thumbnail or false on failure. */ private static function thumbnailGenerationHarness($generator, $ID, $pg = 1) { // handle system page in $pg variable if (is_string($pg) && !is_numeric($pg)) { $temp_path = $pg; } elseif (is_a($generator, 'DG_AbstractThumber')) { // delegate thumbnail generation to $generator if (false === ($temp_path = $generator->getThumbnail($ID, $pg))) { return false; } // NOTE: get string representation to be stored with thumb in DB $generator = get_class($generator); } else { DG_Logger::writeLog(DG_LogLevel::Error, 'Attempted to call thumbnailGenerationHarness with invalid generator: ' . print_r($generator, true)); return false; } // get some useful stuff $doc_path = get_attached_file($ID); $dirname = dirname($doc_path); $basename = basename($doc_path); if (false === ($len = strrpos($basename, '.'))) { $len = strlen($basename); } $extless = substr($basename, 0, $len); $ext = self::getExt($temp_path); $thumb_name = self::getUniqueThumbName($dirname, $extless, $ext); $thumb_path = "{$dirname}/{$thumb_name}"; // scale generated image down $img = wp_get_image_editor($temp_path); if (is_wp_error($img)) { DG_Logger::writeLog(DG_LogLevel::Error, __('Failed to get image editor: ', 'document-gallery') . $img->get_error_message()); return false; } $options = self::getOptions(); $img->resize($options['width'], $options['height'], false); $err = $img->save($thumb_path); if (is_wp_error($err)) { DG_Logger::writeLog(DG_LogLevel::Error, __('Failed to save image: ', 'document-gallery') . $err->get_error_message()); return false; } // do some cleanup @unlink($temp_path); // save new thumb DG_Logger::writeLog(DG_LogLevel::Detail, 'Creating thumb object.'); $upload = wp_upload_dir(); $thumb = new DG_Thumb(); $thumb->setPostId($ID); $thumb->setDimensions($options['width'] . 'x' . $options['height']); $thumb->setTimestamp(time()); $thumb->setRelativePath(substr($thumb_path, strlen($upload['basedir']) + 1)); $thumb->setGenerator($generator); $thumb->save(); return $thumb; }