Changeset 489010
- Timestamp:
- 01/12/2012 09:21:19 PM (14 years ago)
- File:
-
- 1 edited
-
latex-everything/trunk/article-to-latex.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
latex-everything/trunk/article-to-latex.php
r489009 r489010 27 27 var $html_to_latex_script; 28 28 29 var $post; 30 29 31 var $html_file; 30 32 var $latex_file; … … 32 34 var $uploaded_file; 33 35 34 35 function __construct () { 36 var $error; 37 38 39 function __construct ( $post_id ) { 36 40 $this->plugin_dir = plugin_dir_path(__FILE__); 37 41 $this->html_to_latex_script = $this->plugin_dir . 'html2latex.pl'; 42 43 $this->post = get_post( $post_id ); 38 44 } 39 45 … … 46 52 unlink( $this->html_file . '.log' ); 47 53 } 54 55 function create_pdf() { 56 $html_result = $this->make_html_file(); 57 if ( is_wp_error( $html_result ) ) { 58 $this->record_error( $html_result ); 59 return; 60 } 61 62 $latex_result = $this->html_file_to_latex_file(); 63 if ( is_wp_error( $latex_result ) ) { 64 $this->record_error( $latex_result ); 65 return; 66 } 67 68 $pdf_result = $this->latex_file_to_pdf_file(); 69 if ( is_wp_error( $pdf_result ) ) { 70 $this->record_error( $pdf_result ); 71 return; 72 } 73 74 $attach_id = $this->attach_pdf_file(); 75 if ( is_wp_error( $attach_id ) ) { 76 $this->record_error( $pdf_result ); 77 return; 78 } 79 return; 80 } 48 81 49 82 function make_html_file () { 50 $html = '<h1>Header</h1><p>Paragraph.</p>'; 83 $html = $this->post->post_content; 84 $html = apply_filters('the_content', $html); 85 $html = str_replace(']]>', ']]>', $html); 51 86 52 87 if ( !$this->html_file = tempnam( sys_get_temp_dir(), 'a2l-' ) ) // Should fall back on system's temp dir if /tmp does not exist … … 96 131 97 132 function attach_pdf_file () { 98 $upload_dir = wp_upload_dir(); 99 $this->uploaded_file = $upload_dir['path'] . '/' . 'test' . '.pdf'; 100 133 $filename = 'a2l-postie-' . $this->post->ID . '.pdf'; 134 135 $wp_filetype = wp_check_filetype( '.pdf' ); 136 $attachment_data = array( 137 'post_mime_type' => $wp_filetype['type'], 138 'post_title' => $this->post->post_title, 139 'post_name' => 'pdf', 140 'post_content' => '', 141 'post_status' => 'inherit', 142 ); 143 144 /* Check whether this post has an older file and attachment 145 * object we can update 146 */ 147 $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $this->post->ID ); 148 $attachments = get_posts($args); 149 if ($attachments) { 150 foreach ( $attachments as $attachment ) { 151 $attached_file = get_attached_file( $attachment->ID ); 152 if ( basename( $attached_file ) == $filename ) { 153 $this->uploaded_file = $attached_file; 154 $attachment_data['ID'] = $attachment->ID; 155 } 156 } 157 } 158 159 // If it doesn't, find a location for a new file 160 if ( empty( $this->uploaded_file ) ) { 161 $upload_dir = wp_upload_dir(); 162 $this->uploaded_file = $upload_dir['path'] . '/' . $filename; 163 } 164 165 166 // Create the attachment 101 167 copy( $this->pdf_file, $this->uploaded_file ); 168 $attach_id = wp_insert_attachment( $attachment_data, $this->uploaded_file, $this->post->ID ); 169 if ( $attach_id == 0 ) { // Attachment error 170 return WP_Error( 'wp_insert_attachment', 'Could not attach generated pdf' ); 171 } 172 return $attach_id; 173 } 174 175 /* Adds a2l_error to the redirect query when the error is first encounterd. 176 * After the redirect it prints the errors to the top of the admin page. 177 */ 178 function record_error ( $error ) { 179 if ( isset( $_GET['a2l_error'] ) ) { 180 add_action( 'admin_notices', array( $this, 'display_error' ) ); 181 } else { // First time the error has been encountered. 182 add_action('redirect_post_location', array( $this, 'change_redirect_query' ), 99 ); 183 } 184 $this->error = $error; 185 } 186 /* Add a query to the redirect after the post has been saved to remind the post edit 187 * screen to re-run the pdf create so that the error can be displayed. 188 */ 189 function change_redirect_query ($location) { 190 return add_query_arg('a2l_error', $this->post->ID, $location); 191 } 192 /* Displays the error on the admin screen. 193 */ 194 function display_error () { 195 printf( "<div class=\"error\"><p>Article to Latex error</p><p>%s</p><p>%s</p></div>\n", $this->error->get_error_code(), $this->error->get_error_message() ); 102 196 } 103 197 } 104 198 105 function random_test() { 106 $a2l = new Divinenephron_Article_To_Latex; 107 108 $html_result = $a2l->make_html_file(); 109 if ( is_wp_error( $html_result ) ) { 110 print $html_result->get_error_message(); 111 } 112 113 $latex_result = $a2l->html_file_to_latex_file(); 114 if ( is_wp_error( $latex_result ) ) { 115 print $latex_result->get_error_message(); 116 } 117 118 $pdf_result = $a2l->latex_file_to_pdf_file(); 119 if ( is_wp_error( $pdf_result ) ) { 120 printf("<!--\n%s\n-->", $pdf_result->get_error_message()); 121 } 122 123 $a2l->attach_pdf_file(); 199 /* Every time a post is saved run the converter. 200 */ 201 function a2l_save_post ( $post_id ) { 202 if ( get_post_type( $post_id ) == 'post' && !wp_is_post_revision( $post_id ) ) { 203 $a2l = new Divinenephron_Article_To_Latex( $post_id ); 204 $a2l->create_pdf(); 205 } 124 206 } 125 add_action('wp_head', 'random_test'); 207 add_action('save_post', 'a2l_save_post'); 208 209 /* If a2l_error is in the query, a previous post save created an error 210 * for a post (the post ID is its value). Thus we need to re-run the 211 * converter to figure out what the error was and display it. 212 */ 213 function a2l_show_errors () { 214 global $wp_query; 215 if ( isset( $_GET['a2l_error'] ) ) { 216 $post_id = $_GET[ 'a2l_error' ]; 217 $a2l = new Divinenephron_Article_To_Latex( $post_id ); 218 $a2l->create_pdf(); 219 } 220 } 221 add_action( 'admin_head', 'a2l_show_errors' ); 222
Note: See TracChangeset
for help on using the changeset viewer.