/**
  * Bulk PDF import
  * 
  * ## OPTIONS
  * 
  * <source-dir>
  * : required, source directory to import PDF files
  * 
  * <jpeg-compression-quality>
  * : optional, jpeg compression quality, default 60
  *
  * <jpeg-resolution>
  * : optional, jpeg resolution, default 300
  *
  * <post-status>
  * : optional, PDF post status, default "draft"
  *
  * <import-pdf-file>
  * : flag, if set then import PDF file to Wordpress media library
  * 
  * ## EXAMPLES
  * 
  *     wp pdf-light-viewer bulk-import --source-dir="/path/to/pdfs"
  *     wp pdf-light-viewer bulk-import --source-dir="/path/to/pdfs" --jpeg-compression-quality=60 --jpeg-resolution=300 --post-status=publish --import-pdf-file
  *
  * @synopsis --source-dir=<source-dir> [--jpeg-compression-quality=<jpeg-compression-quality>] [--jpeg-resolution=<jpeg-resolution>] [--post-status=<post-status>] [--import-pdf-file]
  * @subcommand bulk-import
  */
 public function bulk_import($args, $assoc_args)
 {
     // options
     $source_dir = $assoc_args['source-dir'];
     $jpeg_compression_quality = isset($assoc_args['jpeg-compression-quality']) ? (int) $assoc_args['jpeg-compression-quality'] : 60;
     $jpeg_resolution = isset($assoc_args['jpeg-resolution']) ? (int) $assoc_args['jpeg-resolution'] : 300;
     $post_status = isset($assoc_args['post-status']) ? $assoc_args['post-status'] : 'draft';
     $import_pdf_file = isset($assoc_args['import-pdf-file']);
     // check requirements
     $plugin_title = PdfLightViewer_Plugin::getData('Title');
     $requirements_met = PdfLightViewer_Plugin::requirements(true);
     if (!$requirements_met) {
         $message = $plugin_title . ': ' . __('requirements not met, please check plugin settings page for more information.', PDF_LIGHT_VIEWER_PLUGIN);
         WP_ClI::error($message, true);
     } else {
         WP_CLI::log($plugin_title . ': ' . __("requirements are met, happy using!", PDF_LIGHT_VIEWER_PLUGIN));
     }
     // check dir
     if (!is_readable($source_dir) || !is_dir($source_dir)) {
         WP_CLI::error(__("Source dir doesn't exist or it's not readable", PDF_LIGHT_VIEWER_PLUGIN), true);
     } else {
         WP_CLI::log(sprintf(__("Searching PDF files in %s", PDF_LIGHT_VIEWER_PLUGIN), $source_dir));
     }
     // check PDF files
     $pdf_files = glob($source_dir . '/*.pdf', GLOB_NOSORT);
     if (empty($pdf_files)) {
         WP_CLI::error(__("Source dir doesn't contain PDF files", PDF_LIGHT_VIEWER_PLUGIN), true);
     } else {
         WP_CLI::log(sprintf(__("%d PDF files found", PDF_LIGHT_VIEWER_PLUGIN), count($pdf_files)));
     }
     // start import
     $pdf_files_count = count($pdf_files);
     $all_pdfs_progress = new \cli\progress\Bar(__("Processing PDF files", PDF_LIGHT_VIEWER_PLUGIN), $pdf_files_count);
     foreach ($pdf_files as $pdf_file_path) {
         // get number of pages
         $im = new Imagick();
         $im->readImage($pdf_file_path);
         $pdf_pages_number = $im->getNumberImages();
         foreach ($im as $_img) {
             $geometry = $_img->getImageGeometry();
             $width = $geometry['width'];
             $height = $geometry['height'];
             break;
         }
         $im->destroy();
         unset($im);
         $current_pdf_progress = new \cli\progress\Bar(sprintf(__("Processing PDF file %s", PDF_LIGHT_VIEWER_PLUGIN), $pdf_file_path), $pdf_pages_number);
         // create PDF post
         $post_id = wp_insert_post(['post_type' => PdfLightViewer_PdfController::$type, 'post_status' => $post_status, 'post_name' => sanitize_title(pathinfo($pdf_file_path, PATHINFO_FILENAME)), 'post_title' => pathinfo($pdf_file_path, PATHINFO_FILENAME)]);
         if (is_wp_error($post_id)) {
             WP_CLI::error(sprintf(__("Could not create PDF post: %s", PDF_LIGHT_VIEWER_PLUGIN), $post_id->get_error_message()), false);
         } else {
             // save pdf to media library
             if ($import_pdf_file) {
                 $image_data = file_get_contents($pdf_file_path);
                 $attach_id = PdfLightViewer_Plugin::create_media_from_data(pathinfo($pdf_file_path, PATHINFO_BASENAME), $image_data);
                 update_post_meta($post_id, 'pdf_file_id', $attach_id);
             }
             $pdf_upload_dir = PdfLightViewer_Plugin::createUploadDirectory($post_id);
             $current_page = 1;
             $ratio = $width / $height;
             do_action(PDF_LIGHT_VIEWER_PLUGIN . ':before_import', $post_id, $pdf_file_path);
             update_post_meta($post_id, '_pdf-light-viewer-import-status', PdfLightViewer_PdfController::STATUS_CLI_PROCESSING);
             update_post_meta($post_id, '_pdf-light-viewer-import-progress', 0);
             update_post_meta($post_id, '_pdf-light-viewer-import-current-page', $current_page);
             update_post_meta($post_id, 'pdf-pages-number', $pdf_pages_number);
             update_post_meta($post_id, 'pdf-page-width', $width);
             update_post_meta($post_id, 'pdf-page-height', $height);
             // process pages
             for ($current_page; $current_page <= $pdf_pages_number; $current_page++) {
                 $page_number = sprintf('%1$05d', $current_page);
                 if (!file_exists($pdf_upload_dir . '/page-' . $page_number . '.jpg')) {
                     try {
                         PdfLightViewer_PdfController::process_pdf_page($post_id, $current_page, $page_number, $pdf_pages_number, $pdf_file_path, $pdf_upload_dir, $jpeg_resolution, $jpeg_compression_quality, $ratio);
                     } catch (Exception $e) {
                         PdfLightViewer_Plugin::log('Import exception: ' . $e->getMessage(), print_r($e, true));
                         $error = $e->getMessage();
                         update_post_meta($post_id, '_pdf-light-viewer-import-status', PdfLightViewer_PdfController::STATUS_FAILED);
                         WP_CLI::warning(sprintf(__('Import of PDF %s failed: %s', PDF_LIGHT_VIEWER_PLUGIN), $pdf_file_path, $error), false);
                     }
                 }
                 $current_pdf_progress->tick();
             }
             do_action(PDF_LIGHT_VIEWER_PLUGIN . ':after_import', $post_id, $pdf_file_path);
             do_action(PDF_LIGHT_VIEWER_PLUGIN . ':finished_import', $post_id, $pdf_file_path);
             update_post_meta($post_id, '_pdf-light-viewer-import-status', PdfLightViewer_PdfController::STATUS_FINISHED);
             WP_CLI::success(sprintf(__('Import of PDF %s finished', PDF_LIGHT_VIEWER_PLUGIN), $pdf_file_path));
         }
         $all_pdfs_progress->tick();
     }
     WP_CLI::success(__('Import finished', PDF_LIGHT_VIEWER_PLUGIN));
 }
Esempio n. 2
0
 public static function requirements($boolean = false)
 {
     $upload_dir_message = __('Upload folder', PDF_LIGHT_VIEWER_PLUGIN) . ': <code>' . PdfLightViewer_Plugin::getUploadDirectory() . '</code>';
     $host_url = site_url();
     $Imagick = null;
     $ImagickVersion = null;
     $pdf_format_support = array();
     if (class_exists("Imagick")) {
         $Imagick = new Imagick();
         $ImagickVersion = $Imagick->getVersion();
         $pdf_format_support = $Imagick->queryFormats('PDF');
     }
     if (PdfLightViewer_AdminController::getSetting('do-not-check-gs')) {
         $ghostscript_version = true;
     } else {
         if (function_exists('shell_exec')) {
             if (stristr(php_uname('s'), 'win')) {
                 $ghostscript_version = @shell_exec('gs --version');
             } else {
                 $ghostscript_version = @shell_exec('$(command -v gs) --version');
                 if (!$ghostscript_version) {
                     $ghostscript_version = @shell_exec('$(which gs) --version');
                 }
                 if (!$ghostscript_version) {
                     $ghostscript_version = @shell_exec('gs --version');
                 }
             }
         } else {
             $ghostscript_version = false;
         }
     }
     $logs_dir_message = __('Logs folder', PDF_LIGHT_VIEWER_PLUGIN) . ': <code>' . self::getLogsPath() . '</code>';
     $requirements = array(array('name' => 'PHP', 'status' => version_compare(PHP_VERSION, '5.3.0', '>='), 'success' => sprintf(__('is %s or higher', PDF_LIGHT_VIEWER_PLUGIN), '5.3'), 'fail' => sprintf(__('is lower than %s', PDF_LIGHT_VIEWER_PLUGIN), '5.3'), 'description' => ''), array('name' => __('Imagick PHP Extension', PDF_LIGHT_VIEWER_PLUGIN), 'status' => extension_loaded('imagick'), 'success' => __('is loaded', PDF_LIGHT_VIEWER_PLUGIN), 'fail' => __('is not loaded', PDF_LIGHT_VIEWER_PLUGIN), 'description' => __("Imagick PHP Extension is required for PDF -> JPEG convertation. It cannot be included with the plugin unfortunately, so you or your hosting provider/server administrator should install it.", PDF_LIGHT_VIEWER_PLUGIN)), array('name' => __('Imagick PHP Wrapper', PDF_LIGHT_VIEWER_PLUGIN), 'status' => class_exists("Imagick"), 'success' => __('is supported', PDF_LIGHT_VIEWER_PLUGIN) . ($ImagickVersion ? '. v.' . $ImagickVersion['versionString'] : ''), 'fail' => __('is not supported', PDF_LIGHT_VIEWER_PLUGIN), 'description' => __("Imagick PHP Wrapper is required to make available Imagick PHP Extension functionality in the plugin. Usually it's integrated through the PECL plugin. It cannot be included with the plugin unfortunately, so you or your hosting provider/server administrator should install it.", PDF_LIGHT_VIEWER_PLUGIN)), array('name' => __('Imagick PDF Support', PDF_LIGHT_VIEWER_PLUGIN), 'status' => $Imagick && !empty($pdf_format_support), 'success' => __('is enabled', PDF_LIGHT_VIEWER_PLUGIN), 'fail' => __('is not enabled', PDF_LIGHT_VIEWER_PLUGIN), 'description' => __("Imagick PDF Support is required for PDF -> JPEG convertation.", PDF_LIGHT_VIEWER_PLUGIN)), array('name' => 'GhostScript', 'status' => $ghostscript_version, 'success' => __('is supported', PDF_LIGHT_VIEWER_PLUGIN) . ($ghostscript_version && is_string($ghostscript_version) ? '. v.' . $ghostscript_version : ''), 'fail' => __('is not supported', PDF_LIGHT_VIEWER_PLUGIN), 'description' => __("GhostScript is required for Imagick PDF Support. For cases, when you are sure that GhostScript is installed, but it was not detected by the plugin correctly you can disable this requirement in options below.", PDF_LIGHT_VIEWER_PLUGIN)), array('name' => $upload_dir_message, 'status' => PdfLightViewer_Plugin::createUploadDirectory(), 'success' => __('is writable', PDF_LIGHT_VIEWER_PLUGIN), 'fail' => __('is not writable', PDF_LIGHT_VIEWER_PLUGIN), 'description' => __("This is the folder for converted images.", PDF_LIGHT_VIEWER_PLUGIN)), array('name' => $logs_dir_message, 'status' => self::createLogsDirectory() && is_writable(self::getLogsPath()), 'success' => __('is writable', PDF_LIGHT_VIEWER_PLUGIN), 'fail' => __('is not writable', PDF_LIGHT_VIEWER_PLUGIN), 'description' => __("We will save plugin-specific log files in this folder.", PDF_LIGHT_VIEWER_PLUGIN)));
     if ($boolean) {
         $status = true;
         foreach ($requirements as $requirement) {
             $status = $status && $requirement['status'];
         }
         return $status;
     } else {
         return $requirements;
     }
 }
 public static function deleted_post($post_id = '', $arg2 = '')
 {
     if ($post_id && get_post_type($post_id) == self::$type) {
         $pdf_upload_dir = PdfLightViewer_Plugin::createUploadDirectory($post_id);
         if ($pdf_upload_dir) {
             self::delete_pages_by_pdf_id($post_id, $pdf_upload_dir);
             rmdir($pdf_upload_dir);
             rmdir($pdf_upload_dir . '-thumbs');
         }
     }
 }