Skip to content

Instantly share code, notes, and snippets.

@bnecreative
Last active November 22, 2017 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnecreative/26848f3f36bbe696242c39207350e357 to your computer and use it in GitHub Desktop.
Save bnecreative/26848f3f36bbe696242c39207350e357 to your computer and use it in GitHub Desktop.
Dev - Testimonials Pro - Zomato API
<?php
/*
* BNE Testimonials Custom API Branding Assets
*
* The API branding is located where the tagline/website is within the
* testimonial. This filters into area to include additional API Source
* branding. Does not effect the badge.
*
* $api_brands array The returned arguments for the tagline branding
* should be an array with img, alt, width, height,
* badge, badge_width, and rating_color keys
*
* returns $api_brands
*
*/
function custom_bne_testimonials_api_branding_assets( $api_brands ) {
/*
* Note: Argument Meanings for $api_brands
*
* img = Brand image used within single testimonial. Shown where the tagline normally is at.
* alt = Brand image alternate tag
* width = Brand image width (px)
* height = Brand image height (px)
* badge = Brand Image/logo used in [bne_testimonials_badge]
* badge_width = Brand Image/logo width used in [bne_testimonials_badge]
* rating_color = rating #/color name for the stars (optional)
*/
// My Source
$api_brands['zomato'] = array(
'img' => 'path_to_image.jpg',
'alt' => 'Powered by Zomato',
'width' => '150px',
'height' => '',
'badge' => 'path_to_image.jpg',
'badge_width' => '50px',
'rating_color' => '#CB202D',
);
return $api_brands;
}
add_filter( 'bne_testimonials_api_branding_assets', 'custom_bne_testimonials_api_branding_assets' );
<?php
/*
* BNE Testimonials Custom API Field Assignments
*
* This allows customizing the final reponse from the API request.
* In general, this makes it to where the correct data is assigned to the
* correct area of the shortcode output.
*
* $testimonial array The assigned data for each individual testimonial
* $source string The source API type
*
* returns $testimonial
*
*/
function custom_bne_testimonials_api_fields( $testimonial, $source ) {
/*
* Note: Argument Meanings for $testimonial
*
* name = The person's name
* image = The person's profile image, if available
* message = The person's review
* website = Direct link to review at source
* tagline = A tagline or shorter review note
* rating = The person's rating
* date = Date of review
* id = Unique ID (optional)
*/
// Zomato API Source
if( $source == 'zomato') {
$id = rand(1,100);
$testimonial = array(
'name' => $testimonial['review']['user']['name'],
'image' => $testimonial['review']['user']['profile_image'],
'message' => $testimonial['review']['review_text'],
'website' => 'https://www.zomato.com/review/'.$testimonial['review']['id'],
'tagline' => $testimonial['review']['rating_text'],
'rating' => $testimonial['review']['rating'],
'date' => date('Y-m-d', $testimonial['review']['timestamp'] ),
'id' => $source.$id
);
}
return $testimonial;
}
add_filter( 'bne_testimonials_api_fields', 'custom_bne_testimonials_api_fields', 10, 2 );
<?php
/*
* BNE Testimonials Custom API Sources
*
* This allows customizing existing API's or adding custom API
* arguments to a request using the API shortcodes.
*
* $api_data array The array arguments for the API request
* $source string The source API type
* $id string The listing ID to pull reviews of and defined in shortcode using id="XXX"
* $key string A API Key or token to use to authorize the API request and can be defined in shortcode using key="XXX"
*
* returns $api_data
*
*/
function custom_bne_testimonials_api_source_args( $api_data, $source, $id, $key ) {
// Zomato API Source
if( $source == 'zomato') {
// Authorize using a Personal Token Key
$api_data['api_remote_args'] = array(
'user-agent' => '',
'headers' => array(
'user-key' => $key,
),
);
$api_data['api_url'] = esc_url( "https://developers.zomato.com/api/v2.1/restaurant?res_id={$id}" );
}
return $api_data;
}
add_filter( 'bne_testimonials_api_source_args', 'custom_bne_testimonials_api_source_args', 10, 4 );
<?php
/*
* BNE Testimonials Custom API Response
*
* This allows customizing the final reponse from the API request.
* In general, this makes it to where the correct data is assigned to the
* correct area of the shortcode output.
*
* $testimonials array The final array of how data is assigned from the API
* $source string The source API type
* $api_response array The initial API resonse array of complete data.
*
* returns $testimonials
*
*/
function custom_bne_testimonials_api_source_response( $testimonials, $source, $api_response ) {
/*
* Note: Argument Meanings for $testimonials
*
* name = Business Name
* icon = An Icon image for the source or business image
* rating = The Rating of the Business
* total = The total number of reviews
* url = The link to the business page
* reviews = An array of reviews
*/
// Zomato API Source
if( $source == 'zomato') {
/*
* With Zomato, the initial API response only includes part of what we need,
* so we need to do one more request for the other part.
*
* The initial response includes our key and id already so we can re-use it.
* Authorize using a Personal Token Key.
*
*/
$api_remote_args = array(
'user-agent' => '',
'headers' => array(
'user-key' => $api_response['apikey'],
),
);
$reviews_request = wp_remote_get( esc_url( "https://developers.zomato.com/api/v2.1/reviews?res_id={$api_response['id']}" ), $api_remote_args );
$reviews_body = wp_remote_retrieve_body( $reviews_request );
$review_data = json_decode( $reviews_body, true );
// Final Assignments
$testimonials = array(
'name' => $api_response['name'],
'icon' => $api_response['thumb'],
'rating' => $api_response['user_rating']['aggregate_rating'],
'total' => $api_response['user_rating']['votes'],
'url' => $api_response['url'],
'reviews' => $review_data['user_reviews'],
);
}
return $testimonials;
}
add_filter( 'bne_testimonials_api_source_response', 'custom_bne_testimonials_api_source_response', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment