Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters and actions #46

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions lib/ManualImageCrop.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public function addAttachementEditLink() { ?>
<script>
var micEditAttachemtnLinkAdded = false;
var micEditAttachemtnLinkAddedInterval = 0;
jQuery(document).ready(function() {
jQuery(document).ready(function() {
micEditAttachemtnLinkAddedInterval = setInterval(function() {
if (jQuery('.details .edit-attachment').length) {
try {
var mRegexp = /\?post=([0-9]+)/;
var mRegexp = /\?post=([0-9]+)/;
var match = mRegexp.exec(jQuery('.details .edit-attachment').attr('href'));
jQuery('.crop-image-ml.crop-image').remove();
jQuery('.details .edit-attachment').after( '<a class="thickbox mic-link crop-image-ml crop-image" rel="crop" title="<?php _e("Manual Image Crop","microp"); ?>" href="' + ajaxurl + '?action=mic_editor_window&postId=' + match[1] + '"><?php _e('Crop Image','microp') ?></a>' );
Expand Down Expand Up @@ -125,7 +125,7 @@ public function addAfterUploadAttachementEditLink() {
if (jQuery('#media-items .edit-attachment').length) {
jQuery('#media-items .edit-attachment').each(function(i, k) {
try {
var mRegexp = /\?post=([0-9]+)/;
var mRegexp = /\?post=([0-9]+)/;
var match = mRegexp.exec(jQuery(this).attr('href'));
if (!jQuery(this).parent().find('.edit-attachment.crop-image').length && jQuery(this).parent().find('.pinkynail').attr('src').match(/upload/g)) {
jQuery(this).after( '<a class="thickbox mic-link edit-attachment crop-image" rel="crop" title="<?php _e("Manual Image Crop","microp"); ?>" href="' + ajaxurl + '?action=mic_editor_window&postId=' + match[1] + '"><?php _e('Crop Image','microp') ?></a>' );
Expand All @@ -140,12 +140,12 @@ public function addAfterUploadAttachementEditLink() {
</script>
<?php
}

private function filterPostData() {
$imageSizes = get_intermediate_image_sizes();

$data = array(
'attachmentId' => filter_var($_POST['attachmentId'], FILTER_SANITIZE_NUMBER_INT),
'attachmentId' => filter_var($_POST['attachmentId'], FILTER_SANITIZE_NUMBER_INT),
'editedSize' => in_array($_POST['editedSize'], $imageSizes) ? $_POST['editedSize'] : null,
'select' => array(
'x' => filter_var($_POST['select']['x'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION),
Expand All @@ -154,7 +154,7 @@ private function filterPostData() {
'h' => filter_var($_POST['select']['h'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION),
),
'previewScale' => filter_var($_POST['previewScale'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)

);

if (isset($_POST['mic_quality'])) {
Expand All @@ -170,12 +170,23 @@ private function filterPostData() {
return $data;
}

public function cropSuccess( $data, $dst_file_url ) {
// update 'mic_make2x' option status to persist choice
if( isset( $data['make2x'] ) && $data['make2x'] !== get_option('mic_make2x') ) {
update_option('mic_make2x', $data['make2x']);
}

//returns the url to the generated image (to allow refreshing the preview)
echo json_encode (array('status' => 'ok', 'file' => $dst_file_url[0] ) );
exit;
}

/**
* Crops the image based on params passed in $_POST array
*/
public function cropImage() {
global $_wp_additional_image_sizes;

$data = $this->filterPostData();

$dst_file_url = wp_get_attachment_image_src($data['attachmentId'], $data['editedSize']);
Expand Down Expand Up @@ -208,19 +219,22 @@ public function cropImage() {
$dst_file = str_replace( $uploadsDir['baseurl'], $uploadsDir['basedir'], $dst_file_url[0] );
}

$dst_file = apply_filters( 'mic_dst_file_path', $dst_file, $data );
$dst_file_url[0] = apply_filters( 'mic_dst_file_url', $dst_file_url[0], $data );

//checks if the destination image file is present (if it's not, we want to create a new file, as the WordPress returns the original image instead of specific one)
if ($dst_file == $src_file) {
$attachmentData = wp_generate_attachment_metadata( $data['attachmentId'], $dst_file );

//overwrite with previous values
$prevAttachmentData = wp_get_attachment_metadata($data['attachmentId']);
if (isset($prevAttachmentData['micSelectedArea'])) {
$attachmentData['micSelectedArea'] = $prevAttachmentData['micSelectedArea'];
}

//saves new path to the image size in the database
wp_update_attachment_metadata( $data['attachmentId'], $attachmentData );

//new destination file path - replaces original file name with the correct one
$dst_file = str_replace( basename($attachmentData['file']), $attachmentData['sizes'][ $data['editedSize'] ]['file'], $dst_file);

Expand Down Expand Up @@ -279,6 +293,13 @@ public function cropImage() {
);
wp_update_attachment_metadata($data['attachmentId'], $imageMetadata);

$dims = array( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
$do_crop = apply_filters( 'mic_do_crop', true, $imageMetadata, $dims );
if ( !$do_crop ) {
// Another plugin has already taken care of the cropping.
$this->cropSuccess( $data, $dst_file_url );
}

if ( function_exists('wp_get_image_editor') ) {
$img = wp_get_image_editor( $src_file );

Expand Down Expand Up @@ -372,21 +393,20 @@ public function cropImage() {
} else {
$imageSaveReturn = imagejpeg($dst_img2x, $dst_file2x, $quality);
}

if ($imageSaveReturn === false ) {
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagejpeg/imagegif/imagepng' ) );
exit;
}
}
}
}
// update 'mic_make2x' option status to persist choice
if( isset( $data['make2x'] ) && $data['make2x'] !== get_option('mic_make2x') ) {
update_option('mic_make2x', $data['make2x']);
}

//returns the url to the generated image (to allow refreshing the preview)
echo json_encode (array('status' => 'ok', 'file' => $dst_file_url[0] ) );
// run an action that other scripts can hook into, letting them
// know that the cropping is done for the given image
do_action('mic_crop_done', $data, $imageMetadata);

$this->cropSuccess( $data, $dst_file_url );
exit;
}
}
32 changes: 26 additions & 6 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Plugin allows you to manually crop all the image sizes registered in your WordPr

== 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.
Simply click on the "Crop" link next to any image in your media library.
The "lightbox" style interface will be brought up and you are ready to go.
Whole cropping process is really intuitive and simple.

Expand Down Expand Up @@ -47,9 +47,29 @@ Please contact me if you want to add a translation (or submit a pull request on
* Activate the plugin through the 'Plugins' menu in WordPress

= Automatically: =
* Navigate to the 'Plugins' menu inside of the wordpress wp-admin dashboard, and select AD NEW
* Search for 'Manual Imag Crop', and click install
* When the plugin has been installed, Click 'Activate'
* Navigate to the 'Plugins' menu inside of the wordpress wp-admin dashboard, and select AD NEW
* Search for 'Manual Imag Crop', and click install
* When the plugin has been installed, Click 'Activate'

== Filters ==
The plugin includes filters that can be used by other plugins:

=mic_do_crop=
Provides $do_crop (bool), $metadata (array), and $dims (array). Returning false for $do_crop will prevent Manual Image Crop from cropping the image. $metadata contains the crop parameters, so another plugin can take over the actual cropping.

=mic_dst_file_path=
Provides $path (string) and $data (array). Manual Image Crop will write the new image to $path and save that path to the image metadata. $data contains the crop parameters that the user chose in WordPress admin.

=mic_dst_file_url=
Provides $url (string) and $data (array). Manual Image Crop will return $url in an AJAX response if the image crop is successful. $data contains the crop parameters that the user chose in WordPress admin.

The admin screen uses this URL to display the updated image. This URL is not stored with the image or used elsewhere in WordPress. wp_get_attachment_image_src is used instead to generate the image URL.

== Actions ==
The plugin includes actions that can be used by other plugins:

= mic_crop_done =
Triggered after a crop has been successfully completed, immediately before the JSON response is sent to the browser. Provides $data (array) and $imageMetadata (array).

== Changelog ==
= 1.12 =
Expand All @@ -65,7 +85,7 @@ Please contact me if you want to add a translation (or submit a pull request on

= 1.09 =
* Dutch translation added
* Better error handling
* Better error handling
* Fixed overwriting of previously saved crops
* Minor tweaks all around

Expand Down Expand Up @@ -104,4 +124,4 @@ Please contact me if you want to add a translation (or submit a pull request on
* Improved compatibility with other plugins using 'thickbox'

= 1.0 =
* Initial version
* Initial version