????
| Current Path : /home2/morganrand/www/wp-content/themes/wanderfuls/framework/classes/ |
| Current File : /home2/morganrand/www/wp-content/themes/wanderfuls/framework/classes/image-sizes.php |
<?php
/**
* Adds image sizes for use with the theme
*
* @package Wanderfuls WordPress Theme
* @subpackage Framework
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Start Class
if ( ! class_exists( 'TB_Image_Sizes' ) ) {
class TB_Image_Sizes {
private $sizes;
/**
* Main constructor
*
* @since Wanderfuls 1.6.3
*/
public function __construct() {
// Array of image sizes
$this->sizes = array();
// Define and add image sizes => Needs low priority for Visual Composer
add_filter( 'init', array( $this, 'define_sizes' ), 0 );
add_filter( 'init', array( $this, 'add_sizes' ), 1 );
// Prevent images from cropping when on the fly is enabled
add_filter( 'intermediate_image_sizes_advanced', array( $this, 'do_not_crop_on_upload' ) );
// Create admin panel
add_action( 'admin_menu', array( $this, 'add_admin_page' ), 10 );
add_action( 'admin_init', array( $this,'register_settings' ) );
}
/**
* Define array of image sizes used by the theme
*
* @return array
* @since 2.0.0
*/
public function define_sizes() {
// Update sizes array
$this->sizes = array(
'lightbox' => array(
'label' => __( 'Lightbox Images', 'tb' ),
'width' => 'lightbox_image_width',
'height' => 'lightbox_image_height',
'crop' => 'lightbox_image_crop',
),
'blog_entry' => array(
'label' => __( 'Blog Entry', 'tb' ),
'width' => 'blog_entry_image_width',
'height' => 'blog_entry_image_height',
'crop' => 'blog_entry_image_crop',
),
'blog_post' => array(
'label' => __( 'Blog Post', 'tb' ),
'width' => 'blog_post_image_width',
'height' => 'blog_post_image_height',
'crop' => 'blog_post_image_crop',
),
'blog_post_full' => array(
'label' => __( 'Blog Post: Full-Width', 'tb' ),
'width' => 'blog_post_full_image_width',
'height' => 'blog_post_full_image_height',
'crop' => 'blog_post_full_image_crop',
),
'blog_related' => array(
'label' => __( 'Blog Post: Related', 'tb' ),
'width' => 'blog_related_image_width',
'height' => 'blog_related_image_height',
'crop' => 'blog_related_image_crop',
),
);
// Apply filters
$this->sizes = apply_filters( 'tb_image_sizes', $this->sizes );
}
/**
* Filter the image sizes automatically generated when uploading an image.
*
* @since 2.0.0
*/
public function do_not_crop_on_upload( $sizes ) {
// Remove my image sizes from cropping if image resizing is enabled
if ( tb_get_mod( 'image_resizing', true ) && ! empty ( $this->sizes ) ) {
foreach( $this->sizes as $size => $args ) {
unset( $sizes[$size] );
}
}
// Return $meta
return $sizes;
}
/**
* Retrieves cached CSS or generates the responsive CSS
*
* @since 2.0.0
*/
public function add_sizes() {
// Get sizes array
$sizes = $this->sizes;
// Loop through sizes
foreach ( $sizes as $size => $args ) {
// Define dims
$size = $size;
// Extract args
extract( $args );
// Get theme mods
$width = tb_get_mod( $width, '9999' );
$height = tb_get_mod( $height, '9999' );
$crop = tb_get_mod( $crop );
$crop = $crop ? $crop : 'center-center';
// Turn crop into array
$crop = ( 'center-center' == $crop ) ? 1 : explode( '-', $crop );
// If image resizing is disabled and a width or height is defined add image size
if ( $width || $height ) {
add_image_size( $size, $width, $height, $crop );
}
}
}
/**
* Add sub menu page
*
* @since 2.0.0
*/
public function add_admin_page() {
add_submenu_page(
TB_THEME_PANEL_SLUG,
__( 'Image Sizes', 'tb' ),
__( 'Image Sizes', 'tb' ),
'administrator',
TB_THEME_PANEL_SLUG . '-image-sizes',
array( $this, 'create_admin_page' )
);
}
/**
* Register a setting and its sanitization callback.
*
* @since 2.0.0
*/
function register_settings() {
register_setting( 'tb_image_sizes', 'tb_image_sizes', array( $this, 'admin_sanitize' ) );
}
/**
* Main Sanitization callback
*
* @since 2.0.0
*/
function admin_sanitize( $options ) {
// Check options first
if ( ! is_array( $options ) || empty( $options ) || ( false === $options ) ) {
return array();
}
// Save checkboxes
$checkboxes = array( 'retina', 'image_resizing' );
// Remove thememods for checkboxes not in array
foreach ( $checkboxes as $checkbox ) {
if ( isset( $options[$checkbox] ) ) {
set_theme_mod( $checkbox, 1 );
} else {
set_theme_mod( $checkbox, 0 );
}
}
// Standard options
foreach( $options as $key => $value ) {
if ( in_array( $key, $checkboxes ) ) {
continue; // checkboxes already done
}
if ( ! empty( $value ) ) {
set_theme_mod( $key, $value );
} else {
remove_theme_mod( $key );
}
}
// No need to save in options table
$options = '';
return $options;
}
/**
* Settings page output
*/
function create_admin_page() { ?>
<div class="wrap">
<h2><?php _e( 'Image Sizes', 'tb' ); ?></h2>
<p><?php _e( 'Define the exact cropping for all the featured images on your site. Leave the width and height empty to display the full image. Set any height to "9999" or empty to disable cropping and simply resize the image to the corresponding width. All image sizes defined below will be added to the list of WordPress image sizes.', 'tb' ); ?></p>
<form method="post" action="options.php">
<?php settings_fields( 'tb_image_sizes' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e( 'Image Resizing', 'tb' ); ?></th>
<td>
<fieldset>
<label>
<input id="tb_image_resizing" type="checkbox" name="tb_image_sizes[image_resizing]" <?php checked( tb_get_mod( 'image_resizing', true ) ); ?>>
<?php _e( 'Enable on the fly image cropping.', 'tb' ); ?>
<p class="description"><?php _e( 'This theme includes an advanced "on the fly" cropping function that uses the safe and native WordPress function "wp_get_image_editor". If enabled whenever you upload a new image it will NOT be cropped into all the different sizes defined below, but rather cropped when loaded on the front-end (cropped once then saved to your uploads directory), thus saving precious server space. However it may conflict with with certain CDN\'s, so you can disable if needed. If disabled you will need to "regenerate your thumbnails".', 'tb' ); ?></p>
</label>
</fieldset>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e( 'Retina', 'tb' ); ?></th>
<td>
<fieldset>
<label>
<input id="tb_retina" type="checkbox" name="tb_image_sizes[retina]" <?php checked( tb_get_mod( 'retina' ), true ); ?>> <?php _e( 'Enable retina support for your site (via retina.js).', 'tb' ); ?>
</label>
</fieldset>
</td>
</tr>
<?php
// Get sizes & crop locations
$sizes = $this->sizes;
$crop_locations = tb_image_crop_locations(); ?>
<?php
// Loop through all sizes
foreach ( $sizes as $size => $args ) : ?>
<?php
// Extract args
extract( $args );
// Label is required
if ( ! $label ) {
continue;
}
// Define values
$width_value = tb_get_mod( $width );
$height_value = tb_get_mod( $height );
$crop_value = tb_get_mod( $crop ); ?>
<tr valign="top">
<th scope="row"><?php echo $label; ?></th>
<td>
<label for="<?php echo $width; ?>"><?php _e( 'Width', 'tb' ); ?></label>
<input name="tb_image_sizes[<?php echo $width; ?>]" type="number" step="1" min="0" value="<?php echo $width_value; ?>" class="small-text" />
<label for="<?php echo $height; ?>"><?php _e( 'Height', 'tb' ); ?></label>
<input name="tb_image_sizes[<?php echo $height; ?>]" type="number" step="1" min="0" value="<?php echo $height_value; ?>" class="small-text" />
<label for="<?php echo $crop; ?>"><?php _e( 'Crop Location', 'tb' ); ?></label>
<select name="tb_image_sizes[<?php echo $crop; ?>]">
<?php foreach ( $crop_locations as $key => $label ) { ?>
<option value="<?php echo $key; ?>" <?php selected( $key, $crop_value, true ); ?>><?php echo $label; ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php submit_button(); ?>
</form>
<div id="tb_regenerating_tools" style="display:none;">
<hr />
<p><?php _e( 'Useful Plugins:', 'tb' ); ?> <a href="https://wordpress.org/plugins/regenerate-thumbnails/" target="_blank"><?php _e( 'Regenerate Thumbnails', 'tb' ); ?></a> | <a href="https://wordpress.org/plugins/image-cleanup/screenshots/" target="_blank"><?php _e( 'Image Cleanup', 'tb' ); ?></a></p>
</div><!-- #tb_regenerating_tools -->
</div><!-- .wrap -->
<script>
( function( $ ) {
"use strict";
// Disable and hide retina if image resizing is deleted
var $imageResizing = $( '#tb_image_resizing' ),
$imageResizingVal = $imageResizing.prop( 'checked' );
// Check initial val
if ( ! $imageResizingVal ) {
$( '#tb_retina' ).attr('checked', false );
$( '#tb_retina' ).closest( 'tr' ).hide();
$( '#tb_regenerating_tools' ).show();
}
// Check on change
$( $imageResizing ).change(function () {
var $checked = $( this ).prop('checked');
if ( $checked ) {
$( '#tb_retina' ).closest( 'tr' ).show();
$( '#tb_regenerating_tools' ).hide();
$( '#tb_retina' ).attr('checked', true );
} else {
$( '#tb_retina' ).attr('checked', false );
$( '#tb_retina' ).closest( 'tr' ).hide();
$( '#tb_regenerating_tools' ).show();
}
});
} ) ( jQuery );
</script>
<?php
}
}
}
new TB_Image_Sizes();