post_content); if ($excerpt !== '') { wp_update_post(['ID' => $post_id, 'post_excerpt' => $excerpt]); self::resolve_in_report($post_id, 'excerpt'); return self::back_url('fixed', ['postlint_fix' => 'excerpt', 'postlint_post' => $post_id, 'check' => $check]); } return self::back_url('nothing'); } if ($fix === 'featured-image') { $attachment_id = isset($raw['attachment_id']) ? absint($raw['attachment_id']) : 0; $settings = Postlint_Settings::get(); $use = $attachment_id > 0 ? $attachment_id : (int) $settings['default_image_id']; if ($use > 0 && get_post($use) && get_post_type($use) === 'attachment') { set_post_thumbnail($post_id, $use); self::resolve_in_report($post_id, 'featured-image'); return self::back_url('fixed', ['postlint_fix' => 'featured-image', 'postlint_post' => $post_id, 'check' => $check]); } return self::back_url('nothing'); } return self::back_url('nothing'); } /** First 55 words of the content, shortcode- and tag-free (WP default excerpt length). */ public static function generate_excerpt($content) { $plain = trim(wp_strip_all_tags(strip_shortcodes((string) $content))); if ($plain === '') { return ''; } $words = preg_split('/\s+/', $plain); return implode(' ', array_slice($words, 0, 55)); } /** * Take one fixed check off one post in the saved report. The report is a stored scan, and * without this a fixed row kept its issue, its button and its count until the next scan. */ public static function resolve_in_report($post_id, $check, $store = null) { $store = $store ?: new Postlint_Store(); $report = $store->read_report(); if (empty($report['issues']) || !is_array($report['issues'])) { return; } $changed = false; foreach ($report['issues'] as $i => $issue) { if ((int) $issue['id'] !== (int) $post_id || !in_array($check, $issue['checks'], true)) { continue; } $report['issues'][$i]['checks'] = array_values(array_diff($issue['checks'], [$check])); if (!$report['issues'][$i]['checks']) { unset($report['issues'][$i]); } $report['totals'][$check] = max(0, (int) ($report['totals'][$check] ?? 0) - 1); $changed = true; } if ($changed) { $report['issues'] = array_values($report['issues']); $store->write_report($report); } } private static function back_url($code, array $extra = []) { $url = 'tools.php?page=postlint-report&postlint_msg=' . rawurlencode($code); foreach ($extra as $key => $value) { if ($value !== '' && $value !== 0) { $url .= '&' . $key . '=' . rawurlencode((string) $value); } } return admin_url($url); } }