Last active
January 24, 2017 18:29
-
-
Save franz-josef-kaiser/1853928 to your computer and use it in GitHub Desktop.
Altering & handling default and custom wordpress image sizes and related stuff
This file contains 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 | |
defined( 'ABSPATH' ) OR exit; | |
add_action( 'init', array( 'oxoImageSizesExtd', 'init' ), 0 ); | |
class oxoImageSizesExtd extends oxoImageSizes | |
{ | |
/** | |
* The Class Object | |
* @var | |
*/ | |
static private $class = null; | |
/** | |
* The array of custom size definitions. | |
* If overriding of defaults is needed, | |
* then 'large', 'medium', 'thumbnail' should be added here. | |
* If custom sizes are needed, then each sizes name. | |
* @var array | |
*/ | |
protected $sizes = null; | |
/** | |
* Handler for the action 'init'. Instantiates this class. | |
* | |
* @return void | |
*/ | |
public static function init() | |
{ | |
if ( null === self::$class ) | |
self :: $class = new self; | |
return self :: $class; | |
} | |
public function __construct() | |
{ | |
$this->sizes = array( | |
'large' => array( | |
'h' => 960 | |
,'w' => 640 | |
,'crop' => false | |
,'attr' => array( | |
) | |
) | |
,'medium' => array( | |
'h' => 114 | |
,'w' => 114 | |
,'crop' => false | |
,'attr' => array( | |
) | |
) | |
,'thumbnail' => array( | |
'h' => 57 | |
,'w' => 57 | |
,'crop' => true | |
,'attr' => array( | |
) | |
) | |
); | |
parent::__construct(); | |
} | |
} // END Class oxoImageSizesExtd |
This file contains 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 | |
defined( 'ABSPATH' ) OR exit; | |
class oxoImageSizes | |
{ | |
/** | |
* Whether post thumbnails should be used or not | |
* @var boolean | |
*/ | |
protected $post_thumbnail = true; | |
/** | |
* The default sizes. | |
* Can be overridden in the extending class | |
* by defining the $sizes array | |
* @var array | |
*/ | |
private $default_sizes = array( | |
'large' => array( | |
'h' => 740 | |
,'w' => 500 | |
,'crop' => false | |
) | |
,'medium' => array( | |
'h' => 320 | |
,'w' => 250 | |
,'crop' => false | |
) | |
,'thumbnail' => array( | |
'h' => 74 | |
,'w' => 50 | |
,'crop' => true | |
) | |
); | |
/** | |
* Construct | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// Merge extd class sizes with default sizes | |
$this->sizes = wp_parse_args( $this->sizes, $this->default_sizes ); | |
add_action( 'init', array( $this, 'image_sizes_init' ) ); | |
add_action( 'init', array( $this, 'image_sizes' ) ); | |
// Image attr filter | |
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'img_attr' ), 10, 2 ); | |
} | |
public function image_sizes_init() | |
{ | |
if ( | |
$this->post_thumbnail | |
AND isset( $this->sizes['post-thumbnail'] ) | |
) | |
{ | |
add_theme_support( 'post-thumbnails' ); | |
extract( $this->sizes['post-thumbnail'], EXTR_SKIP ); | |
set_post_thumbnail_size( $w, $h, $crop ); | |
} | |
} | |
/** | |
* Rework built in image sizes | |
* | |
* @since 0.3 | |
* | |
* @return void | |
*/ | |
public function image_sizes() | |
{ | |
foreach ( array_keys( $this->sizes ) as $size ) | |
{ | |
$filter_array[] = "{$size}_size_w"; | |
$filter_array[] = "{$size}_size_h"; | |
$filter_array[] = "{$size}_crop"; | |
} | |
foreach ( $filter_array as $hook ) | |
add_filter( "pre_option_$hook", array( $this, 'image_filter_cb' ), 10, 1 ); | |
} | |
/** | |
* Callback for image filter return values | |
* | |
* @since 0.3 | |
* @param mixed $val | |
* | |
* @return mixed $value | |
*/ | |
public function image_filter_cb( $val ) | |
{ | |
$filter = current_filter(); | |
$size = str_replace( | |
array( 'pre_option_', '_size', '_crop', '_h', '_w' ) | |
,'' | |
,$filter | |
); | |
switch ( $filter ) | |
{ | |
case "pre_option_{$size}_size_w" : | |
$val = $this->sizes[ $size ]['h']; | |
break; | |
case "pre_option_{$size}_size_h" : | |
$val = $this->sizes[ $size ]['h']; | |
break; | |
case "pre_option_{$size}_crop" : | |
$val = $this->sizes[ $size ]['crop']; | |
break; | |
} | |
return $val; | |
} | |
/** | |
* Alter attributes for images | |
* filters the output of `wp_get_attachment_image()` | |
* | |
* @param array $attr | |
* Gets merged with Defaults | |
* @see /wp-includes/media.php wp_get_attachment_image() | |
'src' => $path, | |
'class' => " class", | |
'alt' => trim( strip_tags( # ) ) | |
'title' => trim( strip_tags( # ) ) | |
* @param unknown_type $attachment | |
* | |
* @return string $html | |
*/ | |
public function img_attr( $attr, $attachment ) | |
{ | |
// current size - only works as long as core doesn't change the default class | |
$size = trim( str_replace( 'attachment-', '', $attr['class'] ) ); | |
if ( | |
! isset( $this->sizes[ $size ]['attr'] ) | |
OR empty( $this->sizes[ $size ]['attr'] ) | |
) | |
return $attr; | |
$curr_attr = $this->sizes[ $size ]['attr']; | |
# >>>> Prepare attributes | |
// Get rid of empty attributes | |
$curr_attr = array_filter( $curr_attr ); | |
// trim, strip tags & esc_html - mapped as custom attributes could get added | |
foreach ( array( 'strip_tags', 'trim', 'esc_html' ) as $cb ) | |
$curr_attr = array_map( $cb, $curr_attr ); | |
// Prevent missing empty spaces for added classes | |
$curr_attr['class'] = " {$curr_attr['class']}"; | |
// esc_attr() gets mapped inside wp_get_attachment_image() | |
# <<<< | |
return wp_parse_args( $curr_attr, $attr ); | |
} | |
} // END Class oxoImageSizes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment