converter = $converter; add_action( 'wp_ajax_adee_bulk_count', array( $this, 'ajax_count' ) ); add_action( 'wp_ajax_adee_bulk_step', array( $this, 'ajax_step' ) ); add_filter( 'bulk_actions-upload', array( $this, 'bulk_actions' ) ); add_filter( 'handle_bulk_actions-upload-edit', array( $this, 'handle_bulk_action' ), 10, 3 ); add_action( 'admin_post_adee_convert_single', array( $this, 'handle_single_convert' ) ); add_action( 'admin_post_adee_restore_single', array( $this, 'handle_single_restore' ) ); } /** * All convertible attachment IDs. * * @return int[] */ public static function eligible_ids() { return get_posts( array( 'post_type' => 'attachment', 'post_mime_type' => array( 'image/jpeg', 'image/png', 'image/gif' ), 'post_status' => 'inherit', 'fields' => 'ids', 'posts_per_page' => -1, 'no_found_rows' => true, 'orderby' => 'ID', 'order' => 'ASC', ) ); } /** * AJAX: count eligible attachments and snapshot the ID list. */ public function ajax_count() { check_ajax_referer( 'adee_bulk', 'nonce' ); if ( ! current_user_can( 'upload_files' ) ) { wp_send_json_error( array( 'message' => 'forbidden' ), 403 ); } $ids = self::eligible_ids(); $converted = 0; foreach ( $ids as $id ) { if ( $this->converter->get_variants( $id ) ) { $converted++; } } set_transient( 'adee_bulk_ids', $ids, HOUR_IN_SECONDS ); wp_send_json_success( array( 'total' => count( $ids ), 'converted' => $converted, 'batch' => max( 1, min( 10, (int) ADEE_Plugin::get_setting( 'batch_size' ) ) ), ) ); } /** * AJAX: process one batch page. */ public function ajax_step() { check_ajax_referer( 'adee_bulk', 'nonce' ); if ( ! current_user_can( 'upload_files' ) ) { wp_send_json_error( array( 'message' => 'forbidden' ), 403 ); } $ids = get_transient( 'adee_bulk_ids' ); if ( ! is_array( $ids ) || empty( $ids ) ) { $ids = self::eligible_ids(); } $page = isset( $_POST['page'] ) ? max( 1, (int) $_POST['page'] ) : 1; $batch = isset( $_POST['batch'] ) ? max( 1, min( 10, (int) $_POST['batch'] ) ) : 3; $skip = ! empty( $_POST['skip_existing'] ); $total = count( $ids ); $pages = max( 1, (int) ceil( $total / $batch ) ); $offset = ( $page - 1 ) * $batch; $slice = array_slice( $ids, $offset, $batch ); $results = array( 'ok' => 0, 'skipped' => 0, 'errors' => 0, 'bytes_saved' => 0, 'log' => array(), ); foreach ( $slice as $id ) { $res = $this->converter->convert_attachment( (int) $id, array( 'skip_existing' => $skip ) ); if ( 'ok' === $res['status'] ) { $results['ok']++; foreach ( $res['variants'] as $formats ) { foreach ( $formats as $data ) { $results['bytes_saved'] += max( 0, (int) $data['src'] - (int) $data['bytes'] ); } } $results['log'][] = '#' . $id . ' → converted'; } elseif ( 'skipped' === $res['status'] ) { $results['skipped']++; } else { $results['errors']++; $results['log'][] = '#' . $id . ' → error: ' . ( isset( $res['reason'] ) ? $res['reason'] : 'unknown' ); } } wp_send_json_success( array( 'page' => $page, 'pages' => $pages, 'total' => $total, 'done' => $page >= $pages, 'results' => $results, ) ); } /** * Add media list bulk action. * * @param array $actions Bulk actions. * @return array */ public function bulk_actions( $actions ) { $actions['adee_convert'] = __( 'Convert to WebP/AVIF', 'adee-webp-avif-converter' ); return $actions; } /** * Handle media list bulk action (max 20 inline). * * @param string $redirect_to Redirect URL. * @param string $doaction Action slug. * @param array $post_ids Selected IDs. * @return string */ public function handle_bulk_action( $redirect_to, $doaction, $post_ids ) { if ( 'adee_convert' !== $doaction ) { return $redirect_to; } $ok = 0; $skip = 0; $err = 0; foreach ( array_slice( (array) $post_ids, 0, 20 ) as $id ) { $res = $this->converter->convert_attachment( (int) $id, array( 'skip_existing' => true ) ); if ( 'ok' === $res['status'] ) { $ok++; } elseif ( 'skipped' === $res['status'] ) { $skip++; } else { $err++; } } return add_query_arg( array( 'adee_bulk' => 1, 'adee_ok' => $ok, 'adee_skip' => $skip, 'adee_err' => $err, ), $redirect_to ); } /** * Single-image convert action from media list row. */ public function handle_single_convert() { $id = isset( $_GET['id'] ) ? (int) $_GET['id'] : 0; check_admin_referer( 'adee_convert_' . $id ); if ( ! current_user_can( 'upload_files' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'adee-webp-avif-converter' ) ); } $res = $this->converter->convert_attachment( $id ); $back = wp_get_referer() ? wp_get_referer() : admin_url( 'upload.php' ); $back = remove_query_arg( array( 'adee_single', 'adee_id' ), $back ); wp_safe_redirect( add_query_arg( array( 'adee_single' => ( 'ok' === $res['status'] ) ? 1 : 2, 'adee_id' => $id, ), $back ) ); exit; } /** * Single-image restore action from media list row. */ public function handle_single_restore() { $id = isset( $_GET['id'] ) ? (int) $_GET['id'] : 0; check_admin_referer( 'adee_restore_' . $id ); if ( ! current_user_can( 'upload_files' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'adee-webp-avif-converter' ) ); } $done = $this->converter->restore_attachment( $id ); $back = wp_get_referer() ? wp_get_referer() : admin_url( 'upload.php' ); $back = remove_query_arg( array( 'adee_single', 'adee_id' ), $back ); wp_safe_redirect( add_query_arg( array( 'adee_single' => $done ? 3 : 4, 'adee_id' => $id, ), $back ) ); exit; } }