global $wpdb;
$wpdb->query(
$wpdb->prepare("DELETE FROM `" . DB::table('pages') . "` WHERE `id` = %d AND (`type` = 'post' OR `type` = 'page' OR `type` = 'product');", esc_sql($postId))
);
}
/**
* Deletes all term hits when the term is deleted.
*
* @param int $term Term ID.
* @param int $ttId Term taxonomy ID.
*
* @return void
*
* @hooked action: `delete_term` - 10
*
* @todo Replace this method with visitor decorator call after the class is ready.
* @todo Also delete from historical table.
*/
public static function deleteTermHits($term, $ttId)
{
global $wpdb;
$wpdb->query(
$wpdb->prepare("DELETE FROM `" . DB::table('pages') . "` WHERE `id` = %d AND (`type` = 'category' OR `type` = 'post_tag' OR `type` = 'tax');", esc_sql($ttId))
);
}
/**
* Returns the data needed for "Statistics - Summary" widget/panel in edit posts.
*
* @param \WP_Post $post
*
* @return array|null Keys:
* - `postId`
* - `fromString`
* - `toString`
* - `publishDateString`
* - `totalVisitors`
* - `totalViews`
* - `topReferrer`
* - `topReferrerCount`
* - `thisPeriodVisitors`
* - `thisPeriodViews`
* - `thisPeriodTopReferrer`
* - `thisPeriodTopReferrerCount`
* - `postChartData`
* - `postChartSettings`
* - `contentAnalyticsUrl`
*/
public static function getPostStatisticsSummary($postId)
{
$dataProvider = null;
$miniChartHelper = new MiniChartHelper();
try {
$dataProvider = new PostSummaryDataProvider($postId);
} catch (\Exception $e) {
return null;
}
$topReferrerAndCountTotal = $dataProvider->getTopReferrerAndCount(true);
$topReferrerAndCountThisPeriod = $dataProvider->getTopReferrerAndCount();
// Data for the sidebar chart
$chartData = [];
$wpDateFormat = get_option('date_format');
$publishDate = $dataProvider->getPublishDate();
// Fill `$chartData` with default 0s
// Use a short date format for indexes and `chartDates` for values
// Short date format will be displayed below summary charts
foreach ($miniChartHelper->getChartDates() as $date) {
$shortDate = date('d M', strtotime($date));
$chartData[$shortDate] = [
'ymdDate' => date('Y-m-d', strtotime($date)),
'hits' => 0,
'fullDate' => date($wpDateFormat, strtotime($date)),
];
}
// Set date range for charts based on MiniChart's `date_range` option
// Also change `to_date` to include today's stats in charts too
$dataProvider->setFrom(TimeZone::getTimeAgo($miniChartHelper->isMiniChartActive() ? Option::getByAddon('date_range', 'mini_chart', '14') : 14));
$dataProvider->setTo(date('Y-m-d'));
// Fill `$dailyHits` based on MiniChart's `metric` option
$dailyHits = Helper::checkMiniChartOption('metric', 'views', 'visitors') ? $dataProvider->getDailyViews() : $dataProvider->getDailyVisitors();
// Fill `$chartData` with real stats
foreach ($dailyHits as $hit) {
if (empty($hit->date) || (empty($hit->visitors) && empty($hit->views))) {
continue;
}
$shortDate = date('d M', strtotime($hit->date));
$chartData[$shortDate] = [
'ymdDate' => $hit->date,
'hits' => !empty($hit->visitors) ? intval($hit->visitors) : intval($hit->views),
'fullDate' => date($wpDateFormat, strtotime($hit->date)),
];
}
// Sort `$chartData` by date
uasort($chartData, function ($a, $b) {
if ($a['ymdDate'] == $b['ymdDate']) {
return 0;
}
return ($a['ymdDate'] < $b['ymdDate']) ? -1 : 1;
});
// Some settings for the chart
$chartSettings = [
'color' => $miniChartHelper->getChartColor(),
'label' => $miniChartHelper->getLabel(),
];
// Reset date range because text summary displays info for the past week
$dataProvider->setFrom(TimeZone::getTimeAgo(7));
$dataProvider->setTo(TimeZone::getTimeAgo());
return [
'postId' => $postId,
'fromString' => $dataProvider->getFromString('', true),
'toString' => $dataProvider->getToString('', true),
'publishDateString' => $publishDate,
'totalVisitors' => $dataProvider->getVisitors(true),
'totalViews' => $dataProvider->getViews(true),
'topReferrer' => $topReferrerAndCountTotal['url'],
'topReferrerCount' => $topReferrerAndCountTotal['count'],
'thisPeriodVisitors' => $dataProvider->getVisitors(),
'thisPeriodViews' => $dataProvider->getViews(),
'thisPeriodTopReferrer' => $topReferrerAndCountThisPeriod['url'],
'thisPeriodTopReferrerCount' => $topReferrerAndCountThisPeriod['count'],
'postChartData' => $chartData,
'postChartSettings' => $chartSettings,
'contentAnalyticsUrl' => $dataProvider->getContentAnalyticsUrl(),
];
}
}