Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More content schema updates #71

Merged
merged 5 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,28 @@ Note that you need to add `data-sophi-feature=<widget_name>` to the wrapper div
</div>
```

### Post content type

By default, Sophi for WordPress uses post format as the content type. This plugin uses `content_type` internally to distinguish between WordPress post type and Sophi type.

Sophi accepts 4 types: article, video, audio, and image. The default type is `article`. Any other types that are not included above will be treated as `article`.

If you're not using post format for content type, you can utilize `sophi_post_content_type` to set the content type.

```php
add_filter( 'sophi_post_content_type', function( $type, $post ) {
// You logic here.

return $new_type;
} );
```

### Canonical URL

Sophi for WordPress uses `wp_get_canonical_url` function introduced in WordPress 4.6 to get the canonical URL for given post. The plugin compares this canonical URL to post permalink to set the `isCanonical` attribute.

WordPress SEO (Yoast) canonical is supported out of the box. For other SEO plugins and custom implementations, [`get_canonical_url`](https://developer.wordpress.org/reference/functions/wp_get_canonical_url/) filter can be used to change the canonical URL.

## Documentation

Sophi for WordPress has an in-depth documentation site that details the available actions and filters found within the plugin. [Visit the hook docs ☞](https://globeandmail.github.io/sophi-for-wordpress/)
Expand Down
57 changes: 32 additions & 25 deletions includes/functions/content-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ function track_event( $new_status, $old_status, $post ) {
);
}

if ( class_exists( 'WPSEO_Meta' ) ) {
return add_action( 'wpseo_saved_postdata', function() use ( $tracker, $post, $action ) {
send_track_event( $tracker, $post, $action );
} );
}

send_track_event( $tracker, $post, $action );
}

/**
* Send the track event to Sophi SnowPlow server.
*
* @since 1.0.4
*
* @param Tracker $tracker Tracker object.
* @param WP_Post $post WP_Post object.
* @param string $action Publishing action.
*/
function send_track_event( $tracker, $post, $action ) {
$data = get_post_data( $post );
$data['action'] = $action;

Expand Down Expand Up @@ -130,30 +149,12 @@ function init_tracker() {
* @return array
*/
function get_post_data( $post ) {
$content = apply_filters( 'the_content', get_the_content( null, false, $post ) );
$content = str_replace( ']]>', ']]&gt;', $content );

/**
* Filter data type of the given post.
*
* @since 1.0.0
* @hook sophi_post_data_type
*
* @param {string} $type Post data type, one of article|video|audio|image
* @param {WP_Post} $post WP_Post object.
*
* @return {string} Post data type.
*/
$type = apply_filters( 'sophi_post_data_type', get_post_format( $post ), $post );

if ( ! in_array( $type, [ 'video', 'audio', 'image' ], true ) ) {
$type = 'article';
}

$content = apply_filters( 'the_content', get_the_content( null, false, $post ) );
$content = str_replace( ']]>', ']]&gt;', $content );
$canonical_url = wp_get_canonical_url( $post );

// Support Yoast SEO canonical URL.
if ( class_exists( 'WPSEO_Options' ) ) {
if ( class_exists( 'WPSEO_Meta' ) ) {
$yoast_canonical = get_post_meta( $post->ID, '_yoast_wpseo_canonical', true );
if ( $yoast_canonical ) {
$canonical_url = $yoast_canonical;
Expand All @@ -167,18 +168,24 @@ function get_post_data( $post ) {
'accessCategory' => 'free access',
'publishedAt' => gmdate( \DateTime::RFC3339, strtotime( $post->post_date_gmt ) ),
'plainText' => wp_strip_all_tags( $content ),
'contentSize' => str_word_count( wp_strip_all_tags( $content ) ),
'size' => str_word_count( wp_strip_all_tags( $content ) ),
'sectionNames' => Utils\get_section_names( Utils\get_post_breadcrumb( $post ) ),
'modifiedAt' => gmdate( \DateTime::RFC3339, strtotime( $post->post_modified_gmt ) ),
'tags' => Utils\get_post_tags( $post ),
'url' => get_permalink( $post ),
'type' => $type,
'type' => Utils\get_post_content_type( $post ),
'isCanonical' => untrailingslashit( $canonical_url ) === untrailingslashit( get_permalink( $post ) ),
'promoImageUri' => get_the_post_thumbnail_url( $post, 'full' ),
];

// Remove empty key.
$data = array_filter( $data );
// Remove empty key, not false ones.
$data = array_filter(
$data,
function( $item ) {
return '' !== $item;
}
);

/**
* Filter post data for content sync events (aka "CMS updates" in Sophi.io terms) sent to Sophi Collector. This allows control over data before it is sent to Collector in case it needs to be modified for unique site needs. Note that if you add, change, or remove any fields with this that those changes will need to be coordinated with the Sophi.io team to ensure content is appropriately received by Collector.
*
Expand Down
9 changes: 5 additions & 4 deletions includes/functions/tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function SophiWP\Utils\get_domain;
use function SophiWP\Utils\get_section_name;
use function SophiWP\Utils\get_breadcrumb;
use function SophiWP\Utils\get_post_content_type;
use function SophiWP\Core\script_url;

/**
Expand Down Expand Up @@ -83,11 +84,11 @@ function get_tracking_data() {
'version' => get_bloginfo( 'version' ),
],
'page' => [
'type' => is_singular() ? 'article' : 'section',
'type' => is_singular() ? get_post_content_type( $post ) : 'section',
'breadcrumb' => get_breadcrumb(),
],
'content' => [
'type' => 'article',
'type' => get_post_content_type( $post ),
],
],
'settings' => [
Expand Down Expand Up @@ -219,14 +220,14 @@ function get_custom_contexts() {
global $post;

$page_data = [
'type' => is_singular() ? 'article' : 'section',
'type' => is_singular() ? get_post_content_type( $post ) : 'section',
'breadcrumb' => get_breadcrumb(),
'sectionName' => get_section_name(),
];

if ( is_singular() ) {
$content_data = [
'type' => 'article',
'type' => get_post_content_type( $post ),
'contentId' => strval( $post->ID ),
];

Expand Down
31 changes: 31 additions & 0 deletions includes/functions/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,34 @@ function( $item ) {
return true;
}
}

/**
* Get post data type.
*
* @since 1.0.4
*
* @param WP_Post $post Post object.
*
* @return string Post data type, can be article|video|audio|image
*/
function get_post_content_type( $post ) {

/**
* Filter data type of the given post.
*
* @since 1.0.0
* @hook sophi_post_content_type
*
* @param {string} $type Post data type, one of article|video|audio|image
* @param {WP_Post} $post WP_Post object.
*
* @return {string} Post data type.
*/
$type = apply_filters( 'sophi_post_content_type', get_post_format( $post ), $post );

if ( ! in_array( $type, [ 'video', 'audio', 'image' ], true ) ) {
$type = 'article';
}

return $type;
}