"", "country" => $options['defaultCountry'], "minstar" => $options['defaultStars'], "recent" => $options['defaultRecent'] ), $atts); //Don't do anything if the ID is blank if ($atts['id'] == "") return; //Lowercase the country code $atts['country'] = strtolower($atts['country']); /* APP */ $urlApp = asrv_make_store_app_url($atts); $cacheFileApp = ASRV_CACHE_DIR.$atts['id']."_app.appstore"; $jsonApp = asrv_fetch_data($urlApp, $cacheFileApp); /* REVIEWS */ $urlReviews = asrv_make_store_url($atts); $cacheFileReviews = ASRV_CACHE_DIR.$atts['id'].$atts['country']."_reviews.appstore"; $jsonReviews = asrv_fetch_data($urlReviews, $cacheFileReviews); //Display data return asrv_review_output($jsonApp, $jsonReviews, $atts['minstar'], $atts['recent']); } /* Fetch data methods */ function asrv_fetch_data($url, $cacheFile) { if (!file_exists(ASRV_CACHE_DIR)) { mkdir(ASRV_CACHE_DIR, 0755); } //First, check if the data in cache is not too old $cacheTime = get_option("asrv_options")["cache"] * 60 * 60; if (is_readable($cacheFile) && (time() - $cacheTime < filemtime($cacheFile))) { $json_data = json_decode(file_get_contents($cacheFile)); } //Otherwise, we download fresh data and store them else { //Call the URL 4 times as it may not work the first time (API error?) $json_data = null; for ($i=0;$i<4;$i++) { if (function_exists('file_get_contents') && ini_get('allow_url_fopen')) { $json_data = asrv_fetch_data_fopen($url); } else if (function_exists('curl_exec')) { $json_data = asrv_fetch_data_curl($url); } else { wp_die('

You must have either file_get_contents() or curl_exec() enabled on your web server.

'); } //Store JSON in its original state. if (!empty($json_data)) { file_put_contents($cacheFile, json_encode($json_data)); //Don't need to try to download anymore break; } } //If no data returned from Apple (error?), we just update the modification time of the file and load the data from the cache. if ($json_data == null) { if (is_readable($cacheFile)) { touch($cacheFile); } } } return $json_data; } function asrv_make_store_url($atts) { $url = str_replace(array("{country}", "{id}"), array($atts['country'], $atts['id']), ASRV_APPSTORE_URL); $url .= "?p" . rand() . "=" . rand(); return $url; } function asrv_make_store_app_url($atts) { $url = str_replace("{id}", $atts['id'], ASRV_APPSTORE_APP_URL); return $url; } function asrv_fetch_data_fopen($url) { $data = file_get_contents($url); return json_decode($data); } function asrv_fetch_data_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $output = curl_exec($ch); curl_close($ch); return json_decode($output); } /* Display data methods */ function asrv_review_output($jsonApp, $jsonReviews, $minStar, $nbToDisplay) { if ($jsonApp->results == null) return; //Parse App info $appInfo = $jsonApp->results[0]; $app['name'] = $appInfo->trackName; $app['icon'] = $appInfo->artworkUrl100; $app['url'] = $appInfo->trackViewUrl; //Parse App reviews and get only the last X based on the number of stars $reviews = array(); foreach ($jsonReviews->feed->entry as $review) { //Parse the review and store it if it has the minimum required amount of stars $r = asrv_get_review($review, $minStar); if ($r) { $reviews[] = $r; if (count($reviews) == $nbToDisplay) { break; } } } //Render HTML if (count($reviews) > 0) { return asrv_render_html($app, $reviews); } } function asrv_get_review($data, $minStar) { $review = array(); $review["author"] = $data->author->name->label; $review["rating"] = $data->{'im:rating'}->label; $review["version"] = $data->{'im:version'}->label; $review["title"] = $data->title->label; $review["content"] = $data->content->label; if ($review["rating"] >= $minStar) { return $review; } } function asrv_transform_rating($rating) { $s1 = "" . str_repeat("★", $rating) . ""; $s2 = "" . str_repeat("★", 5 - $rating) . ""; return $s1 . $s2; } function asrv_render_html($app, $reviews) { ob_start(); ?>