{"id":4072,"date":"2020-03-22T20:22:02","date_gmt":"2020-03-23T03:22:02","guid":{"rendered":"https:\/\/codedcommerce.com\/?post_type=product&#038;p=4072"},"modified":"2023-10-08T19:31:17","modified_gmt":"2023-10-09T02:31:17","slug":"google-reviews-wordpress-short-code","status":"publish","type":"woo-code","link":"https:\/\/codedcommerce.com\/woo\/google-reviews-wordpress-short-code\/","title":{"rendered":"Google reviews WordPress short-code"},"content":{"rendered":"\n<pre class=\"wp-block-code language-php\"><code>add_shortcode( 'google_reviews', function( $atts ) {\n\n\t\/\/ Settings\n\t$default_atts = &#91; 'google_places_id' => '', 'google_places_api_key' => '' ];\n\t$atts = shortcode_atts( $default_atts, $atts, 'google_reviews' );\n\n\t\/\/ Check Cache\n\t$transient = 'ccom_g_reviews_' . substr( $atts&#91;'google_places_id'], -5 );\n\t$reviews = get_transient( $transient );\n\t$reviews = maybe_unserialize( $reviews );\n\n\t\/\/ Fetch Reviews\n\tif( ! $reviews ) {\n\t\t$url = sprintf(\n\t\t\t'https:\/\/maps.googleapis.com\/maps\/api\/place\/details\/json?placeid=%s&amp;key=%s',\n\t\t\t$atts&#91;'google_places_id'], $atts&#91;'google_places_api_key']\n\t\t);\n\t\t$response = wp_remote_get( $url );\n\t\t$response&#91;'body'] = json_decode( $response&#91;'body'] );\n\t\tif( empty( $response&#91;'body']->result->reviews ) ) { return; }\n\t\t$reviews = $response&#91;'body']->result->reviews;\n\t\tset_transient( $transient, $reviews, 86400 );\n\t}\n\n\t\/\/ Output\n\tshuffle( $reviews );\n\tob_start();\n\t?>\n\t&lt;ul class=\"google_reviews\">\n\n\t\t&lt;?php foreach( $reviews as $row ) { ?>\n\t\t&lt;li>\n\t\t\t&lt;img class=\"alignleft\"\n\t\t\t\talt=\"Avatar for &lt;?php echo $row->author_name; ?>\"\n\t\t\t\tsrc=\"&lt;?php echo $row->profile_photo_url; ?>\" \/>\n\t\t\t&lt;a href=\"&lt;?php echo $row->author_url; ?>\" target=\"_blank\">\n\t\t\t\t&lt;?php echo $row->author_name; ?>\n\t\t\t&lt;\/a>\n\t\t\t&lt;small>\n\t\t\t\t&lt;?php echo $row->relative_time_description; ?>\n\t\t\t\ton &lt;?php echo date( 'm\/d\/Y', $row->time ); ?>\n\t\t\t&lt;\/small>\n\t\t\t&lt;div class=\"star-rating\" role=\"img\"\n\t\t\t\taria-label=\"Rated &lt;?php echo $row->rating; ?> out of 5 stars\">\n\t\t\t\t&lt;span style=\"width: &lt;?php echo ( $row->rating \/ 5 ) * 100; ?>%\">\n\t\t\t\t\tRated\n\t\t\t\t\t&lt;strong class=\"rating\">\n\t\t\t\t\t\t&lt;?php echo $row->rating; ?>\n\t\t\t\t\t&lt;\/strong>\n\t\t\t\t\tout of 5\n\t\t\t\t&lt;\/span>\n\t\t\t&lt;\/div>\n\t\t\t&lt;p>&lt;?php echo $row->text; ?>&lt;\/p>\n\t\t&lt;\/li>\n\t\t&lt;?php } ?>\n\n\t&lt;\/ul>\n\t&lt;?php\n\n\t$output = ob_get_clean();\n\n\treturn $output;\n\n} );<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-4072","woo-code","type-woo-code","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/woo-code\/4072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/woo-code"}],"about":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/types\/woo-code"}],"wp:attachment":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/media?parent=4072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}