guard(); $id = isset( $_POST['attachment_id'] ) ? absint( wp_unslash( $_POST['attachment_id'] ) ) : 0; $this->assert_attachment( $id ); $result = ( new Converter() )->convert( $id ); $this->send_result( $id, $result ); } /** * Restore one attachment. * * @return void */ public function restore() { $this->guard(); $id = isset( $_POST['attachment_id'] ) ? absint( wp_unslash( $_POST['attachment_id'] ) ) : 0; $this->assert_attachment( $id ); $result = ( new Converter() )->restore( $id ); $this->send_result( $id, $result ); } /** * Return the library markup and fresh statistics. * * @return void */ public function list_items() { $this->guard(); $status = isset( $_POST['status'] ) ? sanitize_key( wp_unslash( $_POST['status'] ) ) : 'all'; $search = isset( $_POST['s'] ) ? sanitize_text_field( wp_unslash( $_POST['s'] ) ) : ''; $paged = isset( $_POST['paged'] ) ? absint( wp_unslash( $_POST['paged'] ) ) : 1; $list = Library::query( Library::sanitize_filters( $status, $search, $paged ) ); wp_send_json_success( array( 'html' => Admin::library_html( $list ), 'stats' => Stats::get(), ) ); } /** * Return the next pending IDs after a cursor. * * @return void */ public function pending_ids() { $this->guard(); $after = isset( $_POST['after_id'] ) ? absint( wp_unslash( $_POST['after_id'] ) ) : 0; $ids = Library::pending_ids( $after, 30 ); wp_send_json_success( array( 'ids' => $ids, ) ); } /** * Require a valid nonce and the media capability. * * @return void */ private function guard() { $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'flipwebp_admin' ) ) { wp_send_json_error( array( 'message' => __( 'The link you followed has expired. Reload the page and try again.', 'flipwebp' ), ), 403 ); } if ( ! current_user_can( 'upload_files' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to convert images.', 'flipwebp' ), ), 403 ); } } /** * Stop the request when the attachment cannot be edited. * * @param int $attachment_id Attachment ID. * @return void */ private function assert_attachment( $attachment_id ) { if ( ! Capabilities::can_convert( $attachment_id ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to convert this image.', 'flipwebp' ), ), 403 ); } } /** * Send a conversion result and the refreshed row. * * @param int $attachment_id Attachment ID. * @param array|\WP_Error $result Result. * @return void */ private function send_result( $attachment_id, $result ) { if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => wp_strip_all_tags( $result->get_error_message() ), 'status' => 'error', ) ); } $item = Library::item( $attachment_id ); wp_send_json_success( array( 'result' => array( 'status' => isset( $result['status'] ) ? sanitize_key( (string) $result['status'] ) : '', 'message' => isset( $result['message'] ) ? wp_strip_all_tags( (string) $result['message'] ) : '', ), 'item' => $this->public_item( $item ), 'stats' => Stats::get(), ) ); } /** * Remove anything the browser does not need from a library row. * * @param array|null $item Row. * @return array|null */ private function public_item( $item ) { if ( ! is_array( $item ) ) { return null; } $thumb = isset( $item['thumb'] ) ? (string) $item['thumb'] : ''; $thumb = wp_http_validate_url( $thumb ) ? $thumb : ''; return array( 'id' => isset( $item['id'] ) ? (int) $item['id'] : 0, 'filename' => isset( $item['filename'] ) ? (string) $item['filename'] : '', 'mimeLabel' => isset( $item['mimeLabel'] ) ? (string) $item['mimeLabel'] : '', 'dimensions' => isset( $item['dimensions'] ) ? (string) $item['dimensions'] : '', 'sizeLabel' => isset( $item['sizeLabel'] ) ? (string) $item['sizeLabel'] : '', 'savedLabel' => isset( $item['savedLabel'] ) ? (string) $item['savedLabel'] : '', 'status' => isset( $item['status'] ) ? sanitize_key( (string) $item['status'] ) : '', 'statusLabel' => isset( $item['statusLabel'] ) ? (string) $item['statusLabel'] : '', 'actionLabel' => isset( $item['actionLabel'] ) ? (string) $item['actionLabel'] : '', 'restoreUrl' => ( isset( $item['restoreUrl'] ) && is_string( $item['restoreUrl'] ) ) ? $item['restoreUrl'] : '', 'thumb' => $thumb, ); } }