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

Additional content schema update #69

Merged
merged 2 commits into from
Jun 21, 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
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