| 1 | <?php
|
|---|
| 2 | /**
|
|---|
| 3 | * Plugin Name: Add SVG Support
|
|---|
| 4 | * Plugin URI: https://gist.github.com/morganestes/10066447
|
|---|
| 5 | * Description: Adds SVG support to the `.htaccess` file so Chrome displays images in Admin correctly.
|
|---|
| 6 | * Version: 0.1.0
|
|---|
| 7 | * Author: Morgan Estes
|
|---|
| 8 | * Author URI: http://www.morganestes.me
|
|---|
| 9 | * License: GPLv2
|
|---|
| 10 | */
|
|---|
| 11 | class MorganEstes_SVG {
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * Constructor to kick off the magic.
|
|---|
| 15 | */
|
|---|
| 16 | public function __construct() {
|
|---|
| 17 | $this->init_svg();
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Add our functions inside the right hooks.
|
|---|
| 22 | */
|
|---|
| 23 | public function init_svg() {
|
|---|
| 24 | add_action( 'admin_init', array( $this, 'htaccess_writable' ) );
|
|---|
| 25 | add_action( 'mod_rewrite_rules', array( $this, 'add_svg_htaccess' ) );
|
|---|
| 26 |
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Show an admin notice if .htaccess isn't writable.
|
|---|
| 31 | *
|
|---|
| 32 | * @link https://github.com/rub1/lbrl/blob/master/lib/utils.php#L70
|
|---|
| 33 | */
|
|---|
| 34 | function htaccess_writable() {
|
|---|
| 35 | if ( ! is_writable( get_home_path() . '.htaccess' ) ) {
|
|---|
| 36 | if ( current_user_can( 'administrator' ) ) {
|
|---|
| 37 | add_action( 'admin_notices', create_function( '', "echo '<div class=\"error\"><p>" . sprintf( __( 'Please make sure your <a href="%s">.htaccess</a> file is writable ', 'roots' ), admin_url( 'options-permalink.php' ) ) . "</p></div>';" ) );
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * Adds the SVG ruleset to the install's `.htaccess` file.
|
|---|
| 45 | *
|
|---|
| 46 | * Since WordPress adds the permalinks to the .htaccess file,
|
|---|
| 47 | * I'm hooking into the core mod_rewrite_rules action to
|
|---|
| 48 | * copy my rules in at the same place.
|
|---|
| 49 | *
|
|---|
| 50 | * @param $rules
|
|---|
| 51 | *
|
|---|
| 52 | * @return string
|
|---|
| 53 | */
|
|---|
| 54 | public function add_svg_htaccess( $rules ) {
|
|---|
| 55 |
|
|---|
| 56 | if ( ! defined( 'FS_METHOD' ) ) {
|
|---|
| 57 | define( 'FS_METHOD', 'direct' );
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | $svg_rules = <<<EOF
|
|---|
| 61 | # Add SVG support to the server
|
|---|
| 62 | AddType image/svg+xml svg
|
|---|
| 63 | AddType image/svg+xml svgz
|
|---|
| 64 | AddEncoding x-gzip .svgz
|
|---|
| 65 | EOF;
|
|---|
| 66 |
|
|---|
| 67 | return $rules . $svg_rules;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | $morganestes_svg = new MorganEstes_SVG;
|
|---|