定制 bigfork/silverstripe-opengraph-fork 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bigfork/silverstripe-opengraph-fork

Composer 安装命令:

composer require bigfork/silverstripe-opengraph-fork

包简介

Implementation of the Facebook Open Graph protocol for SilverStripe

README 文档

README

This is a Bigfork-maintained fork of tractorcow/silverstripe-opengraph and so may contain opinionated changes.

Opengraph module for Silverstripe

This module provides a complete implementation of each of the Open Graph types as documented at http://ogp.me/

Open Graph object types may be applied to any Page or DataObject by applying the appropriate interface.

For instance, if your page represents a music album you would implement the IOGMusicAlbum interface.

By default, the module will attempt to classify pages as the og:website type, and automatically generate appropriate meta tags for it. This is all that most websites require to adequately interact with Facebook.

Credits and Authors

Requirements

  • SilverStripe 4+
  • PHP 5.6+

Installation Instructions

  • Install using composer
composer require "tractorcow/silverstripe-opengraph" "4@dev"
  • Ensure the namespace is defined in your template with <html $OGNS>
  • If you need to add a prefix attribute to the <head /> tag then you should do this directly in your template.
  • If you are working with video files, you might want to install https://github.com/tractorcow/silverstripe-mediadata alongside this to extract video dimension for opengraph tags.

Configuration

The main configuration options for this module can be found in OpenGraph.yml.

Override these in your own mysite/_config/OpenGraph.yml or mysite/_config.php

---
Name: myopengraphsettings
After: '#opengraphsettings'
---
TractorCow\OpenGraph\OpenGraph:
  application_id: 'SiteConfig'
  admin_id: 'SiteConfig'
  default_locale: 'en_US'
  default_tagbuilder: 'TractorCow\OpenGraph\ObjectBuilders\OpenGraphBuilder'
  • Set application_id to either SiteConfig (to be set in the CMS) or a literal facebook app id
  • Set admin_id to either SiteConfig (to be set in the CMS) or a literal facebook admin_id
  • The default_locale is the literal value to use as the locale tag (if i18n doesn't have a locale set)
  • The default_tagbuilder is the name of the class to use to generate tags (unless a type has one specified explicitly). See below under [Adding new types][#adding-new-types] for details.

Any value above can be set to an empty string to disable it completely. E.g.

---
Name: myopengraphsettings
After: '#opengraphsettings'
---
TractorCow\OpenGraph\OpenGraph:
  application_id: ''
  admin_id: ''

How to do stuff

Implementing Open Graph object properties

To get specific information on each of the fields an opengraph object can have, check out the various implementations of each in the src/Interfaces/ObjectTypes folder, or in the _config/OpenGraphTypes.yml file for the list of types and their respective interfaces.

The basic opengraph object has a set of required properties (as defined by TractorCow\OpenGraph\Interfaces\ObjectTypes\IOGObjectRequired) and additionally a set of optional properties (as defined by TractorCow\OpenGraph\Interfaces\ObjectTypes\IOGObjectExplicit).

Since most of the field values are generated by the page extension class OpenGraphPageExtension automatically, you don't need to explicitly implement either of these. These should however should be used as a guide to what can be specified.

For example, if you wanted to override the getOGImage property (og:image meta tag) you would implement the following in your page classe:

class MyPage extends Page {

    function getOGImage() {
        return $this->Thumbnail();
    }

}

By implementing these properties explicitly in your page classes, you can override the default properties defined in the OpenGraphPageExtension.

Setting open Graph default image

The Open Graph image is a required property and should be supplied a default image. You can set the path to the default image in the yml config

TractorCow\OpenGraph\Extensions\OpenGraphObjectExtension:
  default_image: 'app/images/logo.png'
  theme_name_default_image: 'app/images/theme-logo.png'

Note that you can specify a different image for each theme by prefixing the default_image config name with the theme name (replace everything that is not a letter with an _). This is useful if you want to set a different default image on sub sites.

Adding new types

If you wish to add a new og:type you will need to:

  • Create an interface that extends IOGObject that defines the fields (if any) that your object will publish
  • Extend the OpenGraphBuilder class and override the BuildTags function to generate the actual HTML for the tags in your interface
  • Implement your interface on pages of the new type
  • Register your object type with the following code:
TractorCow\OpenGraph\OpenGraph::register_type('type-name', IOGMyObjectInterface, MyObjectTagBuilder);

Or better still, do this directly in yaml as below

TractorCow\OpenGraph\OpenGraph:
  types:
    'type-name':
      interface: IOGMyObjectInterface
      tagbuilder: MyObjectTagBuilder

Creating a custom tag builder

In order to add an opengraph meta tag to your page, you need to write the code that describes how to translate an object into a piece of html. This can be done by implementing this in PHP with a TagBuilder object.

Note that there are two objects for every request; The entity being viewed (Page or DataObject) and the application (SiteConfig). Each has their own set of tags.

E.g.

class MyObjectTagBuilder extends OpenGraphBuilder {

    public function BuildTags(&$tags, $object, $config) {
        parent::BuildTags($tags, $object, $config);

        $this->appendTag($tags, 'appnamespace:nameofthetag', $object->getOGNameOfTheTag());
    }
}

Our interface might look something like

interface IOGMyObjectInterface extends IOGObject {
	
	function getOGNameOfTheTag();
}

Adding tags to the default type

You can decorate the OpenGraphBuilder object instead of extending it if you need to add additional tags to all object types.

The example below shows how to add extra fields from the Page and SiteConfig to the set of OpenGraph tags.

TractorCow\OpenGraph\ObjectBuilders\OpenGraphBuilder::add_extension('OpenGraphBuilderExtension');

class OpengraphBuilderExtension extends Extension {

    function updateApplicationMetaTags(&$tags, $siteconfig) {
        $this->owner->AppendTag($tags, 'og:application-name', $siteconfig->Title);
    }
    
    function updateDefaultMetaTags(&$tags, $page) {
        $this->owner->AppendTag($tags, 'og:page-menu-name', $page->MenuTitle);
    }

}

Disabling Open Graph for a single page (or page type)

If you need to disable Open Graph for any page then a null value for getOGType() will disable tag generation.

NonOGPage extends Page {

    function getOGType() {
        return null;
    }

}

Using DataObjects as pages

See https://github.com/tractorcow/silverstripe-opengraph/wiki/Using-DataObjects-as-Pages for how to extend your DataObject with TractorCow\OpenGraph\Extensions\OpenGraphObjectExtension.

  • Add the TractorCow\OpenGraph\Extensions\OpenGraphObjectExtension extension to your object
  • Implement AbsoluteLink on your object
  • Implement MetaTags on your object, making sure to call $this->extend('MetaTags', $tags);
  • Make sure the actual page type being viewed delegates the meta tag generation to your dataobject

Need more help?

Message or email me at damian.mooyman@gmail.com or, well, read the code!

License

Copyright (c) 2013, Damian Mooyman All rights reserved.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • The name of Damian Mooyman may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Apologies

I went a bit crazy with this module! Good old interfaces eh?

bigfork/silverstripe-opengraph-fork 适用场景与选型建议

bigfork/silverstripe-opengraph-fork 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 487 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「facebook」 「opengraph」 「silverstripe」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bigfork/silverstripe-opengraph-fork 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bigfork/silverstripe-opengraph-fork 我们能提供哪些服务?
定制开发 / 二次开发

基于 bigfork/silverstripe-opengraph-fork 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 487
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2025-02-21