Created
January 29, 2017 12:12
-
-
Save sergeliatko/04aec543278b0a8c17caf542091f3761 to your computer and use it in GitHub Desktop.
Adds image size to WordPress with setting to control the dimensions in media.php screen and size name in WP user interface.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds image size to WordPress with setting to control the dimensions in media.php screen and size name in WP user interface. | |
* example: | |
* | |
* if( !defined('PIS_TXD') ) { | |
* define( 'PIS_TXD', 'your-text-domain' ); | |
* } | |
* require_once( dirname( __FILE__ ) . '/PostImageSize.php' ); | |
* $teaser_size = new PostImageSize( 'teaser', __( 'Teaser', 'your-text-domain' ), 460, 280, true ); | |
* | |
* | |
* @file PostImageSize.php | |
* @copyright (c) 2017 Serge Liatko <[email protected]> https://sergeliatko.com | |
*/ | |
/** load class only if not previously loaded */ | |
if( !class_exists('PostImageSize') ) { | |
/** | |
* Class PostImageSize | |
*/ | |
class PostImageSize { | |
/** @var string $name image size name */ | |
public $name; | |
/** @var string $title image size title */ | |
public $title; | |
/** @var int $width image width */ | |
public $width; | |
/** @var int $height image height */ | |
public $height; | |
/** @var bool $crop image hard crop */ | |
public $crop; | |
/** | |
* PostImageSize constructor. | |
* | |
* @param string $name | |
* @param string $title | |
* @param int $width | |
* @param int $height | |
* @param bool $crop | |
*/ | |
public function __construct( $name = 'thumbnail', $title, $width = 125, $height = 125, $crop = false ) { | |
/** define text domain */ | |
if( !defined('PIS_TXD') ) { | |
define( 'PIS_TXD', 'post-image-size' ); | |
} | |
$title = empty( $title ) ? $name : $title; | |
$this->setName( $name ); | |
$this->setTitle( $title ); | |
$this->setWidth( $width ); | |
$this->setHeight( $height ); | |
$this->setCrop( $crop ); | |
/** add image size to WP */ | |
add_action( 'after_setup_theme', array( $this, 'add_image_size' ), 10, 0 ); | |
/** add image size name to drop-down of names */ | |
add_filter( 'image_size_names_choose', array( $this, 'image_size_names_choose' ), 10, 1 ); | |
/** add size user interface */ | |
add_action( 'admin_init', array( $this, 'add_setting_field' ), 10, 0 ); | |
/** register setting */ | |
add_action( 'admin_init', array( $this, 'register_setting' ), 10, 0 ); | |
} | |
/** | |
* Displays setting field | |
*/ | |
public function setting_field() { | |
printf( | |
'<legend class="screen-reader-text"><span>%1$s</span></legend>%2$s %3$s<br />%4$s', | |
sprintf( __( '%1$s size', PIS_TXD ), $this->getTitle() ), | |
$this->get_width_field(), | |
$this->get_height_field(), | |
$this->get_crop_field() | |
); | |
} | |
/** | |
* Returns setting field width HTML. | |
* | |
* @return string | |
*/ | |
protected function get_width_field() { | |
return sprintf( | |
'<label for="%2$s">%1$s</label> <input id="%2$s" name="%3$s" type="number" value="%4$s" class="small-text" min="0" step="1" />', | |
( $this->isCrop() ? __( 'Width', PIS_TXD ) : __( 'Max Width', PIS_TXD ) ), | |
sprintf( '%1$s-size-w', $this->getName() ), | |
sprintf( '%1$s_size_w', $this->getName() ), | |
esc_attr( $this->getWidth() ) | |
); | |
} | |
/** | |
* Returns setting field height HTML. | |
* | |
* @return string | |
*/ | |
protected function get_height_field() { | |
return sprintf( | |
'<label for="%2$s">%1$s</label> <input id="%2$s" name="%3$s" type="number" value="%4$s" class="small-text" min="0" step="1" />', | |
( $this->isCrop() ? __( 'Height', PIS_TXD ) : __( 'Max Height', PIS_TXD ) ), | |
sprintf( '%1$s-size-h', $this->getName() ), | |
sprintf( '%1$s_size_h', $this->getName() ), | |
esc_attr( $this->getHeight() ) | |
); | |
} | |
/** | |
* Returns setting field crop HTML. | |
* | |
* @return string | |
*/ | |
protected function get_crop_field() { | |
return sprintf( | |
'<input id="%2$s" name="%3$s" type="checkbox" value="1" %4$s/> <label for="%2$s">%1$s</label>', | |
__( 'Crop image to exact dimensions (leave empty to keep images proportional)', PIS_TXD ), | |
sprintf( '%1$s-crop', $this->getName() ), | |
sprintf( '%1$s_crop', $this->getName() ), | |
checked( true, $this->isCrop(), false ) | |
); | |
} | |
/** | |
* Registers setting field in media options. | |
*/ | |
public function add_setting_field() { | |
add_settings_field( | |
$this->getName() . '_size', | |
sprintf( __( '%1$s size', PIS_TXD ), $this->getTitle() ), | |
array( $this, 'setting_field' ), | |
'media', | |
'default' | |
); | |
} | |
/** | |
* Registers setting for saving. | |
*/ | |
public function register_setting() { | |
register_setting( 'media', $this->getName() . '_size_w', 'absint' ); | |
register_setting( 'media', $this->getName() . '_size_h', 'absint' ); | |
register_setting( 'media', $this->getName() . '_crop', 'absint' ); | |
} | |
/** | |
* Adds image size to WP. | |
*/ | |
public function add_image_size() { | |
add_image_size( $this->getName(), $this->getWidth(), $this->getHeight(), $this->isCrop() ); | |
} | |
/** | |
* Adds image size name to drop-down of available image sizes. | |
* | |
* @param array $sizes | |
* | |
* @return array | |
*/ | |
public function image_size_names_choose( $sizes = array() ) { | |
return array_merge( $sizes, array( $this->getName() => $this->getTitle() ) ); | |
} | |
/** | |
* Returns option from database or default if the option is empty. | |
* | |
* @param string $option | |
* @param mixed $default | |
* | |
* @return mixed | |
*/ | |
protected function get_not_empty_option( $option, $default ) { | |
return $this->is_empty( $value = get_option( $option, $default ) ) ? $default : $value; | |
} | |
/** | |
* Check if data is empty. | |
* | |
* @param mixed $data | |
* | |
* @return bool | |
*/ | |
protected function is_empty( $data ) { | |
return empty( $data ); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() { | |
return $this->name; | |
} | |
/** | |
* @param string $name | |
*/ | |
public function setName( $name ) { | |
$this->name = $name; | |
} | |
/** | |
* @return string | |
*/ | |
public function getTitle() { | |
return $this->title; | |
} | |
/** | |
* @param string $title | |
*/ | |
public function setTitle( $title ) { | |
$this->title = $title; | |
} | |
/** | |
* @return int | |
*/ | |
public function getWidth() { | |
return absint( $this->get_not_empty_option( $this->getName() . '_size_w', $this->width ) ); | |
} | |
/** | |
* @param int $width | |
*/ | |
public function setWidth( $width ) { | |
$this->width = $width; | |
} | |
/** | |
* @return int | |
*/ | |
public function getHeight() { | |
return absint( $this->get_not_empty_option( $this->getName() . '_size_h', $this->height ) ); | |
} | |
/** | |
* @param int $height | |
*/ | |
public function setHeight( $height ) { | |
$this->height = $height; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isCrop() { | |
return !$this->is_empty( get_option( $this->getName() . '_crop', $this->crop ) ); | |
} | |
/** | |
* @param bool $crop | |
*/ | |
public function setCrop( $crop ) { | |
$this->crop = $crop; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment