$actions Bulk actions. * @return array */ public function bulk_actions( $actions ) { if ( current_user_can( 'upload_files' ) ) { $actions['flipwebp_convert'] = __( 'Convert to WebP', 'flipwebp' ); } return $actions; } /** * Send the selected attachments to the converter screen. * * WordPress has already checked the bulk-action nonce. * * @param string $redirect Redirect URL. * @param string $action Bulk action name. * @param int[] $ids Selected IDs. * @return string */ public function handle_bulk( $redirect, $action, $ids ) { if ( 'flipwebp_convert' !== $action || ! current_user_can( 'upload_files' ) ) { return $redirect; } $clean = array(); foreach ( (array) $ids as $id ) { $id = absint( $id ); $post = get_post( $id ); if ( ! $post instanceof \WP_Post || 'attachment' !== $post->post_type ) { continue; } if ( ! Capabilities::can_convert( $id ) || ! Converter::is_manageable_mime( (string) $post->post_mime_type ) ) { continue; } $clean[] = $id; } $clean = array_slice( array_values( array_unique( $clean ) ), 0, 500 ); if ( empty( $clean ) ) { return add_query_arg( 'flipwebp_none', '1', $redirect ); } $token = wp_generate_password( 20, false, false ); set_transient( 'flipwebp_q_' . get_current_user_id(), array( 'token' => $token, 'ids' => $clean, ), 15 * MINUTE_IN_SECONDS ); return add_query_arg( array( 'page' => 'flipwebp', 'queue' => $token, ), admin_url( 'upload.php' ) ); } /** * Row action that opens a confirmation step. It does not convert on GET. * * @param array $actions Row actions. * @param \WP_Post $post Attachment. * @return array */ public function row_actions( $actions, $post ) { if ( ! $post instanceof \WP_Post || ! Capabilities::can_convert( $post->ID ) ) { return $actions; } $mime = (string) get_post_mime_type( $post ); if ( ! in_array( $mime, array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp' ), true ) ) { return $actions; } $label = ( 'image/webp' === $mime ) ? __( 'Recompress WebP', 'flipwebp' ) : __( 'Convert to WebP', 'flipwebp' ); $url = wp_nonce_url( add_query_arg( array( 'page' => 'flipwebp', 'confirm' => (int) $post->ID, ), admin_url( 'upload.php' ) ), 'flipwebp_confirm_' . (int) $post->ID ); $actions['flipwebp'] = '' . esc_html( $label ) . ''; return $actions; } /** * Add a status column that can be hidden from Screen Options. * * @param array $columns Columns. * @return array */ public function columns( $columns ) { $columns['flipwebp'] = __( 'FlipWebP', 'flipwebp' ); return $columns; } /** * Render the status column. * * @param string $column Column name. * @param int $post_id Attachment ID. * @return void */ public function column( $column, $post_id ) { if ( 'flipwebp' !== $column ) { return; } echo esc_html( Library::status_label_for( (int) $post_id ) ); } /** * Add a convert control to the attachment details panel. * * @param array> $fields Fields. * @param \WP_Post $post Attachment. * @return array> */ public function fields( $fields, $post ) { if ( ! $post instanceof \WP_Post || ! Capabilities::can_convert( $post->ID ) ) { return $fields; } $mime = (string) get_post_mime_type( $post ); if ( ! Converter::is_manageable_mime( $mime ) ) { return $fields; } $label = ( 'image/webp' === $mime || 'converted' === get_post_meta( $post->ID, '_flipwebp_status', true ) ) ? __( 'Recompress', 'flipwebp' ) : __( 'Convert to WebP', 'flipwebp' ); $url = wp_nonce_url( add_query_arg( array( 'page' => 'flipwebp', 'confirm' => (int) $post->ID, ), admin_url( 'upload.php' ) ), 'flipwebp_confirm_' . (int) $post->ID ); $html = ''; $html .= esc_html( $label ); $html .= ''; $html .= '

' . esc_html( Library::status_label_for( $post->ID ) ) . '

'; $fields['flipwebp'] = array( 'label' => __( 'FlipWebP', 'flipwebp' ), 'input' => 'html', 'html' => $html, ); return $fields; } /** * Styles for the Media Library column. * * @param string $hook Current admin page hook. * @return void */ public function enqueue( $hook ) { if ( 'upload.php' !== $hook ) { return; } wp_enqueue_style( 'flipwebp-media', FLIPWEBP_URL . 'assets/css/media.css', array(), FLIPWEBP_VERSION ); } /** * Script for the media modal button. * * @return void */ public function enqueue_modal() { if ( ! current_user_can( 'upload_files' ) ) { return; } wp_enqueue_script( 'flipwebp-media', FLIPWEBP_URL . 'assets/js/media.js', array(), FLIPWEBP_VERSION, true ); wp_localize_script( 'flipwebp-media', 'flipwebpMedia', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'flipwebp_admin' ), 'i18n' => array( 'confirm' => __( 'Convert this image to WebP on this server? The original is deleted only when the WebP file is smaller and deletion is enabled in FlipWebP settings.', 'flipwebp' ), 'working' => __( 'Converting…', 'flipwebp' ), 'failed' => __( 'The image could not be converted.', 'flipwebp' ), ), ) ); } }