| 1 |
<?php |
| 2 |
/** |
| 3 |
* Imsanity AJAX functions. |
| 4 |
* |
| 5 |
* @package Imsanity |
| 6 |
*/ |
| 7 |
|
| 8 |
add_action( 'wp_ajax_imsanity_get_images', 'imsanity_get_images' ); |
| 9 |
add_action( 'wp_ajax_imsanity_resize_image', 'imsanity_ajax_resize' ); |
| 10 |
add_action( 'wp_ajax_imsanity_remove_original', 'imsanity_ajax_remove_original' ); |
| 11 |
add_action( 'wp_ajax_imsanity_bulk_complete', 'imsanity_ajax_finish' ); |
| 12 |
|
| 13 |
/** |
| 14 |
* Searches for up to 250 images that are candidates for resize and renders them |
| 15 |
* to the browser as a json array, then dies. |
| 16 |
*/ |
| 17 |
function imsanity_get_images() { |
| 18 |
$permissions = apply_filters( 'imsanity_admin_permissions', 'manage_options' ); |
| 19 |
if ( ! current_user_can( $permissions ) || empty( $_REQUEST['_wpnonce'] ) ) { |
| 20 |
wp_send_json( |
| 21 |
array( |
| 22 |
'success' => false, |
| 23 |
'message' => esc_html__( 'Administrator permission is required', 'imsanity' ), |
| 24 |
) |
| 25 |
); |
| 26 |
} |
| 27 |
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'imsanity-bulk' ) && ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'imsanity-manual-resize' ) ) { |
| 28 |
wp_send_json( |
| 29 |
array( |
| 30 |
'success' => false, |
| 31 |
'message' => esc_html__( 'Access token has expired, please reload the page.', 'imsanity' ), |
| 32 |
) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
$resume_id = ! empty( $_POST['resume_id'] ) ? (int) $_POST['resume_id'] : PHP_INT_MAX; |
| 37 |
global $wpdb; |
| 38 |
// Load up all the image attachments we can find. |
| 39 |
$attachments = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID < %d AND post_type = 'attachment' AND post_mime_type LIKE %s ORDER BY ID DESC", $resume_id, '%%image%%' ) ); |
| 40 |
array_walk( $attachments, 'intval' ); |
| 41 |
wp_send_json( $attachments ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Resizes the image with the given id according to the configured max width and height settings |
| 46 |
* renders a json response indicating success/failure and dies. |
| 47 |
*/ |
| 48 |
function imsanity_ajax_resize() { |
| 49 |
$permissions = apply_filters( 'imsanity_editor_permissions', 'edit_others_posts' ); |
| 50 |
if ( ! current_user_can( $permissions ) || empty( $_REQUEST['_wpnonce'] ) ) { |
| 51 |
wp_send_json( |
| 52 |
array( |
| 53 |
'success' => false, |
| 54 |
'message' => esc_html__( 'Editor permission is required', 'imsanity' ), |
| 55 |
) |
| 56 |
); |
| 57 |
} |
| 58 |
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'imsanity-bulk' ) && ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'imsanity-manual-resize' ) ) { |
| 59 |
wp_send_json( |
| 60 |
array( |
| 61 |
'success' => false, |
| 62 |
'message' => esc_html__( 'Access token has expired, please reload the page.', 'imsanity' ), |
| 63 |
) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
$id = ! empty( $_POST['id'] ) ? (int) $_POST['id'] : 0; |
| 68 |
if ( ! $id ) { |
| 69 |
wp_send_json( |
| 70 |
array( |
| 71 |
'success' => false, |
| 72 |
'message' => esc_html__( 'Missing ID Parameter', 'imsanity' ), |
| 73 |
) |
| 74 |
); |
| 75 |
} |
| 76 |
$results = imsanity_resize_from_id( $id ); |
| 77 |
if ( ! empty( $_POST['resumable'] ) ) { |
| 78 |
update_option( 'imsanity_resume_id', $id, false ); |
| 79 |
sleep( 1 ); |
| 80 |
} |
| 81 |
|
| 82 |
wp_send_json( $results ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Removes the original image with the given id and renders a json response indicating success/failure and dies. |
| 87 |
*/ |
| 88 |
function imsanity_ajax_remove_original() { |
| 89 |
$permissions = apply_filters( 'imsanity_editor_permissions', 'edit_others_posts' ); |
| 90 |
if ( ! current_user_can( $permissions ) || empty( $_REQUEST['_wpnonce'] ) ) { |
| 91 |
wp_send_json( |
| 92 |
array( |
| 93 |
'success' => false, |
| 94 |
'message' => esc_html__( 'Editor permission is required', 'imsanity' ), |
| 95 |
) |
| 96 |
); |
| 97 |
} |
| 98 |
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'imsanity-bulk' ) && ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'imsanity-manual-resize' ) ) { |
| 99 |
wp_send_json( |
| 100 |
array( |
| 101 |
'success' => false, |
| 102 |
'message' => esc_html__( 'Access token has expired, please reload the page.', 'imsanity' ), |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
$id = ! empty( $_POST['id'] ) ? (int) $_POST['id'] : 0; |
| 108 |
if ( ! $id ) { |
| 109 |
wp_send_json( |
| 110 |
array( |
| 111 |
'success' => false, |
| 112 |
'message' => esc_html__( 'Missing ID Parameter', 'imsanity' ), |
| 113 |
) |
| 114 |
); |
| 115 |
} |
| 116 |
$remove_original = imsanity_remove_original_image( $id ); |
| 117 |
if ( $remove_original && is_array( $remove_original ) ) { |
| 118 |
wp_update_attachment_metadata( $id, $remove_original ); |
| 119 |
wp_send_json( array( 'success' => true ) ); |
| 120 |
} |
| 121 |
|
| 122 |
wp_send_json( array( 'success' => false ) ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Finalizes the resizing process. |
| 127 |
*/ |
| 128 |
function imsanity_ajax_finish() { |
| 129 |
$permissions = apply_filters( 'imsanity_admin_permissions', 'manage_options' ); |
| 130 |
if ( ! current_user_can( $permissions ) || empty( $_REQUEST['_wpnonce'] ) ) { |
| 131 |
wp_send_json( |
| 132 |
array( |
| 133 |
'success' => false, |
| 134 |
'message' => esc_html__( 'Administrator permission is required', 'imsanity' ), |
| 135 |
) |
| 136 |
); |
| 137 |
} |
| 138 |
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'imsanity-bulk' ) && ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'imsanity-manual-resize' ) ) { |
| 139 |
wp_send_json( |
| 140 |
array( |
| 141 |
'success' => false, |
| 142 |
'message' => esc_html__( 'Access token has expired, please reload the page.', 'imsanity' ), |
| 143 |
) |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
update_option( 'imsanity_resume_id', 0, false ); |
| 148 |
|
| 149 |
die(); |
| 150 |
} |
| 151 |
|