diff --git a/faf-pdfextract.php b/faf-pdfextract.php index 3ef73e2..9f27199 100644 --- a/faf-pdfextract.php +++ b/faf-pdfextract.php @@ -17,21 +17,37 @@ */ function extractPdfContent($id) { - if (!`which pdftotext` || get_post_mime_type($id) !== 'application/pdf') { + if (wp_is_post_revision($id)){ return; - //throw new Exception('Du benötigst pdftotext (poppler utils) auf deinem Server!'); } - $file = get_attached_file($id); - if (is_file($file)) { - $txt = $file.'.txt'; + if (get_post_mime_type($id) !== 'application/pdf') { + return; + } + + if (!`which pdftotext`) { + //return; + $content = 'Du benötigst pdftotext (poppler utils) auf deinem Server!'; + } else { + $file = get_attached_file($id); + if (is_file($file)) { + $txt = $file . '.txt'; - system('pdftotext -layout '.$file.' '.$txt); + system('pdftotext -layout ' . $file . ' ' . $txt); - if (is_file($txt)) { - $content = file_get_contents($txt); - update_post_meta($id, 'content', $content); + if (is_file($txt)) { + $content = file_get_contents($txt); + } } } + + $data = []; + $data['ID'] = $id; + $data['post_content'] = $content; + + remove_action('edit_attachment', 'extractPdfContent'); + wp_update_post($data); + add_action('edit_attachment', 'extractPdfContent'); } -add_action('save_post', 'extractPdfContent'); \ No newline at end of file +add_action('edit_attachment', 'extractPdfContent', 10, 2); +add_action('add_attachment', 'extractPdfContent', 10, 2);