private function InstapageCmsPluginAjaxController::getStats in Instapage plugin 7.3
Same name and namespace in other branches
- 8.3 core/InstapageCmsPluginAjaxController.php \InstapageCmsPluginAjaxController::getStats()
Gets the stats of landing pages from local cache or from app, if cache is not present / invalid.
1 call to InstapageCmsPluginAjaxController::getStats()
- InstapageCmsPluginAjaxController::doAction in core/
InstapageCmsPluginAjaxController.php - Executes an action set in the request.
File
- core/
InstapageCmsPluginAjaxController.php, line 403
Class
- InstapageCmsPluginAjaxController
- Main controller for AJAX actions. Results are returned as encoded JSON objects. Data for actions are stored in $_POST['data'] table.
Code
private function getStats() {
$post = InstapageCmsPluginHelper::getPostData();
$page = InstapageCmsPluginPageModel::getInstance();
$api = InstapageCmsPluginAPIModel::getInstance();
$subaccount = InstapageCmsPluginSubaccountModel::getInstance();
$pages = isset($post->data->pages) ? $post->data->pages : array();
if (!count($pages)) {
InstapageCmsPluginHelper::writeDiagnostics('Stats cond', 'No pages in request');
echo json_encode((object) array(
'status' => 'OK',
'data' => array(),
));
return true;
}
$cachedStats = $page
->getPageStatsCache($pages);
InstapageCmsPluginHelper::writeDiagnostics($cachedStats, 'Cached stats');
$pagesWithoutStats = array();
foreach ($pages as $instapageId) {
if (!isset($cachedStats[$instapageId])) {
$pagesWithoutStats[] = $instapageId;
}
}
if (empty($pagesWithoutStats)) {
echo json_encode((object) array(
'status' => 'OK',
'data' => $cachedStats,
));
return true;
}
$tokens = isset($post->apiTokens) ? $post->apiTokens : false;
if (!$tokens) {
$tokens = $subaccount
->getAllTokens();
}
$headers = array(
'accountkeys' => InstapageCmsPluginHelper::getAuthHeader($tokens),
);
$data = array(
'pages' => $pagesWithoutStats,
);
$responseJson = $api
->apiCall('page/stats', $data, $headers);
$response = json_decode($responseJson);
if (InstapageCmsPluginHelper::checkResponse($response)) {
$stats = (array) (isset($response->data) ? $response->data : array());
$page
->savePageStatsCache($stats);
if (count($stats)) {
$stats = array_merge($cachedStats, $stats);
}
else {
$stats = $cachedStats;
}
echo json_encode((object) array(
'status' => 'OK',
'data' => $stats,
));
}
else {
return false;
}
}