Re: Як скоротити функцію php
Спроба 1:
function currency_course($atts)
{
$name_prefix = 'currency_rate_';
$atts = shortcode_atts(
array(
'name' => 'BTC',
), $atts, 'currency' );
$key_name = $name_prefix + $atts['name'];
$m = new Memcached();
if(!$m) return false;
if(!$m->addServer('localhost', 11211)) return false;
$rate = $m->get($key_name);
if( !$rate) {
$data = file_get_contents(LINK);
if (!$data) return false;
$courses = json_decode($data, true);
if(!courses) return false;
foreach ($courses as $course){
if(!$m->set($name_prefix+$course['symbol'], $course['price_usd'],5*60)) return false;
}
$rate = $m->get($key_name);
if(!rate) return false;
}
return $rate;
}
add_shortcode( 'currency', 'currency_course' );