| 1 |
<?php |
| 2 |
class Thumbnail_Editor_Process { |
| 3 |
|
| 4 |
public function __construct() { |
| 5 |
add_action('admin_init', array($this, 'thumb_data_update')); |
| 6 |
} |
| 7 |
|
| 8 |
public function thumb_data_update() { |
| 9 |
|
| 10 |
if (isset($_POST['th_edit_option']) and sanitize_text_field($_POST['th_edit_option']) == "thumb_editor_save_settings") { |
| 11 |
|
| 12 |
if (!isset($_POST['thumb_editor_afo_save_action_field']) || !wp_verify_nonce($_POST['thumb_editor_afo_save_action_field'], 'thumb_editor_afo_save_action')) { |
| 13 |
wp_die(__('Sorry, your nonce did not verify.', 'thumbnail-editor')); |
| 14 |
} |
| 15 |
|
| 16 |
if (!empty($_POST['thep_disable_srcset'])) { |
| 17 |
update_option('thep_disable_srcset', sanitize_text_field($_POST['thep_disable_srcset'])); |
| 18 |
} else { |
| 19 |
delete_option('thep_disable_srcset'); |
| 20 |
} |
| 21 |
|
| 22 |
if (!empty($_POST['thep_disable_wh'])) { |
| 23 |
update_option('thep_disable_wh', sanitize_text_field($_POST['thep_disable_wh'])); |
| 24 |
} else { |
| 25 |
delete_option('thep_disable_wh'); |
| 26 |
} |
| 27 |
|
| 28 |
$GLOBALS['msg'] = __('Plugin data saved successfully.', 'thumbnail-editor'); |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
if (isset($_POST['th_edit_option']) and sanitize_text_field($_POST['th_edit_option']) == "imageCrop") { |
| 33 |
$att_id = (int) sanitize_text_field($_REQUEST['att_id']); |
| 34 |
|
| 35 |
if ($att_id == 0 || $att_id == '') { |
| 36 |
wp_die(__('Image not found.', 'thumbnail-editor')); |
| 37 |
} |
| 38 |
|
| 39 |
if (!isset($_POST['th_edit_save_action_field']) || !wp_verify_nonce($_POST['th_edit_save_action_field'], 'th_edit_save_action')) { |
| 40 |
wp_die(__('Sorry, your nonce did not verify.', 'thumbnail-editor')); |
| 41 |
} |
| 42 |
|
| 43 |
$crop_type = sanitize_text_field($_REQUEST['crop_type']); |
| 44 |
$x1 = (int) sanitize_text_field($_REQUEST['x1']); |
| 45 |
$y1 = (int) sanitize_text_field($_REQUEST['y1']); |
| 46 |
$x2 = (int) sanitize_text_field($_REQUEST['x2']); |
| 47 |
$y2 = (int) sanitize_text_field($_REQUEST['y2']); |
| 48 |
$w = round((int) sanitize_text_field($_REQUEST['w'])); |
| 49 |
$h = round((int) sanitize_text_field($_REQUEST['h'])); |
| 50 |
|
| 51 |
$rw = round((int) sanitize_text_field($_REQUEST['rw'])); |
| 52 |
$rh = round((int) sanitize_text_field($_REQUEST['rh'])); |
| 53 |
|
| 54 |
$full_image = wp_get_attachment_image_src($att_id, 'full'); |
| 55 |
$info = pathinfo($full_image[0]); |
| 56 |
$dirname = dirname(get_attached_file($att_id)); |
| 57 |
|
| 58 |
// resize image // |
| 59 |
if ($rw != '' and $rh != '') { |
| 60 |
$file_name = $info['filename'] . '-up-' . time() . '.' . $info['extension']; |
| 61 |
$image_file = $dirname . '/' . $file_name; |
| 62 |
$image = wp_get_image_editor($full_image[0]); |
| 63 |
if (!is_wp_error($image)) { |
| 64 |
$image->set_quality(100); |
| 65 |
$image->resize($rw, $rh, false); |
| 66 |
$image->save($image_file); |
| 67 |
$up_th = wp_get_attachment_metadata($att_id); |
| 68 |
|
| 69 |
$image_info = getimagesize($image_file); |
| 70 |
$up_th['sizes'][$crop_type]['file'] = $file_name; |
| 71 |
$up_th['sizes'][$crop_type]['width'] = $image_info[0]; |
| 72 |
$up_th['sizes'][$crop_type]['height'] = $image_info[1]; |
| 73 |
$up_th['sizes'][$crop_type]['mime-type'] = $image_info['mime']; |
| 74 |
wp_update_attachment_metadata($att_id, $up_th); |
| 75 |
} |
| 76 |
} |
| 77 |
// resize image // |
| 78 |
|
| 79 |
// crop image // |
| 80 |
if ($rw == '' and $rh == '') { |
| 81 |
$parent_image = $full_image[0]; |
| 82 |
$file_name = $info['filename'] . '-up-' . time() . '.' . $info['extension']; |
| 83 |
$image_file = $dirname . '/' . $file_name; |
| 84 |
} else { |
| 85 |
$parent_image = $image_file; |
| 86 |
} |
| 87 |
|
| 88 |
if ($w != '' and $h != '') { |
| 89 |
$image = wp_get_image_editor($parent_image); |
| 90 |
if (!is_wp_error($image)) { |
| 91 |
|
| 92 |
$image->set_quality(100); |
| 93 |
$image->crop($x1, $y1, $w, $h); |
| 94 |
$image->save($image_file); |
| 95 |
$up_th = wp_get_attachment_metadata($att_id); |
| 96 |
|
| 97 |
$image_info = getimagesize($image_file); |
| 98 |
$up_th['sizes'][$crop_type]['file'] = $file_name; |
| 99 |
$up_th['sizes'][$crop_type]['width'] = $image_info[0]; |
| 100 |
$up_th['sizes'][$crop_type]['height'] = $image_info[1]; |
| 101 |
$up_th['sizes'][$crop_type]['mime-type'] = $image_info['mime']; |
| 102 |
wp_update_attachment_metadata($att_id, $up_th); |
| 103 |
|
| 104 |
} |
| 105 |
} |
| 106 |
// crop image // |
| 107 |
|
| 108 |
$GLOBALS['msg'] = "<strong>{$crop_type}</strong> " . __('image successfully updated.', 'thumbnail-editor'); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|