Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
zanderwar committed Feb 17, 2018
0 parents commit 7d88166
Show file tree
Hide file tree
Showing 12 changed files with 592 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,css,scss}]
indent_size = 2
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/.phpunit.xml.dist export-ignore
27 changes: 27 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
language: php

matrix:
include:
- php: 5.6
env: DB=MYSQL PHPCS_TEST=1 PHPUNIT_TEST=1
- php: 7.0
env: DB=PGSQL PHPUNIT_TEST=1
- php: 7.1
env: DB=MYSQL PHPUNIT_COVERAGE_TEST=1

before_script:
- phpenv rehash
- phpenv config-rm xdebug.ini

- composer validate
- composer require --prefer-dist --no-update silverstripe/recipe-core:^1.0
- if [[ $DB == PGSQL ]]; then composer require --prefer-dist --no-update silverstripe/postgresql:^2.0; fi
- composer update

script:
- if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit; fi
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml; fi
- if [[ $PHPCS_TEST ]]; then vendor/bin/phpcs --standard=vendor/silverstripe/framework/phpcs.xml.dist src/ tests/ ; fi

after_success:
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml; fi
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Contributing

We'd love you to contribute! We do have a couple of processes to follow though depending on the "phase" of the module you're using:

**Draft phase:** Please submit pull requests to the appropriate module "specification" file and indicate your suggested changes. Feel free to review other requests too. Requests will be merged by a SilverLeague community member once approved.

**Development and active support phase:** Ideas, proposals and contributions are readily accepted in the form of issues or pull requests on the appropriate SilverLeague repository.
11 changes: 11 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Copyright (c) 2017, [Vulcan Digital Ltd](https://vulcandigital.co.nz). 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.

Neither the name of the copyright holder nor the names of its contributors may 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
1 change: 1 addition & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "vulcandigital/silverstripe-facebookseo",
"description": "A simple page extension to give content authors the ability to modify the content displayed within facebook",
"type": "silverstripe-vendormodule",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Reece Alexander",
"email": "reece@vulcandigital.co.nz"
}
],
"require": {
"silverstripe/recipe-cms": "^1"
},
"require-dev": {
"phpunit/PHPUnit": "^5.7",
"squizlabs/php_codesniffer": "3.*"
},
"autoload": {
"psr-4": {
"Vulcan\\FacebookSeo\\": "src/",
"Vulcan\\FacebookSeo\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
33 changes: 33 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>

<!-- base rules are PSR-2 -->
<rule ref="PSR2" >
<!-- Current exclusions -->
<exclude name="PSR1.Methods.CamelCapsMethodName" />
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
<exclude name="PSR2.Classes.PropertyDeclaration" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration" /> <!-- causes php notice while linting -->
<exclude name="PSR2.ControlStructures.SwitchDeclaration.WrongOpenercase" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration.WrongOpenerdefault" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration.TerminatingComment" />
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" />
<exclude name="Generic.Files.LineLength.TooLong" />
<exclude name="PEAR.Functions.ValidDefaultValue.NotAtEnd" />
</rule>
<rule phpcbf-only="true" ref="Squiz.Strings.ConcatenationSpacing">
<properties>
<property name="spacing" value="1" />
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>

<!-- include php files only -->
<arg name="extensions" value="php,lib,inc,php5"/>

<!-- PHP-PEG generated file not intended for human consumption -->
<exclude-pattern>*/SSTemplateParser.php$</exclude-pattern>
<exclude-pattern>*/_fakewebroot/*</exclude-pattern>
<exclude-pattern>*/fixtures/*</exclude-pattern>
</ruleset>
14 changes: 14 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" stopOnFailure="false">
<testsuite name="Default">
<directory>tests</directory>
</testsuite>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
226 changes: 226 additions & 0 deletions src/Builders/MetaGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php

namespace Vulcan\FacebookSeo\Builders;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\FieldType\DBHTMLText;

/**
* Class MetaGenerator
* @package Vulcan\FacebookSeo\Builders
*/
class MetaGenerator
{
use Injectable, Configurable;

protected $title;

protected $description;

protected $type;

protected $url;

protected $imageUrl;

public function forTemplate()
{
if ($this->getTitle()) {
$tags[] = sprintf('<meta property="og:title" content="%s"/>', $this->getTitle());
}

if ($this->getDescription()) {
$tags[] = sprintf('<meta property="og:description" content="%s"/>', $this->getDescription());
}

if ($this->getType()) {
$tags[] = sprintf('<meta property="og:type" content="%s"/>', $this->getType());
}

if ($this->getUrl()) {
$tags[] = sprintf('<meta property="og:url" content="%s"/>', $this->getUrl());
}

if ($this->getImageUrl()) {
$tags[] = sprintf('<meta property="og:image" content="%s"/>', $this->getImageUrl());
}

return implode(PHP_EOL, $tags);
}

/**
* @param mixed $title
*
* @return MetaGenerator
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}

/**
* @param mixed $description
*
* @return MetaGenerator
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}

/**
* @param mixed $type
*
* @return MetaGenerator
* @throws \Exception
*/
public function setType($type)
{
if (!in_array($type, array_keys(static::getValidTypes()))) {
throw new \Exception("That type [$type] is not a valid type, please see: https://developers.facebook.com/docs/reference/opengraph/");
}

$this->type = $type;
return $this;
}

/**
* @param mixed $url
*
* @return MetaGenerator
*/
public function setUrl($url = null)
{
if ($url && (substr($url, 0, 1) === '/' || substr($url, 0, 4) !== 'http')) {
throw new \InvalidArgumentException('A relative URL was detected, your must provide the full absolute URL instead');
}

$this->url = $url;
return $this;
}

/**
* @param mixed $imageUrl
*
* @return MetaGenerator
*/
public function setImageUrl($imageUrl = null)
{
if ($imageUrl && (substr($imageUrl, 0, 1) === '/' || substr($imageUrl, 0, 4) !== 'http')) {
throw new \InvalidArgumentException('A relative or invalid URL was detected, your must provide the full absolute URL');
}

$this->imageUrl = $imageUrl;
return $this;
}

/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}

/**
* @return mixed
*/
public function getDescription()
{
$obj = DBHTMLText::create();

if (!$this->description) {
return null;
}

return $obj->setValue($this->description)->LimitCharacters(297);
}

/**
* @return mixed
*/
public function getType()
{
return $this->type;
}

/**
* @return mixed
*/
public function getUrl()
{
return $this->url;
}

/**
* @return mixed
*/
public function getImageUrl()
{
return $this->imageUrl;
}

public function __toString()
{
return $this->forTemplate();
}

public static function getValidTypes()
{
return [
'apps.saves' => 'apps.saves',
'books.quotes' => 'books.quotes',
'books.rates' => 'books.rates',
'books.reads' => 'books.reads',
'books.wants_to_read' => 'books.wants_to_read',
'fitness.bikes' => 'fitness.bikes',
'fitness.runs' => 'fitness.runs',
'fitness.walks' => 'fitness.walks',
'games.achieves' => 'games.achieves',
'games.celebrate' => 'games.celebrate',
'games.plays' => 'games.plays',
'games.saves' => 'games.saves',
'music.listens' => 'music.listens',
'music.playlists' => 'music.playlists',
'news.publishes' => 'news.publishes',
'news.reads' => 'news.reads',
'og.follows' => 'og.follows',
'og.likes' => 'og.likes',
'pages.saves' => 'pages.saves',
'restaurant.visited' => 'restaurant.visited',
'restaurant.wants_to_visit' => 'restaurant.wants_to_visit',
'sellers.rates' => 'sellers.rates',
'video.rates' => 'video.rates',
'video.wants_to_watch' => 'video.wants_to_watch',
'video.watches' => 'video.watches',
'article' => 'article',
'book' => 'book',
'books.author' => 'books.author',
'books.book' => 'books.book',
'books.genre' => 'books.genre',
'business.business' => 'business.business',
'fitness.course' => 'fitness.course',
'game.achievement' => 'game.achievement',
'music.album' => 'music.album',
'music.playlist' => 'music.playlist',
'music.radio_station' => 'music.radio_station',
'music.song' => 'music.song',
'place' => 'place',
'product' => 'product',
'product.group' => 'product.group',
'product.item' => 'product.item',
'profile' => 'profile',
'restaurant.menu' => 'restaurant.menu',
'restaurant.menu_item' => 'restaurant.menu_item',
'restaurant.menu_section' => 'restaurant.menu_section',
'restaurant.restaurant' => 'restaurant.restaurant',
'video.episode' => 'video.episode',
'video.movie' => 'video.movie',
'video.other' => 'video.other',
'video.tv_show' => 'video.tv_show',
];
}
}
Loading

0 comments on commit 7d88166

Please sign in to comment.