Skip to content

Commit

Permalink
Merge pull request #69 from globeandmail/fix/content-sync-schema
Browse files Browse the repository at this point in the history
Additional content schema update
  • Loading branch information
jeffpaul committed Jun 21, 2021
2 parents 6a50e76 + 28eeace commit b602bed
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 52 deletions.
4 changes: 2 additions & 2 deletions includes/functions/content-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function track_event( $new_status, $old_status, $post ) {

$tracker->trackUnstructEvent(
[
'schema' => 'iglu:com.sophi/content_update/jsonschema/1-0-4',
'schema' => 'iglu:com.sophi/content_update/jsonschema/2-0-0',
'data' => $data,
],
[
Expand Down Expand Up @@ -158,7 +158,7 @@ function get_post_data( $post ) {
'publishedAt' => gmdate( \DateTime::RFC3339, strtotime( $post->post_date_gmt ) ),
'plainText' => wp_strip_all_tags( $content ),
'contentSize' => str_word_count( wp_strip_all_tags( $content ) ),
'sectionNames' => Utils\get_section_names( Utils\get_breadcrumb( $post ) ),
'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 ),
Expand Down
110 changes: 60 additions & 50 deletions includes/functions/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,73 +21,83 @@ function get_breadcrumb() {

if ( is_singular() ) {
$post = get_post();
return get_post_breadcrumb( $post );
}

// If current post is hierarchical, we use page ancestors for breadcrumb.
if ( is_post_type_hierarchical( $post->post_type ) ) {
$ancestors = array_map(
function( $parent ) {
$post_object = get_post( $parent );
return $post_object->post_name;
},
get_post_ancestors( $post )
);
if ( is_tax() || is_tag() || is_category() ) {
$current_term = get_queried_object();
return get_term_breadcrumb( $current_term );
}

if ( count( $ancestors ) > 0 ) {
return implode(
':',
array_reverse( $ancestors )
);
}
}
return '';
}

// If the current post isn't hierarchical, we use taxonomy.
$taxonomies = array_filter(
get_object_taxonomies( $post ),
'is_taxonomy_hierarchical'
/**
* Get post breadcrumb.
*
* @param WP_Post $post Post object.
*
* @return string
*/
function get_post_breadcrumb( $post ) {
// If current post is hierarchical, we use page ancestors for breadcrumb.
if ( is_post_type_hierarchical( $post->post_type ) ) {
$ancestors = array_map(
function( $parent ) {
$post_object = get_post( $parent );
return $post_object->post_name;
},
get_post_ancestors( $post )
);

if ( count( $taxonomies ) > 0 ) {
$terms = get_the_terms( $post, $taxonomies[0] );

if ( count( $terms ) > 0 ) {
return get_term_breadcrumb( $terms[0] );
}
} else { // Just return the current term if it's not hierarchical.
$non_hierarchial_taxonomies = array_filter(
get_object_taxonomies( $post ),
function( $taxonomy ) {
return ! is_taxonomy_hierarchical( $taxonomy );
}
if ( count( $ancestors ) > 0 ) {
return implode(
':',
array_reverse( $ancestors )
);
if ( count( $non_hierarchial_taxonomies ) > 0 ) {
$terms = get_the_terms( $post, $non_hierarchial_taxonomies[0] );

if ( count( $terms ) > 0 ) {
return $terms[0]->slug;
}
}
}
}

// Use post type archive as the fallback.
$post_type_obj = get_post_type_object( $post->post_type );
// If the current post isn't hierarchical, we use taxonomy.
$taxonomies = array_filter(
get_object_taxonomies( $post ),
'is_taxonomy_hierarchical'
);

if ( ! $post_type_obj->has_archive ) {
return '';
if ( count( $taxonomies ) > 0 ) {
$terms = get_the_terms( $post, $taxonomies[0] );

if ( count( $terms ) > 0 ) {
return get_term_breadcrumb( $terms[0] );
}
} else { // Just return the current term if it's not hierarchical.
$non_hierarchial_taxonomies = array_filter(
get_object_taxonomies( $post ),
function( $taxonomy ) {
return ! is_taxonomy_hierarchical( $taxonomy );
}
);
if ( count( $non_hierarchial_taxonomies ) > 0 ) {
$terms = get_the_terms( $post, $non_hierarchial_taxonomies[0] );

if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
return $post_type_obj->rewrite['slug'];
if ( count( $terms ) > 0 ) {
return $terms[0]->slug;
}
}
}

return $post_type_obj->post_type;
// Use post type archive as the fallback.
$post_type_obj = get_post_type_object( $post->post_type );

if ( ! $post_type_obj->has_archive ) {
return '';
}

if ( is_tax() || is_tag() || is_category() ) {
$current_term = get_queried_object();
return get_term_breadcrumb( $current_term );
if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
return $post_type_obj->rewrite['slug'];
}

return '';
return $post_type_obj->post_type;
}

/**
Expand Down

0 comments on commit b602bed

Please sign in to comment.