/
www
/
wwwroot
/
fxdst.com
/
wp-content
/
mu-plugins
/
Upload File
HOME
<?php /** * Plugin Name: FXDST Article Guard * Description: Keeps blog article layout, tables, images, update notes and SEO schema stable even if the active theme CSS is overwritten. */ if (!defined('ABSPATH')) { exit; } function fxdst_article_guard_is_post() { return !is_admin() && is_singular('post'); } function fxdst_article_guard_adjacent_card($post, $label, $empty_text, $class_name) { if ($post instanceof WP_Post) { return sprintf( '<a class="fxdst-post-nav-card %s" href="%s"><span>%s</span><strong>%s</strong></a>', esc_attr($class_name), esc_url(get_permalink($post)), esc_html($label), esc_html(get_the_title($post)) ); } return sprintf( '<div class="fxdst-post-nav-card fxdst-post-nav-empty %s"><span>%s</span><strong>%s</strong></div>', esc_attr($class_name), esc_html($label), esc_html($empty_text) ); } function fxdst_article_guard_post_nav() { $previous = get_previous_post(); $next = get_next_post(); return '<nav class="fxdst-post-nav" aria-label="文章切换">' . fxdst_article_guard_adjacent_card($previous, '上一篇', '没有更早文章', 'fxdst-post-nav-prev') . fxdst_article_guard_adjacent_card($next, '下一篇', '没有更新文章', 'fxdst-post-nav-next') . '</nav>'; } add_filter('the_content', function ($content) { if (!fxdst_article_guard_is_post()) { return $content; } if (strpos($content, 'fxdst-table-wrap') === false) { $content = preg_replace( '/(<table\b[^>]*>.*?<\/table>)/is', '<div class="fxdst-table-wrap">$1</div>', $content ); } if (strpos($content, 'fxdst-post-nav') === false) { $content .= fxdst_article_guard_post_nav(); } return $content; }, 20); add_action('wp_head', function () { if (!fxdst_article_guard_is_post()) { return; } ?> <style id="fxdst-article-guard-css"> .single-post .panel.panel-default > .panel-body.my-3, .single-post .fxdst-article-body { padding: 4px 34px 34px !important; color: #5f6f7b; font-size: 15px; line-height: 1.95; } .single-post .panel.panel-default > .panel-body.my-3 p, .single-post .fxdst-article-body p { margin: 0 0 18px; } .single-post .panel.panel-default > .panel-body.my-3 h2, .single-post .fxdst-article-body h2 { position: relative; margin: 34px 0 16px; padding-left: 14px; color: #182633; font-size: 26px; line-height: 1.35; } .single-post .panel.panel-default > .panel-body.my-3 h2::before, .single-post .fxdst-article-body h2::before { position: absolute; top: .18em; bottom: .18em; left: 0; width: 4px; border-radius: 999px; background: linear-gradient(180deg, #13c2ff, #24e0a4); content: ""; } .single-post .panel.panel-default > .panel-body.my-3 h3, .single-post .fxdst-article-body h3 { margin: 24px 0 8px; color: #1d2b36; font-size: 19px; line-height: 1.45; } .single-post .panel.panel-default > .panel-body.my-3 ul, .single-post .panel.panel-default > .panel-body.my-3 ol, .single-post .fxdst-article-body ul, .single-post .fxdst-article-body ol { margin: 0 0 22px 20px; padding: 0; } .single-post .panel.panel-default > .panel-body.my-3 li, .single-post .fxdst-article-body li { margin: 7px 0; padding-left: 2px; } .single-post .panel.panel-default > .panel-body.my-3 a, .single-post .fxdst-article-body a { color: #087ea4; text-decoration: none; border-bottom: 1px solid rgba(8, 126, 164, .22); } .single-post .fxdst-table-wrap { width: 100%; margin: 18px 0 28px; overflow-x: auto; } .single-post .panel.panel-default > .panel-body.my-3 table, .single-post .fxdst-article-body table { width: 100% !important; min-width: 720px; margin: 0 !important; border: 1px solid rgba(143, 170, 190, .28) !important; border-radius: 10px; border-spacing: 0; border-collapse: separate !important; background: #fff; box-shadow: 0 14px 32px rgba(21, 39, 56, .06); } .single-post .panel.panel-default > .panel-body.my-3 th, .single-post .panel.panel-default > .panel-body.my-3 td, .single-post .fxdst-article-body th, .single-post .fxdst-article-body td { padding: 13px 15px !important; border-right: 1px solid rgba(143, 170, 190, .14) !important; border-bottom: 1px solid rgba(143, 170, 190, .18) !important; text-align: left; vertical-align: top; word-break: break-word; } .single-post .panel.panel-default > .panel-body.my-3 th:last-child, .single-post .panel.panel-default > .panel-body.my-3 td:last-child, .single-post .fxdst-article-body th:last-child, .single-post .fxdst-article-body td:last-child { border-right: 0 !important; } .single-post .panel.panel-default > .panel-body.my-3 th, .single-post .fxdst-article-body th { color: #1f3442; background: #f4f8fb !important; font-weight: 700; } .single-post .panel.panel-default > .panel-body.my-3 tbody tr:nth-child(even) td, .single-post .fxdst-article-body tbody tr:nth-child(even) td { background: #fbfdff; } .single-post .panel.panel-default > .panel-body.my-3 tbody tr:last-child td, .single-post .fxdst-article-body tbody tr:last-child td { border-bottom: 0 !important; } .single-post .fxdst-article-note { margin: 20px 0 26px; padding: 14px 16px; border: 1px solid rgba(14, 165, 199, .22); border-radius: 10px; background: #f2fbfd; color: #31566a; } .single-post .fxdst-article-figure { margin: 22px 0 30px; } .single-post .fxdst-article-figure img { display: block; width: 100%; height: auto; border-radius: 12px; border: 1px solid rgba(143, 170, 190, .24); box-shadow: 0 16px 36px rgba(21, 39, 56, .08); } .single-post .fxdst-article-figure figcaption { margin-top: 9px; color: #7a8a95; font-size: 13px; line-height: 1.65; } .single-post .fxdst-article-updated { margin-top: 30px; padding-top: 16px; border-top: 1px solid rgba(143, 170, 190, .2); color: #7a8a95; font-size: 13px; } .single-post .fxdst-post-nav { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 30px 0 4px; } .single-post .fxdst-post-nav-card { display: flex; min-height: 92px; flex-direction: column; justify-content: center; padding: 16px 18px; border: 1px solid rgba(143, 170, 190, .28); border-radius: 12px; background: linear-gradient(135deg, #ffffff, #f7fbfd); color: #1f3442; text-decoration: none; box-shadow: 0 14px 32px rgba(21, 39, 56, .06); } .single-post .fxdst-post-nav-card:hover { border-color: rgba(19, 194, 255, .45); color: #087ea4; } .single-post .fxdst-post-nav-card span { margin-bottom: 8px; color: #7a8a95; font-size: 13px; } .single-post .fxdst-post-nav-card strong { display: -webkit-box; overflow: hidden; color: inherit; font-size: 15px; line-height: 1.55; -webkit-box-orient: vertical; -webkit-line-clamp: 2; } .single-post .fxdst-post-nav-next { text-align: right; } .single-post .fxdst-post-nav-empty { opacity: .62; pointer-events: none; } @media (max-width: 767px) { .single-post .panel.panel-default > .panel-body.my-3, .single-post .fxdst-article-body { padding: 2px 20px 24px !important; font-size: 14px; } .single-post .panel.panel-default > .panel-body.my-3 h2, .single-post .fxdst-article-body h2 { font-size: 22px; } .single-post .panel.panel-default > .panel-body.my-3 table, .single-post .fxdst-article-body table { min-width: 680px; } .single-post .panel.panel-default > .panel-body.my-3 th, .single-post .panel.panel-default > .panel-body.my-3 td, .single-post .fxdst-article-body th, .single-post .fxdst-article-body td { padding: 11px 12px !important; } .single-post .fxdst-post-nav { grid-template-columns: 1fr; } .single-post .fxdst-post-nav-next { text-align: left; } } </style> <?php }, 99); add_action('wp_head', function () { if (!fxdst_article_guard_is_post()) { return; } $post_id = get_queried_object_id(); if (!$post_id) { return; } $title = get_the_title($post_id); $permalink = get_permalink($post_id); $description = has_excerpt($post_id) ? get_the_excerpt($post_id) : get_post_field('post_content', $post_id); $description = wp_strip_all_tags(strip_shortcodes($description)); $description = preg_replace('/\s+/u', ' ', trim($description)); if (function_exists('mb_strlen') && mb_strlen($description, 'UTF-8') > 150) { $description = mb_substr($description, 0, 150, 'UTF-8') . '...'; } $image = ''; if (has_post_thumbnail($post_id)) { $image_src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full'); if ($image_src) { $image = $image_src[0]; } } $author_id = (int) get_post_field('post_author', $post_id); $categories = get_the_category($post_id); $primary_category = !empty($categories) ? $categories[0] : null; $article = [ '@context' => 'https://schema.org', '@type' => 'Article', 'mainEntityOfPage' => [ '@type' => 'WebPage', '@id' => $permalink, ], 'headline' => $title, 'description' => $description, 'datePublished' => get_the_date(DATE_W3C, $post_id), 'dateModified' => get_the_modified_date(DATE_W3C, $post_id), 'author' => [ '@type' => 'Person', 'name' => get_the_author_meta('display_name', $author_id), ], 'publisher' => [ '@type' => 'Organization', 'name' => get_bloginfo('name'), 'url' => home_url('/'), ], ]; if ($image) { $article['image'] = [$image]; } $breadcrumbs = [ '@context' => 'https://schema.org', '@type' => 'BreadcrumbList', 'itemListElement' => [ [ '@type' => 'ListItem', 'position' => 1, 'name' => get_bloginfo('name'), 'item' => home_url('/'), ], ], ]; if ($primary_category) { $breadcrumbs['itemListElement'][] = [ '@type' => 'ListItem', 'position' => 2, 'name' => $primary_category->name, 'item' => get_category_link($primary_category->term_id), ]; $breadcrumbs['itemListElement'][] = [ '@type' => 'ListItem', 'position' => 3, 'name' => $title, 'item' => $permalink, ]; } else { $breadcrumbs['itemListElement'][] = [ '@type' => 'ListItem', 'position' => 2, 'name' => $title, 'item' => $permalink, ]; } echo '<script type="application/ld+json">' . wp_json_encode($article, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>' . "\n"; echo '<script type="application/ld+json">' . wp_json_encode($breadcrumbs, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>' . "\n"; }, 100);