Esempio n. 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->assertContains('style=""', $var);
 }
Esempio n. 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_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\\Modules\\Export\\Prince\\Pdf';
         }
         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_bloginfo('url') . '/wp-admin/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']);
         }
         // --------------------------------------------------------------------------------------------------------
         // 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');
         }
     }
 }