diff --git a/lib/ManualImageCrop.php b/lib/ManualImageCrop.php index a7b9466..184b2f7 100644 --- a/lib/ManualImageCrop.php +++ b/lib/ManualImageCrop.php @@ -84,11 +84,11 @@ public function addAttachementEditLink() { ?> 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), @@ -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'])) { @@ -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']); @@ -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); @@ -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 ); @@ -372,7 +393,7 @@ 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; @@ -380,13 +401,12 @@ public function cropImage() { } } } - // 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; } } diff --git a/readme.txt b/readme.txt index cd1175c..6046c60 100644 --- a/readme.txt +++ b/readme.txt @@ -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. @@ -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 = @@ -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 @@ -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 \ No newline at end of file +* Initial version