FacebookReviews

Class FacebookReviews.


Source Source

File: mortgage-facebook-review/inc/FacebookReviews.php

class FacebookReviews {


	/**
	 * Get Facebook reviews.
	 *
	 * @access public
	 * @since 1.0.0
	 *
	 * @param array|string $atts shortcode attributes
	 *
	 * @return array {
	 *       Reviews.
	 *  @type array reviewer {
	 *             Reviewer.
	 *      @type string 'name' The person's full name
	 *      @type string 'link' The link of reviews page
	 *      @type string 'image' The person's profile picture url
	 *      @type string 'rating' Rating
	 *      @type string 'text' Review text included in the review
	 *      @type string 'date' When the reviewer rated this object
	 *  }
	 * }
	 */
	public function get_reviews($atts){

		$page_id = get_option('mortgage_facebook_widget')['page_id'] ?? '';
		$access_token = isset(get_option('mortgage_facebook_widget')['access_token']) ? get_option('mortgage_facebook_widget')['access_token'] : '' ;

        if(empty($page_id) && empty($access_token)) return array();

        $count = isset($atts['count']) && !empty($atts['count']) ? $atts['count'] : (isset(get_option('mortgage_facebook_widget')['count']) ? get_option('mortgage_facebook_widget')['count'] : 3);

		$url = "https://graph.facebook.com/v10.0/".$page_id;
		$query = array(
			"access_token"=>$access_token,
			"fields"=> 'ratings.limit('.$count.').fields(
							reviewer{
								id,
								name,
								picture{url}},
							created_time,
							rating,
							recommendation_type,
							review_text)',
		);

		$url = $url . '?'. http_build_query($query);

		$reviews = $this->request($url,1);

		$reviews = $reviews['ratings']['data'] ?? array();
		$reviews = $this->prepare_reviews($reviews);

		return $reviews;
	}

	/**
	 * Request.
	 *
	 * @access protected
	 * @since 1.0.0
	 *
	 * @param string $url URL
	 * @param bool|null $associative [optional] When true, returned objects will be converted into associative arrays.
	 *
	 * @return mixed $response The body of the response. Empty string if no body or incorrect parameter given.
	 */
	protected function request(string $url, $associative = false ){
		$response = get_transient(md5($url));
		if(!$response) {
			$response = wp_remote_get( $url, array(
					"headers" => array(
						'Content-Type' => 'application/json',
					)
				)
			);
			$response = wp_remote_retrieve_body( $response );

			$response = json_decode( $response, $associative );
			set_transient( md5( $url ), $response, DAY_IN_SECONDS );

		}
		return $response;
	}


	/**
	 * Prepare reviews to display.
	 * @access protected
	 * @since 1.0.0
	 * @param array $reviews Reviews{
	 *                       Array of reviews.
	 *
	 *  @type array review {
	 *             A list of up to three reviews.
	 *  @type array reviewer {
	 *      @type string "id" The app user's App-Scoped User ID. This ID is unique to the app and cannot be used by other apps.
	 *      @type string "name" The person's full name
	 *      @type array "picture" {
	 *                       The person's profile picture.
	 *          @type array "data" {
	 *              @type string "url" The person's profile picture url
	 *          }
	 *  }
	 *  @type string "created_time" When the reviewer rated this object
	 *  @type int "rating" Rating (1-5 stars)
	 *  @type string "recommendation_type" Whether or not this recommendation was "positive" or "negative"
	 *  @type string "review_text" Review text included in the review
	 *
	 * @return array {
	 *       Reviews.
	 *  @type array reviewer {
	 *             Reviewer.
	 *      @type string 'name' The person's full name
	 *      @type string 'link' The link of reviews page
	 *      @type string 'image' The person's profile picture url
	 *      @type string 'rating' Rating
	 *      @type string 'text' Review text included in the review
	 *      @type string 'date' When the reviewer rated this object
	 *  }
	 * }
	 *
	 */
	protected function prepare_reviews($reviews){

		$facebook_review = array();
		foreach ($reviews as $review) {
			$rating = '';
			if (isset($review['rating'])) {
				$rating = $review['rating'];
			} elseif (isset($review['recommendation_type'])) {
				$rating = 'negative' === $review['recommendation_type'] ? 1 : 5;
			}
			$facebook_review[] = array(
				"name" => sanitize_text_field($review['reviewer']['name']),
				"link" => get_option('mortgage_facebook_widget')['page_id'] ? esc_url_raw('https://www.facebook.com/'.get_option('mortgage_facebook_widget')['page_id'] .'/reviews') : '',
				"image" => isset($review['reviewer']['picture']['data']['url']) ? esc_url_raw($review['reviewer']['picture']['data']['url']) : '',
				"rating" => $rating,
				"text" => sanitize_text_field($review['review_text']),
				"date" => date_i18n('j F Y H:i:s',sanitize_text_field($review['created_time'])),
			);
		}
		return $facebook_review;
	}

}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.