Skip to content

anonymous /gist:4400528
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fixes division by zero warnings with crop thumbnail plugin
--- /var/www/narelle/wp-content/plugins/crop-thumbnails/functions/editor.php
+++ /var/www/narelle/wp-content/plugins/crop-thumbnails/functions/editor.php.new
@@ -1,18 +1,18 @@
<?php
class CropPostThumbnailsEditor {
-
+
function __construct() {
if ( is_admin() ) {
//add style and javascript
add_action( 'admin_print_styles', array(&$this, 'adminHeaderCSS') );
add_action( 'admin_print_scripts', array(&$this, 'adminHeaderJS') );
}
-
+
/* for the html inside the thickbox */
add_action('wp_ajax_croppostthumb_ajax', array($this,'ajaxWindow'));
}
-
-
+
+
/**
* this function is called for/from the thickbox - returns ordanary html
*/
@@ -33,7 +33,7 @@
$id = get_post_thumbnail_id(intval($_REQUEST['image_by_post_id']));
if(!empty($id)) {
$_REQUEST['image_id'] = $id;
- $_REQUEST['parent_post_id'] = intval($_REQUEST['image_by_post_id']);
+ $_REQUEST['parent_post_id'] = intval($_REQUEST['image_by_post_id']);
$this->byImageId();
} else {
$failure_msg = '<div class="listEmptyMsg">'.__('No featured Image set for this post until now.',CPT_LANG).'</div>';
@@ -41,10 +41,10 @@
break;
default:
$failure_msg = __('An error happend!',CPT_LANG);
- break;
+ break;
}
}
-
+
if(!empty($failure_msg)) {
wp_enqueue_style( 'cpt-window',plugins_url('css/cpt-window.css',dirname(__FILE__)),array('wp-admin'),CPT_VERSION);
$cptContent = $failure_msg;
@@ -52,7 +52,7 @@
}
die();//to prevent to send back a "0"
}
-
+
/**
* Display a list of images that are attached to this post_id.
* Hightlight the post-thumbnail (if it is attached to this post_id)
@@ -60,17 +60,17 @@
function byPostId() {
global $cptSettings;
$options = $cptSettings->getOptions();
-
+
$data = $this->loadPostIdData(intval($_REQUEST['post_id']));
-
+
$parent_post_type = '';
$_tmp_post = get_post(intval($_REQUEST['post_id']));
if(!empty($_tmp_post)) {
$parent_post_type = $_tmp_post->post_type;
}
-
+
$cptContent = '';
-
+
if($this->shouldBeHiddenOnPostType($options,$parent_post_type)) {
$cptContent = '<div class="postTypeDisabledMsg">'.__('Cropping is disabled for this post-type.',CPT_LANG).'</div>';
} elseif($data==false) {
@@ -91,8 +91,8 @@
<?php
$cptScript = ob_get_clean();
//END the javascript
-
-
+
+
//the content
ob_start();?>
<div class="header"><strong><?php _e('Choose the image you want to crop.',CPT_LANG); ?></strong></div>
@@ -106,7 +106,7 @@
<img src="<?php echo $img_data[0].'?'.time(); ?>" />
</li>
<?php
- $counter++;
+ $counter++;
endforeach; ?>
</ul>
<?php
@@ -127,35 +127,35 @@
*/
function byImageId() {
global $cptSettings,$content_width;
-
+
//make sure $content_width is out of the way
$_remember_content_width = $content_width;
$content_width = null;
-
-
+
+
$options = $cptSettings->getOptions();
$image_obj = get_post(intval($_REQUEST['image_id']));
-
+
//$post_id_attached holds the id of the post the image is attached to - can be null/empty
$post_id_attached = -1;
if(!empty($image_obj->post_parent)) {
- $post_id_attached=$image_obj->post_parent;
- }
-
+ $post_id_attached=$image_obj->post_parent;
+ }
+
//$current_parent_post_type
$current_parent_post_type = '';
$current_parent_post_id = -1;
$_tmp = get_post(intval($_REQUEST['parent_post_id']));
if(!empty($_REQUEST['parent_post_id']) && !empty($_tmp)) {
- $current_parent_post_type = $_tmp->post_type;
+ $current_parent_post_type = $_tmp->post_type;
$current_parent_post_id = $_tmp->ID;
}
-
+
$all_image_sizes = $cptSettings->getImageSizes();
- $orig_img = wp_get_attachment_image_src($image_obj->ID, 'full');
+ $orig_img = wp_get_attachment_image_src($image_obj->ID, 'full');
$cache_breaker = time();//a additional parameter that will be added to the image-urls to prevent the browser to show a cached image
-
-
+
+
//the javascript
ob_start(); ?>
<script>
@@ -170,7 +170,7 @@
<?php
$cptScript = ob_get_clean();
//END the javascript
-
+
/**
* wether or not to show the "back button"
*/
@@ -178,16 +178,16 @@
if(!empty($_REQUEST['viewmode']) && $_REQUEST['viewmode']=='single') {
$headline = false;
}
-
+
//the content
ob_start();
-
+
if($this->shouldBeHiddenOnPostType($options,$current_parent_post_type)) : ?>
<div class="cpt-crop-view">
<div class="postTypeDisabledMsg"><?php _e('Cropping is disabled for this post-type.',CPT_LANG); ?></div>
</div>
<?php else : ?>
-
+
<div class="cpt-crop-view">
<?php if($headline) :?><div class="header"><a class="back" href="<?php echo admin_url( 'admin-ajax.php'); ?>?action=croppostthumb_ajax&post_id=<?php echo $current_parent_post_id; ?>"><?php _e('back to image-list',CPT_LANG); ?></a></div><?php endif; ?>
<div class="waitingWindow hidden"><?php _e('Please wait until the Images are cropped.',CPT_LANG); ?></div>
@@ -208,12 +208,12 @@
<label for="cpt-same-ratio" class="lbl-cpt-same-ratio"><?php _e('select images with same ratio at once',CPT_LANG); ?></label>
<button id="cpt-deselect" class="button"><?php _e('deselect all',CPT_LANG); ?></button>
<ul class="thumbnail-list">
- <?php
+ <?php
foreach($all_image_sizes as $img_size_name=>$value) :
if(!$this->shouldSizeBeHidden($options,$img_size_name,$current_parent_post_type)) :
$gcd = $this->gcd($value['width'],$value['height']);
- $print_ratio = $value['width']/$gcd.':'.$value['height']/$gcd;
-
+ $print_ratio = @($value['width']/$gcd.':'.$value['height']/$gcd);
+
$print_cropped = '';
$crop = 0;
$_class= '';
@@ -224,13 +224,13 @@
$_class = 'hidden';
}
$img_data = wp_get_attachment_image_src($image_obj->ID, $img_size_name);
- $ratio = ($value['width']/$gcd) / ($value['height']/$gcd);
-
+ $ratio = @(($value['width']/$gcd) / ($value['height']/$gcd));
+
$_lowResWarning = '';
if($this->isLowRes($value,$orig_img)) {
$_lowResWarning = ' <span class="lowResWarning">'.__('Original image to small for good crop-quality!',CPT_LANG).'</span>';
}
-
+
?>
<li class="<?php echo $_class; ?>" rel="<?php echo $print_ratio; ?>">
<strong><?php echo $img_size_name.$_lowResWarning; ?></strong>
@@ -248,8 +248,8 @@
endif;
$cptContent = ob_get_clean();
//END the content
-
-
+
+
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jcrop' );
wp_enqueue_script( 'json2' );
@@ -258,11 +258,11 @@
} else {
wp_enqueue_script( 'cpt-crop', plugins_url('js/cpt-crop.js',dirname(__FILE__)));
}
-
+
wp_enqueue_style( 'cpt-window',plugins_url('css/cpt-window.css',dirname(__FILE__)),array('wp-admin'),CPT_VERSION);
wp_enqueue_style( 'jcrop' );
include_once( dirname(__FILE__).'/../html/template.php' );
-
+
$content_width = $_remember_content_width;//reset the content-width
return true;
}
@@ -288,11 +288,11 @@
if(empty($post_type)) {
return false;
}
-
+
if(empty($options['hide_size'][$post_type][$img_size_name])) {
return false;
}
-
+
return true;
}
@@ -308,12 +308,12 @@
'post_parent' => intval($post_id)
);
$images = get_posts($args);
-
+
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
if(!isset($post_thumbnail_id)) {
$post_thumbnail_id = -1;
}
-
+
foreach($images as $key=>$value) {
$mime = $value->post_mime_type;
if( $mime !='image/jpeg' AND $mime !='image/png') {
@@ -324,7 +324,7 @@
}
return $images;
}
-
+
/**
* Checks if the thumb-image-dimensions are bigger than the actuall image.
* @param array thumbnail-data from the add_image_size-funtion (width, height)
@@ -337,7 +337,7 @@
}
return false;
}
-
+
function isUserPermitted() {
$return = false;
if(current_user_can('upload_files') && current_user_can('edit_pages')) {
@@ -346,22 +346,22 @@
//TODO maybe add noence (is it needed? there are no file- or db-operations)
return $return;
}
-
- /**
- * For adding the "thickbox"-style in the mediathek
+
+ /**
+ * For adding the "thickbox"-style in the mediathek
*/
function adminHeaderCSS() {
global $pagenow;
if ( $pagenow == 'post.php'
|| $pagenow == 'post-new.php'
- || $pagenow == 'page.php'
+ || $pagenow == 'page.php'
|| $pagenow == 'page-new.php'
|| $pagenow == 'upload.php') {
wp_enqueue_style( 'thickbox' );
}
}
-
-
+
+
/**
* For adding the "crop-thumbnail"-link on posts, pages and the mediathek
*/
@@ -369,19 +369,19 @@
global $pagenow;
if ( $pagenow == 'post.php'
|| $pagenow == 'post-new.php'
- || $pagenow == 'page.php'
+ || $pagenow == 'page.php'
|| $pagenow == 'page-new.php'
|| $pagenow == 'upload.php') {
-
+
wp_enqueue_script('thickbox', false, array('jquery','jquery-ui-tabs'));
add_action('admin_footer',array($this,'cptAddLinksToAdmin'));
}
}
-
-
+
+
/**
* This is for use inside the plugin only.
- * Removes all other styles and scripts, to make sure the crop-thumbnail is not compromited by other plugins
+ * Removes all other styles and scripts, to make sure the crop-thumbnail is not compromited by other plugins
*/
function cleanWPHead() {
global $wp_scripts, $wp_styles;
@@ -390,17 +390,17 @@
remove_all_actions('wp_print_styles');
remove_all_actions('wp_print_scripts');
}
-
- /**
- * Greatest cummon divisor
+
+ /**
+ * Greatest common divisor
*/
function gcd($a, $b){
$b = ( $a == 0 )? 0 : $b;
- return ( $a % $b )? $this->gcd($b, abs($a - $b)) : $b;
- }
-
-
-
+ return @( $a % $b ) ? $this->gcd($b, abs($a - $b)) : $b;
+ }
+
+
+
/**
* adds the links into post-types and the media-library
*/
@@ -418,8 +418,8 @@
/** add link on top of editor **/
$('#wp-content-media-buttons').append('<a style="margin:0 2em;" class="thickbox" href="' + ajaxurl + '?action=croppostthumb_ajax&amp;post_id=' + post_id_hidden + '&amp;TB_iframe=1&amp;width=800&amp;height=' + boxViewportHeight + '" title="<?php esc_attr_e('Crop Thumbnails',CPT_LANG) ?>"><?php esc_html_e('Crop Thumbnails',CPT_LANG); ?></a>');
-
-
+
+
/** add link to featured image box **/
var featuredImageLink = $('<a class="thickbox" href="' + ajaxurl + '?action=croppostthumb_ajax&amp;image_by_post_id=' + post_id_hidden + '&amp;viewmode=single&amp;TB_iframe=1&amp;width=800&amp;height=' + boxViewportHeight + '" title="<?php esc_attr_e('Crop Featured Image',CPT_LANG) ?>"><?php esc_html_e('Crop Featured Image',CPT_LANG); ?></a>')
.css({'margin':'5px', 'padding':'5px','display':'inline-block','line-height':'1'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.