Changeset 489028
- Timestamp:
- 01/12/2012 09:27:52 PM (14 years ago)
- Location:
- latex-everything/trunk
- Files:
-
- 3 added
- 1 edited
- 2 moved
-
class-latex-document.php (moved) (moved from latex-everything/trunk/latex-document.php) (9 diffs)
-
default-latex-template.php (moved) (moved from latex-everything/trunk/default-latex.php)
-
latex-everything.php (modified) (8 diffs)
-
latex-post-types.php (added)
-
latex-single-posts.php (added)
-
latex-terms.php (added)
Legend:
- Unmodified
- Added
- Removed
-
latex-everything/trunk/class-latex-document.php
r489027 r489028 3 3 define( 'PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); // Plugin directory with trailing slash 4 4 5 include ('html-to-latex.php'); // Include functions to convert html to latex.5 include_once('html-to-latex.php'); // Include functions to convert html to latex. 6 6 7 7 /* 8 * get_source9 8 * get_posts 10 9 * typeset_all_files 11 10 * get_template 12 * get_ attachment_title13 * get_ pdf_filename11 * get_title 12 * get_name 14 13 * get_parent_post_id 15 14 */ 16 15 16 /* LE_Latex_Document 17 * - - - - - - - - - 18 * 19 * The base class representing generated Latex attachments. 20 * 21 * This class isn't of much use on its own, but its subclasses define specific 22 * behaviour that allow you to create and locate different generated Latex 23 * attachments. 24 * 25 * Important methods for using LE_Latex_Document subclasses 26 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - 27 * 28 * new LE_Latex_Document_Subclass( [...] ) 29 * Create a new document representing some Wordpress object. 30 * Args: whatever is needed to identify the object, depending on the subclass. 31 * Return: 0 on success, or a WP_Error object. 32 * 33 * generate() 34 * Creates the Latex document attachment. 35 * Return: 0 on succes, or a WP_Error object. 36 * 37 * get_permalink() 38 * Return the permalink of the attachment. 39 * Return: String containing the permalink 40 * 41 * get_url() 42 * Returns a direct link to the Latex pdf. 43 * Return: String containing the pdf url. 44 * 45 */ 17 46 class LE_Latex_Document { 18 19 var $source;20 47 21 48 var $pdflatex_path; … … 23 50 var $latex_files; 24 51 var $tmp_files; 25 var $pdf_file; 26 var $ uploaded_file;52 53 var $attachment_id; 27 54 28 55 var $unwanted_filter_functions = Array( 'wptexturize', 'convert_chars', 'esc_html', 'ent2ncr', '_wp_specialchars', 'sanitize_text_field', 'capital_P_dangit' ); … … 31 58 function __construct () { 32 59 33 $args = func_get_args();34 $source = call_user_func_array( array( $this, 'get_source' ), $args );35 if ( is_wp_error( $source ) )36 return $source;37 $this->source = $source;38 39 60 $this->latex_files = array(); 40 61 $this->tmp_files = array(); 41 42 if ( !$this->pdflatex_path = exec( 'which pdflatex' ) )43 return new WP_Error( 'LE_Latex_Document::__construct', 'pdflatex not found' );44 62 45 63 return 0; … … 59 77 */ 60 78 function generate() { 79 // Find pdflatex 80 if ( !$this->pdflatex_path = exec( 'which pdflatex' ) ) 81 return new WP_Error( 'LE_Latex_Document::__construct', 'pdflatex not found' ); 82 61 83 // Generate single latex files for each of a term's posts, because Latex 62 84 // wasn't designed to generate multi-article documents. … … 75 97 if ( is_wp_error( $pdf_file ) ) 76 98 return $pdf_file; 77 else78 $this->pdf_file = $pdf_file;79 99 80 100 // Attach pdf file 81 $attach_id = $this->attach_pdf_file( );101 $attach_id = $this->attach_pdf_file( $pdf_file ); 82 102 if ( is_wp_error( $attach_id ) ) 83 103 return $attach_id; … … 133 153 134 154 function get_template() { 135 return PLUGIN_DIR . 'default-latex .php';155 return PLUGIN_DIR . 'default-latex-template.php'; 136 156 } 137 157 … … 192 212 } 193 213 194 195 function attach_pdf_file () { 214 function attach_pdf_file( $pdf_file ) { 196 215 $wp_filetype = wp_check_filetype( '.pdf' ); 197 216 $attachment_data = array( 198 217 'post_mime_type' => $wp_filetype['type'], 199 'post_title' => $this->get_ attachment_title(),218 'post_title' => $this->get_title(), 200 219 'post_name' => 'pdf', 201 220 'post_content' => '', … … 203 222 ); 204 223 205 // Check whether this post has an older file and attachment 206 // object we can update 207 $args = array( 'post_type' => 'attachment', 208 'numberposts' => -1, 209 'post_status' => null, 210 'post_parent' => $this->get_parent_post_id(), ); 211 $attachments = get_posts($args); 212 if ($attachments) { 213 foreach ( $attachments as $attachment ) { 214 $attached_file = get_attached_file( $attachment->ID ); 215 if ( basename( $attached_file ) == $this->get_pdf_filename() ) { 216 $this->uploaded_file = $attached_file; 217 $attachment_data['ID'] = $attachment->ID; 218 } 219 } 220 } 221 222 // If it doesn't, find a location for a new file 223 if ( empty( $this->uploaded_file ) ) { 224 if ( $attachment_id = $this->get_attachment_id() ) { 225 $attachment_data['ID'] = $attachment_id; 226 $uploaded_file = get_attached_file( $attachment_id ); 227 } else { 224 228 $upload_dir = wp_upload_dir(); 225 $ this->uploaded_file = $upload_dir['path'] . '/' . $this->get_pdf_filename();229 $uploaded_file = $upload_dir['path'] . '/' . $this->get_name() . '.pdf'; 226 230 } 227 231 228 232 // Create the attachment 229 copy( $ this->pdf_file, $this->uploaded_file );230 $attach_id = wp_insert_attachment( $attachment_data, $ this->uploaded_file, $this->get_parent_post_id() );233 copy( $pdf_file, $uploaded_file ); 234 $attach_id = wp_insert_attachment( $attachment_data, $uploaded_file, $this->get_parent_post_id() ); 231 235 if ( $attach_id == 0 ) { // Attachment error 232 236 return new WP_Error( 'wp_insert_attachment', 'Could not attach generated pdf' ); 233 237 } 234 add_post_meta( $attach_id, '_le_is_latex', 1, true );235 238 return $attach_id; 236 239 } 237 240 241 /* Returns the id of the existing attachment object for this document. 242 * If this doesn't exists, returns 0. 243 */ 244 function get_attachment_id() { 245 if ( isset( $this->attachment_id ) ) // Check whether we've cached the result. 246 return $this->attachment_id; 247 global $wpdb; 248 249 // If the attachment already exists, it will have an attached 250 // file with the same filename as ours. 251 $query = " 252 SELECT post_id FROM $wpdb->postmeta 253 WHERE meta_key = '_wp_attached_file' 254 AND meta_value LIKE '%{$this->get_name()}.pdf' 255 "; 256 $ids = $wpdb->get_results( $query ); 257 if ( !$ids ) 258 return 0; 259 $attachment_id = $ids[0]->post_id; 260 261 $this->attachment_id = $attachment_id; 262 return $attachment_id; 263 } 264 265 /* Get the permalink of the attachment page. 266 */ 267 function get_permalink() { 268 return get_attachment_url( $this->get_attachment_id() ); 269 } 270 271 /* Get a direct link to the pdf. 272 */ 273 function get_url() { 274 return wp_get_attachment_url( $this->get_attachment_id() ); 275 } 238 276 } 239 277 240 class LE_Latex_Single_Document extends LE_Latex_Document {241 242 function get_source( $id ) {243 return get_post( $id );244 }245 246 function get_posts() {247 return array( $post = $this->source );248 }249 250 function typeset_all_files() {251 return $this->typeset_file( $this->latex_files[0] );252 }253 254 function get_template() {255 $templates = array();256 $templates[] = 'latex';257 $templates[] = "latex-single";258 $templates[] = "latex-single-{$this->source->post_type}";259 $templates[] = "latex-single-{$this->source->post_type}-{$this->source->post_name}";260 $templates[] = "latex-single-{$this->source->post_type}-{$this->source->ID}";261 $template = get_query_template('latex-single', $templates);262 if ( empty( $template) )263 $template = parent::get_template();264 return $template;265 }266 267 function get_pdf_filename() {268 return "{$this->source->post_name}.pdf";269 }270 271 function get_attachment_title() {272 return $this->source->post_title;273 }274 275 function get_parent_post_id() {276 return $this->source->ID;277 }278 }279 280 278 class LE_Latex_Multiple_Document extends LE_Latex_Document { 281 279 282 280 var $pdftk_path; 283 281 284 function __construct() { 282 /* Typsets the latex files seperately then concatenates them with 283 * pdftk. Also ensures the page numbering is correct for each file. 284 */ 285 function typeset_all_files () { 286 // Find pdftk 285 287 if ( !$this->pdftk_path = exec( 'which pdftk' ) ) 286 288 return new WP_Error( 'LE_Latex_Term_Document::__construct', 'pdftk not found' ); 287 call_user_func_array( 'parent::__construct', func_get_args() ); 288 } 289 290 291 /* Typsets the latex files in $doc seperately then concatenates them with 292 * pdftk. Also ensures the page numbering is corret for each file. 293 */ 294 function typeset_all_files () { 289 295 290 // Get a temporary filename for the concatenated pdf. 296 291 if ( !$tmp_file = tempnam( sys_get_temp_dir(), 'le-' ) ) … … 340 335 } 341 336 342 343 class LE_Latex_Post_Type_Document extends LE_Latex_Multiple_Document {344 345 function get_source( $post_type ) {346 $source = get_post_type_object( $post_type );347 if ( !$source )348 return new WP_Error( 'LE_Latex_Term_Document', 'Could not find post type' );349 return $source;350 }351 352 function get_posts() {353 $args = array( 'orderby' => 'date',354 'order' => 'DESC',355 'posts_per_page' => -1,356 'post_type' => $this->source->name, );357 return get_posts( $args );358 }359 360 function get_template() {361 $templates = array();362 $templates[] = 'latex';363 $templates[] = "latex-post-type";364 $templates[] = "latex-post-type-{$this->source->name}";365 $template = get_query_template('latex-term', $templates);366 if ( empty( $template) )367 $template = parent::get_template();368 return $template;369 }370 371 function get_attachment_title() {372 return $this->source->labels->name;373 }374 375 function get_pdf_filename() {376 return "{$this->source->name}.pdf";377 }378 }379 380 class LE_Latex_Term_Document extends LE_Latex_Multiple_Document {381 382 function get_source( $id, $taxonomy ) {383 $source = get_term( $id, $taxonomy );384 return $source;385 }386 387 function get_posts() {388 $args = array( 'tax_query' => array( array(389 'taxonomy' => $this->source->taxonomy,390 'field' => 'id',391 'terms' => $this->source->term_id, )),392 'orderby' => 'date',393 'order' => 'DESC',394 'posts_per_page' => -1,395 'post_type' => null, );396 return get_posts( $args );397 }398 399 function get_template() {400 $templates = array();401 $templates[] = 'latex';402 $templates[] = "latex-term";403 $templates[] = "latex-term-{$this->source->taxonomy}";404 $templates[] = "latex-term-{$this->source->taxonomy}-{$this->source->slug}";405 $templates[] = "latex-term-{$this->source->taxonomy}-{$this->source->term_id}";406 $template = get_query_template('latex-term', $templates);407 if ( empty( $template) )408 $template = parent::get_template();409 return $template;410 }411 412 function get_attachment_title() {413 $tax_name = ucwords( $this->source->taxonomy );414 return "{$tax_name} {$this->source->name}";415 }416 417 function get_pdf_filename() {418 return "{$this->source->taxonomy}-{$this->source->slug}.pdf";419 }420 }421 422 337 ?> -
latex-everything/trunk/latex-everything.php
r489027 r489028 11 11 12 12 // TODO: Make documentation of API and install process. 13 // TODO: Update the API to allow access to the taxonomy and post_type pdfs.14 15 16 include('latex-document.php');17 13 18 14 global $latex_everything; 19 15 $latex_everything = new Latex_Everything; 20 16 17 include('latex-post-types.php'); 18 include('latex-single-posts.php'); 19 include('latex-terms.php'); 20 21 21 /* When the plugin is activated, create cron jobs to create the desired pdf. 22 22 */ 23 23 class Latex_Everything { 24 25 var $controllers; 24 26 25 27 function __construct () { … … 32 34 add_action('admin_init', array( &$this, 'settings_api_init' ) ); 33 35 36 $this->controllers = array(); 37 } 38 39 function add_controller( $name, $controller ) { 40 $this->controllers[$name] = $controller; 41 } 42 43 function remove_controller( $name ) { 44 unset( $this->controllers[$name] ); 34 45 } 35 46 … … 38 49 function activate () { 39 50 // Set the post_type option to 1 if it doesn't already exist 40 $option = get_option( 'le_ post_type_post', "doesn't exist" );51 $option = get_option( 'le_single_post_post', "doesn't exist" ); 41 52 if ( $option == "doesn't exist" ) { 42 53 update_option( 'le_single_post', 1 ); … … 44 55 45 56 // Schedule the creation of pdfs for every post. 46 $args = Array( 'post-type' => 'post',57 $args = Array( 'post-type' => null, 47 58 'numberposts' => -1, 48 59 'orderby' => 'post_date', … … 73 84 // Record which taxonomies and post types are defined (excluding certain ones). 74 85 $needed_settings = array(); 75 $taxonomies = get_taxonomies( '', 'names' ); 76 $taxonomies = array_diff( $taxonomies, array( 'nav_menu', 'link_category', 'post_format' ) ); 77 foreach ( $taxonomies as $taxonomy ) { 78 $taxonomy_obj = get_taxonomy( $taxonomy ); 79 if ( $taxonomy_obj ) { 80 $needed_settings[] = array( 'name' => "le_taxonomy_{$taxonomy}", 81 'title' => "Single {$taxonomy_obj->labels->name}" ); 82 } 83 } 84 $post_types = get_post_types( '', 'names' ); 85 $post_types = array_diff( $post_types, array( 'mediapage', 'attachment', 'revision', 'nav_menu_item' ) ); 86 foreach ( $post_types as $post_type ) { 87 $post_type_obj = get_post_type_object( $post_type ); 88 if ( $post_type_obj ) { 89 $needed_settings[] = array( 'name' => "le_post_type_{$post_type}", 90 'title' => "All {$post_type_obj->labels->name}" ); 91 $needed_settings[] = array( 'name' => "le_single_{$post_type}", 92 'title' => "Single {$post_type_obj->labels->name}" ); 93 } 94 } 86 87 foreach ( $this->controllers as $controller ) 88 $needed_settings = array_merge( $needed_settings, $controller->get_settings() ); 95 89 96 90 foreach ( $needed_settings as $setting ) { … … 120 114 $docs = array(); 121 115 122 // Find out which entities are affected, and make a new document for them. 123 $post_type = get_post_type( $post_id ); 124 if( get_option( "le_single_{$post_type}" ) ) 125 $docs[] = new LE_Latex_Single_Document( $post_id ); 126 if ( get_option( "le_post_type_{$post_type}" ) ) 127 $docs[] = new LE_Latex_Post_Type_Document( $post_type ); 128 129 foreach( get_taxonomies() as $taxonomy ) { 130 if( get_option( "le_taxonomy_{$taxonomy}" ) && $terms = get_the_terms( $post_id, $taxonomy ) ) { 131 if( is_wp_error( $terms ) ) { 132 $this->handle_error( $terms ); 133 continue; 134 } 135 foreach( $terms as $term ) 136 $docs[] = new LE_Latex_Term_Document( $term->term_id, $taxonomy ); 137 } 138 } 139 116 foreach( $this->controllers as $controller ) { 117 $docs = array_merge( $docs, $controller->documents_for_post( $post_id ) ); 118 } 119 120 //error_log( var_export( $docs, true ) ); 140 121 foreach ( $docs as $doc ) { 141 122 if ( is_wp_error( $doc ) ) { … … 143 124 continue; 144 125 } 145 if ( $error = $doc->generate() ) { 126 $error = $doc->generate(); 127 if ( $error ) { 146 128 $this->handle_error( $error ); 147 129 continue; … … 153 135 error_log( "{$error->get_error_code()}: {$error->get_error_message()}" ); 154 136 } 137 138 function get_document( $type /*, [...] */ ) { 139 $args = array_slice( func_get_args(), 1 ); 140 $controller = $this->controllers[$type]; 141 $doc = call_user_func_array( array( &$controller, 'get_document' ), $args ); 142 if ( is_wp_error( $doc ) ) { 143 $this->handle_error( $doc ); 144 return 0; 145 } 146 return $doc; 147 148 } 149 150 function get_latex_permalink( $type /*, [...] */ ) { 151 $doc = call_user_func_array( array( &$this, 'get_document' ), func_get_args() ); 152 if ( $doc ) 153 return $doc->get_permalink(); 154 return ''; 155 } 156 157 function get_latex_url( $type /*, [...] */ ) { 158 $doc = call_user_func_array( array( &$this, 'get_document' ), func_get_args() ); 159 if ( $doc ) { 160 return $doc->get_url(); 161 } 162 return ''; 163 } 164 165 function get_latex_attachment_id( $type /*, [...] */ ) { 166 $doc = call_user_func_array( array( &$this, 'get_document' ), func_get_args() ); 167 if ( $doc ) 168 return $doc->get_attachment_id(); 169 return ''; 170 } 155 171 } 156 172 157 173 /* API */ 158 174 159 /* Display the permalink to the Latex attachment page for the current post.160 * ( not a direct link to the pdf, use get_latex_url() for that).161 */ 162 function the_latex_permalink( ) {163 echo apply_filters('the_latex_permalink', get_latex_permalink());164 } 165 166 167 /* Get the permalink for the current post or a post ID(not a direct link175 /* Echos the permalink to the attachment page for the given thing. 176 * (Not a direct link to the pdf, use get_latex_url() for that). 177 */ 178 function the_latex_permalink($type /*, [...] */ ) { 179 echo call_user_func_array( 'get_latex_permalink', func_get_args() ); 180 } 181 182 183 /* Returns the permalink for the given thing (not a direct link 168 184 * to the pdf, use get_latex_url() for that). 169 185 */ 170 function get_latex_permalink($id = 0) { 171 $attachment = get_latex_attachment( $post->ID ); 172 173 if( !$attachment ) 174 return ''; 175 176 return get_attachment_link($attachment->ID); 177 } 178 179 /* Returns a direct link to the Latex pdf for the current post or a post 186 function get_latex_permalink($type /*, [...] */ ) { 187 global $latex_everything; 188 return call_user_func_array( array( &$latex_everything, 'get_latex_permalink' ), 189 func_get_args() ); 190 } 191 192 /* Echos a direct link to the pdf for the given thing. 193 */ 194 function the_latex_url($type /*, [...] */ ) { 195 echo call_user_func_array( 'get_latex_url', func_get_args() ); 196 } 197 198 /* Returns a direct link to the pdf for the given thing. 180 199 * ID. 181 200 */ 182 function get_latex_url( $id = 0 ) { 183 $attachment = get_latex_attachment( $id ); 184 185 if( !$attachment ) 186 return false; 187 188 return wp_get_attachment_url( $attachment->ID ); 189 } 190 191 /* Returns the latex attachment for the current post or a 192 * post ID. Return false if this doesn't have one. 193 */ 194 function get_latex_attachment( $id=0 ) { 195 if ( is_object($id) ) { 196 $post = $id; 197 } else { 198 $post = &get_post($id); 199 } 200 if ( empty($post->ID) ) 201 return false; 202 203 $args = array( 'post_type' => 'attachment', 204 'numberposts' => 1, 205 'post_parent' => $post->ID, 206 'meta_key' => '_le_is_latex', 207 'meta_value' => 1, 208 ); 209 $attachments = get_posts($args); 210 211 if (!$attachments) 212 return false; 213 214 return $attachments[0]; 215 } 201 function get_latex_url( $type /*, [...] */ ) { 202 global $latex_everything; 203 return call_user_func_array( array( &$latex_everything, 'get_latex_url' ), 204 func_get_args() ); 205 } 206 207 /* Returns the id of the latex pdf attachment for the given thing. 208 * 209 */ 210 function get_latex_attachment_id( $type /*, [...] */ ) { 211 global $latex_everything; 212 return call_user_func_array( array( &$latex_everything, 'get_latex_attachment_id' ), 213 func_get_args() ); 214 } 215 216 216 ?>
Note: See TracChangeset
for help on using the changeset viewer.