Skip to content

Commit

Permalink
Merge branch 'release/2.7.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
JiveDig committed Nov 27, 2023
2 parents 14bf826 + a5a8a9a commit 9994b42
Show file tree
Hide file tree
Showing 53 changed files with 378 additions and 133 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.7.1 (11/27/23)
* Changed: Update updater.

## 2.7.0 (6/20/23)
* Changed: Update updater.
* Fixed: Added gitignore file for repo tidiness.
Expand Down
63 changes: 49 additions & 14 deletions classes/class-grid-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function __construct() {
*/
function hooks() {
add_filter( 'mai_grid_post_types', [ $this, 'post_types' ] );
add_filter( 'mai_link_entry', [ $this, 'disable_entry_link' ], 10, 3 );
add_filter( 'mai_entry_content', [ $this, 'entry_content' ], 10, 3 );
add_filter( 'genesis_markup_entry_close', [ $this, 'entry_schema' ], 10, 2 );
add_filter( 'genesis_markup_entry-title_content', [ $this, 'do_author_info' ], 10, 2 );
Expand All @@ -38,14 +39,42 @@ function hooks() {
*/
function post_types( $post_types ) {
$post_types[] = 'testimonial';

return array_unique( $post_types );
}

/**
* Disables entry link if post type is testimonial.
*
* @since TBD
*
* @param bool $link If linking the entry.
* @param array $args The grid block args.
* @param WP_Post|WP_Term $entry The entry.
*
* @return bool
*/
function disable_entry_link( $link, $args, $entry ) {
if ( 'WP_Post' !== get_class( $entry ) ) {
return $link;
}

if ( 'testimonial' !== $entry->post_type ) {
return $link;
}

return false;
}

/**
* Show full block content on testimonials.
*
* @since 2.1.0
*
* @param bool $link If linking the entry.
* @param array $args The grid block args.
* @param WP_Post|WP_Term $entry The entry.
*
* @return bool
*/
function entry_content( $entry_content, $args, $entry ) {
Expand All @@ -66,7 +95,7 @@ function entry_content( $entry_content, $args, $entry ) {
* @since 2.0.0
*
* @param string $close The closing markup.
* @param array $args The entry args.
* @param array $args The entry args.
*
* @return string
*/
Expand All @@ -81,23 +110,23 @@ function entry_schema( $close, $args ) {

$post = $args['params']['entry'];
$schema = [
'@context' => 'https://schema.org/',
'@type' => 'Review',
'itemReviewed' => [
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
'reviewRating' => [
'@type' => 'Rating',
'ratingValue' => '5',
],
'author' => [
'author' => [
'@type' => 'Person',
'name' => get_the_title( $post ),
],
'reviewBody' => get_the_content( $post ),
'reviewBody' => mai_testimonials_get_schema_content( $post ),
];

$schema = apply_filters( 'mai_testimonials_schema', $schema, $post );
$schema = $schema ? sprintf( '<script type="application/ld+json">%s</script>', json_encode( $schema ) ) : '';
$schema = apply_filters( 'mai_testimonials_review_schema', $schema, $post );

return $schema . $close;
mai_testimonials_get_schema( $schema );

return $close;
}

/**
Expand All @@ -106,7 +135,7 @@ function entry_schema( $close, $args ) {
* @since 2.0.0
*
* @param string $content The content.
* @param array $args The entry args.
* @param array $args The entry args.
*
* @return string
*/
Expand All @@ -120,12 +149,14 @@ function do_author_info( $content, $args ) {

// Byline.
$byline = get_post_meta( $post_id, 'byline', true );

if ( $byline ) {
$content .= sprintf( '<span class="entry-byline">%s</span>', sanitize_text_field( $byline ) );
}

// Website URL.
$url = get_post_meta( $post_id, 'url', true );

if ( $url ) {
$url = esc_url( $url );
$content .= sprintf( '<span class="entry-website"><a class="entry-website-link" href="%s" target="_blank" rel="noopener" itemprop="url">%s</a></span>', $url, $url );
Expand All @@ -139,19 +170,21 @@ function do_author_info( $content, $args ) {
*
* @since 2.0.0
*
* @param array $attributes The entry attributes.
* @param string $context The entry context.
* @param array $args The entry args.
* @param array $attributes The entry attributes.
* @param string $context The entry context.
* @param array $args The entry args.
*
* @return array
*/
function remove_schema( $attributes, $context, $args ) {
if ( ! $this->is_testimonial( $args ) ) {
return $attributes;
}

$attributes['itemprop'] = false;
$attributes['itemtype'] = false;
$attributes['itemscope'] = false;

return $attributes;
}

Expand All @@ -168,9 +201,11 @@ function is_testimonial( $args ) {
if ( ! $args ) {
return;
}

if ( ! isset( $args['params']['args']['context'] ) || 'block' !== $args['params']['args']['context'] ) {
return false;
}

if ( ! ( isset( $args['params']['entry'] ) && is_object( $args['params']['entry'] ) && 'WP_Post' === get_class( $args['params']['entry'] ) ) ) {
return false;
}
Expand Down
76 changes: 76 additions & 0 deletions classes/class-testimonials.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ function get() {
$html .= $this->get_inner();

while ( $query->have_posts() ) : $query->the_post();
global $post;

$content = get_the_content();
$image_id = $this->has_image ? get_post_thumbnail_id() : '';
$image = $this->has_image && $image_id ? wp_get_attachment_image( $image_id, 'tiny' ) : '';
Expand Down Expand Up @@ -250,10 +252,14 @@ function get() {
]
);

$this->add_review( $post );

endwhile;

$html .= '</div>'; // Inner.

$this->add_schema();

$html .= '</div>'; // Open.

if ( $this->has_slider && 1 === $this->args['paged'] ) {
Expand Down Expand Up @@ -368,6 +374,8 @@ function get_query_args() {
}
}

$query_args = apply_filters( 'mai_testimonials_query_args', $query_args, $this->args );

return $query_args;
}

Expand Down Expand Up @@ -725,6 +733,74 @@ function get_next_page( $query ) {
return $page;
}

/**
* Adds review schema to cache.
*
* @since TBD
*
* @param WP_Post The post object.
*
* @return void
*/
function add_review( $post ) {
$schema = [
'@type' => 'Review',
'reviewRating' => [
'@type' => 'Rating',
'ratingValue' => '5',
],
'author' => [
'@type' => 'Person',
'name' => get_the_title( $post ),
],
'reviewBody' => mai_testimonials_get_schema_content( $post ),
];

$schema = apply_filters( 'mai_testimonials_review_schema', $schema, $post );

mai_testimonials_get_schema( $schema );
}

/**
* Adds schemas
*
* @since TBD
*
* @return void
*/
function add_schema() {
$reviews = mai_testimonials_get_schema( [], true );

if ( ! $reviews ) {
return;
}

$total = $best = 0;

foreach ( $reviews as $review ) {
$total = $total + absint( $review['reviewRating']['ratingValue'] );
$best = $best + 5;
}

$schema = [
'@context' => 'https://schema.org/',
'@type' => 'Organization',
'name' => get_bloginfo( 'name' ),
// 'datePublished' => '2023-06-07T12:19:25+00:00'
'aggregateRating' => [
'@type' => 'AggregateRating',
'ratingValue' => $total,
'bestRating' => $best,
'ratingCount' => count( $reviews ),
],
'review' => $reviews,
];

$schema = apply_filters( 'mai_testimonials_schema', $schema );

mai_testimonials_get_schemas( $schema );
}

/**
* Sanitizes taxonomies.
*
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

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

71 changes: 71 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,74 @@ function mai_testimonials_get_suffix() {

return $suffix;
}

/**
* Gets all schemas.
*
* @access private
*
* @since TBD
*
* @param array $schemas
*
* @return array
*/
function mai_testimonials_get_schemas( $schemas = [] ) {
static $cache = [];

if ( $schemas ) {
$cache[] = $schemas;
}

return $cache;
}

/**
* Gets Review schema.
* Optionally add new schema to the static variable.
*
* @access private
*
* @since TBD
*
* @param array $review Array of schema data.
* @param bool $clear If we should clear cache after storing values.
*
* @return array
*/
function mai_testimonials_get_schema( $review = [], $clear = false ) {
static $cache = [];

if ( $review ) {
$cache[] = $review;
}

$return = $cache;

if ( $clear ) {
$cache = [];
}

return $return;
}

/**
* Gets sanitized schema content from post.
*
* @access private
*
* @since TBD
*
* @param WP_post $post The post object.
*
* @return string
*/
function mai_testimonials_get_schema_content( $post ) {
$content = get_the_content( $post );
$content = do_blocks( $content );
$content = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content ); // Strip script and style tags.
$content = strip_tags( $content, [ 'a' ] ); // Strip tags, leave links.
$content = trim( $content );

return wpautop( $content );
}
Loading

0 comments on commit 9994b42

Please sign in to comment.