engines = $engines; $this->converter = $converter; add_action( 'admin_menu', array( $this, 'menu' ) ); add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); add_filter( 'manage_media_columns', array( $this, 'media_columns' ) ); add_action( 'manage_media_custom_column', array( $this, 'media_column_content' ), 10, 2 ); add_filter( 'media_row_actions', array( $this, 'media_row_actions' ), 10, 2 ); add_action( 'admin_notices', array( $this, 'notices' ) ); add_action( 'admin_post_adee_recalc', array( $this, 'handle_recalc' ) ); add_action( 'admin_post_adee_purge_cache', array( $this, 'handle_purge_cache' ) ); add_action( 'admin_post_adee_restore_all', array( $this, 'handle_restore_all' ) ); add_action( 'wp_ajax_adee_serve_test', array( $this, 'ajax_serve_test' ) ); } /** * Register menu page. */ public function menu() { add_menu_page( __( 'ADee - WebP & AVIF Converter', 'adee-webp-avif-converter' ), __( 'ADee', 'adee-webp-avif-converter' ), 'manage_options', self::PAGE_SLUG, array( $this, 'render_page' ), 'dashicons-format-image', 58 ); } /** * Enqueue assets on plugin pages + media library. * * @param string $hook Current admin page hook. */ public function assets( $hook ) { $on_ours = ( false !== strpos( (string) $hook, self::PAGE_SLUG ) ); $on_media = ( 'upload.php' === $hook || 'post.php' === $hook || 'post-new.php' === $hook ); if ( ! $on_ours && ! $on_media ) { return; } wp_enqueue_style( 'adee-admin', ADEE_URL . 'assets/css/admin.css', array(), ADEE_VERSION ); wp_enqueue_script( 'adee-admin', ADEE_URL . 'assets/js/admin.js', array( 'jquery' ), ADEE_VERSION, true ); wp_localize_script( 'adee-admin', 'ADEE', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'adee_bulk' ), ) ); } /** * Settings API registration. */ public function register_settings() { register_setting( 'adee_settings_group', 'adee_settings', array( 'sanitize_callback' => array( $this, 'sanitize_settings' ) ) ); } /** * Sanitize settings input. * * @param array $input Raw input. * @return array */ public function sanitize_settings( $input ) { $defaults = ADEE_Plugin::default_settings(); $input = is_array( $input ) ? $input : array(); $allowed_engines = array( 'auto', 'gd', 'imagick', 'cli' ); $engine_webp = isset( $input['engine_webp'] ) ? $input['engine_webp'] : 'auto'; $engine_avif = isset( $input['engine_avif'] ) ? $input['engine_avif'] : 'auto'; return array( 'auto_convert' => empty( $input['auto_convert'] ) ? 0 : 1, 'convert_webp' => empty( $input['convert_webp'] ) ? 0 : 1, 'convert_avif' => empty( $input['convert_avif'] ) ? 0 : 1, 'webp_quality' => isset( $input['webp_quality'] ) ? min( 100, max( 40, (int) $input['webp_quality'] ) ) : (int) $defaults['webp_quality'], 'avif_quality' => isset( $input['avif_quality'] ) ? min( 90, max( 30, (int) $input['avif_quality'] ) ) : (int) $defaults['avif_quality'], 'serve' => empty( $input['serve'] ) ? 0 : 1, 'serve_mode' => ( isset( $input['serve_mode'] ) && in_array( $input['serve_mode'], array( 'picture', 'rewrite', 'swap' ), true ) ) ? $input['serve_mode'] : 'picture', 'prefer_avif' => empty( $input['prefer_avif'] ) ? 0 : 1, 'batch_size' => isset( $input['batch_size'] ) ? min( 10, max( 1, (int) $input['batch_size'] ) ) : (int) $defaults['batch_size'], 'engine_webp' => in_array( $engine_webp, $allowed_engines, true ) ? $engine_webp : 'auto', 'engine_avif' => in_array( $engine_avif, $allowed_engines, true ) ? $engine_avif : 'auto', 'cwebp_path' => isset( $input['cwebp_path'] ) ? sanitize_text_field( $input['cwebp_path'] ) : '', 'avifenc_path' => isset( $input['avifenc_path'] ) ? sanitize_text_field( $input['avifenc_path'] ) : '', ); } /** * Render the plugin admin page (tabbed). */ public function render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'dashboard'; // phpcs:ignore WordPress.Security.NonceVerification $tabs = array( 'dashboard' => __( 'Dashboard', 'adee-webp-avif-converter' ), 'settings' => __( 'Settings', 'adee-webp-avif-converter' ), 'help' => __( 'Help', 'adee-webp-avif-converter' ), ); if ( ! isset( $tabs[ $tab ] ) ) { $tab = 'dashboard'; } ?>

render_settings(); } elseif ( 'help' === $tab ) { $this->render_help(); } else { $this->render_dashboard(); } ?>
converter->stats(); $detect = $this->engines->detect(); $converted = max( 0, (int) $stats['src_bytes'] - (int) $stats['out_bytes'] ); $percent = $stats['src_bytes'] ? round( 100 - ( 100 * $stats['out_bytes'] / max( 1, $stats['src_bytes'] ) ), 1 ) : 0; $has_webp = ( $detect['gd_webp'] || $detect['imagick_webp'] || $detect['cli_cwebp'] ); $has_avif = ( $detect['gd_avif'] || $detect['imagick_avif'] || $detect['cli_avifenc'] ); $flyn_active = class_exists( 'FlynCache' ); ?>

PHP GD — WebP badge( $detect['gd_webp'], __( 'Available', 'adee-webp-avif-converter' ), __( 'Not available', 'adee-webp-avif-converter' ) ); ?>
PHP GD — AVIF badge( $detect['gd_avif'], __( 'Available', 'adee-webp-avif-converter' ), __( 'Not available', 'adee-webp-avif-converter' ) ); ?>
Imagick — WebP badge( $detect['imagick_webp'], __( 'Available', 'adee-webp-avif-converter' ), __( 'Not available', 'adee-webp-avif-converter' ) ); ?>
Imagick — AVIF badge( $detect['imagick_avif'], __( 'Available', 'adee-webp-avif-converter' ), __( 'Not available', 'adee-webp-avif-converter' ) ); ?>
cwebp binary (CLI)
avifenc binary (CLI)

engines->detect(); ?>
engines->available_for( $format ); $options = array( 'auto' => __( 'Auto (recommended)', 'adee-webp-avif-converter' ), 'gd' => __( 'PHP GD', 'adee-webp-avif-converter' ), 'imagick' => __( 'Imagick', 'adee-webp-avif-converter' ), 'cli' => __( 'CLI binary (cwebp/avifenc)', 'adee-webp-avif-converter' ), ); foreach ( $options as $value => $label ) { if ( 'auto' !== $value && ! in_array( $value, $available, true ) ) { continue; } printf( '', esc_attr( $value ), selected( $selected, $value, false ), esc_html( $label ) ); } } /** * Help tab. */ private function render_help() { ?>

— element with WebP/AVIF sources. Works on every hosting, needs no server configuration, and is safe with every page cache because the HTML is identical for all visitors. The browser picks the best format itself.', 'adee-webp-avif-converter' ); ?>

—

—

# /etc/nginx/conf.d/adee-serve.conf
map $http_accept $adee_img_ext {
    default '';
    '~*image/avif' '.avif';
    '~*image/webp' '.webp';
}

# inside each server { } block of your site:
location ~* ^(?<adee_img_path>/wp-content/uploads/.+)\.(jpe?g|png|gif)$ {
    expires 30d;
    access_log off;
    add_header Vary Accept;
    try_files $adee_img_path$adee_img_ext $uri =404;
}

wp adee status
wp adee convert all --limit=50 --skip-existing
wp adee convert 123 124
wp adee restore all --yes

Wasim Akram
www.digitalwasim.com
%s', esc_html( $on ) ); } else { printf( '%s', esc_html( $off ) ); } } /** * Media library column header. * * @param array $columns Columns. * @return array */ public function media_columns( $columns ) { $columns['adee_status'] = __( 'WebP/AVIF', 'adee-webp-avif-converter' ); return $columns; } /** * Media library column content. * * @param string $column Column key. * @param int $attachment_id Attachment ID. */ public function media_column_content( $column, $attachment_id ) { if ( 'adee_status' !== $column ) { return; } if ( ! wp_attachment_is_image( $attachment_id ) ) { echo '—'; return; } $variants = $this->converter->get_variants( $attachment_id ); if ( empty( $variants ) ) { echo '' . esc_html__( 'Original only', 'adee-webp-avif-converter' ) . ''; return; } $has_webp = false; $has_avif = false; foreach ( $variants as $formats ) { if ( isset( $formats['webp'] ) ) { $has_webp = true; } if ( isset( $formats['avif'] ) ) { $has_avif = true; } } if ( $has_webp ) { echo 'WebP '; } if ( $has_avif ) { echo 'AVIF'; } } /** * Row actions in the media list. * * @param array $actions Row actions. * @param WP_Post $post Attachment post. * @return array */ public function media_row_actions( $actions, $post ) { if ( ! wp_attachment_is_image( $post->ID ) ) { return $actions; } $url_base = admin_url( 'admin-post.php' ); if ( $this->converter->get_variants( $post->ID ) ) { $actions['adee_restore'] = sprintf( '%s', esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'adee_restore_single', 'id' => $post->ID ), $url_base ), 'adee_restore_' . $post->ID ) ), esc_html__( 'Restore original', 'adee-webp-avif-converter' ) ); } else { $actions['adee_convert'] = sprintf( '%s', esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'adee_convert_single', 'id' => $post->ID ), $url_base ), 'adee_convert_' . $post->ID ) ), esc_html__( 'Convert to WebP/AVIF', 'adee-webp-avif-converter' ) ); } return $actions; } /** * Admin notices for convert/restore results. */ public function notices() { // phpcs:disable WordPress.Security.NonceVerification -- read-only notices. if ( isset( $_GET['adee_bulk'] ) ) { $ok = isset( $_GET['adee_ok'] ) ? (int) $_GET['adee_ok'] : 0; $skip = isset( $_GET['adee_skip'] ) ? (int) $_GET['adee_skip'] : 0; $err = isset( $_GET['adee_err'] ) ? (int) $_GET['adee_err'] : 0; printf( '

%s

', esc_html( sprintf( /* translators: 1: converted, 2: skipped, 3: errors */ __( 'Bulk conversion finished: %1$d converted, %2$d skipped, %3$d errors.', 'adee-webp-avif-converter' ), $ok, $skip, $err ) ) ); } if ( isset( $_GET['adee_single'] ) && isset( $_GET['adee_id'] ) ) { $code = (int) $_GET['adee_single']; $id = (int) $_GET['adee_id']; $state = ( 1 === $code ) ? 'success' : 'warning'; $labels = array( /* translators: %d: attachment ID. */ 1 => __( 'Image #%d converted to WebP/AVIF.', 'adee-webp-avif-converter' ), /* translators: %d: attachment ID. */ 2 => __( 'Image #%d could not be converted (no engine, or variant was larger than the original).', 'adee-webp-avif-converter' ), /* translators: %d: attachment ID. */ 3 => __( 'Variants for image #%d removed — original restored.', 'adee-webp-avif-converter' ), /* translators: %d: attachment ID. */ 4 => __( 'Nothing to restore for image #%d.', 'adee-webp-avif-converter' ), ); $text = isset( $labels[ $code ] ) ? sprintf( $labels[ $code ], $id ) : sprintf( /* translators: %d: attachment ID. */ __( 'Done for image #%d.', 'adee-webp-avif-converter' ), $id ); printf( '

%2$s

', esc_attr( $state ), esc_html( $text ) ); } if ( isset( $_GET['adee_recalc'] ) ) { $stats = $this->converter->stats(); printf( '

%s

', esc_html( sprintf( /* translators: 1: variant count, 2: original size, 3: converted size */ __( 'Stats recalculated: %1$d variant files, originals %2$s, converted %3$s.', 'adee-webp-avif-converter' ), (int) $stats['variants'], ADEE_Converter::human_bytes( $stats['src_bytes'] ), ADEE_Converter::human_bytes( $stats['out_bytes'] ) ) ) ); } if ( isset( $_GET['adee_restored'] ) ) { $count = (int) $_GET['adee_restored']; printf( '

%s

', esc_html( sprintf( /* translators: %d: number of attachments */ __( 'Restored %d attachments — run again if more remain.', 'adee-webp-avif-converter' ), $count ) ) ); } if ( isset( $_GET['adee_purged'] ) ) { $ok = (int) $_GET['adee_purged']; printf( '

%2$s

', $ok ? 'success' : 'warning', esc_html( $ok ? __( 'Page cache purged.', 'adee-webp-avif-converter' ) : __( 'Could not purge the page cache.', 'adee-webp-avif-converter' ) ) ); } // phpcs:enable } /** * AJAX: serving test — requests a converted image with an AVIF Accept header * and reports whether the server serves the variant directly. */ public function ajax_serve_test() { check_ajax_referer( 'adee_bulk', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'forbidden' ), 403 ); } // Find a converted attachment with a full-size variant. $test = null; foreach ( ADEE_Bulk::eligible_ids() as $id ) { $variants = $this->converter->get_variants( $id ); if ( ! empty( $variants['full'] ) ) { $test = $variants['full']; $test_id = $id; break; } } if ( ! $test ) { wp_send_json_error( array( 'message' => 'no_test_image' ) ); } $upload = wp_get_upload_dir(); $url = ''; if ( ! empty( $test['avif']['file'] ) ) { $url = $upload['baseurl'] . '/' . $test['avif']['file']; } elseif ( ! empty( $test['webp']['file'] ) ) { $url = $upload['baseurl'] . '/' . $test['webp']['file']; } if ( ! $url ) { wp_send_json_error( array( 'message' => 'no_test_image' ) ); } // Ask for the ORIGINAL image with an AVIF-capable Accept header. $orig_url = preg_replace( '/\.(avif|webp)$/i', '', $url ); $res = wp_safe_remote_get( $orig_url, array( 'timeout' => 10, 'redirection' => 2, 'headers' => array( 'Accept' => 'image/avif,image/webp,image/*,*/*;q=0.8' ), ) ); if ( is_wp_error( $res ) ) { wp_send_json_error( array( 'message' => 'request_failed', 'error' => $res->get_error_message(), ) ); } $code = (int) wp_remote_retrieve_response_code( $res ); $ctype = (string) wp_remote_retrieve_header( $res, 'content-type' ); $vary = (string) wp_remote_retrieve_header( $res, 'vary' ); if ( false !== stripos( $ctype, 'image/avif' ) ) { $status = 'rewrite'; $label = __( 'Server rewrite is active: the original URL already serves AVIF to supporting browsers. Best performance — keep the “Server rewrite” mode.', 'adee-webp-avif-converter' ); } elseif ( false !== stripos( $ctype, 'image/webp' ) ) { $status = 'rewrite'; $label = __( 'Server rewrite is active: the original URL already serves WebP to supporting browsers. Best performance — keep the “Server rewrite” mode.', 'adee-webp-avif-converter' ); } else { $status = 'none'; $label = __( 'No server rewrite detected. No problem — the “Picture tag” mode (default) serves next-gen images without any server configuration.', 'adee-webp-avif-converter' ); } wp_send_json_success( array( 'status' => $status, 'label' => $label, 'http' => $code, 'served' => $ctype, 'vary' => $vary, ) ); } /** * Recalculate stats action. */ public function handle_recalc() { check_admin_referer( 'adee_recalc' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'adee-webp-avif-converter' ) ); } $this->converter->recalculate_stats(); wp_safe_redirect( add_query_arg( 'adee_recalc', 1, admin_url( 'admin.php?page=' . self::PAGE_SLUG ) ) ); exit; } /** * Purge page cache via FlynCache when present. */ public function handle_purge_cache() { check_admin_referer( 'adee_purge_cache' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'adee-webp-avif-converter' ) ); } $ok = 0; if ( class_exists( 'FlynCache' ) && method_exists( 'FlynCache', 'get_instance' ) ) { try { FlynCache::get_instance()->purge_all( 'adee' ); $ok = 1; } catch ( Exception $e ) { $ok = 0; } } wp_safe_redirect( add_query_arg( 'adee_purged', $ok, admin_url( 'admin.php?page=' . self::PAGE_SLUG ) ) ); exit; } /** * Restore-all action. */ public function handle_restore_all() { check_admin_referer( 'adee_restore_all' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'adee-webp-avif-converter' ) ); } $yes = isset( $_GET['yes'] ) ? (int) $_GET['yes'] : 0; if ( ! $yes ) { wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE_SLUG ) ); exit; } $count = $this->converter->restore_all( 200 ); wp_safe_redirect( add_query_arg( 'adee_restored', $count, admin_url( 'admin.php?page=' . self::PAGE_SLUG ) ) ); exit; } }