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