Plugin Directory

Changeset 2445622


Ignore:
Timestamp:
12/24/2020 04:29:12 PM (5 years ago)
Author:
sakuratan
Message:

Version 0.2.3

Location:
external-css/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • external-css/trunk/external-css.php

    r138259 r2445622  
    44Plugin URI: http://sakuratan.biz/contents/external-css
    55Description: External CSS plugin allows you to edit an external CSS file through WordPress dashboard. All blog pages except admin pages imports the CSS file via HTML link tag. This plugin is very SEO friendly and decreases the network load.
    6 Version: 0.2.2
     6Version: 0.2.3
    77Author: sakuratan
    88Author URI: http://sakuratan.biz/
     
    1010
    1111/*
    12 Copyright (c) 2009 sakuratan.
     12Copyright (c) 2009-2020 sakuratan.
    1313
    1414Permission is hereby granted, free of charge, to any person obtaining a copy
     
    3535
    3636function external_css_get_cssdir() {
    37     $upload = wp_upload_dir();
    38     if ( $upload['error'] )
    39     return false;
    40     $dir = "{$upload['basedir']}/external-css";
    41     if ( is_dir( $dir ) ) {
     37    $upload = wp_upload_dir();
     38    if ( $upload['error'] ) {
     39        return false;
     40    }
     41    $dir = "{$upload['basedir']}/external-css";
     42    if ( is_dir( $dir ) ) {
     43        return $dir;
     44    }
     45    if ( ! wp_mkdir_p( $dir ) ) {
     46        return false;
     47    }
    4248    return $dir;
    43     }
    44     if ( !wp_mkdir_p( $dir ) )
    45     return false;
    46     return $dir;
    4749}
    4850
    4951function external_css_get_cssbaseurl() {
    50     $upload = wp_upload_dir();
    51     if ( $upload['error'] )
    52     return false;
    53     return "{$upload['baseurl']}/external-css";
     52    $upload = wp_upload_dir();
     53    if ( $upload['error'] ) {
     54        return false;
     55    }
     56    return "{$upload['baseurl']}/external-css";
    5457}
    5558
     
    5760 * Display link tag for custom.css.
    5861 */
    59 function external_css_embed_link_tag() {
    60     $base = external_css_get_cssbaseurl();
    61     if ( !$base )
    62     return;
    63     $url = htmlspecialchars( "{$base}/custom.css" );
    64     echo "<link href=\"{$url}\" rel=\"stylesheet\" type=\"text/css\" media=\"screen,projection\" />";
     62function external_css_scripts() {
     63    $base = external_css_get_cssbaseurl();
     64    if ( ! $base ) {
     65        return;
     66    }
     67    wp_enqueue_style( 'external-css-custom', "{$base}/custom.css" );
    6568}
    6669
     
    6972 */
    7073function external_css_error( $s ) {
    71     echo '<p class="error">';
    72     echo htmlspecialchars( $s );
    73     echo '</p>';
     74    echo '<p class="error">';
     75    echo htmlspecialchars( $s );
     76    echo '</p>';
    7477}
    7578
     
    7881 */
    7982function external_css_save( $path, $css ) {
    80     $dir = dirname( $path );
    81     $tmpfile = tempnam( $dir, 'tmp' );
    82     if ( !$tmpfile ) {
    83     external_css_error( __( 'Failed to create temporary file', 'external-css' ) );
    84     return false;
    85     }
    86 
    87     $fh = fopen( $tmpfile, 'wb' );
    88     if ( !$fh ) {
    89     external_css_error( __( 'Failed to open temporary file', 'external-css' ) );
    90     unlink( $tmpfile );
    91     return false;
    92     }
    93     fwrite( $fh, $css );
    94     fclose( $fh );
    95 
    96     if ( !chmod( $tmpfile, 0644 ) ) {
    97     external_css_error( __( 'Failed to chmod temporary file', 'external-css' ) );
    98     return false;
    99     }
    100 
    101     /* Update css with POSIX atomic function */
    102     if ( !rename( $tmpfile, $path ) ) {
    103     external_css_error( __( 'Failed to rename CSS', 'external-css' ) );
    104     return false;
    105     }
    106 
    107     return true;
     83    $dir    = dirname( $path );
     84    $tmpfile = tempnam( $dir, 'tmp' );
     85    if ( ! $tmpfile ) {
     86        external_css_error( __( 'Failed to create temporary file', 'external-css' ) );
     87        return false;
     88    }
     89
     90    $fh = fopen( $tmpfile, 'wb' );
     91    if ( ! $fh ) {
     92        external_css_error( __( 'Failed to open temporary file', 'external-css' ) );
     93        unlink( $tmpfile );
     94        return false;
     95    }
     96    fwrite( $fh, $css );
     97    fclose( $fh );
     98
     99    if ( ! chmod( $tmpfile, 0644 ) ) {
     100        external_css_error( __( 'Failed to chmod temporary file', 'external-css' ) );
     101        return false;
     102    }
     103
     104    /* Update css with POSIX atomic function */
     105    if ( ! rename( $tmpfile, $path ) ) {
     106        external_css_error( __( 'Failed to rename CSS', 'external-css' ) );
     107        return false;
     108    }
     109
     110    return true;
    108111}
    109112
     
    112115 */
    113116function external_css_theme_page() {
    114     $dir = external_css_get_cssdir();
    115     if ( !$dir ) {
    116     external_css_error( sprintf( __( 'External CSS cannot continue the process because missing permissions for %s. Add the read and write permissions of the directory to the user of the web server to fix the problem.', 'external-css' ), $dir ) );
    117     return;
    118     }
    119 
    120     $path = "{$dir}/custom.css";
    121     if ( array_key_exists( 'external_css', $_POST ) ) {
    122     if ( external_css_save( $path, stripslashes( $_POST['external_css'] ) ) ) {
    123         echo '<div class="updated"><p><strong>';
    124         _e( 'Options saved.', 'external-css'  );
    125         echo '</strong></p></div>';
    126     }
    127     }
     117    $dir = external_css_get_cssdir();
     118    if ( ! $dir ) {
     119        external_css_error( sprintf( __( 'External CSS cannot continue the process because missing permissions for %s. Add the read and write permissions of the directory to the user of the web server to fix the problem.', 'external-css' ), $dir ) );
     120        return;
     121    }
     122
     123    $path = "{$dir}/custom.css";
     124    if ( array_key_exists( 'external_css', $_POST ) ) {
     125        if ( external_css_save( $path, stripslashes( $_POST['external_css'] ) ) ) {
     126            echo '<div class="updated"><p><strong>';
     127            _e( 'Options saved.', 'external-css' );
     128            echo '</strong></p></div>';
     129        }
     130    }
     131
     132    ?>
     133<h2>External CSS Options</h2>
     134
     135<form method="post" action="<?php echo htmlspecialchars( $_SERVER['REQUEST_URI'] ); ?>">
     136
     137<textarea cols="70" rows="25" name="external_css" style="overflow-y:scroll"><?php
     138    @readfile( $path );
     139?></textarea>
     140
     141<p class="submit"><input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'external-css' ); ?>" /></p>
     142
     143</form>
     144    <?php
     145}
     146
     147/*
     148 * Add theme page to admin menu.
     149 */
     150function external_css_admin_menu() {
     151    add_theme_page(
     152        'External CSS Options',
     153        'External CSS',
     154        'switch_themes',
     155        __FILE__,
     156        'external_css_theme_page'
     157    );
     158}
     159
     160/*
     161 * Initialize the plugin.
     162 */
     163function external_css_init() {
     164    load_plugin_textdomain(
     165        'external-css',
     166        'wp-content/plugins/external-css/po',
     167        'external-css/po'
     168    );
     169    add_action( 'admin_menu', 'external_css_admin_menu' );
     170}
     171
     172add_action( 'init', 'external_css_init' );
     173add_action( 'wp_enqueue_scripts', 'external_css_scripts' );
     174
     175/*
     176 * Fix path of CSS file.
     177 */
     178function external_css_activation() {
     179    $cssdir = external_css_get_cssdir();
     180    if ( ! $cssdir ) {
     181        return;
     182    }
     183    $path = "{$cssdir}/custom.css";
     184
     185    $files = array(
     186        // 0.2.0 or older
     187        WP_PLUGIN_DIR . '/external-css/css/custom.css'    => array(
     188            WP_PLUGIN_DIR . '/external-css/css',
     189        ),
     190        // 0.2.1
     191        WP_CONTENT_DIR . '/cache/external-css/custom.css' => array(
     192            WP_CONTENT_DIR . '/cache/external-css',
     193            WP_CONTENT_DIR . '/cache',
     194        ),
     195    );
     196
     197    $havenewfile = @file_exists( $path );
     198
     199    foreach ( $files as $oldpath => $cleandirs ) {
     200        if ( ! file_exists( $oldpath ) ) {
     201            continue;
     202        }
     203        if ( ! $havenewfile ) {
     204            if ( ! copy( $oldpath, $path ) ) {
     205                continue;
     206            }
     207        }
     208        if ( unlink( $oldpath ) ) {
     209            foreach ( $cleandirs as $dir ) {
     210                if ( ! @rmdir( $dir ) ) {
     211                    break;
     212                }
     213            }
     214        }
     215    }
     216
     217    if ( ! @file_exists( $path ) ) {
     218        $fh = fopen( $path, 'wb' );
     219        if ( $fh ) {
     220            fwrite( $fh, "/* Edit this */\n" );
     221            fclose( $fh );
     222        }
     223    }
     224}
     225
     226register_activation_hook( __FILE__, 'external_css_activation' );
    128227
    129228?>
    130 <h2>External CSS Options</h2>
    131 
    132 <form method="post" action="<?php echo htmlspecialchars( $_SERVER['REQUEST_URI'] ) ?>">
    133 
    134 <textarea cols="70" rows="25" name="external_css" style="overflow-y:scroll"><?php
    135     @readfile( $path );
    136 ?></textarea>
    137 
    138 <p class="submit"><input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'external-css' ) ?>" /></p>
    139 
    140 </form>
    141 <?php
    142 }
    143 
    144 /*
    145  * Add theme page to admin menu.
    146  */
    147 function external_css_admin_menu() {
    148     add_theme_page( 'External CSS Options', 'External CSS', 'switch_themes',
    149            __FILE__, 'external_css_theme_page' );
    150 }
    151 
    152 /*
    153  * Initialize the plugin.
    154  */
    155 function external_css_init() {
    156     load_plugin_textdomain( 'external-css',
    157                 'wp-content/plugins/external-css/po',
    158                 'external-css/po' );
    159     add_action( 'admin_menu', 'external_css_admin_menu' );
    160     add_action( 'wp_head', 'external_css_embed_link_tag', 100 );
    161 }
    162 
    163 add_action( 'init', 'external_css_init' );
    164 
    165 /*
    166  * Fix path of CSS file.
    167  */
    168 function external_css_activation() {
    169     $cssdir = external_css_get_cssdir();
    170     if ( !$cssdir )
    171     return;
    172     $path = "{$cssdir}/custom.css";
    173 
    174     $files = array(
    175     // 0.2.0 or older
    176     WP_PLUGIN_DIR . '/external-css/css/custom.css' => array(
    177         WP_PLUGIN_DIR . '/external-css/css'
    178     ),
    179     // 0.2.1
    180     WP_CONTENT_DIR . '/cache/external-css/custom.css' => array(
    181         WP_CONTENT_DIR . '/cache/external-css',
    182         WP_CONTENT_DIR . '/cache'
    183     )
    184     );
    185 
    186     $havenewfile = @file_exists( $path );
    187 
    188     foreach ( $files as $oldpath => $cleandirs ) {
    189     if ( !file_exists( $oldpath ) )
    190         continue;
    191     if ( !$havenewfile ) {
    192         if ( !copy( $oldpath, $path ) )
    193         continue;
    194     }
    195     if ( unlink( $oldpath ) ) {
    196         foreach ( $cleandirs as $dir ) {
    197         if ( !@rmdir( $dir ) )
    198             break;
    199         }
    200     }
    201     }
    202 
    203     if ( !@file_exists( $path ) ) {
    204     $fh = fopen( $path, 'wb' );
    205     if ( $fh ) {
    206         fwrite( $fh, "/* Edit this */\n" );
    207         fclose( $fh );
    208     }
    209     }
    210 }
    211 
    212 register_activation_hook( __FILE__, 'external_css_activation' );
    213 
    214 ?>
  • external-css/trunk/readme.txt

    r2445019 r2445622  
    2828== Changelog ==
    2929
     30= 0.2.3 =
     31* Using wp_enqueue_style to load CSS file.
     32
    3033= 0.2.2 =
    3134* Spaces for following the wordPress coding standards.
Note: See TracChangeset for help on using the changeset viewer.