Skip to content

Commit

Permalink
Merge pull request #509 from GaryJones/feature/schema-refactor
Browse files Browse the repository at this point in the history
AMP schema refactor
  • Loading branch information
philipjohn committed Sep 16, 2018
2 parents 21df6af + c56aecd commit 91deb2e
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 75 deletions.
84 changes: 9 additions & 75 deletions classes/class-wpcom-liveblog-amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ public static function social_meta_tags() {
return;
}

$request = self::get_request_data();
$request = WPCOM_Liveblog::get_request_data();

// If no entry id set then not on single entry.
if ( false === $request->id ) {
return;
}

$entry = self::get_entry( $request->id, $post->ID );
$title = self::get_entry_title( $entry );
$title = WPCOM_Liveblog_Entry::get_entry_title( $entry );
$description = strip_tags( $entry->content );
$url = self::build_single_entry_permalink( amp_get_permalink( $post->ID ), $entry->id );
$image = self::get_entry_image( $entry );
Expand Down Expand Up @@ -186,55 +186,12 @@ public static function get_entry_image( $entry ) {
*/
public static function append_liveblog_to_metadata( $metadata, $post ) {

// If we are not viewing a liveblog post then exist the filter.
if ( WPCOM_Liveblog::is_liveblog_post( $post->ID ) === false ) {
return $metadata;
}

$request = self::get_request_data();

$publisher_organization = '';
$publisher_name = '';

$entries = WPCOM_Liveblog::get_entries_paged( $request->page, $request->last );

$blog_updates = [];

if ( isset( $entries['entries'] ) && is_array( $entries['entries'] ) ) {
foreach ( $entries['entries'] as $key => $entry ) {

if ( isset( $metadata['publisher']['name'] ) ) {
$publisher_name = $metadata['publisher']['name'];
}

if ( isset( $metadata['publisher']['type'] ) ) {
$publisher_organization = $metadata['publisher']['type'];
}

$blog_item = (object) array(
'@type' => 'BlogPosting',
'headline' => self::get_entry_title( $entry ),
'url' => $entry->share_link,
'datePublished' => date( 'c', $entry->entry_time ),
'dateModified' => date( 'c', $entry->timestamp ),
'author' => (object) array(
'@type' => 'Person',
'name' => $entry->authors[0]['name'],
),
'articleBody' => (object) array(
'@type' => 'Text',
),
'publisher' => (object) array(
'@type' => $publisher_organization,
'name' => $publisher_name,
),
);

array_push( $blog_updates, $blog_item );
}

$metadata['@type'] = 'LiveBlogPosting';
$metadata['liveBlogUpdate'] = $blog_updates;
// Only append metadata to Liveblogs.
if ( false !== WPCOM_Liveblog::is_liveblog_post( $post->ID ) ) {
/**
* This filter is documented in liveblog.php
*/
$metadata = WPCOM_Liveblog::get_liveblog_metadata();
}

return $metadata;
Expand All @@ -253,7 +210,7 @@ public static function append_liveblog_to_content( $content ) {
return $content;
}

$request = self::get_request_data();
$request = WPCOM_Liveblog::get_request_data();

// If AMP Polling request don't restrict content so it knows there is updates are available.
if ( self::is_amp_polling() ) {
Expand Down Expand Up @@ -419,16 +376,6 @@ public static function get_entry_date( $entry ) {
return date_i18n( $date_format, strtotime( $utc_offset, $entry->entry_time ) );
}

/**
* Work out Entry title
*
* @param object $entry Entry.
* @return string Title
*/
public static function get_entry_title( $entry ) {
return wp_trim_words( $entry->content, 10, '...' );
}

/**
* Gets Pagination Links (First, Last, Next, Previous)
*
Expand Down Expand Up @@ -492,19 +439,6 @@ public static function build_single_entry_permalink( $permalink, $id ) {
);
}

/**
* Get Page and Last known entry from the request.
*
* @return object Request Data.
*/
public static function get_request_data() {
return (object) array(
'page' => get_query_var( 'liveblog_page', 1 ),
'last' => get_query_var( 'liveblog_last', false ),
'id' => get_query_var( 'liveblog_id', false ),
);
}

/**
* Get template.
*
Expand Down
11 changes: 11 additions & 0 deletions classes/class-wpcom-liveblog-entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,17 @@ public static function get_authors( $comment_id ) {

return array_merge( $author, $contributors );
}

/**
* Work out Entry title
*
* @param object $entry Entry.
* @return string Title
*/
public static function get_entry_title( $entry ) {
return wp_trim_words( $entry->content, 10, '...' );
}

}

WPCOM_Liveblog_Entry::generate_allowed_tags_for_entry();
100 changes: 100 additions & 0 deletions liveblog.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ private static function add_actions() {
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
add_action( 'wp_ajax_set_liveblog_state_for_post', array( __CLASS__, 'admin_ajax_set_liveblog_state_for_post' ) );
add_action( 'pre_get_posts', array( __CLASS__, 'add_custom_post_type_support' ) );
add_action( 'wp_head', array( __CLASS__, 'print_liveblog_metadata' ) );
}

/**
Expand Down Expand Up @@ -1771,6 +1772,105 @@ public static function get_refresh_interval() {
return $refresh_interval;
}

/**
* Generates metadata for a single liveblog
*
* @param array $metadata Metadata.
* @param WP_Post $post Current Post.
* @return array Updated Meta
*/
public static function get_liveblog_metadata() {

global $post;

// If we are not viewing a liveblog post then exit the filter.
if ( WPCOM_Liveblog::is_liveblog_post( $post->ID ) === false ) {
return $metadata;
}

$request = self::get_request_data();

$entries = WPCOM_Liveblog::get_entries_paged( $request->page, $request->last );

$blog_updates = [];

if ( ! isset( $entries[ 'entries' ] ) || ! is_array( $entries[ 'entries' ] ) ) {
return $metadata;
}

foreach ( $entries[ 'entries' ] as $key => $entry ) {
$blog_item = [
'@type' => 'BlogPosting',
'headline' => WPCOM_Liveblog_Entry::get_entry_title( $entry ),
'url' => $entry->share_link,
'mainEntityOfPage' => $entry->share_link,
'datePublished' => date( 'c', $entry->entry_time ),
'dateModified' => date( 'c', $entry->timestamp ),
'author' => [
'@type' => 'Person',
'name' => $entry->authors[ 0 ][ 'name' ],
],
'articleBody' => [
'@type' => 'Text',
],
];

if ( isset( $metadata['publisher'] ) ) {
$blog_item['publisher'] = $metadata['publisher'];
}

$blog_updates[] = json_decode( json_encode( $blog_item ) );
}

$metadata['@type'] = 'LiveBlogPosting';
$metadata['liveBlogUpdate'] = $blog_updates;

/**
* Filters the Liveblog metadata.
*
* Allows plugins and themes to adapt the metadata printed by the
* liveblog into the head, describing the liveblog and it's entries.
*
* @since 1.9
*
* @param array $metadata An array of metadata.
*/
$metadata = apply_filters( 'liveblog_metadata', $metadata, $post );

return $metadata;
}

public static function print_liveblog_metadata() {

// Bail if we are not viewing a liveblog.
if ( WPCOM_Liveblog::is_liveblog_post( get_the_ID() ) === false ) {
return;
}

$metadata = WPCOM_Liveblog::get_liveblog_metadata();
if ( empty( $metadata ) ) {
return;
}

?>
<script type="application/ld+json"><?php echo wp_json_encode( $metadata ); ?></script>
<?php

}

/**
* Get Page and Last known entry from the request.
*
* @return object Request Data.
*/
public static function get_request_data() {
return (object) array(
'page' => get_query_var( 'liveblog_page', 1 ),
'last' => get_query_var( 'liveblog_last', false ),
'id' => get_query_var( 'liveblog_id', false ),
);
}

}
WPCOM_Liveblog::load();

Expand Down

0 comments on commit 91deb2e

Please sign in to comment.