Delete the Book Object cache(s)
static public deleteBookObjectCache ( ) |
/** * Delete 'pressbooks_current_import' option, delete the file too. * * @return bool */ function revokeCurrentImport() { $current_import = get_option('pressbooks_current_import'); if (is_array($current_import) && isset($current_import['file']) && is_file($current_import['file'])) { unlink($current_import['file']); } \Pressbooks\Book::deleteBookObjectCache(); delete_transient('dirsize_cache'); /** @see get_dirsize() */ return delete_option('pressbooks_current_import'); }
/** * Simple revoke of an import (user hits the 'cancel' button) * * @return type */ static function revokeCurrentImport() { \Pressbooks\Book::deleteBookObjectCache(); return delete_option('pbt_current_import'); }
/** * WP_Ajax hook for pb_delete_cover_image */ function delete_cover_image() { if (current_user_can_for_blog(get_current_blog_id(), 'upload_files') && check_ajax_referer('pb-delete-cover-image')) { $image_url = $_POST['filename']; $pid = $_POST['pid']; // Delete old images $old_id = \PressBooks\Image\attachment_id_from_url($image_url); if ($old_id) { wp_delete_attachment($old_id, true); } update_post_meta($pid, 'pb_cover_image', \PressBooks\Image\default_cover_url()); \PressBooks\Book::deleteBookObjectCache(); } // @see http://codex.wordpress.org/AJAX_in_Plugins#Error_Return_Values // Will append 0 to returned json string if we don't die() die; }
/** * WP Hook for action 'wp_update_attachment_metadata'. Deal with user editing cover image from Media Library. */ function save_attachment($data, $post_id) { if (empty($data['file'])) { return $data; } // Bail $post = get_post($post_id); $meta_post = (new \PressBooks\Metadata())->getMetaPost(); // PHP 5.4+ $upload_dir = wp_upload_dir(); $url = untrailingslashit($upload_dir['baseurl']) . "/{$data['file']}"; if ($meta_post && $post && $post->post_parent == $meta_post->ID) { // Update pb_cover_image to point to edited file update_post_meta($meta_post->ID, 'pb_cover_image', $url); \PressBooks\Book::deleteBookObjectCache(); } elseif ($post && strpos($post->post_name, 'pb-catalog-logo') === 0) { // Update pb_catalog_logo to point to edited file update_user_meta($post->post_author, 'pb_catalog_logo', $url); } return $data; }
/** * Generate thumbnails for a user uploaded cover */ function makeThumbnailsForBookCover() { $post = $this->getMetaPost(); if ($post) { $pb_cover_image = get_post_meta($post->ID, 'pb_cover_image', true); if ($pb_cover_image && !preg_match('~assets/dist/images/default-book-cover\\.jpg$~', $pb_cover_image)) { $path = \Pressbooks\Utility\get_media_path($pb_cover_image); $type = wp_check_filetype($path); $type = $type['type']; // Insert new image, create thumbnails $args = array('post_mime_type' => $type, 'post_title' => __('Cover Image', 'pressbooks'), 'post_content' => '', 'post_status' => 'inherit'); include_once ABSPATH . 'wp-admin/includes/image.php'; $id = wp_insert_attachment($args, $path, $post->ID); wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $path)); Book::deleteBookObjectCache(); } } }
// ------------------------------------------------------------------------------------------------------------------- // Custom Css // ------------------------------------------------------------------------------------------------------------------- add_action('admin_menu', '\\PressBooks\\Admin\\CustomCss\\add_menu'); add_action('load-post.php', '\\PressBooks\\Admin\\CustomCss\\redirect_css_editor'); // ------------------------------------------------------------------------------------------------------------------- // SASS // ------------------------------------------------------------------------------------------------------------------- add_action('update_option_pressbooks_global_typography', '\\PressBooks\\Admin\\Fonts\\update_font_stacks'); if (\PressBooks\Book::isBook()) { // Look & Feel add_action('after_switch_theme', '\\PressBooks\\Admin\\Fonts\\update_font_stacks'); // Posts, Meta Boxes add_action('updated_postmeta', function ($meta_id, $object_id, $meta_key, $meta_value) { if ('pb_language' == $meta_key) { \PressBooks\Book::deleteBookObjectCache(); \PressBooks\Admin\Fonts\update_font_stacks(); } }, 10, 4); // Init add_action('admin_init', '\\PressBooks\\Admin\\Fonts\\fix_missing_font_stacks'); add_action('admin_init', '\\PressBooks\\Editor\\add_editor_style'); } // ------------------------------------------------------------------------------------------------------------------- // "Catch-all" routines, must come after taxonomies and friends // ------------------------------------------------------------------------------------------------------------------- add_action('init', '\\PressBooks\\Modules\\Export\\Export::formSubmit', 50); add_action('init', '\\PressBooks\\Modules\\Import\\Import::formSubmit', 50); add_action('init', '\\PressBooks\\CustomCss::formSubmit', 50); add_action('init', '\\PressBooks\\Catalog::formSubmit', 50); // -------------------------------------------------------------------------------------------------------------------
/** * Catch form submissions * * @see pressbooks/admin/templates/export.php */ static function formSubmit() { if (false == static::isFormSubmission() || false == current_user_can('edit_posts')) { // Don't do anything in this function, bail. return; } // Set locale to UTF8 so escapeshellcmd() doesn't strip valid characters. setlocale(LC_CTYPE, 'UTF8', 'en_US.UTF-8'); putenv('LC_CTYPE=en_US.UTF-8'); // Download if (!empty($_GET['download_export_file'])) { $filename = sanitize_file_name($_GET['download_export_file']); static::downloadExportFile($filename); } // Delete if (isset($_POST['delete_export_file']) && isset($_POST['filename']) && check_admin_referer('pb-delete-export')) { $filename = sanitize_file_name($_POST['filename']); $path = static::getExportFolder(); unlink($path . $filename); delete_transient('dirsize_cache'); /** @see get_dirsize() */ \PressBooks\Redirect\location(get_bloginfo('url') . '/wp-admin/admin.php?page=pb_export'); } // Export if ('yes' == @$_GET['export'] && is_array(@$_POST['export_formats']) && check_admin_referer('pb-export')) { // -------------------------------------------------------------------------------------------------------- // Define modules $x = $_POST['export_formats']; $modules = array(); if (isset($x['pdf'])) { $modules[] = '\\PressBooks\\Export\\Prince\\Pdf'; } if (isset($x['mpdf'])) { $modules[] = '\\PressBooks\\Export\\Mpdf\\Pdf'; } if (isset($x['epub'])) { $modules[] = '\\PressBooks\\Export\\Epub\\Epub201'; // Must be set before MOBI } if (isset($x['epub3'])) { $modules[] = '\\PressBooks\\Export\\Epub3\\Epub3'; // Must be set before MOBI } if (isset($x['mobi'])) { if (!isset($x['epub'])) { // Make sure Epub source file is generated $modules[] = '\\PressBooks\\Export\\Epub\\Epub201'; // Must be set before MOBI } $modules[] = '\\PressBooks\\Export\\Mobi\\Kindlegen'; // Must be set after EPUB } if (isset($x['icml'])) { $modules[] = '\\PressBooks\\Export\\InDesign\\Icml'; } if (isset($x['xhtml'])) { $modules[] = '\\PressBooks\\Export\\Xhtml\\Xhtml11'; } if (isset($x['wxr'])) { $modules[] = '\\PressBooks\\Export\\WordPress\\Wxr'; } if (isset($x['vanillawxr'])) { $modules[] = '\\PressBooks\\Export\\WordPress\\VanillaWxr'; } if (isset($x['odt'])) { $modules[] = '\\PressBooks\\Export\\Odt\\Odt'; } // -------------------------------------------------------------------------------------------------------- // Clear cache? Range is 1 hour. $last_export = get_option('pressbooks_last_export'); $within_range = time() - $last_export; if ($within_range > 60 * 60) { \PressBooks\Book::deleteBookObjectCache(); update_option('pressbooks_last_export', time()); } // -------------------------------------------------------------------------------------------------------- // Do Export @set_time_limit(300); $redirect_url = get_bloginfo('url') . '/wp-admin/admin.php?page=pb_export'; $conversion_error = array(); $validation_warning = array(); $outputs = array(); foreach ($modules as $module) { /** @var \PressBooks\Export\Export $exporter */ $exporter = new $module(array()); if (!$exporter->convert()) { $conversion_error[$module] = $exporter->getOutputPath(); } else { if (!$exporter->validate()) { $validation_warning[$module] = $exporter->getOutputPath(); } } // Add to outputs array $outputs[$module] = $exporter->getOutputPath(); // Stats hook do_action('pressbooks_track_export', substr(strrchr($module, '\\'), 1)); } delete_transient('dirsize_cache'); /** @see get_dirsize() */ // -------------------------------------------------------------------------------------------------------- // MOBI cleanup if (isset($x['mobi']) && !isset($x['epub'])) { unlink($outputs['\\PressBooks\\Export\\Epub\\Epub201']); } // -------------------------------------------------------------------------------------------------------- // No errors? if (empty($conversion_error) && empty($validation_warning)) { // Ok! \PressBooks\Redirect\location($redirect_url); } // -------------------------------------------------------------------------------------------------------- // Error exceptions if (isset($validation_warning['\\PressBooks\\Export\\Prince\\Pdf'])) { // The PDF is garbage and we don't want the user to have it. // Delete file. Report error instead of warning. unlink($validation_warning['\\PressBooks\\Export\\Prince\\Pdf']); $conversion_error['\\PressBooks\\Export\\Prince\\Pdf'] = $validation_warning['\\PressBooks\\Export\\Prince\\Pdf']; unset($validation_warning['\\PressBooks\\Export\\Prince\\Pdf']); } // -------------------------------------------------------------------------------------------------------- // Errors :( if (count($conversion_error)) { // Conversion error \PressBooks\Redirect\location($redirect_url . '&export_error=true'); } if (count($validation_warning)) { // Validation warning \PressBooks\Redirect\location($redirect_url . '&export_warning=true'); } } }