api_url_reviews = get_option("allagentsreviews_api_url_reviews"); $this->api_key_reviews = get_option("allagentsreviews_api_key_reviews"); $this->api_key_overall = get_option("allagentsreviews_api_key_overall"); $this->api_url_overall = get_option("allagentsreviews_api_url_overall"); // Register shortcode and AJAX actions add_action("wp_ajax_allagentsreviews_load_more_reviews_action", [$this, "load_more_reviews"]); add_action("wp_ajax_nopriv_allagentsreviews_load_more_reviews_action", [$this, "load_more_reviews"]); add_shortcode('allagents-display-reviews', [$this, 'display_reviews_shortcode']); } public function fetch_reviews() { // Check if transient data exists $cached_reviews = get_transient('allagentsreviews_review_cached_reviews'); // If transient data exists, return cached reviews if ($cached_reviews) { return $cached_reviews; } // Fetch reviews from the API $user_agent = sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])); $headers = array( 'Authorization' => 'APIKEY ' . sanitize_text_field($this->api_key_reviews), 'Content-Type' => 'application/json', 'User-Agent' => $user_agent, 'timeout' => 10 ); $response = wp_remote_get(sanitize_url($this->api_url_reviews), array('headers' => $headers)); // Check if request was successful if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) { $reviews = json_decode(wp_remote_retrieve_body($response), true); if (json_last_error() === JSON_ERROR_NONE) { // Set transient data to cache reviews for 1 hour set_transient('allagentsreviews_review_cached_reviews', $reviews, HOUR_IN_SECONDS); return $reviews; } } return false; } public function fetch_votes() { // Check if transient data exists $cached_reviews = get_transient('allagentsreviews_review_cached_reviews_overall'); // If transient data exists, return cached reviews if ($cached_reviews) { return $cached_reviews; } // Fetch reviews from the API $user_agent = sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])); $headers = array( 'Authorization' => 'APIKEY ' . sanitize_text_field($this->api_key_overall), 'Content-Type' => 'application/json', 'User-Agent' => $user_agent, 'timeout' => 10 ); $response = wp_remote_get(sanitize_url($this->api_url_overall), array('headers' => $headers)); // Check if request was successful if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) { $votes = json_decode(wp_remote_retrieve_body($response), true); if (json_last_error() === JSON_ERROR_NONE) { // Set transient data to cache reviews for 1 hour set_transient('allagentsreviews_review_cached_reviews_overall', $votes, HOUR_IN_SECONDS); return $votes; } } return false; } public function display_reviews($reviews) { $output = ""; if ($reviews) { $perpage = 5; $index = 0; $slice = array_slice($reviews, $index, $perpage); $lastCount = $perpage; $page = 1; $votes = $this->fetch_votes(); $text_color = get_option("allagentsreviews_shortcode_text_color"); $bg_color = get_option("allagentsreviews_shortcode_container_bg"); $review_url = get_option("allagentsreviews_api_url_reviews"); $main_branch = explode("/reviews", $review_url); $main_url = explode("/api/v1/firms/", $main_branch[0]); ?>
$review): ?>
displayRatingIcons($review['rating']); ?>
timeAgo($review['date_added'])); ?>
.co.uk" target="_blank" class="fw-bold text-dark text-decoration-underline" style="font-size: 16px">
API Error: Error loading reviews.

'; } return $output; } public function display_reviews_shortcode($atts) { // Fetch reviews $reviews = $this->fetch_reviews(); // Display reviews $output = $this->display_reviews($reviews); return wp_kses_post($output); } public function load_more_reviews() { if (isset($_POST["action"]) && $_POST["action"] === "allagentsreviews_load_more_reviews_action" && isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'allagentsreviews_ajax_nonce')) { wp_send_json($this->fetch_reviews()); } else { wp_send_json_error('Invalid request'); } } } ?>