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

Fixes #470: Improve auto-optimization when the image is edited by the user #499

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
67 changes: 67 additions & 0 deletions classes/Optimization/UserImageEdit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Imagify\Optimization;

use Imagify\Traits\InstanceGetterTrait;

defined( 'ABSPATH' ) || exit;

/**
* Class that handles the optimized images that are manually edited by the user with the "Edit Image" button.
*
* @since 1.9.10
*/
class UserImageEdit {
use InstanceGetterTrait;

/**
* Init.
*
* @since 1.9.10
*/
public function init() {
add_action( 'wp_ajax_image-editor', [ $this, 'maybe_restore_media_before_edition' ], -5 ); // Before WP’s hook (priority 1).
}

/**
* Before the image is edited, restore the media.
*
* @since 1.9.10
*/
public function maybe_restore_media_before_edition() {
$media_id = filter_input(
INPUT_POST,
'postid',
FILTER_VALIDATE_INT,
[
'options' => [
'default' => 0,
'min_range' => 0,
],
]
);

$action = filter_input( INPUT_POST, 'do', FILTER_SANITIZE_STRING );

if ( empty( $media_id ) || 'save' !== $action ) {
return;
}

if ( ! current_user_can( 'edit_post', $media_id ) ) {
return;
}

if ( ! check_ajax_referer( "image_editor-$media_id", false, false ) ) {
return;
}

$process = imagify_get_optimization_process( $media_id, 'wp' );

if ( ! $process->is_valid() || ! $process->get_data()->is_optimized() ) {
// Nothing to do if the media is not optimized or invalid.
return;
}

// Work here.
}
}
4 changes: 3 additions & 1 deletion inc/classes/class-imagify-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class_alias( '\\Imagify\\Traits\\InstanceGetterTrait', '\\Imagify\\Traits\\FakeS
\Imagify\Imagifybeat\Actions::get_instance()->init();
}

if ( ! wp_doing_ajax() ) {
if ( wp_doing_ajax() ) {
\Imagify\Optimization\UserImageEdit::get_instance()->init();
} else {
Imagify_Assets::get_instance()->init();
}

Expand Down