From d85e1ef8e2bf9bb32eb8e64146967d6acba06f08 Mon Sep 17 00:00:00 2001 From: Ryan DeBeasi Date: Tue, 1 Dec 2015 11:49:44 -0500 Subject: [PATCH 01/12] Add filters (addresses #37). The plugin now has filters for destination file path, destination file URL, and $do_crop. These filters can be used by other plugins. (If $do_crop is false, manual image crop will save metadata but leave the actual resizing to another plugin.) --- lib/ManualImageCrop.php | 53 +++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/lib/ManualImageCrop.php b/lib/ManualImageCrop.php index a7b9466..e7b4339 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,12 @@ public function cropImage() { ); wp_update_attachment_metadata($data['attachmentId'], $imageMetadata); + $do_crop = apply_filters( 'mic_do_crop', true, $imageMetadata ); + if ( !$do_crop ) { + // Another plugin has already taken care of the croppng. + $this->cropSuccess( $data, $dst_file_url ); + } + if ( function_exists('wp_get_image_editor') ) { $img = wp_get_image_editor( $src_file ); @@ -372,7 +392,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 +400,6 @@ 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] ) ); - exit; + $this->cropSuccess( $data, $dst_file_url ); } } From 718490bf48e704b220e0f0dc1133cf9aff22494d Mon Sep 17 00:00:00 2001 From: Ryan DeBeasi Date: Tue, 1 Dec 2015 12:06:59 -0500 Subject: [PATCH 02/12] Document filters (#37). --- readme.txt | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/readme.txt b/readme.txt index cd1175c..2d0cd03 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,21 @@ 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) and $metadata (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. + +=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. 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. == Changelog == = 1.12 = @@ -65,7 +77,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 +116,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 From 3de89b4b202e0244efa9d43e5ea2af042c39bcbf Mon Sep 17 00:00:00 2001 From: Ryan DeBeasi Date: Tue, 1 Dec 2015 12:06:59 -0500 Subject: [PATCH 03/12] Document filters (#37). --- readme.txt | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/readme.txt b/readme.txt index cd1175c..1556454 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,21 @@ 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) and $metadata (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. 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. $data contains the crop parameters that the user chose in WordPress admin. == Changelog == = 1.12 = @@ -65,7 +77,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 +116,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 From 14a5a0ce813475a3078ff3e174de492ea0cc32b7 Mon Sep 17 00:00:00 2001 From: Joey Line Date: Tue, 1 Dec 2015 16:43:57 -0500 Subject: [PATCH 04/12] Trigger an action when cropping is done Run an action that other scripts can hook into, letting them know that the cropping is done for the given image --- lib/ManualImageCrop.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/ManualImageCrop.php b/lib/ManualImageCrop.php index a7b9466..530de64 100644 --- a/lib/ManualImageCrop.php +++ b/lib/ManualImageCrop.php @@ -385,6 +385,10 @@ public function cropImage() { update_option('mic_make2x', $data['make2x']); } + // 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); + //returns the url to the generated image (to allow refreshing the preview) echo json_encode (array('status' => 'ok', 'file' => $dst_file_url[0] ) ); exit; From 1cb94e92249bd9c9967d17679b97b743623ba9d2 Mon Sep 17 00:00:00 2001 From: Joey Line Date: Tue, 1 Dec 2015 17:01:34 -0500 Subject: [PATCH 05/12] Update readme.txt to document the action --- readme.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index cd1175c..3d610a4 100644 --- a/readme.txt +++ b/readme.txt @@ -51,6 +51,12 @@ Please contact me if you want to add a translation (or submit a pull request on * Search for 'Manual Imag Crop', and click install * When the plugin has been installed, Click 'Activate' +== 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. + == Changelog == = 1.12 = * Fixed 'streched images' issue @@ -104,4 +110,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 From 6f836f2c47849cb1e9a77a89f01a06d803b7613b Mon Sep 17 00:00:00 2001 From: Ryan DeBeasi Date: Tue, 1 Dec 2015 17:58:45 -0500 Subject: [PATCH 06/12] Provide to mic_do_crop filter. --- lib/ManualImageCrop.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/ManualImageCrop.php b/lib/ManualImageCrop.php index e7b4339..2c820ee 100644 --- a/lib/ManualImageCrop.php +++ b/lib/ManualImageCrop.php @@ -293,9 +293,10 @@ public function cropImage() { ); wp_update_attachment_metadata($data['attachmentId'], $imageMetadata); - $do_crop = apply_filters( 'mic_do_crop', true, $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 croppng. + // Another plugin has already taken care of the cropping. $this->cropSuccess( $data, $dst_file_url ); } From 53cd6e465af342280f2ea54d7e94760268f191ab Mon Sep 17 00:00:00 2001 From: Ryan DeBeasi Date: Tue, 1 Dec 2015 17:58:45 -0500 Subject: [PATCH 07/12] Provide dimensions to mic_do_crop filter. --- lib/ManualImageCrop.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/ManualImageCrop.php b/lib/ManualImageCrop.php index e7b4339..2c820ee 100644 --- a/lib/ManualImageCrop.php +++ b/lib/ManualImageCrop.php @@ -293,9 +293,10 @@ public function cropImage() { ); wp_update_attachment_metadata($data['attachmentId'], $imageMetadata); - $do_crop = apply_filters( 'mic_do_crop', true, $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 croppng. + // Another plugin has already taken care of the cropping. $this->cropSuccess( $data, $dst_file_url ); } From d735fe9036853413f3b0ea0483c7c34a9c1aea14 Mon Sep 17 00:00:00 2001 From: Ryan DeBeasi Date: Thu, 10 Dec 2015 12:59:49 -0500 Subject: [PATCH 08/12] Update readme to indicate pull requests. --- readme.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/readme.txt b/readme.txt index 06b8a6a..2d4e53a 100644 --- a/readme.txt +++ b/readme.txt @@ -10,6 +10,14 @@ Stable tag: 1.12 Plugin allows you to manually crop all the image sizes registered in your WordPress theme (in particular featured image). +=== About This Branch === + +The "v1.13-bgmp" branch of the plugin includes some pull requests that haven't been merged back to the main repository yet. Once the pull requests are merged back, this branch will be deprecated in favor of the mainline version of the plugin. + +Included pull requests: +- https://github.com/tomaszsita/wp-manual-image-crop/pull/46 +- https://github.com/tomaszsita/wp-manual-image-crop/pull/47 + == 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. From 9b7911292d363a0ff590be07bbbb267781ac3670 Mon Sep 17 00:00:00 2001 From: Ryan DeBeasi Date: Mon, 14 Dec 2015 16:21:52 -0500 Subject: [PATCH 09/12] Update version number in plugin to make it clear this is the forked version. --- manual-image-crop.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manual-image-crop.php b/manual-image-crop.php index 0b7f6da..07b9a36 100644 --- a/manual-image-crop.php +++ b/manual-image-crop.php @@ -3,7 +3,7 @@ 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.12 +Version: 1.13-bgmp Author: Tomasz Sita Author URI: https://github.com/tomaszsita License: GPL2 From df0cdd6f66266d3bf618a404947104c4505f40b2 Mon Sep 17 00:00:00 2001 From: Ryan DeBeasi Date: Wed, 16 Dec 2015 16:20:02 -0500 Subject: [PATCH 10/12] Remove references to v1.13-bgmp. The version can be incremented once the changes are back in the mainline version of the plugin. --- manual-image-crop.php | 2 +- readme.txt | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/manual-image-crop.php b/manual-image-crop.php index 07b9a36..0b7f6da 100644 --- a/manual-image-crop.php +++ b/manual-image-crop.php @@ -3,7 +3,7 @@ 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.13-bgmp +Version: 1.12 Author: Tomasz Sita Author URI: https://github.com/tomaszsita License: GPL2 diff --git a/readme.txt b/readme.txt index 4b74efe..49b7f4e 100644 --- a/readme.txt +++ b/readme.txt @@ -10,14 +10,6 @@ Stable tag: 1.12 Plugin allows you to manually crop all the image sizes registered in your WordPress theme (in particular featured image). -=== About This Branch === - -The "v1.13-bgmp" branch of the plugin includes some pull requests that haven't been merged back to the main repository yet. Once the pull requests are merged back, this branch will be deprecated in favor of the mainline version of the plugin. - -Included pull requests: -- https://github.com/tomaszsita/wp-manual-image-crop/pull/46 -- https://github.com/tomaszsita/wp-manual-image-crop/pull/47 - == 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. From c0b17a16b6c8c286e345645d153a450cae3bf9eb Mon Sep 17 00:00:00 2001 From: Ryan DeBeasi Date: Wed, 16 Dec 2015 16:32:09 -0500 Subject: [PATCH 11/12] Note that variable is passed to mic_do_crop. --- readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index 49b7f4e..31ea1e9 100644 --- a/readme.txt +++ b/readme.txt @@ -55,7 +55,7 @@ Please contact me if you want to add a translation (or submit a pull request on The plugin includes filters that can be used by other plugins: =mic_do_crop= -Provides $do_crop (bool) and $metadata (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. +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. From 75fbc057e025b72ba3a48ebb8826918131889d56 Mon Sep 17 00:00:00 2001 From: Ryan DeBeasi Date: Wed, 16 Dec 2015 16:33:52 -0500 Subject: [PATCH 12/12] Note parameters for mic_crop_done. --- readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index 31ea1e9..6046c60 100644 --- a/readme.txt +++ b/readme.txt @@ -69,7 +69,7 @@ The admin screen uses this URL to display the updated image. This URL is not sto 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. +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 =