$columns Columns. * @return array */ public function add_columns( array $columns ): array { $columns['pixelkeep_status'] = __( 'Optimization', 'pixelkeep' ); $columns['pixelkeep_original'] = __( 'Original', 'pixelkeep' ); $columns['pixelkeep_optimized'] = __( 'Optimized', 'pixelkeep' ); $columns['pixelkeep_saved'] = __( 'Saved', 'pixelkeep' ); $columns['pixelkeep_formats'] = __( 'Formats', 'pixelkeep' ); return $columns; } /** * Prints a column's value. * * @param string $column Column name. * @param int $id Attachment ID. * @return void */ public function render_column( string $column, int $id ): void { if ( ! str_starts_with( $column, 'pixelkeep_' ) || ! $this->is_supported( $id ) ) { return; } $totals = $this->items->attachment_totals( $id ); switch ( $column ) { case 'pixelkeep_status': $code = (string) ( $this->state->get( $id )['error_code'] ?? '' ); echo '' . Format::status_pill( $this->state->status( $id ), $code ) . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- status_pill() escapes its parts. break; case 'pixelkeep_original': echo $totals['before'] > 0 ? esc_html( Format::bytes( $totals['before'] ) ) : '—'; break; case 'pixelkeep_optimized': echo $totals['after'] > 0 ? esc_html( Format::bytes( $totals['after'] ) ) : '—'; break; case 'pixelkeep_saved': echo $totals['before'] > 0 ? esc_html( Format::saved_percent( $totals['before'], $totals['after'] ) ) : '—'; break; case 'pixelkeep_formats': $formats = array_merge( array( str_replace( 'image/', '', (string) get_post_mime_type( $id ) ) ), $this->derivatives->formats( $id ) ); echo esc_html( strtoupper( implode( ', ', array_unique( $formats ) ) ) ); break; } } /** * Adds the row actions. * * @param array $actions Row actions. * @param WP_Post $post Attachment. * @return array */ public function add_row_actions( array $actions, WP_Post $post ): array { if ( ! $this->is_supported( $post->ID ) || ! current_user_can( 'edit_post', $post->ID ) ) { return $actions; } $status = $this->state->status( $post->ID ); $done = in_array( $status, array( 'optimized', 'already_optimal' ), true ); if ( $done ) { $actions['pixelkeep_reoptimize'] = $this->action_button( 'reoptimize', $post->ID, __( 'Re-optimize', 'pixelkeep' ) ); } else { $actions['pixelkeep_optimize'] = $this->action_button( 'optimize', $post->ID, __( 'Optimize', 'pixelkeep' ) ); } $actions['pixelkeep_details'] = $this->action_button( 'details', $post->ID, __( 'Details', 'pixelkeep' ) ); if ( isset( $this->state->get( $post->ID )['backup'] ) ) { $actions['pixelkeep_restore'] = $this->action_button( 'restore', $post->ID, __( 'Restore', 'pixelkeep' ) ); } return $actions; } /** * Prints the filter dropdown on the list view. * * @return void */ public function render_filter(): void { $screen = get_current_screen(); if ( null === $screen || 'upload' !== $screen->id ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only list filter. $current = isset( $_GET[ self::FILTER_PARAM ] ) ? sanitize_key( wp_unslash( $_GET[ self::FILTER_PARAM ] ) ) : ''; $options = array( '' => __( 'All optimization states', 'pixelkeep' ), 'optimized' => __( 'Optimized', 'pixelkeep' ), 'not_optimized' => __( 'Not optimized', 'pixelkeep' ), 'failed' => __( 'Failed', 'pixelkeep' ), 'webp' => __( 'Has WebP', 'pixelkeep' ), 'avif' => __( 'Has AVIF', 'pixelkeep' ), ); printf( ''; } /** * Applies the filter to the list view's query. * * @param WP_Query $query Query. * @return void */ public function apply_filter( WP_Query $query ): void { if ( ! is_admin() || ! $query->is_main_query() || 'attachment' !== $query->get( 'post_type' ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only list filter. $filter = isset( $_GET[ self::FILTER_PARAM ] ) ? sanitize_key( wp_unslash( $_GET[ self::FILTER_PARAM ] ) ) : ''; $key = AttachmentState::META_STATUS; switch ( $filter ) { case 'optimized': $query->set( 'meta_query', array( array( 'key' => $key, 'value' => array( 'optimized', 'already_optimal' ), 'compare' => 'IN', ), ) ); break; case 'webp': case 'avif': $query->set( 'meta_query', array( array( 'key' => 'webp' === $filter ? AttachmentState::META_HAS_WEBP : AttachmentState::META_HAS_AVIF, 'compare' => 'EXISTS', ), ) ); break; case 'failed': $query->set( 'meta_query', array( array( 'key' => $key, 'value' => 'failed', ), ) ); break; case 'not_optimized': $query->set( 'meta_query', array( 'relation' => 'OR', array( 'key' => $key, 'compare' => 'NOT EXISTS', ), array( 'key' => $key, 'value' => array( 'pending', 'restored', 'skipped' ), 'compare' => 'IN', ), ) ); break; } } /** * Adds the details panel to the attachment modal in grid view. * * @param array $fields Form fields. * @param WP_Post $post Attachment. * @return array */ public function add_details_field( array $fields, WP_Post $post ): array { if ( ! $this->is_supported( $post->ID ) || ! current_user_can( 'edit_post', $post->ID ) ) { return $fields; } $totals = $this->items->attachment_totals( $post->ID ); $saved = $totals['before'] > 0 ? sprintf( /* translators: 1: Size before, 2: Size after, 3: Percentage saved. */ __( '%1$s → %2$s (%3$s saved)', 'pixelkeep' ), Format::bytes( $totals['before'] ), Format::bytes( $totals['after'] ), Format::saved_percent( $totals['before'], $totals['after'] ) ) : ''; $fields['pixelkeep'] = array( 'label' => __( 'PixelKeep', 'pixelkeep' ), 'input' => 'html', 'show_in_edit' => true, 'html' => sprintf( '
%1$s%2$s %3$s
', Format::status_pill( $this->state->status( $post->ID ) ), esc_html( $saved ), $this->action_button( 'details', $post->ID, __( 'Details', 'pixelkeep' ) ) ), ); return $fields; } /** * Adds bulk actions to the list view. * * @param array $actions Bulk actions. * @return array */ public function add_bulk_actions( array $actions ): array { $actions['pixelkeep_optimize'] = __( 'Optimize with PixelKeep', 'pixelkeep' ); $actions['pixelkeep_restore'] = __( 'Restore originals with PixelKeep', 'pixelkeep' ); return $actions; } /** * Queues the selected images. Core has already verified the bulk-action nonce. * * @param string $redirect Redirect URL. * @param string $action Bulk action. * @param int[] $ids Selected attachment IDs. * @return string */ public function handle_bulk_action( $redirect, $action, $ids ) { if ( 'pixelkeep_optimize' !== $action && 'pixelkeep_restore' !== $action ) { return $redirect; } $allowed = array(); foreach ( (array) $ids as $id ) { $id = (int) $id; if ( $this->is_supported( $id ) && current_user_can( 'edit_post', $id ) ) { $allowed[] = $id; } } if ( 'pixelkeep_restore' === $action ) { if ( array() !== $allowed ) { $this->jobs->enqueue_restore( $allowed ); } } else { foreach ( $allowed as $id ) { $this->jobs->enqueue_optimize( $id ); } $this->jobs->kick_queue( true ); } return add_query_arg( 'pixelkeep_queued', count( $allowed ), $redirect ); } /** * Confirms how many images a bulk action queued. * * @return void */ public function render_bulk_notice(): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only; the value is cast to an integer. $queued = isset( $_GET['pixelkeep_queued'] ) ? absint( $_GET['pixelkeep_queued'] ) : 0; $screen = get_current_screen(); if ( $queued <= 0 || null === $screen || 'upload' !== $screen->id ) { return; } printf( '

%s

', esc_html( sprintf( /* translators: %d: Number of images. */ _n( 'PixelKeep queued %d image. It will be processed in the background.', 'PixelKeep queued %d images. They will be processed in the background.', $queued, 'pixelkeep' ), $queued ) ) ); } /** * Whether the plugin handles this attachment. * * @param int $id Attachment ID. * @return bool */ private function is_supported( int $id ): bool { return in_array( get_post_mime_type( $id ), LibraryScanner::SUPPORTED_MIMES, true ); } /** * Builds an action button. * * @param string $action Action name. * @param int $id Attachment ID. * @param string $label Button label. * @return string Safe HTML. */ private function action_button( string $action, int $id, string $label ): string { return sprintf( '', esc_attr( $action ), $id, esc_html( $label ) ); } }