Skip to content

Instantly share code, notes, and snippets.

@gmazzap
Last active December 21, 2015 11:49
Show Gist options
  • Save gmazzap/6302022 to your computer and use it in GitHub Desktop.
Save gmazzap/6302022 to your computer and use it in GitHub Desktop.
WordPress plugin that gives to users of WP 3.6 chance to change wysiwyg editor height by setting options in the user profile page. Two options: one for desktops, one for mobile.
<?php
/**
* Plugin Name: GM Custom Editor Height
*
* Description: Gives to users of WP 3.6 chance to change wysiwyg editor height by setting options in the user profile page. Two options: one for desktops, one for mobile
* Version: 0.1.0
* Author: Giuseppe Mazzapica
* Requires at least: 3.6
* Tested up to: 3.6
*
*
* @package GMCustomEditorHeight
* @author Giuseppe Mazzapica
*
*/
/**
* GMCustomEditorHeight class
*
* @package GMCustomEditorHeight
* @author Giuseppe Mazzapica
*
*/
class GMCustomEditorHeight {
/**
* Plugin version
*
* @since 0.1.0
*
* @access protected
*
* @var string
*/
protected static $version = '0.1.0';
/**
* If the class was inited or not
*
* @since 0.1.0
*
* @access protected
*
* @var bool|null
*/
private static $inited = NULL;
/**
* The current logged in user
*
* @since 0.1.0
*
* @access private
*
* @var object|null
*/
private static $user = NULL;
/**
* Inizializize the class
*
* @since 0.1.0
*
* @access public
* @return null
*
*/
public static function init() {
if( self::$inited == NULL ) {
self::$inited = 1;
self::actions();
}
}
/**
* Add the hoock for plugin class methods
*
* @since 0.1.0
*
* @access private
* @return null
*
*/
private static function actions() {
add_action( 'admin_init', array( __CLASS__ , 'who') );
add_action( 'admin_init', array( __CLASS__ , 'set_filter') );
add_action( 'show_user_profile', array( __CLASS__ , 'fields') );
add_action( 'edit_user_profile', array( __CLASS__ , 'fields') );
add_action( 'personal_options_update', array( __CLASS__ , 'save_fields') );
add_action( 'edit_user_profile_update', array( __CLASS__ , 'save_fields') );
}
/**
* Set the filter for the_editor hook if in the post creation/editing page. Fired by admin_init action
*
* @since 0.1.0
*
* @access public
* @return null
*
*/
public static function set_filter() {
global $pagenow;
if ( $pagenow == 'post-new.php' || $pagenow == 'post.php' )
add_filter('the_editor', array( __CLASS__, 'change_height' ), 20, 1);
}
/**
* Store the current user in a static variable. Fired by admin_init action
*
* @since 0.1.0
*
* @access public
* @return null
*
*/
public static function who() {
self::$user = wp_get_current_user();
}
/**
* Change the height of the editor according to settings and wp_is_mobile. Fired by the_editor filter
*
* @since 0.1.0
*
* @access public
* @param string $html the html for the editor container as passed by the_editor filter
* @return string $html the html for the editor container
*
*/
public static function change_height( $html ) {
if ( is_null(self::$user) ) return $html;
if ( ! substr_count($html, 'id="wp-content') ) return $html;
$key = wp_is_mobile() ? 'custom_editor_height_mobile' : 'custom_editor_height';
$height = get_user_meta( self::$user->ID, $key, true ) ? : '360px';
$height = apply_filters('gm_custom_editor_height', $height, $key, self::$user);
if ( ! self::check_height($height) ) $height = '360px';
return preg_replace('/height: [0-9]+px/', 'height: ' . $height, $html, 1);
}
/**
* Print the html for the profile fields. Used by fields method
*
* @since 0.1.0
*
* @access private
* @param bool $for_mobile if true print the field for mobile devices otpion
* @return null
*
*/
private static function fields_tr( $for_mobile = false ) {
if ( is_null(self::$user) ) return;
$value_f = $for_mobile ? 'custom_editor_height_value_mobile' : 'custom_editor_height_value';
$value_u = $for_mobile ? 'custom_editor_height_unit_mobile' : 'custom_editor_height_unit';
$key = $for_mobile ? 'custom_editor_height_mobile' : 'custom_editor_height';
$now = get_user_meta( self::$user->ID, $key, true ) ? : "360px"; // 360 is default height
$height = substr($now, 0, -2);
$unit = substr($now, (strlen($now)-2 ));
$label = $for_mobile ? __('Editor Height for Mobile') : __('Editor Height');
printf( '<tr><th><label for="%s">%s</label></th>', $value_f, $label );
printf( '<td><input type="text" name="%s" id="%s" value="%d" class="small-text" />', $value_f, $value_f, $height );
printf( '<select name="%s" id="%s">', $value_u, $value_u);
foreach ( array('px','em') as $u ) echo '<option value="' . $u . '"' . selected($u, $unit, false) . '>' . $u . '</option>';
echo '</select> <span class="description">' . __('Max 5000px or 70em') . '</span><td></tr>';
}
/**
* Print the html for the profile fields. Use by fields_tr method. Fired by show_user_profile and edit_user_profile actions
*
* @since 0.1.0
*
* @access public
* @return null
*
*/
public static function fields() {
if ( is_null(self::$user) ) return;
echo '<table class="form-table">';
self::fields_tr();
self::fields_tr(true);
echo '</table>';
}
/**
* Save the fields to user metadata. Fired by personal_options_update and edit_user_profile_update actions
*
* @since 0.1.0
*
* @access public
* @param int $user_id current user id
* @return null
*
*/
public static function save_fields( $user_id ) {
if (
isset($_POST['custom_editor_height_value']) && isset($_POST['custom_editor_height_unit']) &&
self::check_height('', $_POST['custom_editor_height_value'], $_POST['custom_editor_height_unit'] ) )
{
$height = intval($_POST['custom_editor_height_value']) . $_POST['custom_editor_height_unit'];
update_user_meta($user_id, 'custom_editor_height', $height);
}
if (
isset($_POST['custom_editor_height_value_mobile']) && isset($_POST['custom_editor_height_unit_mobile']) &&
self::check_height('', $_POST['custom_editor_height_value_mobile'], $_POST['custom_editor_height_unit_mobile'] ) )
{
$height = intval($_POST['custom_editor_height_value_mobile']) . $_POST['custom_editor_height_unit_mobile'];
update_user_meta($user_id, 'custom_editor_height_mobile', $height);
}
}
/**
* Check the value to saved for user mta is well formatted.
*
* @since 0.1.0
*
* @access public
* @param string $full the full meta value like '360px'
* @param int|string $value the numeric part of meta value like '360'
* @param string $unit the unit part of meta value like 'px'
* @return null
*
*/
private static function check_height( $full, $value = null, $unit= null ) {
if ( ! empty($full) || ( ! empty($value) && ! empty($value)) ) {
if ( ! empty($full) ) {
$value = substr($full, 0, -2);
$unit = substr($full, (strlen($full)-2 ));
}
return ( intval($value) && in_array( $unit, array('px','em') ) );
}
return false;
}
}
/**
* Inizialize plugin
*
* @since 0.1.0
*
* @access public
* @return null
*
*/
function initGMCustomEditorHeight() {
GMCustomEditorHeight::init();
}
add_action('init', 'initGMCustomEditorHeight');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment