<?php define('AERIA', 'vTest'); define('AERIA_URL', ''); define('WPINC', __DIR__); require_once __DIR__ . '/wp-includes-functions.php'; require_once __DIR__ . '/../classes/AeriaNetwork.php'; require_once __DIR__ . '/../classes/AeriaCache.php'; require_once __DIR__ . '/../classes/AeriaSocial.php'; AeriaCache::setDriver('Redis'); AeriaSocial::init(); print_r(AeriaSocial::getCount(['http://google.it', 'http://google.com', 'http://facebook.com']));
public static function getCount($uris, $only_cache = false) { if (!is_array($uris)) { $uris = [$uris]; } $result = []; $uris_to_fetch = []; $hashes = []; foreach (array_keys(static::$config['services']) as $service_key) { $uris_to_fetch[$service_key] = []; } foreach ($uris as $uri) { $hashes[$uri] = static::getHash($uri); $data = ['uri' => $uri, 'hash' => $hashes[$uri], 'services' => [], 'errors' => [], 'sum' => 0]; foreach (array_keys(static::$config['services']) as $service_key) { $count = AeriaCache::get(static::$hash_prefix . $hashes[$uri] . '_' . $service_key); if ($count == null || time() > $count['expire']) { if ($only_cache === false) { $uris_to_fetch[$service_key][] = $uri; } } $data['services'][$service_key] = !is_null($count) ? intval($count['count']) : 0; $result[$uri] = $data; } } foreach ($uris_to_fetch as $service_key => $uris_to_fetch_per_service) { if (empty($uris_to_fetch_per_service)) { continue; } $api_data = forward_static_call(['static', 'getCountForService' . ucfirst($service_key)], $uris_to_fetch_per_service); foreach ($uris_to_fetch_per_service as $uri) { if (isset($api_data['errors'][$uri])) { $result[$uri]['errors'][$service_key] = $api_data['errors'][$uri]; } else { if (isset($api_data['count'][$uri])) { $count = $api_data['count'][$uri] ?: 0; if ($count < $result[$uri]['services'][$service_key]) { $result[$uri]['errors'][$service_key] = "Greater count in cache"; } else { if ($count > $result[$uri]['services'][$service_key]) { $result[$uri]['services'][$service_key] = $count; } } } else { $result[$uri]['errors'][$service_key] = "Empty count"; } } AeriaCache::set(['expire' => time() + static::$config['cache_ttl'], 'count' => $result[$uri]['services'][$service_key]], static::$hash_prefix . $hashes[$uri] . '_' . $service_key); } } foreach ($result as $uri => $data) { $result[$uri]['sum'] = array_reduce($data['services'], function ($carry, $item) { return $carry + intval($item); }, 0); AeriaCache::set(['uri' => $uri, 'sum' => $data['sum']], static::$hash_prefix . 'SUMS_' . $hashes[$uri]); } return $result; }