Skip to content

Commit

Permalink
replace 3rd-party schema with built-in
Browse files Browse the repository at this point in the history
  • Loading branch information
pryley committed Jun 25, 2018
1 parent e0dbf59 commit bd629b9
Show file tree
Hide file tree
Showing 35 changed files with 1,143 additions and 971 deletions.
15 changes: 0 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ scrap.md
/node_modules
/vendor/*/*

!/vendor/geminilabs/schema-org
!/vendor/qcubed/i18n
!/vendor/sinergi/browser-detector
!/vendor/vectorface/whip
Expand All @@ -30,24 +29,10 @@ scrap.md
/vendor/**/README*
/vendor/**/UPGRADING*
/vendor/composer/installers
/vendor/geminilabs/schema-org/src/*.php
/vendor/sinergi/browser-detector/src/AcceptLanguage.php
/vendor/sinergi/browser-detector/src/Device*
/vendor/sinergi/browser-detector/src/Language*
/vendor/sinergi/browser-detector/src/Os*
/vendor/qcubed/i18n/src/*.php
/vendor/qcubed/i18n/test
/vendor/qcubed/i18n/tools

!/vendor/geminilabs/schema-org/src/AggregateRating.php
!/vendor/geminilabs/schema-org/src/BaseType.php
!/vendor/geminilabs/schema-org/src/CreativeWork.php
!/vendor/geminilabs/schema-org/src/Intangible.php
!/vendor/geminilabs/schema-org/src/LocalBusiness.php
!/vendor/geminilabs/schema-org/src/Organization.php
!/vendor/geminilabs/schema-org/src/Person.php
!/vendor/geminilabs/schema-org/src/Rating.php
!/vendor/geminilabs/schema-org/src/Review.php
!/vendor/geminilabs/schema-org/src/Thing.php
!/vendor/geminilabs/schema-org/src/Type.php
!/vendor/geminilabs/schema-org/src/Unknown.php
1 change: 0 additions & 1 deletion autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
$namespaces = [
'GeminiLabs\\SiteReviews\\' => __DIR__.'/plugin/',
'GeminiLabs\\SiteReviews\\Tests\\' => __DIR__.'/tests/',
'GeminiLabs\\SchemaOrg\\' => __DIR__.'/vendor/geminilabs/schema-org/src/',
'Sepia\\PoParser\\' => __DIR__.'/vendor/qcubed/i18n/src/Sepia/',
'Sinergi\\BrowserDetector\\' => __DIR__.'/vendor/sinergi/browser-detector/src/',
'Vectorface\\Whip\\' => __DIR__.'/vendor/vectorface/whip/src/',
Expand Down
7 changes: 0 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,11 @@
},
"minimum-stability": "dev",
"prefer-stable": true,
"repositories": [
{
"type": "vcs",
"url": "https://github.com/geminilabs/schema-org.git"
}
],
"require": {
"php": ">=5.6"
},
"require-dev": {
"phpunit/phpunit": "^6.5.8",
"geminilabs/schema-org": "dev-master as 1.3.0",
"qcubed/i18n": "dev-master",
"sinergi/browser-detector": "^6.1.2",
"vectorface/whip": "^0.3.2"
Expand Down
113 changes: 17 additions & 96 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
'settings.schema.type.custom' => [
'default' => '',
'depends_on' => ['settings.schema.type.default' => 'custom'],
'description' => __( 'Google supports review ratings for the following schema content types: Local businesses, Movies, Books, Music, and Products. <a href="https://schema.org/docs/schemas.html">View more information on schema types here</a>.', 'site-reviews' ),
'description' => glsr( 'Modules\Html\Builder' )->a( __( 'View more information on schema types here', 'site-reviews' ), ['href' => 'https://schema.org/docs/schemas.html'] ),
'label' => __( 'Custom Schema Type', 'site-reviews' ),
'type' => 'text',
],
Expand Down
46 changes: 27 additions & 19 deletions plugin/Modules/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace GeminiLabs\SiteReviews\Modules;

use DateTime;
use GeminiLabs\SchemaOrg\Review as ReviewSchema;
use GeminiLabs\SchemaOrg\Schema as SchemaOrg;
use GeminiLabs\SchemaOrg\Thing as ThingSchema;
use GeminiLabs\SiteReviews\Database;
use GeminiLabs\SiteReviews\Database\OptionManager;
use GeminiLabs\SiteReviews\Helper;
use GeminiLabs\SiteReviews\Modules\Rating;
use GeminiLabs\SiteReviews\Modules\Schema\BaseType;
use GeminiLabs\SiteReviews\Modules\Schema\UnknownType;
use WP_Post;

class Schema
Expand All @@ -33,6 +32,9 @@ public function build( array $args = [] )
$schema = $this->buildSummary( $args );
$reviews = [];
foreach( glsr( Database::class )->getReviews( $this->args )->results as $review ) {
// Only include critic reviews that have been directly produced by your site, not reviews from third- party sites or syndicated reviews.
// @see https://developers.google.com/search/docs/data-types/review
if( $review->review_type != 'local' )continue;
$reviews[] = $this->buildReview( $review );
}
if( !empty( $reviews )) {
Expand Down Expand Up @@ -60,7 +62,7 @@ public function buildSummary( $args = null )
? $this->$buildSummary()
: $this->buildSummaryForCustom();
if( !empty( $count )) {
$schema->aggregateRating( SchemaOrg::AggregateRating()
$schema->aggregateRating( $this->getSchemaType( 'AggregateRating' )
->ratingValue( $this->getRatingValue() )
->reviewCount( $count )
->bestRating( Rating::MAX_RATING )
Expand Down Expand Up @@ -89,7 +91,7 @@ public function render()
*/
public function store( array $schema )
{
$schemas = (array) glsr()->schemas;
$schemas = glsr()->schemas;
$schemas[] = $schema;
glsr()->schemas = array_map( 'unserialize', array_unique( array_map( 'serialize', $schemas )));
}
Expand All @@ -100,18 +102,18 @@ public function store( array $schema )
*/
protected function buildReview( $review )
{
$schema = SchemaOrg::Review()
->doIf( !in_array( 'title', $this->args['hide'] ), function( ReviewSchema $schema ) use( $review ) {
$schema = $this->getSchemaType( 'Review' )
->doIf( !in_array( 'title', $this->args['hide'] ), function( $schema ) use( $review ) {
$schema->name( $review->title );
})
->doIf( !in_array( 'excerpt', $this->args['hide'] ), function( ReviewSchema $schema ) use( $review ) {
->doIf( !in_array( 'excerpt', $this->args['hide'] ), function( $schema ) use( $review ) {
$schema->reviewBody( $review->content );
})
->datePublished(( new DateTime( $review->date ))->format( DateTime::ISO8601 ))
->author( SchemaOrg::Person()->name( $review->author ))
->author( $this->getSchemaType( 'Person' )->name( $review->author ))
->itemReviewed( $this->getSchemaType()->name( $this->getSchemaOptionValue( 'name' )));
if( !empty( $review->rating )) {
$schema->reviewRating( SchemaOrg::Rating()
$schema->reviewRating( $this->getSchemaType( 'Rating' )
->ratingValue( $review->rating )
->bestRating( Rating::MAX_RATING )
->worstRating( Rating::MIN_RATING )
Expand All @@ -121,9 +123,9 @@ protected function buildReview( $review )
}

/**
* @return ThingSchema
* @return BaseType
*/
protected function buildSchemaValues( ThingSchema $schema, array $values = [] )
protected function buildSchemaValues( BaseType $schema, array $values = [] )
{
foreach( $values as $value ) {
$option = $this->getSchemaOptionValue( $value );
Expand All @@ -134,7 +136,7 @@ protected function buildSchemaValues( ThingSchema $schema, array $values = [] )
}

/**
* @return ThingSchema
* @return BaseType
*/
protected function buildSummaryForCustom()
{
Expand All @@ -144,7 +146,7 @@ protected function buildSummaryForCustom()
}

/**
* @return ThingSchema
* @return BaseType
*/
protected function buildSummaryForLocalBusiness()
{
Expand All @@ -154,11 +156,11 @@ protected function buildSummaryForLocalBusiness()
}

/**
* @return ThingSchema
* @return BaseType
*/
protected function buildSummaryForProduct()
{
$offers = $this->buildSchemaValues( SchemaOrg::AggregateOffer(), [
$offers = $this->buildSchemaValues( $this->getSchemaType( 'AggregateOffer' ), [
'highPrice', 'lowPrice', 'priceCurrency',
]);
return $this->buildSummaryForCustom()
Expand Down Expand Up @@ -256,12 +258,18 @@ protected function getSchemaOptionValue( $option, $fallback = 'post' )
}

/**
* @param null|string $type
* @return \GeminiLabs\SchemaOrg\Type
*/
protected function getSchemaType()
protected function getSchemaType( $type = null )
{
$type = $this->getSchemaOption( 'type', 'LocalBusiness' );
return SchemaOrg::$type( $type );
if( !is_string( $type )) {
$type = $this->getSchemaOption( 'type', 'LocalBusiness' );
}
$className = glsr( Helper::class )->buildClassName( $type, 'Modules\Schema' );
return class_exists( $className )
? new $className()
: new UnknownType( $type );
}

/**
Expand Down
Loading

0 comments on commit bd629b9

Please sign in to comment.