is_main_query() || 'aieo_seo' !== $query->get( 'orderby' ) ) { return $clauses; } $type = (string) $query->get( 'post_type' ); if ( ! in_array( $type, self::post_types(), true ) ) { return $clauses; } global $wpdb; $t = $wpdb->prefix . 'aieo_seo_scores'; if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $t ) ) ) !== $t ) { return $clauses; } $order = 'DESC' === strtoupper( (string) $query->get( 'order' ) ) ? 'DESC' : 'ASC'; $clauses['join'] .= $wpdb->prepare( " LEFT JOIN `{$t}` aieo_ss ON aieo_ss.object_type = %s AND aieo_ss.object_id = {$wpdb->posts}.ID", $type ); $clauses['orderby'] = "aieo_ss.score IS NULL, aieo_ss.score {$order}, {$wpdb->posts}.post_date DESC"; return $clauses; } public static function prime( $posts, $query ) { if ( ! is_admin() || empty( $posts ) || ! is_object( $query ) || ! $query->is_main_query() ) { return $posts; } $types = self::post_types(); $ids = array(); foreach ( $posts as $p ) { if ( is_object( $p ) && isset( $p->ID ) && in_array( (string) $p->post_type, $types, true ) ) { $ids[] = (int) $p->ID; } } if ( empty( $ids ) || empty( $types ) ) { return $posts; } global $wpdb; $t = $wpdb->prefix . 'aieo_seo_scores'; $in = implode( ',', array_map( 'intval', $ids ) ); $type_in = "'" . implode( "','", array_map( 'esc_sql', $types ) ) . "'"; $rows = $wpdb->get_results( "SELECT object_id, score, fix_count, is_pillar FROM `{$t}` WHERE object_type IN ({$type_in}) AND object_id IN ({$in})", ARRAY_A ); foreach ( (array) $rows as $r ) { self::$cache[ (int) $r['object_id'] ] = $r; } return $posts; } public static function render_column( $col, $post_id ) { if ( 'aieo_seo' !== $col ) { return; } $post_id = (int) $post_id; if ( ! isset( self::$cache[ $post_id ] ) ) { echo '—'; return; } $r = self::$cache[ $post_id ]; $score = (int) $r['score']; $fix = (int) ( $r['fix_count'] ?? 0 ); $color = $score >= 80 ? '#15803d' : ( $score >= 50 ? '#b45309' : '#b91c1c' ); $out = '' . $score . ''; if ( $fix > 0 ) { $out .= ' (' . $fix . ')'; } if ( ! empty( $r['is_pillar'] ) ) { $out .= ' ★'; } echo $out; } }