Plugin Directory

Changeset 489028


Ignore:
Timestamp:
01/12/2012 09:27:52 PM (14 years ago)
Author:
divinenephron
Message:

Major refactoring of code into single_post, post_type and term
controller objects. Also allowed access to all Latex attachments
via the API.

Location:
latex-everything/trunk
Files:
3 added
1 edited
2 moved

Legend:

Unmodified
Added
Removed
  • latex-everything/trunk/class-latex-document.php

    r489027 r489028  
    33define( 'PLUGIN_DIR', plugin_dir_path( __FILE__ ) );            // Plugin directory with trailing slash
    44
    5 include('html-to-latex.php'); // Include functions to convert html to latex.
     5include_once('html-to-latex.php'); // Include functions to convert html to latex.
    66
    77/*
    8  * get_source
    98 * get_posts
    109 * typeset_all_files
    1110 * get_template
    12  * get_attachment_title
    13  * get_pdf_filename
     11 * get_title
     12 * get_name
    1413 * get_parent_post_id
    1514 */
    1615
     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 */
    1746class LE_Latex_Document {
    18 
    19     var $source;
    2047
    2148    var $pdflatex_path;
     
    2350    var $latex_files;
    2451    var $tmp_files;
    25     var $pdf_file;
    26     var $uploaded_file;
     52
     53    var $attachment_id;
    2754
    2855    var $unwanted_filter_functions = Array( 'wptexturize', 'convert_chars', 'esc_html', 'ent2ncr', '_wp_specialchars', 'sanitize_text_field', 'capital_P_dangit' );
     
    3158    function __construct () {
    3259
    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 
    3960        $this->latex_files = array();
    4061        $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' );
    4462
    4563        return 0;
     
    5977     */
    6078    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
    6183        // Generate single latex files for each of a term's posts, because Latex
    6284        // wasn't designed to generate multi-article documents.
     
    7597        if ( is_wp_error( $pdf_file ) )
    7698            return $pdf_file;
    77         else
    78             $this->pdf_file = $pdf_file;
    7999
    80100        // Attach pdf file
    81         $attach_id = $this->attach_pdf_file();
     101        $attach_id = $this->attach_pdf_file( $pdf_file );
    82102        if ( is_wp_error( $attach_id ) )
    83103            return $attach_id;
     
    133153
    134154    function get_template() {
    135         return PLUGIN_DIR . 'default-latex.php';
     155        return PLUGIN_DIR . 'default-latex-template.php';
    136156    }
    137157
     
    192212    }
    193213
    194 
    195     function attach_pdf_file () {
     214    function attach_pdf_file( $pdf_file ) {
    196215        $wp_filetype = wp_check_filetype( '.pdf' );
    197216        $attachment_data = array(
    198217            'post_mime_type' => $wp_filetype['type'],
    199             'post_title' => $this->get_attachment_title(),
     218            'post_title' => $this->get_title(),
    200219            'post_name' => 'pdf',
    201220            'post_content' => '',
     
    203222        );
    204223
    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 {
    224228            $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';
    226230        }
    227231       
    228232        // 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() );
    231235        if ( $attach_id == 0 ) { // Attachment error
    232236            return new WP_Error( 'wp_insert_attachment', 'Could not attach generated pdf' );
    233237        }
    234         add_post_meta( $attach_id, '_le_is_latex', 1, true );
    235238        return $attach_id;
    236239    }
    237240
     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    }
    238276}
    239277
    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 
    280278class LE_Latex_Multiple_Document extends LE_Latex_Document {
    281279
    282280    var $pdftk_path;
    283281
    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
    285287        if ( !$this->pdftk_path = exec( 'which pdftk' ) )
    286288            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
    295290        // Get a temporary filename for the concatenated pdf.
    296291        if ( !$tmp_file = tempnam( sys_get_temp_dir(), 'le-' ) )
     
    340335}
    341336
    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 
    422337?>
  • latex-everything/trunk/latex-everything.php

    r489027 r489028  
    1111
    1212// 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');
    1713
    1814global $latex_everything;
    1915$latex_everything = new Latex_Everything;
    2016
     17include('latex-post-types.php');
     18include('latex-single-posts.php');
     19include('latex-terms.php');
     20
    2121/* When the plugin is activated, create cron jobs to create the desired pdf.
    2222 */
    2323class Latex_Everything {
     24   
     25    var $controllers;
    2426
    2527    function __construct () {
     
    3234        add_action('admin_init', array( &$this, 'settings_api_init' ) );
    3335
     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] );
    3445    }
    3546
     
    3849    function activate () {
    3950        // 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" );
    4152        if ( $option == "doesn't exist" ) {
    4253            update_option( 'le_single_post', 1 );
     
    4455
    4556        // Schedule the creation of pdfs for every post.
    46         $args = Array( 'post-type' => 'post',
     57        $args = Array( 'post-type' => null,
    4758                'numberposts' => -1,
    4859                'orderby' => 'post_date',
     
    7384        // Record which taxonomies and post types are defined (excluding certain ones).
    7485        $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() );
    9589       
    9690        foreach ( $needed_settings as $setting ) {
     
    120114        $docs = array();
    121115
    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 ) );
    140121        foreach ( $docs as $doc ) {
    141122            if ( is_wp_error( $doc ) ) {
     
    143124                continue;
    144125            }
    145             if ( $error = $doc->generate() ) {
     126            $error = $doc->generate();
     127            if ( $error ) {
    146128                $this->handle_error( $error );
    147129                continue;
     
    153135        error_log( "{$error->get_error_code()}: {$error->get_error_message()}" );
    154136    }
     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    }
    155171}
    156172
    157173/* API */
    158174
    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 link
     175/* 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 */
     178function 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
    168184 * to the pdf, use get_latex_url() for that).
    169185 */
    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
     186function 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 */
     194function 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.
    180199 * ID.
    181200 */
    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 }
     201function 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 */
     210function 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
    216216?>
Note: See TracChangeset for help on using the changeset viewer.