Changeset 489027
- Timestamp:
- 01/12/2012 09:27:19 PM (14 years ago)
- Location:
- latex-everything/trunk
- Files:
-
- 2 edited
-
latex-everything.php (modified) (4 diffs)
-
uninstall.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
latex-everything/trunk/latex-everything.php
r489026 r489027 11 11 12 12 // TODO: Make documentation of API and install process. 13 // TODO: Allow access to the post_type and term pdfs. 14 // TODO: Use wp-cron to trigger the generation of some stuff, since php is running out of memory. 15 // TODO: Allow people to define what is Latexed. 13 // TODO: Update the API to allow access to the taxonomy and post_type pdfs. 16 14 17 15 … … 25 23 class Latex_Everything { 26 24 27 var $taxonomies;28 var $post_types;29 30 25 function __construct () { 31 register_activation_hook( __FILE__, array( $this, 'activate' ) );32 register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );26 register_activation_hook( __FILE__, array( &$this, 'activate' ) ); 27 register_deactivation_hook( __FILE__, array( &$this, 'deactivate' ) ); 33 28 34 add_action('save_post', array( $this, 'update_post' ) ); 35 add_action('le_activation', array( $this, 'update_post' ) ); 36 add_action('init', array( $this, 'get_everything' ) ); 37 38 //add_action( 'admin_head', array( &$this, 'show_errors' ) ); 39 } 40 41 function get_everything () { 42 // Find all entities you can generate stuff for 43 $this->taxonomies = get_taxonomies( '', 'names' ); 44 $this->taxonomies = array_diff( $this->taxonomies, array( 'nav_menu', 'link_category', 'post_format' ) ); 45 $this->post_types = get_post_types( '', 'names' ); 46 $this->post_types = array_diff( $this->post_types, array( 'mediapage', 'attachment', 'revision', 'nav_menu_item' ) ); 47 } 48 29 add_action('save_post', array( &$this, 'update_post' ) ); 30 add_action('le_activation', array( &$this, 'update_post' ) ); 31 32 add_action('admin_init', array( &$this, 'settings_api_init' ) ); 33 34 } 35 49 36 /* Create cron-jobs to re-create the pdf for every post. 50 37 */ 51 38 function activate () { 39 // Set the post_type option to 1 if it doesn't already exist 40 $option = get_option( 'le_post_type_post', "doesn't exist" ); 41 if ( $option == "doesn't exist" ) { 42 update_option( 'le_single_post', 1 ); 43 } 44 45 // Schedule the creation of pdfs for every post. 52 46 $args = Array( 'post-type' => 'post', 53 47 'numberposts' => -1, … … 68 62 } 69 63 70 /* Create a latex document for a post/taxonomy. 64 function settings_api_init() { 65 66 // Add the section to reading settings so we can add our 67 // fields to it 68 add_settings_section('le_setting_section', 69 'Latex Everything', 70 array( &$this, 'setting_section' ), 71 'reading'); 72 73 // Record which taxonomies and post types are defined (excluding certain ones). 74 $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 } 95 96 foreach ( $needed_settings as $setting ) { 97 add_settings_field( $setting['name'], 98 $setting['title'], 99 array( &$this, 'setting' ), 100 'reading', 101 'le_setting_section', 102 array( 'name' => $setting['name'] ) ); 103 register_setting('reading', $setting['name'] ); 104 } 105 } 106 107 /* Prints a description at the top of the setting section. 108 */ 109 function setting_section() { 110 echo '<p>Generate documents containing:</p>'; 111 } 112 113 /* Creates a checkbox for the option $args['name']. 71 114 */ 72 function create_document ( $id, $taxonomy='' ) { 73 $doc = new LE_Latex_Document( $id, $taxonomy ); 74 if ( is_wp_error( $doc ) ) { 75 error_log( "{$doc->get_error_code()}: {$doc->get_error_message()}" ); 76 return; 77 } 78 $error = $doc->generate(); 79 if (is_wp_error( $error ) ) { 80 error_log( "{$error->get_error_code()}: {$error->get_error_message()}" ); 81 return; 82 } 83 } 115 function setting( $args ) { 116 echo '<input name="' . $args['name'] . '" id="' . $args['name'] . '" type="checkbox" value="1" class="code" ' . checked( 1, get_option( $args['name'], 0 ), false ) . '>'; 117 } 84 118 85 119 function update_post ( $post_id ) { 86 // TODO: Re-do the error handling.87 120 $docs = array(); 88 121 89 122 // Find out which entities are affected, and make a new document for them. 90 123 $post_type = get_post_type( $post_id ); 91 if( in_array( $post_type, $this->post_types ) ) {124 if( get_option( "le_single_{$post_type}" ) ) 92 125 $docs[] = new LE_Latex_Single_Document( $post_id ); 126 if ( get_option( "le_post_type_{$post_type}" ) ) 93 127 $docs[] = new LE_Latex_Post_Type_Document( $post_type ); 94 } 95 foreach( $this->taxonomiesas $taxonomy ) {96 if( $terms = get_the_terms( $post_id, $taxonomy ) ) {97 if( is_wp_error( $terms ) ) 98 error_log( "{$terms->get_error_code()}: {$terms->get_error_message()}");99 else100 foreach( $terms as $term )101 $i = 1;102 $docs[] = new LE_Latex_Term_Document( $term->term_id, $taxonomy );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 ); 103 137 } 104 138 } … … 106 140 foreach ( $docs as $doc ) { 107 141 if ( is_wp_error( $doc ) ) { 108 error_log( "{$terms->get_error_code()}: {$terms->get_error_message()}");142 $this->handle_error( $doc ); 109 143 continue; 110 144 } 111 145 if ( $error = $doc->generate() ) { 112 error_log( "{$error->get_error_code()}: {$error->get_error_message()}");146 $this->handle_error( $error ); 113 147 continue; 114 148 } 115 149 } 116 150 } 117 /* If le_error is in the query, a previous post save created an error 118 * for a post (the post ID is its value). Thus we need to re-run the 119 * converter to figure out what the error was and display it. 120 */ 121 /* 122 function show_errors () { 123 global $wp_query; 124 if ( isset( $_GET['le_error'] ) ) { 125 $post_id = $_GET[ 'le_error' ]; 126 global $latex_everything; 127 $le = new LE_Latex_Document( $post_id ); 128 $le->create_pdf(); 129 } 130 } 131 */ 132 /* Adds le_error to the redirect query when the error is first encounterd. 133 * After the redirect it prints the errors to the top of the admin page. 134 */ 135 /* 136 function record_error ( $error ) { 137 if ( isset( $_GET['le_error'] ) ) { 138 add_action( 'admin_notices', array( $this, 'display_error' ) ); 139 } else { // First time the error has been encountered. 140 add_action('redirect_post_location', array( $this, 'change_redirect_query' ), 99 ); 141 } 142 $this->error = $error; 143 } 144 */ 145 /* Add a query to the redirect after the post has been saved to remind the post edit 146 * screen to re-run the pdf create so that the error can be displayed. 147 */ 148 /* 149 function change_redirect_query ($location) { 150 return add_query_arg('le_error', $this->post->ID, $location); 151 } 152 */ 153 /* Displays the error on the admin screen. 154 */ 155 /* 156 function display_error () { 157 printf( "<div class=\"error\"><p>Article to Latex error: %s</p><pre>%s</pre></div>\n", $this->error->get_error_code(), $this->error->get_error_message() ); 158 } 159 */ 151 152 function handle_error( $error ) { 153 error_log( "{$error->get_error_code()}: {$error->get_error_message()}" ); 154 } 160 155 } 161 156 -
latex-everything/trunk/uninstall.php
r489019 r489027 1 1 <?php 2 /* When uninstalling, remove every generated pdf 3 */ 2 if( !defined( 'ABSPATH') && !defined('WP_UNINSTALL_PLUGIN') ) 3 exit(); 4 5 // Remove every generated pdf 4 6 $args = array( 'post_type' => 'attachment', 5 7 'numberposts' => -1, … … 10 12 foreach ($attachments as $attachment) 11 13 wp_delete_attachment( $attachment->ID ); 14 15 /* Remove the options 16 */ 17 global $wpdb; 18 $options = $wpdb->get_results( "SELECT * FROM $wpdb->options ORDER BY option_name" ); 19 foreach ( (array) $options as $option ) 20 if ( strncmp( 'le_', $option->option_name, 3 ) == 0 ) // option_name has le_ prefix 21 delete_option( $option->option_name ); 12 22 ?> 23
Note: See TracChangeset
for help on using the changeset viewer.