'OR', array( 'key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS', ), array( 'key' => '_wp_attachment_image_alt', 'value' => '', 'compare' => '=', ), ); } $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => min( 100, max( 1, absint( $per_page ) ) ), 'paged' => max( 1, absint( $paged ) ), 'orderby' => 'date', 'order' => 'DESC', 's' => sanitize_text_field( $search ), 'ignore_sticky_posts' => true, 'update_post_term_cache' => false, ); $author_scope = $this->get_author_scope(); if ( $author_scope ) { $args['author'] = $author_scope; } if ( ! empty( $meta_query ) ) { $args['meta_query'] = $meta_query; } return new WP_Query( $args ); } private function count_images( $missing_only = false ) { $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 1, 'paged' => 1, 'fields' => 'ids', 'orderby' => 'ID', 'order' => 'DESC', 'ignore_sticky_posts' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ); $author_scope = $this->get_author_scope(); if ( $author_scope ) { $args['author'] = $author_scope; } if ( $missing_only ) { $args['meta_query'] = array( 'relation' => 'OR', array( 'key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS', ), array( 'key' => '_wp_attachment_image_alt', 'value' => '', 'compare' => '=', ), ); } $query = new WP_Query( $args ); return (int) $query->found_posts; } private function normalize_view( $view ) { return in_array( $view, array( 'missing', 'all' ), true ) ? $view : 'missing'; } private function get_return_url_from_request() { $view = isset( $_POST['return_view'] ) ? sanitize_key( wp_unslash( $_POST['return_view'] ) ) : 'missing'; $paged = isset( $_POST['return_paged'] ) ? max( 1, absint( $_POST['return_paged'] ) ) : 1; $search = isset( $_POST['return_s'] ) ? sanitize_text_field( wp_unslash( $_POST['return_s'] ) ) : ''; $view = $this->normalize_view( $view ); $args = array( 'page' => '3mpstudio-alt-finder', 'view' => $view, ); if ( $paged > 1 ) { $args['paged'] = $paged; } if ( '' !== $search ) { $args['s'] = $search; } return add_query_arg( $args, admin_url( 'upload.php' ) ); } private function redirect_with_error( $code ) { $allowed = array( 'invalid', 'forbidden', 'request', 'too_long', 'save_failed' ); $code = in_array( $code, $allowed, true ) ? $code : 'invalid'; wp_safe_redirect( add_query_arg( 'alt_error', $code, $this->get_return_url_from_request() ) ); exit; } public function save_alt_text() { if ( 'POST' !== strtoupper( isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : '' ) ) { $this->redirect_with_error( 'request' ); } if ( ! current_user_can( 'upload_files' ) ) { wp_die( esc_html__( 'You do not have permission to edit image ALT text.', '3mpstudio-alt-finder' ), esc_html__( 'Permission denied', '3mpstudio-alt-finder' ), array( 'response' => 403 ) ); } check_admin_referer( self::NONCE_ACTION, self::NONCE_NAME ); $attachment_id = isset( $_POST['attachment_id'] ) ? absint( $_POST['attachment_id'] ) : 0; if ( ! $attachment_id || 'attachment' !== get_post_type( $attachment_id ) || ! wp_attachment_is_image( $attachment_id ) ) { $this->redirect_with_error( 'invalid' ); } if ( ! current_user_can( 'edit_post', $attachment_id ) ) { $this->redirect_with_error( 'forbidden' ); } $raw_alt = isset( $_POST['alt_text'] ) ? wp_unslash( $_POST['alt_text'] ) : ''; if ( ! is_string( $raw_alt ) ) { $this->redirect_with_error( 'invalid' ); } $alt_text = sanitize_text_field( $raw_alt ); $length = function_exists( 'mb_strlen' ) ? mb_strlen( $alt_text ) : strlen( $alt_text ); if ( $length > self::MAX_ALT_LENGTH ) { $this->redirect_with_error( 'too_long' ); } $existing = (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( $existing !== $alt_text ) { $updated = update_post_meta( $attachment_id, '_wp_attachment_image_alt', $alt_text ); if ( false === $updated ) { $this->redirect_with_error( 'save_failed' ); } } wp_safe_redirect( add_query_arg( 'alt_updated', $attachment_id, $this->get_return_url_from_request() ) ); exit; } public function render_admin_page() { if ( ! current_user_can( 'upload_files' ) ) { wp_die( esc_html__( 'You do not have permission to access ALT Finder.', '3mpstudio-alt-finder' ), esc_html__( 'Permission denied', '3mpstudio-alt-finder' ), array( 'response' => 403 ) ); } $view = isset( $_GET['view'] ) ? sanitize_key( wp_unslash( $_GET['view'] ) ) : 'missing'; $view = $this->normalize_view( $view ); $missing_only = 'missing' === $view; $paged = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1; $search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; $query = $this->get_image_attachments( $missing_only, $paged, self::PER_PAGE, $search ); $missing_count = $this->count_images( true ); $all_count = $this->count_images( false ); $coverage = $all_count > 0 ? round( ( ( $all_count - $missing_count ) / $all_count ) * 100 ) : 100; $current_url_args = array( 'page' => '3mpstudio-alt-finder', 'view' => $view, ); if ( $paged > 1 ) { $current_url_args['paged'] = $paged; } if ( '' !== $search ) { $current_url_args['s'] = $search; } $current_url = add_query_arg( $current_url_args, admin_url( 'upload.php' ) ); $notice_error = isset( $_GET['alt_error'] ) ? sanitize_key( wp_unslash( $_GET['alt_error'] ) ) : ''; $error_messages = array( 'invalid' => __( 'The request was invalid. No ALT text was changed.', '3mpstudio-alt-finder' ), 'forbidden' => __( 'You do not have permission to edit that image.', '3mpstudio-alt-finder' ), 'request' => __( 'The save request was not accepted.', '3mpstudio-alt-finder' ), 'too_long' => sprintf( __( 'ALT text must be %d characters or fewer.', '3mpstudio-alt-finder' ), self::MAX_ALT_LENGTH ), 'save_failed' => __( 'WordPress could not save the ALT text. Please try again.', '3mpstudio-alt-finder' ), ); ?>