quinninteractive / silverstripe-seo
适用于 SilverStripe 的多功能 SEO 模块
Requires
- php: ^8.1
- jonom/silverstripe-text-target-length: ^2.0
- kub-at/php-simple-html-dom-parser: ^1.9
- silverstripe/cms: ^5
- wilr/silverstripe-googlesitemaps: ^3.1
Requires (Dev)
- phpunit/phpunit: ^9.5
- rector/rector: ^1.2
README
适用于 SilverStripe 的多功能 SEO 模块。
注意:本存储库的 1.0.4 版本取代了 vulcandigital/silverstripe-seo
的 1.0.3 版本。
特性
- 页面编辑器中的 SEO 健康分析
- 自动生成 Facebook OpenGraph 元标签(可覆盖)
- 自动生成 Twitter 元标签(可覆盖)
- 还为
SilverStripe\Security\Member
添加了一个TwitterAccountName
字段,用于创建者标签。创建新页面时,会记录创建者及其 Twitter 账户名称,用于元标签。
- 还为
示例元标签输出
<link rel="canonical" href="http://atmtanks.local/"/> <meta property="og:title" content="Home"/> <meta property="og:description" content="ATM Industrial Panel Tanks (ATM) specialises in tank builds, modifications and maintenance. ATM has performed significant tank refurbishments, re-lines and roof replacements for Government Hospitals, Power Stations, Food Process Companies, Mines and more."/> <meta property="og:type" content="website"/> <meta property="og:url" content="http://atmtanks.local/"/> <meta property="og:locale" content="en_GB" /> <meta property="og:site_name" content="ATM Industrial Panel Tanks" /> <meta name="twitter:card" content="summary"/> <meta name="twitter:title" content="Home"/> <meta name="twitter:description" content="ATM Industrial Panel Tanks (ATM) specialises in tank builds, modifications and maintenance. ATM has performed significant tank refurbishments, re-lines and roof replacements for Government Hospitals, Power Stations, Food Process Companies, Mines and more."/> <meta name="twitter:creator" content="@zanderwar"/> <meta name="twitter:site" content="@vulcandigital" /> <meta property="article:published_time" content="2018-04-08T00:22:10+10:00" /> <meta property="article:modified_time" content="2018-04-16T21:52:52+10:00" />
如果您认为可以向此输出添加有益的内容,请毫不犹豫地提交 PR 或打开问题进行讨论。请参阅 CONTRIBUTING.md。
要求
有关详细信息,请参阅 composer.json。
安装
composer require quinninteractive/silverstripe-seo
入门
安装此模块后,会自动应用必要的扩展,并执行 dev/build。
编写您自己的分析
健康分析已被抽象化,以便开发者能够创建自己的分析检查。
要这样做,您只需创建一个扩展 QuinnInteractive\Seo\Analysis\Analysis
的新类。
例如,让我们创建一个新的分析,检查当前页面的标题是否为 Hello World!
。
首先创建以下文件
mysite\code\Analysis\HelloWorldTitleAnalysis.php
<?php namespace Vendor\Project\Analysis; use QuinnInteractive\Seo\Analysis\Analysis; class HelloWorldTitleAnalysis extends Analysis { const FAILED = 0; const SUCCESS = 1; public function run() { if (!strstr($this->getPage()->Title, 'Hello World!')) { return static::FAILED; } return static::SUCCESS; } public function responses() { return [ static::FAILED => ['The string "Hello World!" does not appear in the page title', 'danger'], static::SUCCESS => ['Hoorah!!! "Hello World!" appears in the page title', 'success'], ]; } }
然后执行 dev/build。您将立即在编辑任何页面时在 CMS 中的“SEO 健康分析”折叠下看到此新分析运行,然后更改标题以包含“Hello World”,您将注意到指示器将显示成功。
需要注意的一点是,分析始终可以通过 $this->getPage()
访问其正在运行的 \Page
对象,因此您的响应也可以是动态的。
如果您已创建分析并认为它可以作为此模块的补充,我们强烈建议您提交拉取请求,您将获得您工作的全部信用。请参阅 CONTRIBUTING.md。
说明:run()
您必须重写此方法,因为这是您将执行所有检查的地方,然后返回与您在 responses()
中定义的键相对应的整数。使用表示这些整数的常量进行可读性是一个好主意
说明:responses()
所有分析都必须重写 responses()
方法,以提供响应消息和响应级别(用于指标)。
run()
应返回一个整数,该整数与 responses()
返回的数组中的一个键匹配,例如如果 run()
返回 1
,那么使用上面的示例,显示的消息将是 欢呼!!! "Hello World!" 出现在页面标题中
,指标级别为 success
可用的指标级别有:default
、danger
、warning
、success
,分别对应灰色、红色、橙色和绿色。
您可以可选地防止某些级别在内容分析选项卡中显示。在上述示例中添加以下内容将导致仅当指标级别不是值为 success
时才显示条目
private static $hidden_levels = [ 'success' ];
配置选项
enable_creator_tag
默认情况下,此模块向 \SilverStripe\Security\Member
添加一个扩展,该扩展添加一个名为 TwitterAccountName
的单个字段。如果设置了此字段,当此特定用户创建页面时,将生成带有会员账户名称的 twitter:creator
元标记
您可以通过 YAML 禁用此功能
QuinnInteractive\Seo\Extensions\PageSeoExtension: enable_creator_tag: false
元描述长度
该模块有 2 个与元描述长度相关的配置选项。下面的示例 YAML 展示了它们及其默认值。除非您想从默认值更改这些值,否则不需要创建此类文件。
QuinnInteractive\Seo\Analysis\MetaDescAnalysis: meta_desc_target_length: 160 meta_desc_too_long_threshold: 320
其他选项
其他选项可以在以下文件的 private static
变量中找到。它们可以通过 YAML 以常规方式重写。
- PageHealthExtension.php
- PageSeoExtension.php
- SiteConfigSettingsExtension.php
假设
此模块假设您使用 \Page
提供的默认 Content
字段。如果特定页面没有这样做,则可以指定一个或多个包含您的内容的字段。
它们应该按正确的顺序排列,以便最终用户可以看到
在您的 \Page
子类中,您会有
public function seoContentFields() { return [ 'Content', 'MyBlock.Title', 'MyBlock.Content', 'BottomTitle', 'BottomContent', ]; }
在您的 \Page
子类中,您可以添加一个 updateCollateContentFields 来提供使用 seoContentFields 无法提供的自定义内容
public function updateCollateContentFields($content) { // Content is an array of strings with content in the order in which they appear to the user $content[] = 'This is example content'; return $content; }
路线图(可能更改)
- 完成对此模块及其分析的国际化实现
- 更多内容分析
- 鉴于内容实际上可以来自 SilverStripe 页面的任何位置,引入了
seoContentFields
方法以更好地通过收集所有内容字段到一个字符串来改进内容分析,这支持点表示法用于has_one
关系,但可能不支持(或目前不支持)has_many
和many_many
关系。从长远来看,我们希望使用 DOM 解析器(部分实现)并依赖于此。 - 寻找社区支持以帮助改善和改进此模块,使其对所有 SilverStripe 用户都有所帮助
许可证
版本
2.0.0