Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from RadGH/wp4.5
Browse files Browse the repository at this point in the history
RadGH, Part 2 — Wp4.5
  • Loading branch information
mcaskill authored Dec 6, 2016
2 parents 2086ef7 + b5f7c6a commit 662a276
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 62 deletions.
5 changes: 5 additions & 0 deletions assets/css/mic-admin.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#TB_load,
#TB_window {
z-index: 260001 !important;
}
Expand Down Expand Up @@ -100,6 +101,10 @@
margin-top: 10px;
}

input.micQuality {
width: 3.5em;
}

.attachment-info .mic-link.crop-image {
display: block;
text-decoration: none;
Expand Down
23 changes: 23 additions & 0 deletions extensions/regenerate-thumbnails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

function mic_after_regenerate_thumbnails( $attachment ) {
if ( !isset($_POST['action']) ) return $attachment;
if ( stripslashes($_POST['action']) !== 'regeneratethumbnail' ) return $attachment;

$id = (int) $_REQUEST['id'];
if ( empty($id) ) return $attachment;

remove_filter( 'wp_update_attachment_metadata', 'mic_after_regenerate_thumbnails', 100 );
foreach( $attachment['sizes'] as $size_key => $size_data ) {
$crop_data = get_post_meta( $id, '_mic_resizesize-' . $size_key, true );

if ( $crop_data ) {
$ManualImageCrop = ManualImageCrop::getInstance();
$ManualImageCrop->cropImage( $crop_data, true );
}
}
add_filter( 'wp_update_attachment_metadata', 'mic_after_regenerate_thumbnails', 100 );

return $attachment;
}
add_filter( 'wp_update_attachment_metadata', 'mic_after_regenerate_thumbnails', 100 );
113 changes: 85 additions & 28 deletions lib/ManualImageCrop.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ public function addAfterUploadAttachementEditLink() {
var micEditAttachemtnLinkAddedInterval = 0;
jQuery(document).ready(function() {
micEditAttachemtnLinkAddedInterval = setInterval(function() {
if (jQuery('#media-items .edit-attachment').length) {
jQuery('#media-items .edit-attachment').each(function(i, k) {
var $editAttachment = jQuery('#media-items').find('.edit-attachment');

if ($editAttachment.length) {
$editAttachment.each(function(i, k) {
try {
var mRegexp = /\?post=([0-9]+)/;
var match = mRegexp.exec(jQuery(this).attr('href'));
Expand Down Expand Up @@ -178,18 +180,29 @@ private function filterPostData() {

/**
* Crops the image based on params passed in $_POST array
*
* Optional parameter $data can be used by plugins to call this method using previous configurations.
*
* @param null $data
*/
public function cropImage() {
public function cropImage( $data = null, $silent_result = false ) {
global $_wp_additional_image_sizes;

$data = $this->filterPostData();

if ( $data === null ) {
$data = $this->filterPostData();
}

$dst_file_url = wp_get_attachment_image_src($data['attachmentId'], $data['editedSize']);

if (!$dst_file_url) {
exit;
if ( $silent_result ) return;
else exit;
}

update_post_meta( $data['attachmentId'], '_mic_resizesize-' . $data['editedSize'], $data );

$quality = $data['mic_quality'];

$uploadsDir = wp_upload_dir();

// checks for ssl. wp_upload_dir does not handle ssl (ssl admin trips on this and subsequent ajax success to browser)
Expand All @@ -206,8 +219,12 @@ public function cropImage() {
$src_file_url = wp_get_attachment_image_src( $data['attachmentId'], 'full' );

if ( ! $src_file_url ) {
echo json_encode( array( 'status' => 'error', 'message' => 'wrong attachment' ) );
exit;
if ( $silent_result ) {
return;
}else{
echo json_encode( array( 'status' => 'error', 'message' => 'wrong attachment' ) );
exit;
}
}

$src_file = str_replace( $uploadsDir['baseurl'], $uploadsDir['basedir'], $src_file_url[0] );
Expand Down Expand Up @@ -244,8 +261,12 @@ public function cropImage() {
}

if (!$dst_w || !$dst_h) {
echo json_encode (array('status' => 'error', 'message' => 'wrong dimensions' ) );
exit;
if ( $silent_result) {
return;
}else{
echo json_encode (array('status' => 'error', 'message' => 'wrong dimensions' ) );
exit;
}
}

//prepares coordinates that will be passed to cropping function
Expand Down Expand Up @@ -295,12 +316,20 @@ public function cropImage() {
$saveStatus = $img->save( $dst_file );

if ( is_wp_error( $saveStatus ) ) {
echo json_encode( array( 'status' => 'error', 'message' => 'WP_ERROR: ' . $saveStatus->get_error_message() ) );
exit;
if ( $silent_result ) {
return;
}else{
echo json_encode( array( 'status' => 'error', 'message' => 'WP_ERROR: ' . $saveStatus->get_error_message() ) );
exit;
}
}
}else {
echo json_encode (array('status' => 'error', 'message' => 'WP_ERROR: ' . $img->get_error_message() ) );
exit;
if ( $silent_result ) {
return;
}else{
echo json_encode (array('status' => 'error', 'message' => 'WP_ERROR: ' . $img->get_error_message() ) );
exit;
}
}
} else {
//determines what's the image format
Expand All @@ -314,16 +343,24 @@ public function cropImage() {
}

if ($src_img === false ) {
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: Cannot create image from the source file' ) );
exit;
if ( $silent_result ) {
return;
}else{
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: Cannot create image from the source file' ) );
exit;
}
}

$dst_img = imagecreatetruecolor($dst_w, $dst_h);
$resampleReturn = imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);

if ($resampleReturn === false ) {
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagecopyresampled' ) );
exit;
if ( $silent_result ) {
return;
}else{
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagecopyresampled' ) );
exit;
}
}

$imageSaveReturn = true;
Expand All @@ -336,8 +373,12 @@ public function cropImage() {
}

if ($imageSaveReturn === false ) {
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagejpeg/imagegif/imagepng' ) );
exit;
if ( $silent_result ) {
return;
}else{
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagejpeg/imagegif/imagepng' ) );
exit;
}
}
}

Expand All @@ -358,16 +399,24 @@ public function cropImage() {
$img->set_quality( $quality );
$img->save($dst_file2x);
}else {
echo json_encode (array('status' => 'error', 'message' => 'WP_ERROR: ' . $img->get_error_message() ) );
exit;
if ( $silent_result ) {
return;
}else{
echo json_encode (array('status' => 'error', 'message' => 'WP_ERROR: ' . $img->get_error_message() ) );
exit;
}
}
} else {
$dst_img2x = imagecreatetruecolor($dst_w2x, $dst_h2x);
$resampleReturn = imagecopyresampled($dst_img2x, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w2x, $dst_h2x, $src_w, $src_h);

if ($resampleReturn === false ) {
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagecopyresampled' ) );
exit;
if ( $silent_result ) {
return;
}else{
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagecopyresampled' ) );
exit;
}
}

$imageSaveReturn = true;
Expand All @@ -380,8 +429,12 @@ public function cropImage() {
}

if ($imageSaveReturn === false ) {
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagejpeg/imagegif/imagepng' ) );
exit;
if ( $silent_result ) {
return;
}else{
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagejpeg/imagegif/imagepng' ) );
exit;
}
}
}
}
Expand All @@ -392,7 +445,11 @@ public function cropImage() {
}

//returns the url to the generated image (to allow refreshing the preview)
echo json_encode (array('status' => 'ok', 'file' => $dst_file_url[0] ) );
exit;
if ( $silent_result ) {
return;
}else{
echo json_encode (array('status' => 'ok', 'file' => $dst_file_url[0] ) );
exit;
}
}
}
13 changes: 4 additions & 9 deletions lib/ManualImageCropEditorWindow.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,10 @@ public function renderWindow() {
<?php
$ext = strtolower( pathinfo($src_file, PATHINFO_EXTENSION) );
if ($ext == 'jpg' || $ext == 'jpeg') {
echo '<div class="mic-option"><label for="micQuality">' . __('Target JPEG Quality', 'microp') . '</label> <select id="micQuality" name="mic_quality">
<option value="100">' . __('100 (best quality, biggest file)', 'microp') . '</option>
<option value="80" ' . ( $sizesSettings[$editedSize]['quality'] == '80' ? 'selected' : '' ) . '>' . __('80 (very high quality)', 'microp') . '</option>
<option value="70" ' . ( $sizesSettings[$editedSize]['quality'] == '70' ? 'selected' : '' ) . '>' . __('70 (high quality)', 'microp') . '</option>
<option value="60" ' . ( $sizesSettings[$editedSize]['quality'] == '60' ? 'selected' : '' ) . '>' . __('60 (good)', 'microp') . '</option>
<option value="50" ' . ( $sizesSettings[$editedSize]['quality'] == '50' ? 'selected' : '' ) . '>' . __('50 (average)', 'microp') . '</option>
<option value="30" ' . ( $sizesSettings[$editedSize]['quality'] == '30' ? 'selected' : '' ) . '>' . __('30 (low)', 'microp') . '</option>
<option value="10" ' . ( $sizesSettings[$editedSize]['quality'] == '10' ? 'selected' : '' ) . '>' . __('10 (very low, smallest file)', 'microp') . '</option>
</select></div>';
echo '<div class="mic-option">';
echo '<label for="micQuality">' . __('Target JPEG Quality', 'microp') . ':</label> ';
echo '<input type="number" pattern="[0-9\.]+" min="0" max="100" step="1" id="micQuality" class="micQuality" name="mic_quality" value="'. esc_attr( $sizesSettings[$editedSize]['quality'] ) .'"> %';
echo '</div>';
}
?>
<?php
Expand Down
24 changes: 7 additions & 17 deletions lib/ManualImageCropSettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function create_admin_page()
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php _e('Manual Image Crop Settings', 'microp'); ?></h2>
<form method="post" action="options.php" class="mic-settings-page">
<form method="post" action="<?php echo admin_url('options.php'); ?>" class="mic-settings-page">
<?php
// This prints out all hidden setting fields
settings_fields( 'mic_options_group' );
Expand Down Expand Up @@ -147,12 +147,6 @@ public function sizes_settings_callback()

$imageSizes = get_intermediate_image_sizes();

$sizeLabels = apply_filters( 'image_size_names_choose', array(
'thumbnail' => __('Thumbnail'),
'medium' => __('Medium'),
'large' => __('Large'),
'full' => __('Full Size'),
) );
$sizeLabels = apply_filters( 'image_size_names_choose', array() );

echo '<table class="widefat fixed mic-table striped" cellspacing="0">';
Expand Down Expand Up @@ -182,22 +176,18 @@ public function sizes_settings_callback()
if ($cropMethod == 0) {
continue;
}


if ( empty($sizesSettings[$s]['quality']) && $sizesSettings[$s]['quality'] !== 0 ) {
$sizesSettings[$s]['quality'] = 80; // Default
}

echo '<tr>
<td class="mic-size">' . $label. '</td>
<td class="mic-visible"><select name="mic_options[sizes_settings][' . $s . '][visibility]">
<option value="visible">' . __('Yes', 'microp') . '</option>
<option value="hidden" ' . ( $sizesSettings[$s]['visibility'] == 'hidden' ? 'selected' : '' ) . '>' . __('No', 'microp') . '</option>
</select></td>
<td class="mic-quality"><select name="mic_options[sizes_settings][' . $s . '][quality]">
<option value="100">' . __('100 (best quality, biggest file)', 'microp') . '</option>
<option value="80" ' . ( !isset ($sizesSettings[$s]['quality']) || $sizesSettings[$s]['quality'] == '80' ? 'selected' : '' ) . '>' . __('80 (very high quality)', 'microp') . '</option>
<option value="70" ' . ( $sizesSettings[$s]['quality'] == '70' ? 'selected' : '' ) . '>' . __('70 (high quality)', 'microp') . '</option>
<option value="60" ' . ( $sizesSettings[$s]['quality'] == '60' ? 'selected' : '' ) . '>' . __('60 (good)', 'microp') . '</option>
<option value="50" ' . ( $sizesSettings[$s]['quality'] == '50' ? 'selected' : '' ) . '>' . __('50 (average)', 'microp') . '</option>
<option value="30" ' . ( $sizesSettings[$s]['quality'] == '30' ? 'selected' : '' ) . '>' . __('30 (low)', 'microp') . '</option>
<option value="10" ' . ( $sizesSettings[$s]['quality'] == '10' ? 'selected' : '' ) . '>' . __('10 (very low, smallest file)', 'microp') . '</option>
</select></td>
<td class="mic-quality"><input type="number" pattern="[0-9\.]+" min="0" max="100" step="1" class="micQuality" name="mic_options[sizes_settings][' . $s . '][quality]" value="'. esc_attr( $sizesSettings[$s]['quality'] ) .'"> %</td>
<td class="mic-label"><input name="mic_options[sizes_settings][' . $s . '][label]" type="text" placeholder="' . $label . '" value="' . str_replace('"', '&quot;', $sizesSettings[$s]['label']) . '"/></td>
</tr>';
}
Expand Down
5 changes: 0 additions & 5 deletions lib/plugin-conflicts.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ function mic_check_conflicting_plugins() {
// Collect conflicting plugins into an array.
$conflicting = array();

if ( $v = _mic_find_plugin('Regenerate Thumbnails') ) {
$v['reason'] = __('Will overwrite custom thumbnail cropping when regenerating thumbnails.', 'microp');
$conflicting[] = $v;
}

if ( $v = _mic_find_plugin('WP Smush') ) {
$v['reason'] = __('Will overwrite custom thumbnail cropping positions when images are "smushed".', 'microp');
$conflicting[] = $v;
Expand Down
11 changes: 8 additions & 3 deletions manual-image-crop.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
Plugin Name: Manual Image Crop
Plugin URI: https://github.com/tomaszsita/wp-manual-image-crop
Description: Plugin allows you to manually crop all the image sizes registered in your WordPress theme (in particular featured image). Simply click on the "Crop" link next to any image in your media library and select the area of the image you want to crop.
Version: 1.12r3
Version: 1.12r4
Author: Tomasz Sita
Author URI: https://github.com/tomaszsita
License: GPL2
Text Domain: microp
Domain Path: /languages/
*/

define('mic_VERSION', '1.12r3');
define('mic_VERSION', '1.12r4');

include_once(dirname(__FILE__) . '/lib/ManualImageCropSettingsPage.php');

Expand Down Expand Up @@ -41,6 +41,11 @@ function mic_init_plugin() {
//attach admin actions
add_action('wp_ajax_mic_editor_window', 'mic_ajax_editor_window');
add_action('wp_ajax_mic_crop_image', 'mic_ajax_crop_image');

// Support regenerate thumbnails to regenerate them at the specified size
if ( class_exists('RegenerateThumbnails') ) {
include_once( dirname(__FILE__) . '/extensions/regenerate-thumbnails.php' );
}
}

/**
Expand All @@ -67,7 +72,7 @@ function mic_ajax_crop_image() {
* add settings link on plugin page
*/
function mic_settings_link($links) {
$settings_link = '<a href="options-general.php?page=Mic-setting-admin">' . __('Settings') . '</a>';
$settings_link = '<a href="'. admin_url( 'options-general.php?page=Mic-setting-admin' ) .'">' . __('Settings') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
Expand Down

0 comments on commit 662a276

Please sign in to comment.