Plugin Directory

Changeset 1219543


Ignore:
Timestamp:
08/12/2015 09:27:49 PM (11 years ago)
Author:
s-hiroshi
Message:

Refactor

Location:
resize-editor/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • resize-editor/trunk/readme.txt

    r705218 r1219543  
    22Contributors: s-hiroshi
    33Tags: editor
    4 Requires at least: 3.5.1
    5 Tested up to: 3.5.1
    6 Stable tag: 1.0.4
     4Requires at least: 4.2.4
     5Tested up to: 4.2.4
     6Stable tag: 1.1.0
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • resize-editor/trunk/resize-editor.php

    r705219 r1219543  
    22/*
    33Plugin Name: Resize Editor
    4 Plugin URI: http://www.findxfine.com
     4Plugin URI: http://www.creationlabs.net/resize-editor
    55Description: Resize post editor width.
    66Text Domain: resize-editor
    77Domain Path: /languages/
    88Author: Hiroshi Sawai
    9 Author URI: http://www.findxfine.com
    10 Version: 1.0.4
     9Author URI: http://www.info-town.jp
     10Version: 1.1.0
    1111*/
    1212
     
    2828*/
    2929
     30/*
     31 * Defines plugin constant values
     32 */
    3033if ( ! defined( 'RESIZE_EDITOR_DIR' ) ) {
    31     define( 'RESIZE_EDITOR_DIR', dirname( __FILE__ ) );
     34    define( 'RESIZE_EDITOR_DIR', dirname( __FILE__ ) );
    3235}
    3336if ( ! defined( 'RESIZE_EDITOR_STYLE' ) ) {
    34     define( 'RESIZE_EDITOR_STYLE', RESIZE_EDITOR_DIR . '/' . 'style.css' );
     37    define( 'RESIZE_EDITOR_STYLE', RESIZE_EDITOR_DIR . '/' . 'style.css' );
    3538}
    3639
    3740/*
    38  * validate parameter is number
    39  */
    40 function isPlusInteger( $str )
    41 {
    42     if ( '0' === $str ) {
    43         return false;
    44     }
    45     if ( 1 !== preg_match( '/^[0-9]+$/', $str ) ) {
    46         return false;
    47     }
    48     if ( 0 === intval( $str ) ) {
    49         return false;
    50     }
    51     return true;
    52 }
    53 
    54 /*
    55  * load text domain
     41 * Set up plugin
    5642 */
    5743add_action( 'init', 'resize_editor_init' );
    5844function resize_editor_init()
    5945{
    60     load_plugin_textdomain( 'resize-editor', false, 'resize-editor/languages' );
    61     if ( function_exists( 'register_uninstall_hook' ) ) {
    62         register_uninstall_hook( __FILE__, 'uninstall_resize_editor' );
    63     }
    64     if ( function_exists( 'register_deactivation_hook' ) ) {
    65         register_deactivation_hook(__FILE__, 'deactivation_resize_editor');
    66     }
     46    // load text domin
     47    load_plugin_textdomain( 'resize-editor', false, 'resize-editor/languages' );
     48    // register deactivation function
     49    if ( function_exists( 'register_deactivation_hook' ) ) {
     50        register_deactivation_hook( __FILE__, 'deactivation_resize_editor' );
     51    }
     52    // register uninstall function
     53    if ( function_exists( 'register_uninstall_hook' ) ) {
     54        register_uninstall_hook( __FILE__, 'uninstall_resize_editor' );
     55    }
    6756}
    68 
    6957
    7058/*
    7159 * Deactivate Resize Editor plugin
    7260 *
    73  * delete Resize Editor stylesheet(style.css)
    74  * delete option(postdivrichwidth) from wp_options
     61 * delete Resize Editor css(/style.css)
     62 * delete added option(postdivrichwidth) from wp_options
    7563 */
    76 function deactivation_resize_editor() {
    77     delete_option( 'postdivrichwidth' );
    78     if ( true === file_exists( RESIZE_EDITOR_STYLE ) ) {
    79         unlink( RESIZE_EDITOR_STYLE );
    80     }
     64function deactivation_resize_editor()
     65{
     66    // delete options that has been added by Resize Editor
     67    delete_option( 'postdivrichwidth' );
     68    // delete css that has been added by Resize Eitor
     69    if ( true === file_exists( RESIZE_EDITOR_STYLE ) ) {
     70        unlink( RESIZE_EDITOR_STYLE );
     71    }
    8172}
    8273
    83 
    8474/*
    85  *  Delete Resize Editor plugin
     75 *  Uninstall Resize Editor plugin
    8676 *
    87  * delete Resize Editor stylesheet(style.css)
    88  * delete option(postdivrichwidth) from wp_options
     77 * delete Resize Editor css(/style.css)
     78 * delete added option(postdivrichwidth) from wp_options
    8979 */
    9080function uninstall_resize_editor()
    9181{
    92     delete_option( 'postdivrichwidth' );
    93     if ( true === file_exists( RESIZE_EDITOR_STYLE ) ) {
    94         unlink( RESIZE_EDITOR_STYLE );
    95     }
     82    // delete options that has been added by Resize Editor
     83    delete_option( 'postdivrichwidth' );
     84    // delete css that has been added by Resize Eitor
     85    if ( true === file_exists( RESIZE_EDITOR_STYLE ) ) {
     86        unlink( RESIZE_EDITOR_STYLE );
     87    }
    9688}
    9789
    98 
    9990/*
    100  * add resize editor stylesheet(style.css)
     91 * Add resize editor css(style.css)
    10192 */
    10293add_action( 'admin_init', 'add_resize_editor_style' );
    103 function add_resize_editor_style() {
    104     wp_register_style( 'resize_editor', plugins_url( '', __FILE__) . '/style.css' );
    105     wp_enqueue_style( 'resize_editor' );
     94function add_resize_editor_style()
     95{
     96    wp_register_style( 'resize_editor', plugins_url( '', __FILE__ ) . '/style.css' );
     97    wp_enqueue_style( 'resize_editor' );
    10698}
    107 
    10899
    109100/*
     
    113104function resize_editor()
    114105{
    115     add_options_page( 'Resize Editor Options', 'Resize Editor', 'manage_options', 'resize-editor', 'resize_editor_options' );
     106    add_options_page(
     107        'Resize Editor Options',
     108        'Resize Editor',
     109        'manage_options',
     110        'resize-editor',
     111        'resize_editor_options'
     112    );
    116113}
    117 // display setting page
     114
     115/*
     116 * Display setting page
     117 */
    118118function resize_editor_options()
    119119{
    120     if ( ! current_user_can( 'manage_options' ) ) {
    121         wp_die( __( 'You do not have sufficient permissions to access this page' ) );
    122     }
    123     if ( isset( $_POST[ 'postdivrichwidth' ] ) ) {
    124         if ( isPlusInteger( $_POST[ 'postdivrichwidth'] ) ) {
    125             $width =  $_POST[ 'postdivrichwidth' ];
    126             // update options
    127             check_admin_referer( 'update-options' );
    128             update_option( 'postdivrichwidth', $width );
    129             // create style.css
    130             $filename = 'style.css';
    131             $css = <<<CSS
    132 @charset "utf-8";
    133 #postdivrich {
    134   width: {$width}px;
    135   overflow: hidden;
    136 }
    137 CSS;
    138             $fp = fopen( RESIZE_EDITOR_STYLE, 'w' ) or die( __( 'Can not open file' ) );
    139             flock( $fp, LOCK_EX );
    140             fputs( $fp, $css );
    141             flock( $fp, LOCK_UN );
    142             fclose( $fp );
    143         } else {
     120    if ( ! current_user_can( 'manage_options' ) ) {
     121        wp_die( __( 'You do not have sufficient permissions to access this page' ) );
     122    }
     123    if ( isset( $_POST['_wpnonce'] ) && $_POST['_wpnonce'] ) {
     124        if ( check_admin_referer( 'safe-resize', '_wpnonce' ) ) {
     125            if ( isset( $_POST['postdivrichwidth'] ) && absint( $_POST['postdivrichwidth'] ) ) {
     126                $width = absint( $_POST['postdivrichwidth'] );
     127                // update option
     128                update_option( 'postdivrichwidth', $width );
     129                // create style.css
     130                $css = '@charset "utf-8";' . PHP_EOL
     131                    . '#postdivrich { width:' . $width . 'px; overflow: hidden; }';
     132                $fp = fopen( RESIZE_EDITOR_STYLE, 'w' ) or die( __( 'Can not open file' ) );
     133                flock( $fp, LOCK_EX );
     134                fputs( $fp, $css );
     135                flock( $fp, LOCK_UN );
     136                fclose( $fp );
     137            } else {
     138                delete_option( 'postdivrichwidth' );
     139                if ( true === file_exists( RESIZE_EDITOR_STYLE ) ) {
     140                    unlink( RESIZE_EDITOR_STYLE );
     141                }
     142            }
     143        }
     144    }
    144145
    145             delete_option( 'postdivrichwidth' );
    146             if ( true === file_exists( RESIZE_EDITOR_STYLE ) ) {
    147                 unlink( RESIZE_EDITOR_STYLE );
    148 
    149             }
    150         }
    151     }
    152 
    153     // options setting form
    154     echo '<div class="wrap">' . PHP_EOL;
    155     echo '<h2>Resize Editor</h2>' . PHP_EOL;
    156     echo '<form method="post" action="">' . PHP_EOL;
    157     echo wp_nonce_field( 'update-options' ) . PHP_EOL;
    158     echo '<table class="form-table">' . PHP_EOL;
    159     echo '<tr><th>Rich Editor Width</th><td><input type="text" name="postdivrichwidth" '
    160         . 'value="' . esc_html( get_option( 'postdivrichwidth' ) ) . '" size="6"/>&nbsp;px<br>'
    161         . __('Empty or 0 reset existing value', 'resize-editor' ) . '</td></tr>' . PHP_EOL;
    162     echo '</table>' . PHP_EOL;
    163     echo '<p class="submit"><input type="submit" class="button-primary" value="' . __('Save changes', 'resize-editor') . '" /></p>' . PHP_EOL;
    164     echo '</form>' . PHP_EOL;
    165     echo '</div>';
     146    // options setting form
     147    echo '<div class="wrap">' . PHP_EOL;
     148    echo '<h2>Resize Editor</h2>' . PHP_EOL;
     149    echo '<form method="post" action="">' . PHP_EOL;
     150    echo wp_nonce_field( 'safe-resize', '_wpnonce' ) . PHP_EOL;
     151    echo '<table class="form-table">' . PHP_EOL;
     152    echo '<tr><th>Rich Editor Width</th><td><input type="text" name="postdivrichwidth" '
     153        . 'value="' . esc_html( get_option( 'postdivrichwidth' ) ) . '" size="6"/>&nbsp;px<br>'
     154        . __( 'Empty or 0 reset existing value', 'resize-editor' ) . '</td></tr>' . PHP_EOL;
     155    echo '</table>' . PHP_EOL;
     156    echo '<p class="submit"><input type="submit" class="button-primary" value="' . __(
     157            'Save changes',
     158            'resize-editor'
     159        ) . '" /></p>' . PHP_EOL;
     160    echo '</form>' . PHP_EOL;
     161    echo '</div>';
    166162}
    167163
Note: See TracChangeset for help on using the changeset viewer.