public
anonymous / gist:4400528
Created

Fixes division by zero warnings with crop thumbnail plugin

  • Download Gist
gistfile1.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
--- /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'})

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.