From ad49ac2f9f562a9023aac8983484cc8bb28e62ae Mon Sep 17 00:00:00 2001 From: Celso Bessa Date: Tue, 14 Aug 2018 01:24:01 -0300 Subject: [PATCH 1/3] wip20180814a - new filter: testimonials_get_custom_fields_settings (possible fix for #70 - Initial WordPress Coding Standards revision - Changed create_function for an anonymous function (create_functions are deprecated in PHP 7.2), possible fix for #79 - new function woothemes_testimonials_generate_ld_json, which generates LD+JSON markup that includes itemReviewed information, possible fix for #80 and #81 --- classes/class-woothemes-testimonials.php | 36 +++---- .../class-woothemes-widget-testimonials.php | 6 +- ld+json-example.json | 54 +++++++++++ woothemes-testimonials-template.php | 97 ++++++++++++++++--- 4 files changed, 160 insertions(+), 33 deletions(-) create mode 100644 ld+json-example.json diff --git a/classes/class-woothemes-testimonials.php b/classes/class-woothemes-testimonials.php index d2f47d4..65169c4 100644 --- a/classes/class-woothemes-testimonials.php +++ b/classes/class-woothemes-testimonials.php @@ -17,7 +17,7 @@ class Woothemes_Testimonials { private $assets_dir; private $assets_url; private $token; - public $version; + public $version; private $file; /** @@ -344,34 +344,34 @@ public function enqueue_admin_styles () { * @since 1.0.0 * @return array */ - public function get_custom_fields_settings () { + public function get_custom_fields_settings() { $fields = array(); $fields['gravatar_email'] = array( - 'name' => __( 'Gravatar E-mail Address', 'woothemes-testimonials' ), - 'description' => sprintf( __( 'Enter in an e-mail address, to use a %sGravatar%s, instead of using the "Featured Image".', 'woothemes-testimonials' ), '', '' ), - 'type' => 'text', - 'default' => '', - 'section' => 'info' + 'name' => __( 'Gravatar E-mail Address', 'woothemes-testimonials' ), + 'description' => sprintf( __( 'Enter in an e-mail address, to use a %sGravatar%s, instead of using the "Featured Image".', 'woothemes-testimonials' ), '', '' ), + 'type' => 'text', + 'default' => '', + 'section' => 'info', ); $fields['byline'] = array( - 'name' => __( 'Byline', 'woothemes-testimonials' ), - 'description' => __( 'Enter a byline for the customer giving this testimonial (for example: "CEO of WooThemes").', 'woothemes-testimonials' ), - 'type' => 'text', - 'default' => '', - 'section' => 'info' + 'name' => __( 'Byline', 'woothemes-testimonials' ), + 'description' => __( 'Enter a byline for the customer giving this testimonial (for example: "CEO of WooThemes").', 'woothemes-testimonials' ), + 'type' => 'text', + 'default' => '', + 'section' => 'info', ); $fields['url'] = array( - 'name' => __( 'URL', 'woothemes-testimonials' ), - 'description' => __( 'Enter a URL that applies to this customer (for example: http://woothemes.com/).', 'woothemes-testimonials' ), - 'type' => 'url', - 'default' => '', - 'section' => 'info' + 'name' => __( 'URL', 'woothemes-testimonials' ), + 'description' => __( 'Enter a URL that applies to this customer (for example: http://woothemes.com/).', 'woothemes-testimonials' ), + 'type' => 'url', + 'default' => '', + 'section' => 'info', ); - return $fields; + return apply_filters( 'testimonials_get_custom_fields_settings', $fields ); } // End get_custom_fields_settings() /** diff --git a/classes/class-woothemes-widget-testimonials.php b/classes/class-woothemes-widget-testimonials.php index 53e022b..da807eb 100644 --- a/classes/class-woothemes-widget-testimonials.php +++ b/classes/class-woothemes-widget-testimonials.php @@ -267,5 +267,7 @@ protected function get_order_options () { } // End Class /* Register the widget. */ -add_action( 'widgets_init', create_function( '', 'return register_widget("Woothemes_Widget_Testimonials");' ), 1 ); -?> \ No newline at end of file +add_action( 'widgets_init', function() { + return register_widget( 'Woothemes_Widget_Testimonials' ); +}, 1 ); +?> diff --git a/ld+json-example.json b/ld+json-example.json new file mode 100644 index 0000000..ca041ed --- /dev/null +++ b/ld+json-example.json @@ -0,0 +1,54 @@ + \ No newline at end of file diff --git a/woothemes-testimonials-template.php b/woothemes-testimonials-template.php index 91c6236..ca64194 100644 --- a/woothemes-testimonials-template.php +++ b/woothemes-testimonials-template.php @@ -57,7 +57,6 @@ function woothemes_testimonials ( $args = '' ) { // Allow child themes/plugins to filter here. $args = apply_filters( 'woothemes_testimonials_args', $args ); $html = ''; - do_action( 'woothemes_testimonials_before', $args ); // The Query. @@ -105,21 +104,23 @@ function woothemes_testimonials ( $args = '' ) { $author = ''; $author_text = ''; - // If we need to display the author, get the data. - if ( ( get_the_title( $post ) != '' ) && true == $args['display_author'] ) { + $post_title = get_the_title( $post ); + $byline = $post->byline; + $author_url = esc_url( $post->url ); + if ( ( $post_title != '' ) && true == $args['display_author'] ) { $author .= '' . "\n"; @@ -134,8 +135,8 @@ function woothemes_testimonials ( $args = '' ) { $template = str_replace( '%%ID%%', get_the_ID(), $template ); $template = str_replace( '%%CLASS%%', esc_attr( $css_class ), $template ); - if ( isset( $post->image ) && ( '' != $post->image ) && true == $args['display_avatar'] && ( '' != $post->url ) ) { - $template = str_replace( '%%AVATAR%%', '' . $post->image . '', $template ); + if ( isset( $post->image ) && ( '' != $post->image ) && true == $args['display_avatar'] && ( '' != $author_url ) ) { + $template = str_replace( '%%AVATAR%%', '' . $post->image . '', $template ); } elseif ( isset( $post->image ) && ( '' != $post->image ) && true == $args['display_avatar'] ) { $template = str_replace( '%%AVATAR%%', $post->image, $template ); } else { @@ -156,6 +157,7 @@ function woothemes_testimonials ( $args = '' ) { if( is_numeric( $args['per_row'] ) && ( $args['per_row'] > 0 ) && ( 0 == $count % $args['per_row'] ) ) { $html .= '
' . "\n"; } + $html .= woothemes_testimonials_generate_ld_json( $post ); } wp_reset_postdata(); @@ -175,7 +177,6 @@ function woothemes_testimonials ( $args = '' ) { // Allow child themes/plugins to filter here. $html = apply_filters( 'woothemes_testimonials_html', $html, $query, $args ); - if ( $args['echo'] != true ) { return $html; } // Should only run is "echo" is set to true. @@ -194,7 +195,7 @@ function woothemes_testimonials ( $args = '' ) { * @return string Output using the template tag. */ function woothemes_testimonials_shortcode ( $atts, $content = null ) { - $args = (array)$atts; + $args = (array) $atts; $defaults = array( 'limit' => 5, @@ -248,4 +249,74 @@ function woothemes_testimonials_content_default_filters () { } // End woothemes_testimonials_content_default_filters() add_action( 'woothemes_testimonials_before', 'woothemes_testimonials_content_default_filters' ); -} \ No newline at end of file +} + +/** + * Generates LD+JSON markup for testimonials. + * + * Generates LD+JSON markup for testimonials. + * + * @since 1.6.0 + * @param WP_Post $post A WP_Post object. + * @return string $ld_json_output The JSON+LD markup. + */ +function woothemes_testimonials_generate_ld_json( $post ) { + $byline = get_post_meta( $post->ID, '_byline', true ); + $author_url = esc_url( get_post_meta( $post->ID, '_url', true ) ); + $post_title = get_the_title( $post ); + $post_permalink = esc_url( get_post_permalink( $post->ID ) ); + $post_published_date = str_replace( ' ', 'T', $post->post_date_gmt ); + $language = get_bloginfo( 'language' ); + $organization = get_bloginfo( 'name' ); + $organization_url = esc_url( get_bloginfo( 'url' ) ); + $content = wp_strip_all_tags( apply_filters( 'the_content', $post->post_content ), true ); + $ld_json_output = << + { + "@context": "http://schema.org", + "@type": "Review", + "author": { + "@type": "Person", + "name": "$post_title", + "jobTitle": "$byline", + "sameAs": "$author_url" + }, + "url": "$post_permalink", + "datePublished": "$post_published_date", + "description": "$content", + "inLanguage": "$language", + "itemReviewed": { + "@type": "Organization", + "name": "$organization", + "sameAs": "$organization_url", + "telephone": "+12122459600" + }, + "reviewRating": { + "@type": "Rating", + "worstRating": 1, + "bestRating": 5, + "ratingValue": 5 + } + } + +EOD; + return $ld_json_output; +} + +/** + * Adds LD+JSON markup for single testimonials. + * + * Adds LD+JSON markup for single testimonials. + * + * @since 1.6.0 + * @param WP_Post $post A WP_Post object. + * @return string $ld_json_output The JSON+LD markup. + */ +function woothemes_testimonials_single_post_ld_json() { + if ( ! is_singular( 'testimonial' ) ) { + return; + } + global $post; + echo woothemes_testimonials_generate_ld_json( $post ); // WPCS: XSS ok. +} +add_action( 'wp_head', 'woothemes_testimonials_single_post_ld_json' ); From f35b049c55d77eec91296069cbba1f2647e9c04d Mon Sep 17 00:00:00 2001 From: Celso Bessa Date: Tue, 14 Aug 2018 01:26:41 -0300 Subject: [PATCH 2/3] URda phone number --- woothemes-testimonials-template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/woothemes-testimonials-template.php b/woothemes-testimonials-template.php index ca64194..d279aa2 100644 --- a/woothemes-testimonials-template.php +++ b/woothemes-testimonials-template.php @@ -289,7 +289,7 @@ function woothemes_testimonials_generate_ld_json( $post ) { "@type": "Organization", "name": "$organization", "sameAs": "$organization_url", - "telephone": "+12122459600" + "telephone": "+5511975414069" }, "reviewRating": { "@type": "Rating", From 9020f3804ff9e76ad0f1e09cb590aebfbe0ea39c Mon Sep 17 00:00:00 2001 From: Celso Bessa Date: Tue, 14 Aug 2018 01:32:07 -0300 Subject: [PATCH 3/3] removed inline schema.org markup --- ld+json-example.json | 54 ----------------------------- woothemes-testimonials-template.php | 14 ++++---- 2 files changed, 8 insertions(+), 60 deletions(-) delete mode 100644 ld+json-example.json diff --git a/ld+json-example.json b/ld+json-example.json deleted file mode 100644 index ca041ed..0000000 --- a/ld+json-example.json +++ /dev/null @@ -1,54 +0,0 @@ - \ No newline at end of file diff --git a/woothemes-testimonials-template.php b/woothemes-testimonials-template.php index d279aa2..0c0388a 100644 --- a/woothemes-testimonials-template.php +++ b/woothemes-testimonials-template.php @@ -84,11 +84,13 @@ function woothemes_testimonials ( $args = '' ) { $html .= '
' . "\n"; // Begin templating logic. - $tpl = '
%%TEXT%%
%%AVATAR%% %%AUTHOR%%
'; + $tpl = '
%%TEXT%%
%%AVATAR%% %%AUTHOR%%
'; $tpl = apply_filters( 'woothemes_testimonials_item_template', $tpl, $args ); $count = 0; - foreach ( $query as $post ) { $count++; + foreach ( $query as $post ) { + $count++; + $template = $tpl; $css_class = 'quote'; @@ -109,18 +111,18 @@ function woothemes_testimonials ( $args = '' ) { $byline = $post->byline; $author_url = esc_url( $post->url ); if ( ( $post_title != '' ) && true == $args['display_author'] ) { - $author .= '