"", "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']); //Retrieve data and store it $json_data = asrv_fetch_data($atts); //Display data return asrv_review_output($json_data, $atts['minstar'], $atts['recent']); } /* Fetch data methods */ function asrv_fetch_data($atts) { 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; $cacheFile = ASRV_CACHE_DIR.$atts['id'].$atts['country'].".appstore"; 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($atts); } else if (function_exists('curl_exec')) { $json_data = asrv_fetch_data_curl($atts); } 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 ($json_data->feed->entry) { 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 || $json_data->feed->entry == null) { if (is_readable($cacheFile)) { touch($cacheFile); } } } return $json_data->feed; } 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_fetch_data_fopen($atts) { $data = file_get_contents(asrv_make_store_url($atts)); return json_decode($data); } function asrv_fetch_data_curl($atts) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, asrv_make_store_url($atts)); 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($json_data, $minStar, $nbToDisplay) { if ($json_data->entry == null) return; //Parse App info $appInfo = $json_data->entry[0]; $app['name'] = $appInfo->{'im:name'}->label; $app['icon'] = asrv_get_appIcon($appInfo); $app['url'] = $appInfo->link->attributes->href; //Parse App reviews and get only the last X based on the number of stars $first = true; $reviews = array(); foreach ($json_data->entry as $review) { //Skip first entry as it contains only app information if ($first) { $first = false; continue; } //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_appIcon($appInfo) { foreach ($appInfo->{'im:image'} as $img) { if ($img->attributes->height == 53) { return $img->label; } } return null; } 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(); ?>