get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value LIKE %s LIMIT 50", '%' . $wpdb->esc_like( $stem ) . '%' ) ); $ids = array(); foreach ( $rows as $row ) { if ( alexibility_fixes_wp_stem( alexibility_fixes_file_name_of( $row->meta_value ) ) === $stem ) { $ids[] = (int) $row->post_id; } } return $ids; } /** Saved content that contains this text, in every post type that can hold the problem (including block theme templates and parts). */ private static function find_posts( $term ) { global $wpdb; // WP_Query's search splits the term into words and also matches titles, so an exact match in saved content needs SQL. // An admin applying fixes needs current rows, not cached ones. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type NOT IN ('revision','attachment','nav_menu_item','customize_changeset','oembed_cache','wp_global_styles') AND post_status IN ('publish','private','future','draft','pending') AND post_content LIKE %s LIMIT 200", '%' . $wpdb->esc_like( $term ) . '%' ) ); return array_map( 'intval', $ids ); } /** * Works out every change without writing anything. * * @return array{ results: array, media: array, posts: array } */ public static function plan_changes( $fixes ) { $results = array(); $media = array(); $posts = array(); $load = function ( $id ) use ( &$posts ) { if ( ! isset( $posts[ $id ] ) ) { $content = (string) get_post_field( 'post_content', $id, 'raw' ); $posts[ $id ] = array( 'before' => $content, 'content' => $content, 'fixes' => array() ); } }; foreach ( $fixes as $fix ) { $where = array(); if ( 'image-alt' === $fix['kind'] ) { foreach ( self::find_attachments( $fix['stem'] ) as $id ) { if ( get_post_meta( $id, '_wp_attachment_image_alt', true ) !== $fix['alt'] ) { $media[ $id ] = array( 'alt' => $fix['alt'], 'fix' => $fix['id'] ); /* translators: %d: attachment ID */ $where[] = sprintf( __( 'media library item #%d', 'alexibility-fixes' ), $id ); } } foreach ( self::find_posts( $fix['stem'] ) as $id ) { $load( $id ); list( $html, $n ) = alexibility_fixes_add_img_alt( $posts[ $id ]['content'], array( $fix['stem'] => $fix['alt'] ), empty( $fix['replace_empty'] ) ? array() : array( $fix['stem'] ) ); if ( $n > 0 ) { $posts[ $id ]['content'] = $html; $posts[ $id ]['fixes'][] = $fix['id']; $where[] = get_the_title( $id ) ? get_the_title( $id ) : '#' . $id; } } } elseif ( 'accessible-name' === $fix['kind'] ) { $term = $fix['criteria']['href'] ?? $fix['criteria']['id'] ?? $fix['criteria']['name'] ?? reset( $fix['criteria'] ); foreach ( self::find_posts( (string) $term ) as $id ) { $load( $id ); list( $html, $n ) = empty( $fix['decorative'] ) ? alexibility_fixes_add_aria_label( $posts[ $id ]['content'], $fix['tag'], $fix['criteria'], $fix['name'] ) : alexibility_fixes_hide_graphic( $posts[ $id ]['content'], $fix['tag'], $fix['criteria'] ); if ( $n > 0 ) { $posts[ $id ]['content'] = $html; $posts[ $id ]['fixes'][] = $fix['id']; $where[] = get_the_title( $id ) ? get_the_title( $id ) : '#' . $id; } } } $results[ $fix['id'] ] = $where; } $posts = array_filter( $posts, function ( $p ) { return $p['content'] !== $p['before']; } ); return array( 'results' => $results, 'media' => $media, 'posts' => $posts ); } /** Applies the content fixes and records the run. */ public static function apply( $fixes ) { $planned = self::plan_changes( $fixes ); $changes = array(); $failed = array(); $failed_fixes = array(); foreach ( $planned['media'] as $id => $m ) { $before = (string) get_post_meta( $id, '_wp_attachment_image_alt', true ); update_post_meta( $id, '_wp_attachment_image_alt', $m['alt'] ); $changes[] = array( 'type' => 'media_alt', 'id' => $id, 'before' => $before, 'after' => $m['alt'] ); } foreach ( $planned['posts'] as $id => $p ) { // Someone may have saved this page since it was read; never overwrite their work. if ( (string) get_post_field( 'post_content', $id, 'raw' ) !== $p['before'] ) { $failed[ $id ] = __( 'edited by someone else meanwhile; apply again', 'alexibility-fixes' ); $failed_fixes = array_merge( $failed_fixes, $p['fixes'] ); continue; } $saved = wp_update_post( wp_slash( array( 'ID' => $id, 'post_content' => $p['content'] ) ), true ); if ( is_wp_error( $saved ) ) { $failed[ $id ] = $saved->get_error_message(); $failed_fixes = array_merge( $failed_fixes, $p['fixes'] ); continue; } $changes[] = array( 'type' => 'post_content', 'id' => $id, 'before' => $p['before'], 'after' => (string) get_post_field( 'post_content', $id, 'raw' ) ); } $run = array( 'id' => gmdate( 'YmdHis' ) . wp_rand( 100, 999 ), 'time' => current_time( 'mysql' ), 'user' => get_current_user_id(), 'changes' => $changes, 'failed' => $failed, 'results' => $planned['results'], // The fixes whose page couldn't be saved, so the workbench can say which ones didn't land. 'failed_fixes' => array_values( array_unique( $failed_fixes ) ), ); add_option( 'alexibility_fixes_run_' . $run['id'], $run, '', false ); $runs = get_option( self::RUNS, array() ); array_unshift( $runs, array( 'id' => $run['id'], 'time' => $run['time'], 'changes' => count( $changes ), 'rolled_back' => false ) ); update_option( self::RUNS, $runs, false ); return $run; } /** Restores a run's changes, newest first, skipping anything edited since. */ public static function rollback( $run_id ) { $run = get_option( 'alexibility_fixes_run_' . preg_replace( '/\D/', '', $run_id ) ); if ( ! is_array( $run ) ) { return new WP_Error( 'alexibility_fixes_no_run', __( 'That run was not found.', 'alexibility-fixes' ) ); } $restored = 0; $skipped = array(); foreach ( array_reverse( $run['changes'] ) as $c ) { if ( 'media_alt' === $c['type'] ) { if ( (string) get_post_meta( $c['id'], '_wp_attachment_image_alt', true ) !== $c['after'] ) { /* translators: %d: attachment ID */ $skipped[] = sprintf( __( 'media #%d (changed since)', 'alexibility-fixes' ), $c['id'] ); continue; } update_post_meta( $c['id'], '_wp_attachment_image_alt', $c['before'] ); } else { if ( (string) get_post_field( 'post_content', $c['id'], 'raw' ) !== $c['after'] ) { /* translators: %s: post title */ $skipped[] = sprintf( __( '"%s" (edited since; restore it from its revisions)', 'alexibility-fixes' ), get_the_title( $c['id'] ) ); continue; } wp_update_post( wp_slash( array( 'ID' => $c['id'], 'post_content' => $c['before'] ) ) ); } ++$restored; } $runs = get_option( self::RUNS, array() ); foreach ( $runs as &$r ) { if ( $r['id'] === $run['id'] ) { $r['rolled_back'] = true; } } update_option( self::RUNS, $runs, false ); return array( 'restored' => $restored, 'skipped' => $skipped ); } }