WordPress.org

Plugin Directory

Changeset 490477


Ignore:
Timestamp:
01/16/12 06:35:56 (2 years ago)
Author:
mattwiebe
Message:

Better backwards-compatibility for non-Markdown posts that were written before installing this plugin.

Location:
markdown-on-save-improved/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • markdown-on-save-improved/trunk/markdown-on-save.php

    r488246 r490477  
    33Plugin Name: Markdown on Save Improved 
    44Description: Allows you to compose content in Markdown on a per-item basis from WP admin or mobile/3rd-party apps. The markdown version is stored separately, so you can deactivate this plugin and your posts won't spew out Markdown. Based on <a href="http://wordpress.org/extend/plugins/markdown-osi/">Mark Jaquith's plugin</a>. 
    5 Version: 2.1 
     5Version: 2.2 
    66Author: Matt Wiebe 
    77Author URI: http://somadesign.ca/ 
     
    1919class SD_Markdown { 
    2020    const PM = '_sd_disable_markdown'; 
     21    const MD = '_sd_is_markdown'; 
    2122    const CONVERT = 'sd_convert_to_markdown'; 
     23    const VERSION = '2.2'; 
     24    const VERSION_OPT = 'mosi-version'; 
    2225    var $instance; 
    2326 
     
    2528        $this->instance =& $this; 
    2629        add_action( 'init', array( $this, 'init' ) ); 
     30         
     31        register_activation_hook(__FILE__, array($this, 'activate') ); 
    2732    } 
    2833 
     
    3742        add_action( 'load-post-new.php', array( $this, 'load') ); 
    3843        remove_action( 'the_content', 'wpautop' ); 
     44        remove_action( 'the_content', 'wptexturize' ); 
     45        add_action( 'the_content', array($this, 'maybe_default_formatting') ); 
    3946        // Markdown breaks autoembedding by wrapping URLs on their own line in paragraphs 
    4047        if ( get_option('embed_autourls') ) 
    4148            add_filter( 'the_content', array($this, 'oembed_fixer'), 8 ); 
     49        #$this->activate(); 
     50    } 
     51     
     52    public function maybe_default_formatting($content) { 
     53        if ( ! $this->is_markdown( get_the_ID() ) ) { 
     54            $content = wpautop($content); 
     55            $content = wptexturize($content); 
     56        } 
     57        return $content; 
    4258    } 
    4359     
     
    96112        $id = ( isset( $postarr['ID']) ) ? $postarr['ID'] : 0; 
    97113         
    98         // Maybe do HTML → Markdown 
     114        // Maybe do HTML --> Markdown 
    99115        if ( $do_html_to_markdown )  
    100116            $data['post_content'] = $this->html_to_markdown($data['post_content']); 
     
    106122            $data['post_content'] = $this->process($data['post_content'], $id); 
    107123            if ( $id ) 
    108                 delete_post_meta( $id, self::PM ); 
     124                update_post_meta( $id, self::MD, 1 ); 
    109125        } else { 
    110126            $data['post_content_filtered'] = ''; 
    111127            if ( $id ) 
    112                 update_post_meta( $id, self::PM, true ); 
     128                update_post_meta( $id, self::MD, false ); 
    113129        } 
    114130        return $data; 
     
    158174    public function meta_box() { 
    159175        global $post; 
     176        $screen = get_current_screen(); 
     177        #mlog($screen); 
    160178        wp_nonce_field( 'sd-markdown-save', '_sd_markdown_nonce', false, true ); 
    161179        echo '<p><input type="checkbox" name="sd_disable_markdown" id="sd_disable_markdown" value="1" '; 
    162         checked( !! get_post_meta( $post->ID, self::PM, true ) ); 
     180        if ( 'add' !== $screen->action ) 
     181            checked( ! get_post_meta( $post->ID, self::MD, true ) ); 
    163182        echo ' /> <label for="sd_disable_markdown">' . __( 'Disable Markdown formatting', 'markdown-osi' ) . '</label></p>'; 
    164183        printf( '<p><label><input type="checkbox" name="%s" /> %s</label></p>', self::CONVERT, __('Convert HTML to Markdown (experimental)', 'markdown-osi') ); 
     
    166185 
    167186    private function is_markdown( $id ) { 
    168         return ! get_post_meta( $id, self::PM, true ); 
     187        return (bool) get_post_meta( $id, self::MD, true ); 
    169188    } 
    170189 
     
    181200        return $content; 
    182201    } 
    183  
     202     
     203    public function activate() { 
     204        $previous_version = get_option( self::VERSION_OPT, '2.1' ); 
     205        // upgrade to new determining of MD 
     206        if ( version_compare( '2.1', $previous_version, '=') ) { 
     207            $this->update_schema(); 
     208        } 
     209        update_option( self::VERSION_OPT, self::VERSION ); 
     210    } 
     211     
     212    /** 
     213    * Previously, we only set a meta value for disabling. Now we'll set one regardless. Old options need updating. 
     214    */ 
     215    private function update_schema() { 
     216        global $wpdb; 
     217        // formerly MD-disabled posts get updated to new meta key with false value 
     218        $query = $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_key = %s, meta_value = '' WHERE meta_key = %s ", self::MD, self::PM ); 
     219        $wpdb->query( $query ); 
     220         
     221        // posts with stuff in post_content_filtered get the new meta key with true value 
     222        $ids = $wpdb->get_col( "SELECT ID from {$wpdb->posts} WHERE post_content_filtered != '' " ); 
     223        if ( $ids && ! empty($ids) ) { 
     224            foreach ($ids as $id) { 
     225                // reprocess markdown -> we used to strip <p> tags and rely on wpautop 
     226                $post = get_post($id); 
     227                $post->post_content = $this->process( $post->post_content_filtered, $id ); 
     228                wp_update_post($post); 
     229                update_post_meta( $id, self::MD, 1 ); 
     230            } 
     231        } 
     232    } 
    184233} 
    185234 
  • markdown-on-save-improved/trunk/readme.txt

    r488246 r490477  
    33Tags: markdown, formatting, mobile 
    44Requires at least: 3.2 
    5 Tested up to: 3.3 
    6 Stable tag: 2.1 
     5Tested up to: 3.3.1 
     6Stable tag: 2.2 
    77 
    88Markdown + WordPress = writing bliss. The markdown version is stored separately, so you can deactivate this plugin and your posts won't spew out Markdown. 
     
    5858== Changelog == 
    5959 
     60= 2.2 = 
     61 
     62* Better backwards-compatibility for non-Markdown posts that were written before installing this plugin. (i.e. don't turn off wpautop except when needed.) 
     63 
    6064= 2.1 = 
    6165 
Note: See TracChangeset for help on using the changeset viewer.