Пример #1
0
 /**
  * @covers \Pressbooks\Sanitize\fix_audio_shortcode
  */
 public function test_fix_audio_shortcode()
 {
     \Pressbooks\Sanitize\fix_audio_shortcode();
     $this->assertTrue(has_filter('wp_audio_shortcode'));
     // Verify that style attribute is empty.
     $var = wp_audio_shortcode(array('src' => 'http://foo/audio.mp3'));
     $this->assertFalse(strpos($var, 'style='));
 }
Пример #2
0
 /**
  * Catch form submissions
  *
  * @see pressbooks/templates/admin/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');
     // Override some WP behaviours when exporting
     \Pressbooks\Sanitize\fix_audio_shortcode();
     // 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_admin_url(get_current_blog_id(), '/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\\Modules\\Export\\Prince\\Pdf';
         }
         if (isset($x['print_pdf'])) {
             $modules[] = '\\Pressbooks\\Modules\\Export\\Prince\\PrintPdf';
         }
         if (isset($x['mpdf'])) {
             $modules[] = '\\Pressbooks\\Modules\\Export\\Mpdf\\Pdf';
         }
         if (isset($x['epub'])) {
             $modules[] = '\\Pressbooks\\Modules\\Export\\Epub\\Epub201';
             // Must be set before MOBI
         }
         if (isset($x['epub3'])) {
             $modules[] = '\\Pressbooks\\Modules\\Export\\Epub\\Epub3';
             // Must be set before MOBI
         }
         if (isset($x['mobi'])) {
             if (!isset($x['epub'])) {
                 // Make sure Epub source file is generated
                 $modules[] = '\\Pressbooks\\Modules\\Export\\Epub\\Epub201';
                 // Must be set before MOBI
             }
             $modules[] = '\\Pressbooks\\Modules\\Export\\Mobi\\Kindlegen';
             // Must be set after EPUB
         }
         if (isset($x['icml'])) {
             $modules[] = '\\Pressbooks\\Modules\\Export\\InDesign\\Icml';
         }
         if (isset($x['xhtml'])) {
             $modules[] = '\\Pressbooks\\Modules\\Export\\Xhtml\\Xhtml11';
         }
         if (isset($x['wxr'])) {
             $modules[] = '\\Pressbooks\\Modules\\Export\\WordPress\\Wxr';
         }
         if (isset($x['vanillawxr'])) {
             $modules[] = '\\Pressbooks\\Modules\\Export\\WordPress\\VanillaWxr';
         }
         if (isset($x['odt'])) {
             $modules[] = '\\Pressbooks\\Modules\\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_admin_url(get_current_blog_id(), '/admin.php?page=pb_export');
         $conversion_error = array();
         $validation_warning = array();
         $outputs = array();
         foreach ($modules as $module) {
             /** @var \Pressbooks\Modules\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\\Modules\\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\\Modules\\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\\Modules\\Export\\Prince\\Pdf']);
             $conversion_error['\\Pressbooks\\Modules\\Export\\Prince\\Pdf'] = $validation_warning['\\Pressbooks\\Modules\\Export\\Prince\\Pdf'];
             unset($validation_warning['\\Pressbooks\\Modules\\Export\\Prince\\Pdf']);
         }
         if (isset($validation_warning['\\Pressbooks\\Modules\\Export\\Prince\\PrintPdf'])) {
             // 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\\Modules\\Export\\Prince\\PrintPdf']);
             $conversion_error['\\Pressbooks\\Modules\\Export\\Prince\\PrintPdf'] = $validation_warning['\\Pressbooks\\Modules\\Export\\Prince\\PrintPdf'];
             unset($validation_warning['\\Pressbooks\\Modules\\Export\\Prince\\PrintPdf']);
         }
         // --------------------------------------------------------------------------------------------------------
         // 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');
         }
     }
 }