0.3; refactor

This commit is contained in:
Peter Molnar 2016-06-28 17:44:33 +00:00
parent 2613db9ee7
commit e3ef6d9829
2 changed files with 198 additions and 205 deletions

View file

@ -4,10 +4,9 @@ Donate link: https://paypal.me/petermolnar/3
Tags: image, cache, image quality, Tags: image, cache, image quality,
Requires at least: 3.0 Requires at least: 3.0
Tested up to: 4.4.2 Tested up to: 4.4.2
Stable tag: 0.2.1 Stable tag: 0.3
License: GPLv3 License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html License URI: http://www.gnu.org/licenses/gpl-3.0.html
Required minimum PHP version: 5.3
Sharpen, enchance and move resized images to cache folder Sharpen, enchance and move resized images to cache folder
@ -40,6 +39,12 @@ Version numbering logic:
* every .B version indicates new features. * every .B version indicates new features.
* every ..C indicates bugfixes for A.B version. * every ..C indicates bugfixes for A.B version.
= 0.3 =
*2016-06-28*
* code refactor
= 0.2.1 = = 0.2.1 =
*2016-03-08* *2016-03-08*

View file

@ -3,11 +3,10 @@
Plugin Name: wp-resized2cache Plugin Name: wp-resized2cache
Plugin URI: https://github.com/petermolnar/wp-resized2cache Plugin URI: https://github.com/petermolnar/wp-resized2cache
Description: Sharpen, enchance and move resized images to cache folder Description: Sharpen, enchance and move resized images to cache folder
Version: 0.2.1 Version: 0.3
Author: Peter Molnar <hello@petermolnar.eu> Author: Peter Molnar <hello@petermolnar.eu>
Author URI: http://petermolnar.eu/ Author URI: http://petermolnar.eu/
License: GPLv3 License: GPLv3
Required minimum PHP version: 5.3
*/ */
/* Copyright 2015 Peter Molnar ( hello@petermolnar.eu ) /* Copyright 2015 Peter Molnar ( hello@petermolnar.eu )
@ -26,39 +25,33 @@ Required minimum PHP version: 5.3
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
if (!class_exists('WP_RESIZED2CACHE')): namespace WP_RESIZED2CACHE;
class WP_RESIZED2CACHE { define ( 'cachedir', \WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'cache' );
\register_activation_hook( __FILE__ , 'WP_RESIZED2CACHE\plugin_activate' );
\add_action( 'init', 'WP_RESIZED2CACHE\init' );
\add_action( 'delete_attachment', 'WP_RESIZED2CACHE\delete_from_cache' );
const cachedir = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'cache'; function init () {
if ( ! is_dir( cachedir ) ) {
public function __construct () { if ( ! mkdir( cachedir ) ) {
register_activation_hook( __FILE__ , array( &$this, 'plugin_activate' ) ); debug('failed to create ' . cachedir, 4);
if (!is_dir(static::cachedir)) {
if (!mkdir(static::cachedir)) {
static::debug('failed to create ' . static::cachedir, 4);
} }
} }
add_action( 'init', array( &$this, 'init'));
add_action( 'delete_attachment', array (&$this, 'delete_from_cache'));
}
public function init () {
// set higher jpg quality // set higher jpg quality
add_filter( 'jpeg_quality', array( &$this, 'jpeg_quality' ) ); \add_filter( 'jpeg_quality', 'WP_RESIZED2CACHE\jpeg_quality' );
add_filter( 'wp_editor_set_quality', array( &$this, 'jpeg_quality' ) ); \add_filter( 'wp_editor_set_quality', 'WP_RESIZED2CACHE\jpeg_quality' );
// sharpen resized images on upload // sharpen resized images on upload
add_filter( 'image_make_intermediate_size',array ( &$this, 'sharpen' ),10); \add_filter( 'image_make_intermediate_size', 'WP_RESIZED2CACHE\sharpen', 10 );
} }
/** /**
* activate hook * activate hook
*/ */
public static function plugin_activate() { function plugin_activate() {
if ( version_compare( phpversion(), 5.3, '<' ) ) { if ( version_compare( phpversion(), 5.3, '<' ) ) {
die( 'The minimum PHP version required for this plugin is 5.3' ); die( 'The minimum PHP version required for this plugin is 5.3' );
} }
@ -69,21 +62,21 @@ class WP_RESIZED2CACHE {
* called on attachment deletion and takes care of removing the moved files * called on attachment deletion and takes care of removing the moved files
* *
*/ */
public static function delete_from_cache ( $aid = null ) { function delete_from_cache ( $aid = null ) {
static::debug( "DELETE is called and aid is: " . $aid, 5 ); debug( "DELETE is called and aid is: {$aid}", 5 );
if ($aid === null) if ($aid === null)
return false; return false;
$attachment = get_post( $aid ); $attachment = \get_post( $aid );
if ( static::is_post($attachment)) { if ( is_post($attachment)) {
$meta = wp_get_attachment_metadata($aid); $meta = \wp_get_attachment_metadata($aid);
if ( isset( $meta['sizes'] ) && ! empty( $meta['sizes'] ) ) { if ( isset( $meta['sizes'] ) && ! empty( $meta['sizes'] ) ) {
foreach ( $meta['sizes'] as $size => $data ) { foreach ( $meta['sizes'] as $size => $data ) {
$file = static::cachedir . DIRECTORY_SEPARATOR . $data['file']; $file = cachedir . DIRECTORY_SEPARATOR . $data['file'];
if ( isset( $data['file'] ) && is_file( $file ) ) { if ( isset( $data['file'] ) && is_file( $file ) ) {
static::debug( " removing " . $file, 5 ); debug( " removing {$file}", 5 );
unlink ( $file ); unlink ( $file );
} }
} }
@ -96,7 +89,7 @@ class WP_RESIZED2CACHE {
/** /**
* better jpgs * better jpgs
*/ */
public static function jpeg_quality () { function jpeg_quality () {
$jpeg_quality = (int)92; $jpeg_quality = (int)92;
return $jpeg_quality; return $jpeg_quality;
} }
@ -104,10 +97,10 @@ class WP_RESIZED2CACHE {
/** /**
* adaptive sharpen images w imagemagick * adaptive sharpen images w imagemagick
*/ */
static public function sharpen( $resized ) { function sharpen( $resized ) {
if (!class_exists('Imagick')) { if ( ! class_exists( '\Imagick' ) ) {
static::debug('Please install Imagick extension; otherwise this plugin will not work as well as it should.', 4); debug('Please install Imagick extension; otherwise this plugin will not work as well as it should.', 4);
} }
/* /*
@ -123,45 +116,45 @@ class WP_RESIZED2CACHE {
$size = @getimagesize($resized); $size = @getimagesize($resized);
if ( !$size ) { if ( !$size ) {
static::debug("Unable to get size for: {$resized}", 4); debug( "Unable to get size for: {$resized}", 4);
return $resized; return $resized;
} }
//$cachedir = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'cache'; //$cachedir = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'cache';
$fname = basename( $resized ); $fname = basename( $resized );
$cached = static::cachedir . DIRECTORY_SEPARATOR . $fname; $cached = cachedir . DIRECTORY_SEPARATOR . $fname;
if ( $size[2] == IMAGETYPE_JPEG && class_exists('Imagick')) { if ( $size[2] == IMAGETYPE_JPEG && class_exists('\Imagick')) {
static::debug( "adaptive sharpen " . $resized, 6 ); debug( "adaptive sharpen {$resized}", 6 );
try { try {
$imagick = new Imagick($resized); $imagick = new \Imagick( $resized );
$imagick->unsharpMaskImage( 0, 0.5, 1, 0 ); $imagick->unsharpMaskImage( 0, 0.5, 1, 0 );
$imagick->setImageFormat( "jpg" ); $imagick->setImageFormat( "jpg" );
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG); $imagick->setImageCompression( \Imagick::COMPRESSION_JPEG );
$imagick->setImageCompressionQuality(static::jpeg_quality()); $imagick->setImageCompressionQuality( jpeg_quality() );
$imagick->setInterlaceScheme(Imagick::INTERLACE_PLANE); $imagick->setInterlaceScheme( \Imagick::INTERLACE_PLANE );
$imagick = apply_filters( "wp_resized2cache_imagick", $imagick, $resized ); $imagick = \apply_filters( "wp_resized2cache_imagick", $imagick, $resized );
$imagick->writeImage($cached); $imagick->writeImage($cached);
$imagick->destroy(); $imagick->destroy();
} }
catch (Exception $e) { catch (Exception $e) {
static::debug( 'something went wrong with imagemagick: ', $e->getMessage(), 4 ); debug( 'something went wrong with imagemagick: ', $e->getMessage(), 4 );
return $resized; return $resized;
} }
static::debug( "removing " . $resized, 5 ); debug( "removing " . $resized, 5 );
unlink ($resized); unlink ($resized);
} }
else { else {
static::debug( "moving " . $cached, 5 ); debug( "moving {$cached}", 5 );
if ( copy( $resized, $cached ) ) { if ( copy( $resized, $cached ) ) {
static::debug( "removing " . $resized, 5 ); debug( "removing {$resized}", 5 );
unlink( $resized ); unlink( $resized );
} }
else { else {
static::debug( "\tmove failed, passing on this", 4 ); debug( "\tmove failed, passing on this", 4 );
} }
} }
@ -179,7 +172,7 @@ class WP_RESIZED2CACHE {
* @output log to syslog | wp_die on high level * @output log to syslog | wp_die on high level
* @return false on not taking action, true on log sent * @return false on not taking action, true on log sent
*/ */
public static function debug( $message, $level = LOG_NOTICE ) { function debug( $message, $level = LOG_NOTICE ) {
if ( empty( $message ) ) if ( empty( $message ) )
return false; return false;
@ -202,8 +195,8 @@ class WP_RESIZED2CACHE {
$level_ = $levels [ $level ]; $level_ = $levels [ $level ];
// in case WordPress debug log has a minimum level // in case WordPress debug log has a minimum level
if ( defined ( 'WP_DEBUG_LEVEL' ) ) { if ( defined ( '\WP_DEBUG_LEVEL' ) ) {
$wp_level = $levels [ WP_DEBUG_LEVEL ]; $wp_level = $levels [ \WP_DEBUG_LEVEL ];
if ( $level_ > $wp_level ) { if ( $level_ > $wp_level ) {
return false; return false;
} }
@ -211,7 +204,7 @@ class WP_RESIZED2CACHE {
// ERR, CRIT, ALERT and EMERG // ERR, CRIT, ALERT and EMERG
if ( 3 >= $level_ ) { if ( 3 >= $level_ ) {
wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' ); \wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' );
exit; exit;
} }
@ -228,14 +221,9 @@ class WP_RESIZED2CACHE {
/** /**
* *
*/ */
public static function is_post ( &$post ) { function is_post ( &$post ) {
if ( !empty($post) && is_object($post) && isset($post->ID) && !empty($post->ID) ) if ( !empty($post) && is_object($post) && isset($post->ID) && !empty($post->ID) )
return true; return true;
return false; return false;
} }
}
$WP_RESIZED2CACHE = new WP_RESIZED2CACHE();
endif;